Singly Linked List Implementation program is given below:
//code for implementation of singly linked list
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
} *head = NULL;
void insertbegin(int item)
{
struct node *newnode;
newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = item;
newnode->link = NULL;
if (head == NULL)
{
head = newnode;
}
else
{
newnode->link = head;
head = newnode;
}
}
void insertend(int item)
{
struct node *newnode, *t;
newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = item;
newnode->link = NULL;
t = head;
if (head == NULL)
{
head = newnode;
}
else
{
while (t->link != NULL)
{
t = t->link;
}
t->link = newnode;
}
}
void insertpos(int item, int pos)
{
struct node *newnode, *t;
int i;
newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = item;
newnode->link = NULL;
t = head;
if (head == NULL)
{
head = newnode;
}
else
{
for (i = 1; i < pos - 1; i++)
{
t = t->link;
}
newnode->link = t->link;
t->link = newnode;
}
}
void deletefront()
{
struct node *t;
if (head == NULL)
{
printf("empty");
}
else
{
t = head;
printf("deleted data is %d \n", t->data);
head = head->link;
free(t);
}
}
void deleteend()
{
struct node *t1, *t2;
t1 = NULL;
t2 = head;
if (head == NULL)
{
printf(" list is empty");
}
else
{
while (t2->link != NULL)
{
t1=t2;
t2=t2->link;
}
if(t2==head)
{
head = NULL;
}
else{
t1->link=NULL;
}
printf("deleted element is %d \n", t2->data);
free(t2);
}
}
void deletepos(int pos)
{
int i;
struct node *t1, *t2;
t1 = head;
if (head == NULL)
{
printf("empty");
}
else
{
for (i = 1; i < pos - 1; i++)
{
t1 = t1->link;
}
if(t1==head)
{
deletefront();
}
else{
t2 = t1->link;
t1->link = t2->link;
printf("deleted element is %d \n", t2->data);
free(t2);
}
}
}
void display()
{
struct node *t = head;
if(head == NULL)
{
printf("LIst is empty");
}
else{
while (t != NULL)
{
printf("_%d_", t->data);
t = t->link;
}
}
}
void length()
{
int length = 0;
struct node *t;
t=head;
while(t != NULL)
{
length++;
t = t ->link;
}
printf("\nthe length of current list: %d",length);
}
void main()
{
int choice ,c,data,x;
do{
printf("press 1 to insert in begning\n");
printf("press 2 to insert in end\n");
printf("press 3 to insert in any positions\n");
printf("press 4 to delete first element\n");
printf("press 5 to delete end element\n");
printf("press 6 to to delete in any position\n");
printf("press 7 to display\n");
printf("press 8 for the length of current list\n");
printf("press 0 to Exit\n");
fflush(stdin);
scanf(" %d",&choice);
switch(choice)
{
case 1 :
printf(" Enter the data to insert :");
scanf(" %d",&data);
insertbegin(data);
break;
case 2 :
printf(" Enter the data to insert :");
scanf(" %d",&data);
insertend(data);
break;
case 3 :
printf(" Enter the data to insert :");
scanf(" %d",&data);
printf(" Enter the position to insert :");
scanf(" %d",&x);
insertpos(data,x);
break;
case 4 :
deletefront();
break;
case 5 :
deleteend();
break;
case 6 :
printf(" Enter the position to delete :");
scanf(" %d",&x);
deletepos(x);
break;
case 7 :
display();
break;
case 8 :
length();
break;
case 0:
exit(1);
break;
default:
printf("\nEnter the correct value");
}
printf("\nEnter 1 to continue :");
fflush(stdin);
scanf(" %d",&c);
}while(c==1);
}
Output
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
1
Enter the data to insert :55
Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
7
_55__22__11__33__66_
Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
5
deleted element is 66
Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
7
_55__22__11__33_
Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
6
Enter the position to delete :2
deleted element is 22
Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
7
_55__11__33_
Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
8
the length of current list :3
Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
0