Singly Linked-List Operations in C
This code demonstrates various operations on a singly linked-list in C.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} *head = NULL, *travel, *ptr;
// Function to insert a node at the end of the linked-list
void InsertAtEnd(int data)
{
int new_data = data;
struct node *new_node = (struct node *)malloc(sizeof(struct node));
struct node *last;
travel = head;
last = head;
new_node->data = new_data;
if (head == NULL)
{
head = new_node;
new_node->next = head;
return;
}
else
{
while (last->next != head)
{
last = last->next;
}
}
last->next = new_node;
new_node->next = head;
}
// Function to insert a node at the beginning of the linked-list
void InsertAtBegin(int data)
{
struct node *new_node, *last = head;
new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = data;
if (head == NULL)
{
new_node->next = head;
head = new_node;
}
while (last->next != head)
{
last = last->next;
}
new_node->next = last->next;
last->next = new_node;
head = new_node;
}
// Function to insert a node before a specified node
void add_before()
{
struct node *cur, *pre;
cur = head;
pre = head;
int loc, new_data;
ptr = (struct node *)malloc(sizeof(struct node));
printf("\\nEnter the data that before you want to add the node::");
scanf("%d", &loc);
printf("Enter the data to add in new node::");
scanf("%d", &new_data);
ptr->data = new_data;
while (cur->data != loc)
{
pre = cur;
cur = cur->next;
}
pre->next = ptr;
ptr->next = cur;
}
// Function to delete the first node of the linked-list
void del_begin()
{
struct node *ptr;
if (head == NULL)
{
printf("\\nUNDERFLOW\\n");
}
else if (head->next == head)
{
head = NULL;
free(head);
printf("\\nNode Deleted\\n");
}
else
{
ptr = head;
while (ptr->next != head)
ptr = ptr->next;
ptr->next = head->next;
free(head);
head = ptr->next;
printf("\\nNode Deleted\\n");
}
}
// Function to delete the last node of the linked-list
void del_last()
{
struct node *pre = head, *cur = head;
if (head == NULL)
{
printf("\\nLinked-List is empty");
}
while (cur->next != head)
{
pre = cur;
cur = cur->next;
}
pre->next = cur->next;
free(cur);
cur = NULL;
}
// Function to delete a specified node from the linked-list
void del_specified()
{
struct node *cur=head,*pre=head;
int val;
printf("ENTER DATA THAT YOU WANT TO DELETE::");
scanf("%d",&val);
if(head==NULL)
{
printf("Linked-List is empty.\\n");
}
else if(cur->next==head)
{
free(cur);
}
while (cur->data!=val)
{
pre=cur;
cur=cur->next;
}
pre->next=cur->next;
free(cur);
cur=NULL;
}
// Function to print the linked-list
void print()
{
struct node *last;
last = head;
if (last == NULL)
{
printf("Linked-List is empty.\\n");
}
else
{
while (last->next != head)
{
printf("%d ", last->data);
last = last->next;
}
printf("%d ", last->data);
}
}
// Main function
void main()
{
int choice, data;
while (1)
{
printf("\\nSELECT YOUR CHOICE::");
printf("\\n1.INSERT NODE AT END::");
printf("\\n2.INSERT NODE AT BEGINNING::");
printf("\\n3.INSERT NEW-NODE BEFORE SPECIFIED NODE::");
printf("\\n4.DELETE FIRST NODE::");
printf("\\n5.DELETE LAST NODE::");
printf("\\n6.PRINT LIST::");
printf("\\n7.DELETE SPECIFIED NODE");
printf("\\n8.Exit");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the data to add in new node::");
scanf("%d", &data);
InsertAtEnd(data);
break;
case 2:
printf("Enter the data to add in new node::");
scanf("%d", &data);
InsertAtBegin(data);
break;
case 3:
add_before();
break;
case 4:
del_begin();
break;
case 5:
del_last();
break;
case 6:
print();
break;
case 7:
del_specified();
break;
case 8:
exit(0);
default:
break;
}
}
}
This code implements various operations on a singly linked-list, including inserting nodes at the end and beginning, inserting before a specified node, deleting nodes, and printing the linked-list.
Feel free to experiment with this code and understand how each singly linked-list operation is implemented!
Output:
ReplyDeleteSELECT YOUR CHOICE::
1.INSERT NODE AT END::
2.INSERT NODE AT BEGINING::
3.INSERT NEW-NODE BEFORE SPECIFIED NODE::
4.DELETE FIRST NODE::
5.DELETE LAST NODE::
6.PRINT LIST::
7.DELETE SPECIFIED NODE
8.Exit1
Enter the data to add in new node::12
SELECT YOUR CHOICE::
1.INSERT NODE AT END::
2.INSERT NODE AT BEGINING::
3.INSERT NEW-NODE BEFORE SPECIFIED NODE::
4.DELETE FIRST NODE::
5.DELETE LAST NODE::
6.PRINT LIST::
7.DELETE SPECIFIED NODE
8.Exit1
Enter the data to add in new node::56
SELECT YOUR CHOICE::
1.INSERT NODE AT END::
2.INSERT NODE AT BEGINING::
3.INSERT NEW-NODE BEFORE SPECIFIED NODE::
4.DELETE FIRST NODE::
5.DELETE LAST NODE::
6.PRINT LIST::
7.DELETE SPECIFIED NODE
8.Exit1
Enter the data to add in new node::23
SELECT YOUR CHOICE::
1.INSERT NODE AT END::
2.INSERT NODE AT BEGINING::
3.INSERT NEW-NODE BEFORE SPECIFIED NODE::
4.DELETE FIRST NODE::
5.DELETE LAST NODE::
6.PRINT LIST::
7.DELETE SPECIFIED NODE
8.Exit1
Enter the data to add in new node::54
SELECT YOUR CHOICE::
1.INSERT NODE AT END::
2.INSERT NODE AT BEGINING::
3.INSERT NEW-NODE BEFORE SPECIFIED NODE::
4.DELETE FIRST NODE::
5.DELETE LAST NODE::
6.PRINT LIST::
7.DELETE SPECIFIED NODE
8.Exit1
Enter the data to add in new node::89
SELECT YOUR CHOICE::
1.INSERT NODE AT END::
2.INSERT NODE AT BEGINING::
3.INSERT NEW-NODE BEFORE SPECIFIED NODE::
4.DELETE FIRST NODE::
5.DELETE LAST NODE::
6.PRINT LIST::
7.DELETE SPECIFIED NODE
8.Exit6
12 56 23 54 89
SELECT YOUR CHOICE::
1.INSERT NODE AT END::
2.INSERT NODE AT BEGINING::
3.INSERT NEW-NODE BEFORE SPECIFIED NODE::
4.DELETE FIRST NODE::
5.DELETE LAST NODE::
6.PRINT LIST::
7.DELETE SPECIFIED NODE
8.Exit2
Enter the data to add in new node::47
SELECT YOUR CHOICE::
1.INSERT NODE AT END::
2.INSERT NODE AT BEGINING::
3.INSERT NEW-NODE BEFORE SPECIFIED NODE::
4.DELETE FIRST NODE::
5.DELETE LAST NODE::
6.PRINT LIST::
7.DELETE SPECIFIED NODE
8.Exit6
47 12 56 23 54 89
SELECT YOUR CHOICE::
1.INSERT NODE AT END::
2.INSERT NODE AT BEGINING::
3.INSERT NEW-NODE BEFORE SPECIFIED NODE::
4.DELETE FIRST NODE::
5.DELETE LAST NODE::
6.PRINT LIST::
7.DELETE SPECIFIED NODE
8.Exit4
Node Deleted
SELECT YOUR CHOICE::
1.INSERT NODE AT END::
2.INSERT NODE AT BEGINING::
3.INSERT NEW-NODE BEFORE SPECIFIED NODE::
4.DELETE FIRST NODE::
5.DELETE LAST NODE::
6.PRINT LIST::
7.DELETE SPECIFIED NODE
8.Exit6
12 56 23 54 89
SELECT YOUR CHOICE::
1.INSERT NODE AT END::
2.INSERT NODE AT BEGINING::
3.INSERT NEW-NODE BEFORE SPECIFIED NODE::
4.DELETE FIRST NODE::
5.DELETE LAST NODE::
6.PRINT LIST::
7.DELETE SPECIFIED NODE
8.Exit5
SELECT YOUR CHOICE::
1.INSERT NODE AT END::
2.INSERT NODE AT BEGINING::
3.INSERT NEW-NODE BEFORE SPECIFIED NODE::
4.DELETE FIRST NODE::
5.DELETE LAST NODE::
6.PRINT LIST::
7.DELETE SPECIFIED NODE
8.Exit6
12 56 23 54
SELECT YOUR CHOICE::
1.INSERT NODE AT END::
2.INSERT NODE AT BEGINING::
3.INSERT NEW-NODE BEFORE SPECIFIED NODE::
4.DELETE FIRST NODE::
5.DELETE LAST NODE::
6.PRINT LIST::
7.DELETE SPECIFIED NODE
8.Exit7
ENTER DATA THAT YOU WANT TO DELETE::56
SELECT YOUR CHOICE::
1.INSERT NODE AT END::
2.INSERT NODE AT BEGINING::
3.INSERT NEW-NODE BEFORE SPECIFIED NODE::
4.DELETE FIRST NODE::
5.DELETE LAST NODE::
6.PRINT LIST::
7.DELETE SPECIFIED NODE
8.Exit6
12 23 54
SELECT YOUR CHOICE::
1.INSERT NODE AT END::
2.INSERT NODE AT BEGINING::
3.INSERT NEW-NODE BEFORE SPECIFIED NODE::
4.DELETE FIRST NODE::
5.DELETE LAST NODE::
6.PRINT LIST::
7.DELETE SPECIFIED NODE
8.Exit:8