Write a C program that uses functions to perform the following operations:

  1. Create a singly linked list of integer elements.
  2. Traverse the above list and display the elements.

Algorithm:

Step 1:  Start
Step 2:  Declare a structure named linked-list
Step 3:  Declare the pointers next, first, fresh, ptr
Step 4:  print main menu
Step 5:  Read choice
Step 6:  switch(choice)
Step 7:  if(choice == 1)
         7.1: Assign fresh = malloc(size of (node))
         7.2: Read the element fresh -> data
         7.3: Read the choice where to insert
         7.4: switch(choice)
              7.4.1: if choice == 1
              7.4.2: call the function IBegin()
              7.4.3: if choice == 2
              7.4.4: call the function Iend()
              7.4.5: if choice == 3
              7.4.6: call the function Imiddle()
Step 8: if(choice == 2)
        8.1: Read the position to delete
        8.2: switch(choice)
             8.2.1: if choice == 1
             8.2.2: call the function DBegin()
             8.2.3: if choice == 2
             8.2.4: call the function Dend()
             8.2.5: if choice == 3
             8.2.6: call the function Dmiddle()
Step 9:  if choice == 3
         9.1 Call function view
Step 10: if choice == 4
         10.1 exit()
Step 11: Start insert function
Step 12: if(first == null)
Step 13: first -> data = e
Step 14: first -> next = null
Step 15: else declare new node
Step 16: fresh -> data = e
Step 17: if choice = 1
Step 18: first -> next = first
Step 19: first = fresh
Step 20: if choice = 2
Step 21: ptr = first
Step 22: ptr -> next = fresh
Step 23: fresh -> next = full
Step 24: if choice = 3
Step 25: Enter the position
Step 26: at p - 1 node
Step 27: fresh -> next = ptr -> next
Step 28: ptr -> next = fresh
Step 29: for delete function
Step 30: if first != null
Step 31: Enter the position to delete
Step 32: if choice = 1
Step 33: d = first -> data
Step 34: first = first -> next
Step 35: if choice = 2
Step 36: ptr = first
Step 37: Traverse to last node
Step 38: d = ptr -> next -> data
Step 39: ptr -> next = ptr -> next -> next
Step 40: Print d value
Step 41: for function view
Step 42: for ptr = first and ptr != null and ptr = ptr -> next
Step 43: print ptr -> data
Step 44: End

Flowchart:

Program:

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
void create();
void insert();
void del();
void display();
struct node
{
 int data;
 struct node *link;
};
struct node *first = null, *last = null ,*next, *curr, *prev;
int ch;
void main()
{
 clrscr();
 printf(“singly linked list \n”);
 do
 {
  printf(“\n 1.create  \n 2.insert \n 3.delete \n 4.exit \n ”);
  printf(“Enter your choice”);
  scanf(“%d”, &ch);
  switch(ch)
  {
   case 1: create();
           display();
           break;
   case 2: insert();
           display();
           break;
   case 3: del();
           display();
           break;
   case 4: exit(0);
  }
 }
 while(ch<=3);
}
void create()
{
 curr = (struct node *) malloc(sizeof(struct node));
 printf(“Enter the data: ”);
 scanf(“%d”, &curr -> data);
 curr -> link = null;
 first = curr;
 last = curr;
}
void insert()
{
 int pos, c = 1;
 curr=(struct node *)malloc(sizeof(struct node));
 printf(“Enter the data:”);
 scanf(“%d”, &curr -> data);
 printf(“Enter the position:”);
 scanf(“%d”, &pos);
 if((pos == 1) && (first != null))
 {
  curr -> link = first;
  first = curr;
 }
 else
 {
  next = first;
  while(c < pos)
  {
   prev = next;
   next = prev -> link;
   c++;
  }
  if(prev == null)
  {
  printf(“\n Invalid position”);
 }
 else
 {
  curr -> link = prev -> link;
  prev -> link = curr;
  if(curr -> link == null)
  {
   last = curr;
  }
 }
}
void del()
{
 int pos, c = 1;
 printf(“Enter the position”);
 scanf(“%d”, &pos);
 if(first = null)
 {
  printf(“\n list is empty”);
 }
 else if(pos == 1) && (first -> link == null)
 {
  printf(“\n Deleted element is %d \n”, curr -> data);
  free(curr);
 }
 else
 { 
  next = first;
  while(c < pos)
  {
   prev = next;
   next = next -> link;
   c++;
  }
  prev -> link = next -> link;
  next -> link = null;
  if(next = null)
  {
   printf(“\n Invalid position”);
  }
  else
  {
   printf(“\n Deleted element is:%d\n”, next -> data);
   free(next);
   if(prev -> link == null)
   {
    last = prev;
   }
  }
 }
}
void display()
{
 curr = first;
 while(curr != null)
 {
  printf(“\n %d”, curr -> data);
  curr = curr -> link;
 }
}

Input & Output:

Singly linked list
1.create
2.insert
3.del
4.exit
Enter your choice 1
Enter the data:2
1.create
2.insert
3.del
4.exit
Enter your choice 2
Enter the data: 4
Enter the position: 2
2
4
1.create
2.insert
3.del
4.exit
Enter your choice 4