Advertisement

Linked List

Image result for link list free copyright images
A) Insertion Operation
void insertFirst(int key, int data) {

   //create a link
   struct node *link = (struct node*) malloc(sizeof(struct node));
   link->key = key;
   link->data = data;
           
   if(isEmpty()) {
      last = link;
   } else {
      //update first prev link
      head->prev = link;
   }

   link->next = head;
           
   head = link;
}
B) Deletion Operation
struct node* deleteFirst() {

   struct node *tempLink = head;
           
   //if only one link
   if(head->next == NULL) {
      last = NULL;
   } else {
      head->next->prev = NULL;
   }
           
   head = head->next;
           
   return tempLink;
}

C) Insertion at the End of an Operation
void insertLast(int key, int data) {

   struct node *link = (struct node*) malloc(sizeof(struct node));
   link->key = key;
   link->data = data;
           
   if(isEmpty()) {
      //make it the last link
      last = link;
   } else {
      last->next = link;    
     
      link->prev = last;
   }

   last = link;
}