linkedList
linkedList
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..");
}
}
}
//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 ");
}
}
}