Monday, 22 February 2016

Data Structures - C Program to implement Linked List Operations - Insert , Delete , Update , Display

Linked List is another data structure consisting of group of nodes which together represent a sequence. Each node has two parts one for the value and other for link to the next node.
Linked lists allows dynamic memory allocation.

Singly-linked-list.svg
A singly linked list whose nodes contain two fields: an integer value and a link to the next node



Some of the operations in a linked list are :

  • Insertion into the list
  • Deletion from the list
  • Updation of the value
  • Display the list





Download code

PROGRAM :

#include <stdio.h>
 #include <stdlib.h>
 #include <malloc.h>

 struct node
 {
     int value;
     struct node *next;
 };
 typedef struct node snode;
 snode *newnode, *ptr, *prev, *temp;
 snode *first = NULL, *last = NULL;

 int main()
 {
     int ch;
     char ans = 'Y';
     while (ans == 'Y'||ans == 'y')
     {
  printf("\n1.Insert node at first");
  printf("\n2.Insert node at last");
  printf("\n3.Insert node at position");
  printf("\n4.Delete Node from any Position");
  printf("\n5.Update Node Value");
  printf("\n6.Display List");
  printf("\n7.Exit\n");
  printf("\nEnter your choice: \t");
  scanf("%d", &ch);

  switch (ch)
  {
  case 1:
      insertNodeFirst();
      break;
  case 2:
      insertNodeLast();
      break;
  case 3:
      insertNodePosition();
      break;
  case 4:
      deleteNode();
      break;
  case 5:
      updateNode();
      break;
  case 6:
      displayList();
      break;
  case 7:
      return 0;
      break;
  default:
      printf("\n...Invalid Choice...\n");
      break;
  }
  printf("\nDo you want to continue ? (Y/N) \t");
  scanf(" %c", &ans);
     }
     return 0;
 }
 snode* createNode(int val)
 {
     newnode = (snode *)malloc(sizeof(snode));
     if (newnode == NULL)
     {
  printf("\nMemory was not allocated");
  return 0;
     }
     else
     {
  newnode->value = val;
  newnode->next = NULL;
  return newnode;
     }
 }
 void insertNodeFirst()
 {
     int val;
     printf("\nEnter the value for the node: \t");
     scanf("%d", &val);
     newnode = createNode(val);
     if (first == last && first == NULL)
     {
  first = last = newnode;
  first->next = NULL;
  last->next = NULL;
     }
     else
     {
  temp = first;
  first = newnode;
  first->next = temp;
     }
     printf("\nInserted Successfully");
 }
 void insertNodeLast()
 {
     int val;

     printf("\nEnter the value for the Node: \t");
     scanf("%d", &val);
     newnode = createNode(val);
     if (first == last && last == NULL)
     {
  first = last = newnode;
  first->next = NULL;
  last->next = NULL;
     }
     else
     {
  last->next = newnode;
  last = newnode;
  last->next = NULL;
     }
     printf("\nInserted Successfully");
 }
 void insertNodePosition()
 {
     int pos, val, count = 0, i;
     printf("\nEnter the value for the Node: \t");
     scanf("%d", &val);
     newnode = createNode(val);
     printf("\nEnter the position: \t");
     scanf("%d", &pos);
     ptr = first;
     while (ptr != NULL)
     {
  ptr = ptr->next;
  count++;
     }
     if (pos == 1)
     {
  if (first == last && first == NULL)
  {
      first = last = newnode;
      first->next = NULL;
      last->next = NULL;
  }
  else
  {
      temp = first;
      first = newnode;
      first->next = temp;
  }
  printf("\nInserted Successfully");
     }
     else if (pos>1 && pos<=count)
     {
  ptr = first;
  for (i = 1; i < pos; i++)
  {
      prev = ptr;
      ptr = ptr->next;
  }
  prev->next = newnode;
  newnode->next = ptr;
  printf("\nInserted Successfully");
     }
     else
     {
  printf("Position is out of range");
     }
 }
 void deleteNode()
 {
     int pos, count = 0, i;

     if (first == NULL)
     {
  printf("No nodes in the list to delete\n");
     }
     else
     {
  printf("\nEnter the position of value to be deleted: \t");
  scanf(" %d", &pos);
  ptr = first;
  if (pos == 1)
  {
      first = ptr->next;
      printf("\nElement deleted successfully");
  }
  else
  {
      while (ptr != NULL)
      {
          ptr = ptr->next;
          count = count + 1;
      }
      if (pos > 0 && pos <= count)
      {
          ptr = first;
          for (i = 1; i < pos; i++)
          {
              prev = ptr;
              ptr = ptr->next;
          }
          prev->next = ptr->next;
      }
      else
      {
          printf("Position is out of range");
      }
      free(ptr);
      printf("\nElement deleted successfully");
  }
     }
 }
 void updateNode()
 {
     int oldval, newval, flag = 0;

     if (first == NULL)
     {
  printf("No nodes in the list to update\n");
     }
     else
     {
  printf("\nEnter the value to be updated: \t");
  scanf("%d", &oldval);
  printf("\nEnter the new value: \t");
  scanf("%d", &newval);
  for (ptr = first; ptr != NULL; ptr = ptr->next)
  {
      if (ptr->value == oldval)
      {
          ptr->value = newval;
          flag = 1;
          break;
      }
  }
  if (flag == 1)
  {
      printf("\nUpdated Successfully");
  }
  else
  {
      printf("\nValue not found in List");
  }
     }
 }
 void displayList()
 {
     if (first == NULL)
     {
  printf("No nodes in the list to display\n");
     }
     else
     {
  for (ptr = first; ptr != NULL; ptr = ptr->next)
  {
      printf("%d\t", ptr->value);
  }
     }
 }
 

Download code

OUTPUT :

C - Linked List Operations - Insertion at front

C - Linked List Operations - Insertion at last and  any position

C - Linked List Operations -  Deletion of node

C - Linked List Operations - Updation of value

C - Linked List Operations - Display List