#include <stdio.h>
#include <stdlib.h>
// Linked List Node
struct node
{
int data;
struct node *link,*next;
};
struct node *start = NULL;
// Function to traverse the linked list
void display()
{
struct node *temp;
// check for underflow
if (start == NULL)
printf("\n!!OOPS!!,Underflow...\n");
else
{
temp = start;
printf("\nThis is your list-");
while (temp != NULL)
{
printf("Element is-%d,", temp->data);
temp = temp->link;
}
}
}
// Function to insert at the front
// of the linked list
void insertAtBeginning()
{
int d;
struct node *temp;
temp = malloc(sizeof(struct node));
printf("\nEnter element to be inserted-");
scanf("%d", &d);
temp->data = d;
// Pointer of temp will be
// assigned to start
temp->link = start;
start = temp;
printf("!!Element inserted successfuly");
}
void insertAtEnd()
{
int d;
struct node *temp, *head;
temp = malloc(sizeof(struct node));
// Enter the number
printf("\nEnter number to"
" be inserted : ");
scanf("%d", &d);
// Changes links
temp->link = 0;
temp->data = d;
head = start;
while (head->link != NULL)
{
head = head->link;
}
head->link = temp;
}
void insertAtPosition()
{
struct node *temp, *newnode;
int pos, d, i = 1;
newnode = malloc(sizeof(struct node));
// Enter the position and data
printf("\nEnter position where you want to be insert-");
scanf("%d", &pos);
printf("Enter your element-");
scanf("%d", &d);
// Change Links
temp = start;
newnode->data = d;
newnode->link = 0;
while (i < pos - 1)
{
temp = temp->link;
i++;
}
newnode->link = temp->link;
temp->link = newnode;
printf("\n!!Element inserted successfuly....");
}
void deleteAtBeginning()
{
struct node *temp;
if (start == NULL)
printf("\n!!OOPS,List Underflow .....\n");
else
{
temp = start;
start = start->link;
free(temp);
printf("\n!!Deleted Successfully....\n");
}
}
void deleteAtEnd()
{
struct node *temp, *prevnode;
if (start == NULL)
printf("\n!!OOPS,List Underflow.....\n");
else
{
temp = start;
while (temp->link != 0)
{
prevnode = temp;
temp = temp->link;
}
free(temp);
prevnode->link = 0;
printf("\n!!Deleted Successfully....\n");
}
}
void deletePosition()
{
struct node *temp, *position;
int i = 1, pos;
if (start == NULL)
printf("\n!!OOPS,List Underflow....\n");
else
{
printf("\nEnter index number-");
scanf("%d", &pos);
position = malloc(sizeof(struct node));
temp = start;
while (i < pos - 1)
{
temp = temp->link;
i++;
}
position = temp->link;
temp->link = position->link;
// Free memory
free(position);
printf("\n!!Deleted Successfully..\n");
}
}
void search()
{
struct node *ptr,*temp,*head;
int d,i=0,flag;
ptr = head;
if(start == NULL)
{
printf("\n!!OOPS,List Underflow\n");
}
else
{
printf("\nEnter element which you want to search-");
scanf("%d",&d);
while (temp !=NULL)
{
if(ptr->data == d)
{
printf("Element found at location %d ",i+1);
flag=0;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("\n!!SOORY,Element not found!!\n");
}
}
}
void main()
{
int choice;
do
{
printf("\n----Linked List----");
printf("\n 1. Display");
printf("\n 2. Insert at starting");
printf("\n 3. Insert at random");
printf("\n 4. Insert at end");
printf("\n 5. Delete first element");
printf("\n 6. Delete last element");
printf("\n 7. Delete random element");
printf("\n 8. Exit");
printf("\n 9. Search");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: display();
break;
case 2: insertAtBeginning();
break;
case 3: insertAtPosition();
break;
case 4: insertAtEnd();
break;
case 5: deleteAtBeginning();
break;
case 6: deleteAtEnd();
break;
case 7: deletePosition();
break;
case 8: exit(1);
break;
case 9: search();
break;
default:printf("\nEnter valid choice.");
}
} while(choice!=8);
}