0% found this document useful (0 votes)
16 views67 pages

DS SLLDLL

SLLDLL

Uploaded by

sajood
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)
16 views67 pages

DS SLLDLL

SLLDLL

Uploaded by

sajood
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/ 67

DATA STRUCTURES

WITH ‘C’

1
Sesson

by

Dr. A. Syed Mustafa Ph.D


Professor & HOD
Dept. of ISE, HKBKCE
2
SYLLABUS

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

a. Create a SLL of N Students Data by using front insertion.


b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of SLL
d. Perform Insertion and Deletion at Front of SLL
e. Demonstrate how this SLL can be used as STACK and QUEUE
f. Exit

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.

Linked lists have a few advantages over arrays:


1. Items can be added or removed from the middle of the list
2. There is no need to define an initial size
3. An array is a static data structure. This means the length of
array cannot be altered at run time. While, a linked list is a
dynamic data structure.
4. In an array, all the elements are kept at consecutive memory
locations while in a linked list the elements (or nodes) may be
kept at any location but still connected to each other.

8
Linked List Definition
• Linked List is Series of Nodes

• Each node Consist of two Parts


viz Data Part & Pointer Part

• Pointer Part stores the address of the next node

9
Linked List
What is linked list Node ?
• Each Linked List Consists of Series of Nodes

• In above Diagram , Linked List Consists of three nodes


Node1, Node2 and Node3 etc
• Node 1 has two part one data part which consists of the 5
as data and the second part which contain the address of
the next node.

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:

USN Name Branch Sem Phone_no *Link

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:

USN Name Branch Sem Phone_no *Link


1HK15IS001 Syed ISE 3 28440 NULL

typedef struct student STUD;

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:

temp USN Name Branch Sem Phone_no *Link


1HK15IS001 Syed ISE 3 28440 NULL

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:

temp USN Name Branch Sem Phone_no *Link


1HK15IS002 mustafa ISE 3 28441 0x2000
0x5000

USN Name Branch Sem Phone_no *Link


First
1HK15IS001 Syed ISE 3 28440 NULL
0x2000
First
0x2000
0x1000
16
Prof. A. Syed Mustafa (Ph.D), Professor, HKBKCE 17
Singly Linked List
• Insert the Student node at the end of the list:

temp USN Name Branch Sem Phone_no *Link


1HK15IS001 Syed ISE 3 28440 NULL

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

first USN Name Branch Sem Phone_no *Link


1HK15IS002 mustafa ISE 3 28441 0x2000
0x5000 Prev->link!=NULL

Prev->link!=NULL

USN Name Branch Sem Phone_no *Link


1HK15IS001 Syed ISE 3 28440 NULL
0x2000

USN Name Branch Sem Phone_no *Link


1HK15IS003 natraj ISE 3 28442 NULL
first
0x5000 18
Singly Linked List
• Insert the Student node at the end of the list:
prev=first=0x5000

first USN Name Branch Sem Phone_no *Link


1HK15IS002 mustafa ISE 3 28441 0x2000
0x5000 Prev->link!=NULL

Prev->link!=NULL

USN Name Branch Sem Phone_no *Link


1HK15IS001 Syed ISE 3 28440 0x3000
0x2000

USN Name Branch Sem Phone_no *Link


1HK15IS003 natraj ISE 3 28442 NULL
0x3000 first
0x5000 19
Singly Linked List
• Insert the Student node at the end of the list:
prev=first=0x5000

first USN Name Branch Sem Phone_no *Link


1HK15IS002 mustafa ISE 3 28441 0x2000
0x5000 Prev->link!=NULL

Prev->link!=NULL

USN Name Branch Sem Phone_no *Link


1HK15IS001 Syed ISE 3 28440 0x3000
0x2000

USN Name Branch Sem Phone_no *Link


1HK15IS003 natraj ISE 3 28442 NULL
0x3000 first
0x5000 20
Singly Linked List
• Delete the Student node in front of the list:

first USN Name Branch Sem Phone_no *Link


1HK15IS001 Syed ISE 3 28440 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 USN Name Branch Sem Phone_no *Link


1HK15IS002 mustafa ISE 3 28441 0x2000
0x5000

USN Name Branch Sem Phone_no *Link


1HK15IS001 Syed ISE 3 28440 0x3000
0x2000

USN Name Branch Sem Phone_no *Link


1HK15IS003 natraj ISE 3 28442 NULL
0x3000 first
0x5000 23
Singly Linked List
• Delete the Student node at the end of the list:
STUD *delete_end(STUD *first)
{
STUD *prev,*cur; prev=NULL
if(first==NULL) Cur=first=0x5000
{
printf("List is empty\n"); First
return first; 0x5000
}
prev=NULL; 0x1000
cur=first;
while(cur->link!=NULL)
{
prev=cur;
cur=cur->link;
}
prev->link=NULL;
free(cur);
return first; 24
}
Singly Linked List
• Delete the Student node at the end of the list:
cur=first=0x5000

first USN Name Branch Sem Phone_no *Link


1HK15IS002 mustafa ISE 3 28441 0x2000
0x5000
cur->link!=NULL
Prev=cur=0x5000
Cur=cur->link=0x2000

USN Name Branch Sem Phone_no *Link


1HK15IS001 Syed ISE cur->link!=NULL
3 28440 0x3000
Prev=cur=0x2000
0x2000 Cur=cur->link=0x3000
USN Name Branch Sem Phone_no *Link
1HK15IS003 natraj ISE 3cur->link!=NULL
28442 NULL
0x3000 first
0x5000 25
Singly Linked List
• Delete the Student node at the end of the list:

first USN Name Branch Sem Phone_no *Link


1HK15IS002 mustafa ISE 3 28441 0x2000
0x5000

USN Name Branch Sem Phone_no *Link


1HK15IS001 Syed ISE prev->link=NULL
3 28440 0x3000
0x2000

USN Name Branch Sem Phone_no *Link


1HK15IS003 natraj ISE 3cur->link!=NULL
28442 NULL
0x3000 first
0x5000 26
Singly Linked List
• Delete the Student node at the end of the list:

first USN Name Branch Sem Phone_no *Link


1HK15IS002 mustafa ISE 3 28441 0x2000
0x5000

USN Name Branch Sem Phone_no *Link


1HK15IS001 Syed ISE prev->link=NULL
3 28440 NULL
0x2000

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

first USN Name Branch Sem Phone_no *Link


1HK15IS002 mustafa ISE 3 28441 0x2000
0x5000

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

Doubly Linked List (DLL) of Employee Data with the fields:


SSN, Name, Dept, Designation,Sal, PhNo

a. Create a DLL of N Employees Data by using end insertion.


b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue
f. Exit

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:

*llink SSN name dept design salary Phone_no *rlink

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:

*llink SSN name dept design salary Phone_no *rlink

typedef struct emp EMP;

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

EMP *insert_front(EMP *head) head


{ NULL 0x2000
EMP *temp,*next; 0x1000
temp=read_data();
next=head->rlink;
head->rlink=temp;
temp->llink=head;
next->llink=temp;
temp->rlink=next;
return head; 43
} Prof. A. Syed Mustafa Professor, HKBKCE
Doubly Linked List
• Insert the Node- Employee at the end 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

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.

The last node points back to the first node.

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;
}

coeff x_exp y_exp z_exp *link


6 2 2 1 NULL

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

You might also like