You are on page 1of 5

Data Structures Lab S.Y. B. Tech.

(Sem III)
Practical No. 4
Implementation of linked list using menu driven approach.

Program :
#include<stdio.h>
#include<stdlib.h>
struct node{
int num;
struct node *next;
};
struct node *first, *cur, *prev;
void init(){
first=NULL;
}
void add_node(){
struct node *temp;
int t,ch,pos,st,found=0;
while(1){
printf("\n\tAdd Menu \n1. Add at start\n2. Add at End\n3.Add at intermediate position\n4.
Exit submenu\nEnter choice :");
scanf("%d",&ch);
switch(ch){
case 1: temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter data:");
scanf("%d",&t);
temp->num=t;
temp->next=first;
first=temp;
break;
case 2: temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter data:");
scanf("%d",&t);
temp->num=t;
cur=first;
if(cur==NULL){
first=temp;
}
else{
while(cur->next!=NULL){
cur=cur->next;
}
cur->next=temp;
}
temp->next=NULL;
break;
case 3: printf("\nEnter the position after which you want to enter data:");
scanf("%d",&pos);
st=1;
cur=first;
while(cur->next!=NULL){
if(st==pos){
found=1;
break;
}
else{
cur=cur->next;
st++;
}
}
if(found==1){
temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter data:");
scanf("%d",&t);
temp->num=t;
temp->next=cur->next;
cur->next=temp;
}
else{
printf("\nPosition out of range");
}
break;
case 4: return;
default:printf("\n\nWrong choice\n\n");
}
}
}
void del_node(){
struct node *temp;
int t, ch, pos, st, found=0;
while(1){
printf("\n\tDelete Menu\n1. Delete starting node\n2. Delete End node\n3. Delete a
intermediate node\n4.Exit submenu\nEnter choice:");
scanf("%d",&ch);
switch(ch){
case 1: if(first==NULL){
printf("\nEmpty list: nothing deleted");
break;
}
temp=(struct node*)malloc(sizeof(struct node));
temp=first;
first=first->next;
printf("\nNode deleted");
free(temp);
break;
case 2: if(first==NULL){
printf("\nEmpty list: nothing deleted");
break;
}
temp=(struct node*)malloc(sizeof(struct node));
cur=prev=first;
cur=cur->next;
while(cur->next!=NULL){
prev=cur;
cur=cur->next;
}
temp=cur;
prev->next=NULL;
printf("\nNode deleted");
free(temp);
break;
case 3: printf("\nEnter the position after which you want to delete node:");
scanf("%d",&pos);
st=1;
cur=first;
while(cur->next!=NULL){
if(st==pos){
found=1;
break;
}
else{
cur=cur->next;
st++;
}
}
if(found==1){
temp=cur->next;
cur->next=temp->next;
printf("\nNode deleted");
free(temp);
}
else{
printf("\nPosition out of range.");
}
break;
case 4: return;
default: printf("\nWrong choice\n\n");
}
}
}
void display(){
cur=first;
if(cur==NULL){
printf("\nEmpty List!");
return;
}
printf("\nLink list contents are:\n");
do{
printf("%d\n",cur->num);
cur=cur->next;
}while(cur!=NULL);
}
int main(){
int ch;
init();
while(1){
printf("\n\tMain Menu \n1. Add element\n2. Delete element\n3. Display elements\n4.
Exit\nEnter choice:");
scanf("%d",&ch);
switch(ch){
case 1: add_node();
break;
case 2: del_node();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong choice.\n");
}
}
}
Output:

You might also like