0% found this document useful (0 votes)
2 views

linkedList

The document contains a C program that implements a singly linked list with various operations such as insertion, deletion, searching, and displaying the list. The main function provides a menu-driven interface for users to choose operations like inserting at the beginning or end, deleting nodes, and searching for elements. Each operation is defined in separate functions, and memory management is handled using dynamic allocation.

Uploaded by

mdafzal.aiml
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

linkedList

The document contains a C program that implements a singly linked list with various operations such as insertion, deletion, searching, and displaying the list. The main function provides a menu-driven interface for users to choose operations like inserting at the beginning or end, deleting nodes, and searching for elements. Each operation is defined in separate functions, and memory management is handled using dynamic allocation.

Uploaded by

mdafzal.aiml
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

#include<stdio.

h>
#include<stdlib.h>
//node declaration
struct node
{
int data;
struct node* next;
};

//head declaration
struct node* head;

//function declarations
void insertAtBegining();
void insertAtEnd();
void insertRandom();
void deleteFromBegining();
void deleteFromEnd();
void deleteRandom();
void display();
void search();

//Driver code
int main(void)
{
int choice =0;
while(choice != 9)
{
printf("\n\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert at begining");
printf("\n2.Insert at end");
printf("\n3.Insert at any random location");
printf("\n4.Delete from Beginning");
printf("\n5.Delete from end");
printf("\n6.Delete at any random location");
printf("\n7.Search for an element");
printf("\n8.Show");
printf("\n9.Exit\n");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insertAtBegining();
break;
case 2:
insertAtEnd();
break;
case 3:
insertRandom();
break;
case 4:
deleteFromBegining();
break;
case 5:
deleteFromEnd();
break;
case 6:
deleteRandom();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}

//Inserting node at the begining of the Singly Linked List


void insertAtBegining()
{
struct node* newNode;
int item;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
printf("\nMEMORY OVERFLOW");
else
{
printf("\nEnter value: ");
scanf("%d",&item);
newNode‐>data = item;
newNode‐>next = head;
head = newNode;
printf("\nNode inserted Successfully...:)");
}
}

//Inserting node at the end of the Singly Linked List


void insertAtEnd()
{
struct node* newNode;
struct node* temp;
int item;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
printf("\nMEMORY OVERFLOW");
else
{
printf("\nEnter value: ");
scanf("%d",&item);
newNode‐>data = item;
if(head == NULL)
{
newNode‐>next = NULL;
head = newNode;
printf("\nNode inserted Successfully...:)");
}
else
{
temp = head;
while (temp‐>next!=NULL)
temp = temp‐>next;
temp‐>next = newNode;
newNode‐>next = NULL;
printf("\nNode inserted Successfully...:)");
}
}
}

//Inserting node at any random location of the Singly Linked List


void insertRandom()
{
int i,loc,item;
struct node* newNode;
struct node* temp;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
printf("\nMEMORY OVERFLOW");
else
{
printf("\nEnter value: ");
scanf("%d",&item);
newNode‐>data = item;
printf("\nEnter the location after which you want to insert: ");
scanf("%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp = temp‐>next;
if(temp == NULL)
{
printf("\nCan't be inserted (Select different location)\n");
return;
}
}
newNode‐>next = temp‐>next;
temp‐>next = newNode;
printf("\n\nNode inserted Successfully...:)");
}
}

//Deleting node from the begining of the Singly Linked List


void deleteFromBegining()
{
struct node* temp;
if(head == NULL)
printf("\nList is empty...:(\n");
else
{
temp = head;
head = temp‐>next;
free(temp);
printf("\nNode deleted from the begining ...\n");
}
}

//Deleting node from the end of the Singly Linked List


void deleteFromEnd()
{
struct node* temp1;
struct node* temp2;
if(head == NULL)
printf("\nList is empty...:(\n");
else if(head‐>next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the list deleted ...\n");
}
else
{
temp1 = head;
while(temp1‐>next != NULL)
{
temp2 = temp1;
temp1 = temp1 ‐>next;
}
temp2‐>next = NULL;
free(temp1);
printf("\nDeleted Node from the end ...\n");
}
}

//Deleting node from any random location of the Singly Linked List
void deleteRandom()
{
struct node* temp1;
struct node* temp2;
int loc,i;
if(head == NULL)
printf("\nList is empty...:(\n");
else
{
printf("\n Enter the location of the node after which you want to perform delet
scanf("%d",&loc);
temp1=head;
for(i=0;i<loc;i++)
{
temp2 = temp1;
temp1 = temp1‐>next;
if(temp1 == NULL)
{
printf("\nCan't be deleted (Select different location)");
return;
}
}
temp2‐>next = temp1‐>next;
free(temp1);
printf("\nDeleted Node %d ",loc+1);
}
}

//Searching for any specific data item into the Singly Linked List
void search()
{
struct node* temp;
int item,i=0,flag;
temp = head;
if(temp == NULL)
printf("\nList is Empty...:(\n");
else
{
printf("\nEnter item which you want to search: ");
scanf("%d",&item);
while (temp!=NULL)
{
if(temp‐>data == item)
{
printf("Item found at location %d ",i);
flag=0;
}
else
flag=1;
i++;
temp = temp‐>next;
}
if(flag==1)
printf("Item not found\n");
}
}

//Traversal Operation
void display()
{
struct node* temp;
temp = head;
if(temp == NULL)
printf("\nList is Empty...:(\n");
else
{
printf("\nPrinting values . . . :)\n\n");
while (temp!=NULL)
{
printf("%d",temp‐>data);
temp = temp ‐> next;
if(temp!=NULL) printf(" ‐> ");
else printf(" ‐> NULL ");
}
}
}

You might also like