Home > Back-end >  Defining linked list structure with struct in c program
Defining linked list structure with struct in c program

Time:07-01

I created a linked list using a C program. My codes are below.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

struct node {
   int data;
   int key;
   struct node *next;
};

struct node *current = NULL;
struct node *head = NULL;
int numberOfElements = 0;

//display the list
void printList() {
   struct node *ptr = head;
   printf("\n[ ");
    
   //start from the beginning
   while(ptr != NULL) {
      printf("(%d,%d) ",ptr->key,ptr->data);
      ptr = ptr->next;
   }
    
   printf(" ]");
}

//insert link at the first location
void insertFirst(int key, int data) {
   //create a link
   struct node *link = (struct node*) malloc(sizeof(struct node));
    
   link->key = key;
   link->data = data;
    
   //point it to old first node
   link->next = head;
    
   //point first to new first node
   head = link;
   numberOfElements   ;
}

//delete first item
struct node* deleteFirst() {

   //save reference to first link
   struct node *tempLink = head;
   
    
   //mark next to first link as first 
   head = head->next;
    
   //return the deleted link
   
   return tempLink;
    numberOfElements --;
}

//is list empty
bool isEmpty() {
   return head == NULL;
}

int length() {
   
   return numberOfElements;
}

//find a link with given key
struct node* find(int key) {

   //start from the first link
   struct node* current = head;

   //if list is empty
   if(head == NULL) {
      return NULL;
   }

   //navigate through list
   while(current->key != key) {
    
      //if it is last node
      if(current->next == NULL) {
         return NULL;
      } else {
         //go to next link
         current = current->next;
      }
   }      
    
   //if data found, return the current Link
   return current;
}

//delete a link with given key
struct node* delete(int key) {
    numberOfElements --;

   //start from the first link
   struct node* current = head;
   struct node* previous = NULL;
    
   //if list is empty
   if(head == NULL) {
      return NULL;
   }

   //navigate through list
   while(current->key != key) {

      //if it is last node
      if(current->next == NULL) {
         return NULL;
      } else {
         //store reference to current link
         previous = current;
         //move to next link
         current = current->next;
      }
   }

   //found a match, update the link
   if(current == head) {
      //change first to point to next link
      head = head->next;
   } else {
      //bypass the current link
      previous->next = current->next;
   }    
    
   return current;
}


void main() {
   insertFirst(1,10);
   insertFirst(2,20);
   insertFirst(3,30);
   insertFirst(4,1);
   insertFirst(5,40);
   insertFirst(6,56); 

   printf("Original List: "); 
    
   //print list
   printList();
   printf("\nlength is %d\n",length());
     

   struct node *foundLink = find(4);
    
   if(foundLink != NULL) {
      printf("\nElement found: ");
      printf("(%d,%d) ",foundLink->key,foundLink->data);
      printf("\n");  
   } else {
      printf("Element not found.");
   }

   delete(4);
   printf("List after deleting an item: ");
   printList();
   printf("\n");
   foundLink = find(4);
    
   if(foundLink != NULL) {
      printf("\nElement found: ");
      printf("(%d,%d) ",foundLink->key,foundLink->data);
      printf("\n");
   } else {
      printf("Element not found.");
   }
   printf("\nlength is %d\n",length());
    
}

In the linked list structure, I only defined the node structure as a struct, the linked list structure is not defined as a separate struct. By defining a separate struct for the linked list structure, as follows;

struct MyList {
   struct node *head;
   int numberOfElements;
};

I want to exclude the variables 'head' and 'numberOfElements' from global variability. With this change, how can I update my code above and run it?

CodePudding user response:

The simplest solution is to modify your list managing functions so that they take a struct MyList* parameter, and change all references to head and numberOfElements to list->head and list->numberOfElements, then put a struct MyList in whatever function is using it (main() in your case), and pass a reference to your struct MyList to every function that needs to modify it, for example:

//find a link with given key
struct node* find(struct MyList *list,int key) {

   //start from the first link
   struct node* current = list->head;

   //if list is empty
   if(list->head == NULL) {
      return NULL;
...
//is list empty
bool isEmpty(struct MyList *list) {
   return list->head == NULL;
}

int length(struct MyList *list) {
   
   return list->numberOfElements;
}
int main(){
   struct MyList list = (struct MyList){.head=NULL, .numberOfElements = 0};

   insertFirst(&list,1,10);
   insertFirst(&list,2,20);
   insertFirst(&list,3,30);
   insertFirst(&list,4,1);
   ... 
  • Related