0% found this document useful (0 votes)
9 views10 pages

Experiment No 6 DSLAB

Uploaded by

akbhai000008
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)
9 views10 pages

Experiment No 6 DSLAB

Uploaded by

akbhai000008
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/ 10

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

CLASS: SE COMPUTER/AIML/CS SUBJECT: DSLAB ODD SEM 2024-25

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.

What is a linked list in C?

A linked list can be defined as a collection of connected nodes. It is a linear data


structure. A linked list contains two parts such as:-

 Data Part:- Contains the data of the user.


 Pointer Part:- It points to the next member of the linked list.
In the above image, Head contains the address of the first node. And the last
node in the linked list points to NULL.
Nodes are stored at different locations.
Types of Linked List in C:-

There are 3 types of linked list such as:-

 Singly Linked List


 Doubly Linked List
 Circular Linked List

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

CLASS: SE COMPUTER/AIML/CS SUBJECT: DSLAB ODD SEM 2024-25


Singly linked list is the most common linked list among the others. The singly
linked list can be traversed only in one direction. It is a collection of ordered
sets of elements. In singly linked list, Each node has a data and a pointer to the
next node.

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.

Uses of Linked List in C:-

 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

CLASS: SE COMPUTER/AIML/CS SUBJECT: DSLAB ODD SEM 2024-25


1. Creating Nodes in Linked List
2. Insertion in Linked list

In a Singly linked list, you can perform insertion at different locations.

Operation What it does


Doing insertion at the beginning You can insert any element at the front of
of the list the linked list.
Inserting at the end of the linked You can perform insertion at the last of the
list linked list.
Inserting after the specified You can also insert a new node after a
node specified node.
3. Deletion, Traversing and Searching in Linked List

In a singly linked list, you can perform deletion at different locations.

Operation What it does


Performing deletion at
You can delete a node at the beginning of the
the beginning of the
linked list.
linked list
Performing deletion at You can perform deletion of the last node in
the end of the linked list the linked list.
You can also delete a node after a specified
Performing deletion
node.
after the specified node

Through traversing, we visit each node of the


Traversing linked list at least once to perform some
operations on it.
Through searching, you can match each
Searching element of the linked list with a given
element.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<alloc.h>

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

CLASS: SE COMPUTER/AIML/CS SUBJECT: DSLAB ODD SEM 2024-25


typedef struct emp_data
{
int eno;
char enm[30];
struct emp_data *next;
}emp;

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

CLASS: SE COMPUTER/AIML/CS SUBJECT: DSLAB ODD SEM 2024-25


case 1: create();
break;
case 2: display();
break;
case 3: append();
break;
case 4: insert();
break;
case 5: delet();
break;
case 6: search();
break;
case 7: modify();
break;
case 8: exit(0);
default:printf("\nEnter Proper Choice..");
}
}
printf("\nprogram exits here..");
getch();
}

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

CLASS: SE COMPUTER/AIML/CS SUBJECT: DSLAB ODD SEM 2024-25


{
start=node;
list=node;
}
else
{
list->next=node;
list=list->next;
}
printf("\nDo u want to continue(if yes press y): ");
fflush(stdin);
ch=getchar();

}
}

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

CLASS: SE COMPUTER/AIML/CS SUBJECT: DSLAB ODD SEM 2024-25


{
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);
list=start;
while(list->next!=NULL)
{
list=list->next;
}
list->next=node;
}

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

CLASS: SE COMPUTER/AIML/CS SUBJECT: DSLAB ODD SEM 2024-25


{
break;
}
}

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

CLASS: SE COMPUTER/AIML/CS SUBJECT: DSLAB ODD SEM 2024-25


}

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

CLASS: SE COMPUTER/AIML/CS SUBJECT: DSLAB ODD SEM 2024-25


{
printf("\nRecord is found..");
printf("\n emp no is :%d emp name is: %s ",list->eno,list->enm);
printf("\nEnter new emp name: ");
scanf("%s",list->enm);

printf("\none record is modified...");


break;
}

list=list->next;

if(list==NULL)
{
printf("\nRecord is not Found...");
}
}

OUTPUT

Prof. Melasagare S M

You might also like