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