Seit1 Rollno.27 Dsa Exp5
Seit1 Rollno.27 Dsa Exp5
Seit1 Rollno.27 Dsa Exp5
PRACTICAL. 5
Aim: - To implement menu driven linked list with basic operation:
insertion (all cases), deletion (all cases) and display all the elements.
Code: -
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head = NULL;
void display()
{
struct node *temp;
if(head == NULL)
{
printf("\n Linked List in empty");
return;
}
temp = head;
printf("\n Linked list is: ");
while(temp != NULL)
{
printf(" %d ",temp ->data);
temp = temp ->next;
}
}
void insert_beg()
{
struct node *newnode;
newnode = (struct node *)malloc(sizeof(struct node));
printf("\n Enter the value to be inserted: ");
scanf("%d", &newnode->data);
if(head == NULL)
{
newnode->next = NULL;
}
else{
newnode->next = head;
}
head = newnode;
}
void insert_end()
{
struct node *newnode, *temp;
newnode = (struct node *)malloc(sizeof(struct node));
printf("\n Enter the value to be inserted at end: ");
scanf("%d", &newnode->data);
newnode->next = NULL;
if(head == NULL){
head = newnode;
return;
}
temp = head;
while(temp ->next != NULL)
{
temp = temp ->next;
}
temp ->next = newnode;
}
void insert_bet()
{
struct node *temp;
int pos, count = 1, i = 1;
printf("\n Enter the position at which value is to be inserted: ");
scanf("%d", &pos);
temp = head;
while (temp ->next != NULL)
{
temp = temp ->next;
count += 1;
}
if( pos>count)
{
printf("\n Invalid position");
return;
}
else
{
temp = head;
while(i<pos)
{
temp = temp ->next;
i++;
}
struct node *newnode = (struct node *)malloc(sizeof(struct
node));
printf("\n Enter the value to be inserted: ");
int x;
scanf("%d", &x);
newnode->data = x;
newnode->next = temp ->next;
temp ->next = newnode;
}
}
void delete_beg()
{
struct node *temp;
temp = head;
head = head ->next;
free(temp);
printf("\n First Node Deleted");
}
void delete_end()
{
struct node *prevnode, *temp;
temp = head;
while(temp ->next != NULL)
{
prevnode = temp;
temp = temp ->next;
}
if(temp == head)
{
head = NULL;
printf("\n End node deleted");
free(temp);
}
else
{
prevnode->next = NULL;
printf("\n End node deleted");
free(temp);
}
}
void delete_bet()
{
struct node *temp,*nextnode;
int pos,count = 1, i = 1;
printf("\n Enter the position after which value is to be deleted: ");
scanf("%d",& pos);
temp = head;
while (temp ->next != NULL)
{
temp = temp ->next;
count += 1;
}
if( pos>count)
{
printf("\n Invalid position");
return;
}
else
{
temp = head;
while(i<pos)
{
temp = temp ->next;
i++;
}
nextnode = temp;
nextnode = temp ->next;
temp ->next = temp ->next ->next;
free(nextnode);
printf("\n Node Deleted after %d position", pos);
}
}
void main()
{
int ch;
while(ch!=9)
{
printf("\n ** MAIN MENU **");
printf("\n 1. Insert At Beginning:");
printf("\n 2. Insert At End:");
printf("\n 3. Insert At A Position(Between):");
printf("\n 4. Display");
printf("\n 5. Delete From Beginning");
printf("\n 6. Delete From End");
printf("\n 7. Delete From A Position(Between)");
printf("\n 8. To Exit");
printf("\n Enter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:insert_beg();
break;
case 2:insert_end();
break;
case 3:insert_bet();
break;
case 4:display();
break;
case 5:delete_beg();
break;
case 6:delete_end();
break;
case 7:delete_bet();
break;
case 8:printf("Exited Successfully!!");
exit(0);
default:printf("\n Invalid Input");
break;
}
}
}
Output: -
Conclusion: - Hence, we implemented a menu driven linked list in the
program.