Experiment No 6 DSLAB
Experiment No 6 DSLAB
College of Engineering
A/P : Harkul Budruk, Tal : Kankavli, Dist : Sindhudurg, Pin : 416602, Tel : (02367) 231525, 230214, Fax : 02367-
230214 Affiliated to University of Mumbai
EXPERIMENT NO.6
AIM Implement Singly Linked List ADT.
THEORY Linked List
From the name, you can easily say that it is used for linking lists together. But
to put it in a nice way, you can say that a linked list is the sequence of linked
lists which are connected with each other through links. Linked list in C is a
linear data structure where the data is stored at different locations and they are
linked using pointers. You have to use pointers to implement the linked list
data structure.
Prof. Melasagare S M
Sindhudurg Shikshan Prasarak Mandal’s
College of Engineering
A/P : Harkul Budruk, Tal : Kankavli, Dist : Sindhudurg, Pin : 416602, Tel : (02367) 231525, 230214, Fax : 02367-
230214 Affiliated to University of Mumbai
Syntax:-
struct node{
int data;
struct node *next;
}
There are several advantages and disadvantages while using an array. But with
a linked list, you can overcome the problems while using an array.
If you use array in the program code then you will find these following
limitations:-
You have to know the size of the array before using it in the program code.
The elements of the array are stored in contiguous memory locations.
You cannot increase the size of the array at run time.
To prevent the above limitations, you can make use of Linked List.
In the linked list, we do not have to define the size during declaration.
Linked list dynamically allocates the memory. It is not like an array. The
nodes are stored at non-contiguous memory locations.
Each and every node must have data stored into it.
You don’t have to declare the size during declaration. It is limited to
memory size.
In a singly linked list, you can store the values of primitive types or objects.
Performing operations on Singly Linked List:-
Below is a list of operations which you can perform upon a Singly Linked List.
Prof. Melasagare S M
Sindhudurg Shikshan Prasarak Mandal’s
College of Engineering
A/P : Harkul Budruk, Tal : Kankavli, Dist : Sindhudurg, Pin : 416602, Tel : (02367) 231525, 230214, Fax : 02367-
230214 Affiliated to University of Mumbai
Prof. Melasagare S M
Sindhudurg Shikshan Prasarak Mandal’s
College of Engineering
A/P : Harkul Budruk, Tal : Kankavli, Dist : Sindhudurg, Pin : 416602, Tel : (02367) 231525, 230214, Fax : 02367-
230214 Affiliated to University of Mumbai
emp *start,*list,*node;
void create();
void append();
void display();
void insert();
void delet();
void search();
void modify();
void main()
{
int ch=0;
clrscr();
while(ch!=8)
{
printf("\n****MAIN MENU****");
printf("\n1.CREATE");
printf("\n2.DISPLAY");
printf("\n3.APPEND NODE");
printf("\n4.INSERT");
printf("\n5.DELETE");
printf("\n6.SEARCH");
printf("\n7.MODIFY");
printf("\n8.EXIT");
printf("\nEnter Your Choice: ");
scanf("%d",&ch);
switch(ch)
{
Prof. Melasagare S M
Sindhudurg Shikshan Prasarak Mandal’s
College of Engineering
A/P : Harkul Budruk, Tal : Kankavli, Dist : Sindhudurg, Pin : 416602, Tel : (02367) 231525, 230214, Fax : 02367-
230214 Affiliated to University of Mumbai
void create()
{
char ch='y';
start=NULL;
list=NULL;
while(ch=='y')
{
node=(emp*)malloc(1*sizeof(emp));
node->next=NULL;
printf("\nEnter emp no: ");
scanf("%d",&node->eno);
printf("\nEnter emp name: ");
scanf("%s",&node->enm);
if(start==NULL)
Prof. Melasagare S M
Sindhudurg Shikshan Prasarak Mandal’s
College of Engineering
A/P : Harkul Budruk, Tal : Kankavli, Dist : Sindhudurg, Pin : 416602, Tel : (02367) 231525, 230214, Fax : 02367-
230214 Affiliated to University of Mumbai
}
}
void display()
{
if(start==NULL)
{
printf("\nlist is an empty..");
}
else
{
list=start;
while(list!=NULL)
{
printf("\nemp no:%d",list->eno);
printf("\nemp noame:%s",list->enm);
list=list->next;
}
}
getch();
}
void append()
Prof. Melasagare S M
Sindhudurg Shikshan Prasarak Mandal’s
College of Engineering
A/P : Harkul Budruk, Tal : Kankavli, Dist : Sindhudurg, Pin : 416602, Tel : (02367) 231525, 230214, Fax : 02367-
230214 Affiliated to University of Mumbai
void insert()
{
int c,p;
node=(emp*)malloc(1*sizeof(emp));
node->next=NULL;
printf("\nEnter emp no: ");
scanf("%d",&node->eno);
printf("\nEnter emp name: ");
scanf("%s",&node->enm);
printf("\nInsert position where u want to insert node: ");
scanf("%d",&p);
c=1;
list=start;
while(list->next!=NULL)
{
if(c<(p-1))
{
list=list->next;
c++;
}
else
Prof. Melasagare S M
Sindhudurg Shikshan Prasarak Mandal’s
College of Engineering
A/P : Harkul Budruk, Tal : Kankavli, Dist : Sindhudurg, Pin : 416602, Tel : (02367) 231525, 230214, Fax : 02367-
230214 Affiliated to University of Mumbai
node->next=list->next;
list->next=node;
list=list->next;
}
void delet()
{
int c,p;
node=(emp*)malloc(1*sizeof(emp));
node->next=NULL;
printf("\nenter position that u want to delete node: ");
scanf("%d",&p);
c=1;
list=start;
while(c<(p-1))
{
list=list->next;
c++;
}
if(p>1)
{
node=list->next;
list->next=node->next;
free(node);
}
else if(p==1)
{
node=start;
start=node->next;
free(node);
Prof. Melasagare S M
Sindhudurg Shikshan Prasarak Mandal’s
College of Engineering
A/P : Harkul Budruk, Tal : Kankavli, Dist : Sindhudurg, Pin : 416602, Tel : (02367) 231525, 230214, Fax : 02367-
230214 Affiliated to University of Mumbai
void search()
{
int sno;
printf("\nEnter emp no to search record of employee: ");
scanf("%d",&sno);
list=start;
while(list!=NULL)
{
if(list->eno==sno)
{
printf("\nRecord is Found..");
printf("emp name is: %s ",list->enm);
break;
}
list=list->next;
if(list==NULL)
{
printf("\nRecord not found..");
}
}
}
void modify()
{
int mno;
printf("\nEnter emp no to modify record of employee: ");
scanf("%d",&mno);
list=start;
while(list!=NULL)
{
if(list->eno==mno)
Prof. Melasagare S M
Sindhudurg Shikshan Prasarak Mandal’s
College of Engineering
A/P : Harkul Budruk, Tal : Kankavli, Dist : Sindhudurg, Pin : 416602, Tel : (02367) 231525, 230214, Fax : 02367-
230214 Affiliated to University of Mumbai
list=list->next;
if(list==NULL)
{
printf("\nRecord is not Found...");
}
}
OUTPUT
Prof. Melasagare S M