DS SLLDLL
DS SLLDLL
WITH ‘C’
1
Sesson
by
3
Course outcomes
➢ On the completion of this laboratory course, the
students will be able to:
➢ Analyze and Compare various linear and non-
linear data structures
➢ Code, debug and demonstrate the working nature
of different types of data structures and their
applications
➢ Implement, analyze and evaluate the searching
and sorting algorithms
➢ Choose the appropriate data structure for solving
real world problems
4
Graduate Attributes
(as per NBA)
•
1. Engineering Knowledge
2. Problem Analysis
3. Design/Development of Solutions
4. Modern Tool Usage
5
Experiment
Design, Develop and Implement a menu driven Program in C for
the following operations on
Singly Linked List (SLL) of Student Data with the fields:
USN, Name, Branch, Sem, PhNo
6
Linear data structure:
A linear data structure traverses the data elements
sequentially, in which only one node can directly be
reached.
Ex: Arrays, Linked Lists
Linked List:
Linked list is an example of linear data storage or
structure.
Linked list stores data in an organized linear fashion.
They store data in the form of a list.
7
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 8
Linked List
Linked lists are the best and simplest example of a dynamic
data structure that uses pointers for its implementation.
8
Linked List Definition
• Linked List is Series of Nodes
9
Linked List
What is linked list Node ?
• Each Linked List Consists of Series of Nodes
10
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 11
Singly Linked List
• Singly linked list is the most basic linked data structure.
• In this the elements can be placed anywhere in the heap
memory unlike array which uses contiguous locations.
• Nodes in a linked list are linked together using a
link(next) field, which stores the address of the next i.e.
each node of the list refers to its successor
• The last node contains the NULL reference.
• It has a dynamic size, which can be determined only at
run time.
11
Singly Linked List
• Design the Node- Student:
struct student
{
char usn[12];
char name[25];
char branch[25];
int sem;
int phone_no;
struct student *link;
};
12
Singly Linked List
• Read the Student details:
STUD *temp;
temp=(STUD *)malloc(sizeof(STUD));
13
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 14
Singly Linked List
STUD *read_data()
{
char usn[12],name[25],branch[25]; printf("Enter Semester\n");
int sem,phone_no; scanf("%d",&sem);
STUD *temp; temp->sem=sem;
temp=(STUD *)malloc(sizeof(STUD)); printf("Enter Phone
printf("Enter the Students Number\n");
Details:\n"); scanf("%d",&phone_no);
printf("Enter USN\n"); temp->phone_no=phone_no;
scanf("%s",usn); temp->link=NULL;
strcpy(temp->usn,usn); return temp;
printf("Enter Name\n"); }
scanf("%s",name);
strcpy(temp->name,name);
printf("Enter Branch \n");
scanf("%s",branch);
strcpy(temp->branch,branch);
14
Singly Linked List
• Insert the Student node in front of the list:
0x2000 temp->link=first=NULL
First
NULL
STUD *insert_front(STUD *first)
{ 0x1000
STUD *temp;
temp=read_data();
temp->link=first;
return temp;
}
15
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 16
Singly Linked List
• Insert the Student node in front of the list:
0x2000
First
STUD *insert_end(STUD *first)
NULL
{
STUD *temp,*prev; 0x1000
temp=read_data();
if(first==NULL)
return temp;
prev=first;
while(prev->link!=NULL)
prev=prev->link;
prev->link=temp;
return first;
}
17
Singly Linked List
• Insert the Student node at the end of the list:
prev=first=0x5000
Prev->link!=NULL
Prev->link!=NULL
Prev->link!=NULL
0x2000 first=first->link=NULL
First
STUD *delete_front(STUD *first)
{ 0x2000
STUD *cur; 0x1000
if(first==NULL)
{
printf("List is empty\n");
return first;
}
cur=first;
first=first->link;
free(cur);
return first;
} 21
Singly Linked List
• Delete the Student node in front of the list:
first=first->link=NULL
STUD *delete_front(STUD *first)
{ First
STUD *cur; NULL
if(first==NULL)
{ 0x1000
printf("List is empty\n");
return first;
}
cur=first;
first=first->link;
free(cur);
return first;
}
22
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 23
Singly Linked List
• Delete the Student node at the end of the list:
first
0x5000 27
Singly Linked List
• Display the Students list:
void display(STUD *first)
{
STUD *temp; temp=first=0x5000
int count=0;
if(first==NULL)
{
printf("List is empty\n"); First
return;
0x5000
}
printf("USN\tNAME\tBRANCH\tSEM\tPHONE NO.\n"); 0x1000
temp=first;
while (temp!=NULL)
{
printf("%s\t%s\t%s\t%d\t%d\n",temp->
usn,temp->name,temp->branch,temp->sem,temp->
phone_no);
temp=temp->link;
count++;
}
printf("The number of nodes in SLL=%d\n",count); 28
}
Singly Linked List
• Delete the Student node at the end of the list:
temp=first=0x5000 temp!=NULL
temp=temp->link=0x2000
temp!=NULL
USN Name Branch Sem Phone_no *Link
1HK15IS001 Syed ISE 3 28440 0x3000
0x2000 temp=temp->link=0x3000 temp!=NULL
USN Name Branch Sem Phone_no *Link
1HK15IS003 natraj ISE 3 28442 NULL
0x3000 first
temp=temp->link=NULL
0x5000 29
Experiment No. 8
Design, Develop and Implement a menu driven Program in
C for the following operations on
30
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 31
Doubly Linked List
• A doubly linked list is a linked data structure that consists
of a set of sequentially linked records called nodes.
• Each node contains two fields, called links, that are
references to the previous and to the next node in the
sequence of nodes.
31
Doubly Linked List
• Design the Node- Employee:
struct emp
{
char SSN[12];
char name[25];
char dept[25];
char designation[25];
float salary;
int phone_no;
struct node *llink;
struct node *rlink;
};
32
Doubly Linked List
• Design the Node- Employee:
EMP *temp;
temp = (EMP *) malloc (sizeof(EMP));
33
Doubly Linked List
EMP *read_data()
{
char SSN[12],name[25],dept[25], printf("Enter Designation\n");
designation[25]; scanf("%s",designation);
int phone_no; strcpy(temp->designation,
float salary; designation);
EMP *temp; printf("Enter Salary\n");
temp=(EMP *)malloc(sizeof(EMP)); scanf("%f",&salary);
printf("Enter the Employees temp->salary=salary;
Details:\n"); printf("Enter PhoneNumber\n");
printf("Enter SSN\n"); scanf("%d",&phone_no);
scanf("%s",SSN); temp->phone_no=phone_no;
strcpy(temp->SSN,SSN); temp->rlink=temp->llink=NULL;
printf("Enter Name\n"); return temp;
scanf("%s",name); }
strcpy(temp->name,name);
printf("Enter Department\n");
scanf("%s",dept);
strcpy(temp->dept,dept);
34
Doubly Linked List
• Insert the Node- Employee at the front of list:
head
EMP *insert_front(EMP *head) NULL NULL
{
0x1000
EMP *temp,*next;
temp=read_data(); next
next=head->rlink; NULL NULL
head->rlink=temp; 0x3000
temp->llink=head;
next->llink=temp; temp
temp->rlink=next; NULL NULL
return head; 0x4000
}
35
Doubly Linked List
• Insert the Node- Employee at the front of list:
temp
*llink SSN Name Dept Design Salary Phone_no *rlink
NULL 1000 Laxmi cse prof 50000 2544 NULL
0x2000
head
NULL NULL
temp=read_data(); 0x1000
next
NULL NULL
0x3000
temp
NULL 0x2000 NULL
0x4000 36
Doubly Linked List
• Insert the Node- Employee at the front of list:
temp
*llink SSN Name Dept Design Salary Phone_no *rlink
NULL 1000 Laxmi cse prof 50000 2544 NULL
0x2000
head
NULL NULL
next=head->rlink; 0x1000
next
NULL NULL NULL
0x3000
temp
NULL 0x2000 NULL
0x4000 37
Doubly Linked List
• Insert the Node- Employee at the front of list:
temp
*llink SSN Name Dept Design Salary Phone_no *rlink
NULL 1000 Laxmi cse prof 50000 2544 NULL
0x2000
head
NULL NULL
next=head->rlink; 0x1000
next
NULL NULL NULL
0x3000
temp
NULL 0x2000 NULL
0x4000 38
Doubly Linked List
• Insert the Node- Employee at the front of list:
temp
*llink SSN Name Dept Design Salary Phone_no *rlink
NULL 1000 Laxmi cse prof 50000 2544 NULL
0x2000
head
NULL 0x2000
head->rlink=temp; 0x1000
next
NULL NULL NULL
0x3000
temp
NULL 0x2000 NULL
0x4000 39
Doubly Linked List
• Insert the Node- Employee at the front of list:
temp
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1000 Laxmi cse prof 50000 2544 NULL
0x2000
head
NULL 0x2000
temp->llink=head; 0x1000
next
NULL NULL NULL
0x3000
temp
0x1000 0x2000 NULL
0x4000 40
Doubly Linked List
• Insert the Node- Employee at the front of list:
temp
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1000 Laxmi cse prof 50000 2544 NULL
0x2000
head
NULL 0x2000
next->llink=temp; 0x1000
next
0x2000 NULL NULL
0x3000
temp
0x1000 0x2000 NULL
0x4000 41
Doubly Linked List
• Insert the Node- Employee at the front of list:
temp
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1000 Laxmi cse prof 50000 2544 NULL
0x2000
head
NULL 0x2000
temp->rlink=next; 0x1000
next
0x2000 NULL NULL
0x3000
temp
0x1000 0x2000 NULL
0x4000 42
Doubly Linked List
• Insert the Node- Employee at the front of list:
temp
*llink SSN Name Dept Design Salary Phone_no *rlink
NULL 1000 Laxmi cse prof 50000 2544 NULL
0x2000
head
NULL 0x2000
0x1000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1000 Laxmi cse prof 50000 2544 0x3000
0x2000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x2000 1001 adil cse prof 60000 2545 0x4000
0x3000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x3000 1002 ravi ise prof 70000 2546 NULL
0x4000
44
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 44
Doubly Linked List
• Insert the Node- Employee at the end of list:
temp
*llink SSN Name Dept Design Salary Phone_no *rlink
NULL 1003 heena ise prof 45000 2547 NULL
0x5000
EMP *insert_end(EMP *head)
{
EMP *cur,*temp;
temp=read_data();
cur=head;
while(cur->rlink!=NULL)
cur=cur->rlink;
cur->rlink=temp;
temp->llink=cur;
return head;
45
}
Doubly Linked List
• Insert the Node- Employee at the end of list:
head
NULL 0x2000
0x1000 cur=head
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1000 Laxmi cse prof 50000 2544 0x3000
0x2000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x2000 1001 adil cse prof 60000 2545 0x4000
0x3000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x3000 1002 ravi ise prof 70000 2546 0x5000
0x4000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x4000 1003 heena ise prof 45000 2547 NULL
46
0x5000 Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 46
Doubly Linked List
• Delete the Node- Employee at the front of list:
EMP *delete_front(EMP *head)
{
EMP *cur,*next;
if(head->rlink==NULL)
printf("List is empty\n");
else
{
cur=head->rlink;
if (cur->rlink==NULL)
head->rlink=NULL;
else
{
next=cur->rlink;
next->llink=head;
head->rlink=next;
}
free(cur);
}
return head;
} 47
Doubly Linked List
• Delete the Node- Employee at the front of list:
head
NULL 0x2000
1.
0x1000 cur=head->rlink
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1000 Laxmi cse prof 50000 2544 NULL
0x2000
if (cur->rlink==NULL)
head->rlink=NULL;
head
NULL NULL
0x1000
48
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 48
Doubly Linked List
• Delete the Node- Employee at the front of list:
head
NULL 0x2000
0x1000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1000 Laxmi cse prof 50000 2544 0x3000
0x2000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x2000 1001 adil cse prof 60000 2545 0x4000
0x3000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x3000 1002 ravi ise prof 70000 2546 NULL
0x4000
49
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 49
Doubly Linked List
• Delete the Node- Employee at the front of list:
head
NULL 0x2000
0x1000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1000 Laxmi cse prof 50000 2544 0x3000
0x2000 next=cur->rlink;
*llink SSN Name Dept Design Salary Phone_no *rlink
0x2000 1001 adil cse prof 60000 2545 0x4000
0x3000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x3000 1002 ravi ise prof 70000 2546 NULL
0x4000
50
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 50
Doubly Linked List
• Delete the Node- Employee at the front of list:
head
NULL 0x2000
0x1000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1000 Laxmi cse prof 50000 2544 0x3000
0x2000 next=cur->rlink
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1001 adil cse prof 60000 2545 0x4000
0x3000 next->llink=head
*llink SSN Name Dept Design Salary Phone_no *rlink
0x3000 1002 ravi ise prof 70000 2546 NULL
0x4000
51
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 51
Doubly Linked List
• Delete the Node- Employee at the front of list:
head
NULL 0x1000
0x1000 head->rlink=next
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1000 Laxmi cse prof 50000 2544 0x3000
0x2000 next=cur->rlink
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1001 adil cse prof 60000 2545 0x4000
0x3000 next->llink=head
*llink SSN Name Dept Design Salary Phone_no *rlink
0x3000 1002 ravi ise prof 70000 2546 NULL
0x4000
52
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 52
Doubly Linked List
• Delete the Node- Employee at the front of list:
EMP *delete_end(EMP *head)
{
EMP *cur,*prev;
free(cur);
if(head->rlink==NULL)
}
printf("List is empty\n");
return head;
else
}
{
cur=head->rlink;
if (cur->rlink==NULL)
head->rlink=NULL;
else
{
while(cur->rlink!=NULL)
{
prev=cur;
cur=cur->rlink;
}
prev->rlink=NULL;
}
53
Doubly Linked List
• Delete the Node- Employee at the end of list:
head
NULL 0x1000
0x1000 cur=head->rlink
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1000 Laxmi cse prof 50000 2544 0x3000
0x2000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1001 adil cse prof 60000 2545 0x4000
0x3000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x3000 1002 ravi ise prof 70000 2546 NULL
0x4000
54
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 54
Doubly Linked List
• Delete the Node- Employee at the end of list:
head
NULL 0x1000
0x1000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1000 Laxmi cse prof 50000 2544 0x3000
0x2000 prev=cur cur=cur->rlink
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1001 adil cse prof 60000 2545 0x4000
0x3000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x3000 1002 ravi ise prof 70000 2546 NULL
0x4000
55
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 55
Doubly Linked List
• Delete the Node- Employee at the end of list:
head
NULL 0x1000
0x1000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1000 Laxmi cse prof 50000 2544 0x3000
0x2000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1001 adil cse prof 60000 2545 0x4000
0x3000 prev=cur cur=cur->rlink
*llink SSN Name Dept Design Salary Phone_no *rlink
0x3000 1002 ravi ise prof 70000 2546 NULL
0x4000
56
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 56
Doubly Linked List
• Delete the Node- Employee at the end of list:
head
NULL 0x1000
0x1000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1000 Laxmi cse prof 50000 2544 0x3000
0x2000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1001 adil cse prof 60000 2545 0x4000
0x3000 prev=cur cur=cur->rlink
*llink SSN Name Dept Design Salary Phone_no *rlink
0x3000 1002 ravi ise prof 70000 2546 NULL
0x4000
57
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 57
Doubly Linked List
• Delete the Node- Employee at the end of list:
head
NULL 0x1000
0x1000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1000 Laxmi cse prof 50000 2544 0x3000
0x2000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1001 adil cse prof 60000 2545 NULL
0x3000 prev prev->rlink=NULL
*llink SSN Name Dept Design Salary Phone_no *rlink
0x3000 1002 ravi ise prof 70000 2546 NULL
0x4000 cur
58
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 58
Doubly Linked List
• Delete the Node- Employee at the end of list:
head
NULL 0x1000
0x1000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1000 Laxmi cse prof 50000 2544 0x3000
0x2000
*llink SSN Name Dept Design Salary Phone_no *rlink
0x1000 1001 adil cse prof 60000 2545 NULL
0x3000 prev prev->rlink=NULL
59
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 59
Experiment No. 9
Design, Develop and Implement a Program in C for the
following operations on Singly Circular Linked List (SCLL)
with header nodes.
a. Represent and Evaluate a Polynomial
P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
b. Find the sum of two polynomials
POLY1(x,y,z) and POLY2(x,y,z) and
store the result in POLYSUM(x,y,z)
Support the program with appropriate functions for each of the
above operations
60
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 31
Singly Circular Linked List
Nodes in a linked list are linked together using a link/next
field, which stores the address of the next node in the
link/next field of the previous node i.e. each node of the list
refers to its successor.
61
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 62
Singly Circular Linked List
62
Singly Circular Linked List
Design the Node- polynode
typedef struct
{
int coeff;
int x_exp;
int y_exp;
int z_exp;
struct polynode *link;
}polynode;
63
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 63
Singly Circular Linked List
read the Polynomial Node
polynode *readpoly()
{
polynode *poly=(polynode*)malloc(sizeof(polynode));
poly->link=poly;
int n,i;
int coeff,x_exp,y_exp,z_exp;
polynode *temp;
printf("Enter number of terms\n");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("Term %d:\n",i+1); printf("Enter the coefficient\n");
scanf("%d",&coeff);
printf("Enter exponent values for x,y and z\n");
scanf("%d%d%d",&x_exp,&y_exp,&z_exp);
temp=createnode(coeff,x_exp,y_exp,z_exp);
poly=attachnode(temp,poly);
}
return poly;
} 64
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 64
Singly Circular Linked List
read the Polynomial Node
polynode *createnode(int coeff,int x_exp,int y_exp,int z_exp)
{
polynode *node;
node=(polynode*)malloc(sizeof(polynode));
node->coeff=coeff;
node->x_exp=x_exp;
node->y_exp=y_exp;
node->z_exp=z_exp;
node->link=NULL;
return node;
}
65
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 65
Singly Circular Linked List
Attach the Polynomial Node
polynode *attachnode(polynode *node,polynode *poly)
{
polynode *cur;
cur=poly->link;
while(cur->link!=poly)
{
cur=cur->link;
}
cur->link=node;
node->link=poly;
return poly;
}
66
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 66
Thank You
67
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE