C Program to implement Singly Linked List to insert and delete a node from rear and display the contents of the Singly List using Header Node
Program
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
typedef struct node* NODE;
NODE getnode();
NODE insert_rear(NODE , int);
NODE delete_rear(NODE);
void display(NODE);
void main()
{
NODE head;
int choice, item;
head = getnode();
head->info = 0;
head->link = NULL;
for(;;)
{
printf("Enter\n");
printf("1. Insert Rear\n");
printf("2. Delete Rear\n");
printf("3. Display the list\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter item to be inserted\n");
scanf("%d", &item);
head = insert_rear(head, item);
break;
case 2:
head = delete_rear(head);
break;
case 3:
display(head);
break;
default:
exit(0);
}
}
}
NODE getnode()
{
NODE x;
x = (NODE) malloc(sizeof(struct node));
if(x == NULL)
{
printf("Node creation error\n");
return NULL;
}
return x;
}
NODE insert_rear(NODE head , int item)
{
NODE temp, current;
temp = getnode();
temp->info = item;
temp->link = NULL;
current = head;
while(current->link != NULL)
{
current = current->link;
}
current->link = temp;
head->info += 1;
return head;
}
NODE delete_rear(NODE head)
{
NODE current, prev;
if (head->link == NULL)
{
printf("Cannot delete. List is empty\n");
return head;
}
current = head->link;
prev = head;
if(current->link == NULL)
{
printf("Deleted item is %d\n", current->info);
free(current);
head->link = NULL;
head->info -= 1;
return head;
}
while(current->link != NULL)
{
prev = current;
current = current->link;
}
printf("Deleted item is %d\n", current->info);
free(current);
prev->link = NULL;
head->info -= 1;
return head;
}
void display(NODE head)
{
NODE temp;
if(head->link == NULL)
{
printf("Empty list\n");
return;
}
temp = head->link;
printf("Contents of linked list is:\n");
while(temp != NULL)
{
printf("%d\t", temp->info);
temp = temp->link;
}
printf("\nNode count:\t%d\n", head->info);
}
Output
Enter
1. Insert Rear
2. Delete Rear
3. Display the list
4. Exit
2
Cannot delete. List is empty
Enter
1. Insert Rear
2. Delete Rear
3. Display the list
4. Exit
1
Enter item to be inserted
10
Enter
1. Insert Rear
2. Delete Rear
3. Display the list
4. Exit
1
Enter item to be inserted
20
Enter
1. Insert Rear
2. Delete Rear
3. Display the list
4. Exit
1
Enter item to be inserted
30
Enter
1. Insert Rear
2. Delete Rear
3. Display the list
4. Exit
1
Enter item to be inserted
40
Enter
1. Insert Rear
2. Delete Rear
3. Display the list
4. Exit
3
Contents of linked list is:
10 20 30 40
Node count: 4
Enter
1. Insert Rear
2. Delete Rear
3. Display the list
4. Exit
2
Deleted item is 40
Enter
1. Insert Rear
2. Delete Rear
3. Display the list
4. Exit
2
Deleted item is 30
Enter
1. Insert Rear
2. Delete Rear
3. Display the list
4. Exit
2
Deleted item is 20
Enter
1. Insert Rear
2. Delete Rear
3. Display the list
4. Exit
2
Deleted item is 10
Enter
1. Insert Rear
2. Delete Rear
3. Display the list
4. Exit
3
Empty list
Enter
1. Insert Rear
2. Delete Rear
3. Display the list
4. Exit
1
Enter item to be inserted
10
Enter
1. Insert Rear
2. Delete Rear
3. Display the list
4. Exit
3
Contents of linked list is:
10
Node count: 1
Enter
1. Insert Rear
2. Delete Rear
3. Display the list
4. Exit
4