Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
26 views5 pages

Practical 4 DS

This document describes a C program that implements a linked list with menu-driven options to add nodes to the start, end, or intermediate positions of the list, delete nodes from the start, end, or by position, and display the list. The program uses structs to define list nodes with a data element and pointer to the next node. Functions are defined to initialize the list, add and delete nodes, and display the list. The main menu calls these functions to manipulate and view the linked list.

Uploaded by

Paresh Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Practical 4 DS

This document describes a C program that implements a linked list with menu-driven options to add nodes to the start, end, or intermediate positions of the list, delete nodes from the start, end, or by position, and display the list. The program uses structs to define list nodes with a data element and pointer to the next node. Functions are defined to initialize the list, add and delete nodes, and display the list. The main menu calls these functions to manipulate and view the linked list.

Uploaded by

Paresh Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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