C Program to implement Singly Linked List to insert and delete a node from front 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_front(NODE , int);
NODE delete_front(NODE);
void display(NODE);
void main()
{
NODE head;
int choice, item;
head = getnode();
head->info = 0;
head->link = NULL;
while(1)
{
printf("Enter\n");
printf("1. Insert Front\n");
printf("2. Delete Front\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_front(head, item);
break;
case 2:
head = delete_front(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_front(NODE head , int item)
{
NODE temp, next;
temp = getnode();
temp->info = item;
next = head->link;
head->link = temp;
temp->link = next;
head->info += 1;
return head;
}
NODE delete_front(NODE head)
{
NODE temp, next;
if(head->link == NULL)
{
printf("Cannot delete. Empty List\n");
return head;
}
temp = head->link;
next = temp->link;
head->link = next;
head->info -= 1;
printf("Deleted node is %d\n", temp->info);
free(temp);
return head;
}
void display(NODE head)
{
NODE temp;
if(head->link == NULL)
{
printf("Cannot print. 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 Front
2. Delete Front
3. Display the list
4. Exit
1
Enter item to be inserted
10
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
1
Enter item to be inserted
20
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
3
Contents of linked list is:
20 10
Node count: 2
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
1
Enter item to be inserted
30
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
1
Enter item to be inserted
40
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
3
Contents of linked list is:
40 30 20 10
Node count: 4
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
2
Deleted node is 40
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
2
Deleted node is 30
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
2
Deleted node is 20
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
3
Contents of linked list is:
10
Node count: 1
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
2
Deleted node is 10
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
2
Cannot delete. Empty List
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
3
Cannot print. Empty list
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
4