C Program to implement Singly Linked List to insert and delete a node at a specified position and display the contents of the Singly List
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;
}
return x;
}
NODE insert_position(NODE first, int item, int pos)
{
int count = 1;
NODE temp, cur, prev;
temp = getnode();
temp->info = item;
temp->link = NULL;
if(first == NULL && pos == 1)
return temp;
if(first == NULL && pos > 1)
{
printf("Invalid position\n");
free(temp);
return first;
}
if(pos == 1)
{
temp->link = first;
return temp;
}
cur = first;
prev = NULL;
while(cur != NULL && pos != count)
{
prev = cur;
cur = cur->link;
count++;
}
if(cur == NULL && pos > count)
{
printf("Invalid position\n");
free(temp);
return first;
}
prev->link = temp;
temp->link = cur;
return first;
}
NODE delete_position(NODE first, int pos)
{
int count = 1;
NODE temp, cur, prev;
if(first == NULL)
{
printf("List is empty. Cannot delete\n");
return first;
}
if(first->link == NULL && pos > 1)
{
printf("Invalid position to delete\n");
return first;
}
if(pos == 1)
{
temp = first->link;
free(first);
return temp;
}
cur = first;
prev = NULL;
while(cur != NULL && pos != count)
{
prev = cur;
cur = cur->link;
count++;
}
if(cur == NULL && pos >= count)
{
printf("Invalid position to delete\n");
return first;
}
prev->link = cur->link;
printf("Item deleted is: %d\n", cur->info);
free(cur);
return first;
}
void display(NODE first)
{
NODE cur;
if(first == NULL)
{
printf("List is empty\n");
return;
}
cur = first;
while(cur != NULL)
{
printf("%d\n", cur->info);
cur = cur->link;
}
}
void main()
{
int item, pos, choice;
NODE first;
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);
first = insert_position(first, item, pos);
break;
case 2: printf("Enter the position to be deleted\n");
scanf("%d", &pos);
first = delete_position(first, pos);
break;
case 3: display(first);
break;
default: exit(0);
}
}
}
Output
Enter the choice
1. Insert position
2. Delete position
3. Display
4. Exit
2
Enter the position to be deleted
1
List is empty. Cannot delete
Enter the choice
1. Insert position
2. Delete position
3. Display
4. Exit
2
Enter the position to be deleted
2
List is empty. Cannot delete
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
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
4
Enter the choice
1. Insert position
2. Delete position
3. Display
4. Exit
1
Enter the item to be inserted
50
Enter the position
5
Enter the choice
1. Insert position
2. Delete position
3. Display
4. Exit
3
10
20
30
40
50
Enter the choice
1. Insert position
2. Delete position
3. Display
4. Exit
2
Enter the position to be deleted
6
Invalid position to delete
Enter the choice
1. Insert position
2. Delete position
3. Display
4. Exit
2
Enter the position to be deleted
2
Item deleted is: 20
Enter the choice
1. Insert position
2. Delete position
3. Display
4. Exit
3
10
30
40
50
Enter the choice
1. Insert position
2. Delete position
3. Display
4. Exit
2
Enter the position to be deleted
1
Enter the choice
1. Insert position
2. Delete position
3. Display
4. Exit
3
30
40
50
Enter the choice
1. Insert position
2. Delete position
3. Display
4. Exit
4