Professional Documents
Culture Documents
Department of Computer Science and Engineering
Department of Computer Science and Engineering
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Course Learning Objectives:
• Identify the importance of data structures & memory allocation.
• Perform operations on stacks and queues and its applications.
• Apply the operations of linked list, Trees & Graphs in various applications.
• Apply searching and sorting operations in real time applications.
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B) status.
Accredited by NBA and NAAC.
Course outcomes:
CO1 Identify the necessity of data structure and its storage process.
CO2: Analyse the various operations performed on stack and queues for
different applications.
CO3: Perform various operations on linked list for different applications.
CO4: Learn Trees and its applications.
CO5: Analyse the concepts of Graphs, searching, sorting & hashing in real
time.
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Syllabus
Module-1
Introduction: Data Structures, Classifications (Primitive & Non Primitive), Data structure
Operations, Review of Arrays, Structures, Self-Referential Structures. Pointers and
Dynamic Memory Allocation Functions. Representation of Linear Arrays in Memory,
Dynamically allocated arrays. Array Operations: Traversing, inserting, deleting, searching,
and sorting. Multidimensional Arrays, Polynomials and Sparse Matrices. Strings: Basic
Terminology, Storing, Operations and Pattern Matching algorithms. Programming
Examples.
Module 2
Stacks: Definition, Stack Operations, Array Representation of Stacks, Stacks using
Dynamic Arrays, Stack Applications: Polish notation, Infix to postfix conversion,
evaluation of postfix expression. Recursion - GCD, Tower of Hanoi. Queues:
Definition, Array Representation, Queue Operations, Circular Queues, Circular
queues using Dynamic arrays, Dequeues, Priority Queues. Programming 4
Examples.
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Module-3
Linked Lists: Definition, Representation of linked lists in Memory, Memory
allocation; Garbage Collection. Linked list operations: Traversing, Searching,
Insertion, and Deletion. Doubly Linked lists, Circular linked lists, and header linked
lists. Linked Stacks and Queues. Applications of Linked lists – Polynomials.
Programming Examples
Module-4
Trees: Terminology, Binary Trees, Properties of Binary trees, Array and linked
Representation of Binary Trees, Binary Tree Traversals - Inorder, postorder,
preorder; Additional Binary tree operations. Threaded binary trees, Binary Search
Trees – Definition, Insertion, Deletion, Traversal, Searching, Application of Trees-
Evaluation of Expression, AVL Trees, Splay Trees, B-Tree, Programming Examples
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Module-5
Graphs: Definitions, Terminologies, Matrix and Adjacency List Representation of
Graphs, Elementary Graph operations, Traversal methods: Breadth First Search
and Depth First Search, Topological Sort. Sorting and Searching: Quick sort,
Insertion Sort, Radix sort, Merge Sort, Address Calculation Sort.
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Reference Books:
1. Ellis Horowitz and Sartaj Sahni, Fundamentals of Data Structures in C, 2nd
Ed, Universities Press, 2014.
2. Seymour Lipschutz, Data Structures Schaum's Outlines, Revised 1st Ed,
McGraw Hill, 2014.
3.Reema Thareja, Data Structures using C, 3rd Ed, Oxford press, 2012
4.Mark Allen Weiss, ―Data Structures and Algorithm Analysis in C‖, 2nd
Edition, Pearson Education,1997
5.Gilberg & Forouzan, Data Structures: A Pseudo-code approach with C, 2nd
Ed, Cengage Learning,2014
6. Jean-Paul Tremblay & Paul G. Sorenson, An Introduction to Data Structures
with Applications, 2 nd Ed, McGraw Hill, 2013
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Bridge Topics
• Array
sequential representation
• Disadvantage
• data movements during insertion and deletion
• waste space in storing n ordered lists of varying size
• Possible solution
• linked list
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Bridge Topics Cont..
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Bridge Topics Cont..
• Structure
Ordinary structure
struct group
{
int a;
float b;
};
struct group gh;
Here gh is a structure variable and we can access the members of structure by using ‘.’
operator
ie, gh.a and gh.b
10
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Bridge Topics Cont..
• Structure
Consider another example of ordinary structure
struct group
{
int a;
float b;
};
struct group *gh;
• Here gh is a pointer to structure and it can store address of another structure variable of
same type or it can store NULL value.
• we can access the members of structure by using ‘.’ operator
• ie, *gh.a and *gh.b
• Another way of writing *gh.a is gha and *gh.b is ghb(read as gh pointer a and gh
pointer b. 11
13
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Types of Linked List
Linked List
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Linked List Cont..
RESENTATION OF LINKED LISTS IN MEMORY
Let LIST be a linked list. Then LIST will be maintained in memory as follows.
1. LIST requires two linear arrays such as INFO and LINK-such that INFO[K] and
LINK[K] contains the information part and the next pointer field of a node of LIST.
2. LIST also requires a variable name such as START which contains the location of the
beginning of the list, and a next pointer sentinel denoted by NULL-which indicates the
end of the list.
15
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
RESENTATION OF LINKED LISTS IN MEMORY Cont..
START
INFO LINK
• START=9 INFO[9]=I LINK[9]=3
9 1
2 • INFO[3]=N LINK[3]=6
INFO[6]=D LINK[6]=11
3 N 6
INFO[11]=I LINK[11]=7
4
• INFO[7]= A LINK[7]=10
5
6 D 11
• INFO[10]= N LINK[10]=NULL value,
So the list has ended
7 A 10
8
9 I 3
10 N NULL
11 I 7
12
16
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
MEMORY ALLOCATION - GARBAGE COLLECTION
17
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
MEMORY ALLOCATION - GARBAGE COLLECTION Cont..
START INFO LINK
9 1 NULL
2 5
3 N 6
4 1
5 12
AVAIL
6 D 11
8 7 A 10
8 2
9 I 3
10 N NULL
11 I 7
12 4
18
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Garbage Collection
• Suppose some memory space becomes reusable because a node is deleted from a list
or an entire list is deleted from a program. So space is need to be available for future
use.
• One way to bring this is to immediately reinsert the space into the free-storage list.
However, this method may be too time-consuming for the operating system of a
computer, which may choose an alternative method, as follows.
• The operating system of a computer may periodically collect all the deleted space onto
the free storage list. Any technique which does this collection is called garbage
collection. Garbage collection takes place in two steps.
1. First the computer runs through all lists, tagging those cells which are currently in
use
2. And then the computer runs through the memory, collecting all untagged space
19
onto the free-storage list.
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Overflow
• Sometimes new data are to be inserted into a data structure but there is no
available space, i.e., the free-storage list is empty. This situation is usually called
overflow.
• The programmer may handle overflow by printing the message OVERFLOW. In
such a case, the programmer may then modify the program by adding space to
the underlying arrays.
• Overflow will occur with linked lists when AVAIL = NULL and there is an insertion.
Underflow
• The term underflow refers to the situation where one wants to delete data from a
data structure that is empty.
• The programmer may handle underflow by printing the message UNDERFLOW.
• The underflow will occur with linked lists when START = NULL and there is a
20
deletion.
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Singly Linked List
• A node can be viewed as a block consisting of two major fields : a data field and
a pointer field. The pointer field is used to interconnect nodes of a list.
• A linked list is a data structure which can change during execution.
• Successive elements are connected by pointers.
• Last element points to NULL.
• It can grow or shrink in size during execution of a program.
• It can be made just as long as required.
• It does not waste memory space.
start
23 20 45 NULL
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Singly Linked List
A simple representation of singly linked list would look like the below:
typedef struct
{
int data;
data next
struct node *next;
}node;
node
22
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Singly Linked List Cont..
• Declaration of variable of type node
node *start,*p,*temp;
• Initially start would contain NULL value
start = NULL; 10 NULL
p= (node *)malloc(sizeof(node)); p
• This instruction creates one node of type node . Memory gets allocated using DMA function
malloc(). Created node in the above instruction is referred to as “p”.
p->data=10;
• This instruction assigns value 10 to the data part of node p
p->next =NULL;
• This instruction sets NULL value to the next part of p
23
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Singly Linked List Cont..
temp= (node *)malloc(sizeof(node));
• This instruction creates one node of type struct . Memory gets allocated using DMA
function malloc(). Created node in the above instruction is referred to as “temp”
temp->data=20;
• This instruction assigns value 20 to the data part of node temp
temp->next =NULL;
• This instruction sets NULL value to the next part of temp
p->next =temp;
• This instruction assigns next part of p with temp node address
temp
p
10 NULL
2000 20 NULL
24
1000 2000
Creation of singly linked list
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Singly linked list operations
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Insertion at beginning
Step1:
Create a new node. Assign value to the data part of node. Set next part with NULL
value
10 NULL
p
Step 2:
If start = NULL ,then assign start with newly created node address
start
10 NULL
p
Step3:
If start !=NULL, then assign newly created node next part with start node address
26
and set start to point to newly created node
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Insertion at beginning Cont..
insertfront(int item) (Algorithm) start
1. Create node
1. p=(node *)malloc(sizeof(node)); 23 20 45 NULL
2. p->data=item , p->next=NULL
3. IF start==NULL Then Figure 3.7– Before insertion
start=p;
4. ELSE
p->next=start; start
start=p
[END IF] 23 20 45 NULL
5. Return
10 NULL
27
Figure 3.8– After insertion
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
C Function
Case 1: Linked list empty
void insertfront(int item)
{ node *p,*t;
start NULL 10 NULL
p=(node *)malloc(sizeof(node));
p->data=item; p
p->next=NULL;
if(start==NULL)
{
start=p;
}
Case 2: Linked list already contains some data value
else start
{
p->next=start; 23 20 45 NULL
start=p;
}
}
10 NULL
28
p
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Insertion at end
Step1:
Create a new node. Assign value to the data part of node. Set next part with NULL
value
10 NULL
p
Step 2:
If start = NULL ,then assign start with newly created node address
start
10 NULL
Step3:
If start !=NULL, then traverse the list to find the last node. Assign next part of last node
with new node address.
29
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Insertion at end Cont..
start
23 20 45 NULL
start temp
23 20 45 NULL
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
C Function
void insertend(int item)
{
node *p,*t;
p=(node *)malloc(sizeof(node));
p->data=item;
p->next=NULL; start t
if(start==NULL)
start=p; 23 20 45 NULL
else
{
t=start;
while(t->next!=NULL) 10 NULL
{
t=t->next; p
}
t->next=p;
}
31
}
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Insertion at Specified Position
Step1
Create a new node. Assign value to the data part of node.
10
p
Step2
If we want to insert the new node at the position i, then search and find (i-1) position
node address.
Step3
Set the next part of new node with (i+1) position node address
Step 4
Set the next part of (i-1) position node with new node address
32
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Insertion at Specified Position Cont..
start
23 20 45 50 NULL
start i
23 20 45 50 NULL
pos=4
10
p 33
Figure 3.10– After insertion
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
C Function
Position , pos=4
void insertpos(int item,int pos)
start
{
node *p,*t; t
int i;
p=(node *)malloc(sizeof(node));
p->data=item; 23 20 45 50 NULL
t=start; pos=4
for(i=1;i<pos-1;i++)
{
10
t=t->next;
} p
p->next=t->next;
t->next=p;
}
34
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Deletion at the beginning position of the linked list
Step1
Check list is empty or not. If the list is empty print empty list
Step2
If the list contains only one node then delete that node and set start pointer to NULL
value, goto step4
Step3
Set the start pointer to point to the second node in the linked list and delete the first node
Step4
Exit 35
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Deletion at the beginning position of the linked list Cont..
start
23 20 45 50 NULL
start
23 20 45 50 NULL
start
20 45 50 NULL
36
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Deletion at the ending position of the linked list
Step1
Check list is empty or not. If the list is empty print empty list
Step2
If the list contains only one node then delete that node and set start pointer to NULL
value
Step3
Traverse the list to find the second last node and make the address field of that node to
NULL value. Then delete the last node
38
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Deletion at the ending position of the linked list Cont..
start
23 20 45 50 NULL
start t
23 20 45 NULL 50 NULL
start
23 20 45 NULL
39
Figure 3.14– After deletion
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
C Function
void deleteend()
{
node *p,*t;
if(start==NULL)
printf("Linked list empty\n");
else if (start->next==NULL)
{
printf("Element deleted:%d\n",start->data); Case 3: More than one node in the list
free(start);
start=NULL;
} start
else tp
{
p=start; 23 20 NULL 45 NULL
while(p->next!=NULL)
{
t=p;
p=p->next;
}
t->next=NULL;
printf("element deleted:%d\n",p->data);
free(p); 40
p=NULL;
}
}
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Deletion at the specified position of the linked list
Step1
Check list is empty or not. If the list is empty print empty list
Step2
If we want to delete a node from position i , traverse the list to find addresses of (i-1)
position node and position i node.
Step3
Make the address field of (i-1) position node to the address of (i+1) position node
41
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Deletion at the specified position of the linked list Cont..
start
23 20 45 50 NULL
23 20 45 50 NULL
pos =3
start
23 20 50 NULL
42
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
C Function
void deletespecifiedpos(int pos)
{
node *p,*t;
int i;
if(start==NULL)
printf("Linked list empty\n");
else
{ pos =3
p=start; start p
for(i=1;i<pos;i++) t
{ 23 20 45 50 NULL
t=p;
p=p->next;
}
t->next=p->next;
printf("element deleted:%d\n",p->data);
free(p);
p=NULL;
}
43
}
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Traversing Singly Linked list
• This operation is used to visit each one of the list exactly once in order to access
the info stored in nodes.
1: if start=NULL
a: print “list is empty”
b: exit
End if.
2: Set t = start
3: Repeat steps 4 and 5 until t! =null
4: access and display t->data.
5: set t= t ->next
End repeat
6: Exit 44
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
C Function
start
t
void display()
{ 23 20 45 NULL
node *t;
if(start==NULL)
{
printf("Linked list empty\n");
}
else
{
23 20 45
printf("Linked list elements are\n");
t=start;
while(t!=NULL)
{
printf("%d\t",t->data);
t=t->next;
}}
}
45
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Lab Program 6
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, Programme, 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 / Deletion at End of SLL
d. Perform Insertion / Deletion at Front of SLL
e. Exit
46
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Linked Stacks
Push Operation void push(int item)
{
struct node node *p;
{ p=(node *)malloc(sizeof(node));
top NULL int data; p->data=item;
struct node *next; p->next=NULL;
}; if(top==NULL)
p 30 NULL typedef struct node node; {
node *top=NULL; top=p;
}
p 20 NULL else
{
p->next=top;
p top=p;
10 NULL
}
data next }
push(10) push(20) push(30)
47
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
void pop()
Linked Stacks {
Pop Operation node *p;
if(top==NULL)
{
printf("Stack empty\n");
top NULL }
else if(top->next==NULL)
{
printf("Element deleted : %d\n",top->data);
p 30 free(top);
top=NULL;
}
p else
20 {
p=top;
top=top->next;
p printf("Element deleted : %d\n",p->data);
10 NULL
free(p);
data next p=NULL;
}
pop( ) pop() pop() }
48
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Linked Queue
insert Operation void insert(int item)
{
struct node node *p;
{ p=(node *)malloc(sizeof(node));
int data; p->data=item;
struct node *next; p->next=NULL;
}; if(front==NULL)
typedef struct node node; {
node *rear=NULL; front=p;
node *front=NULL; rear=p;
front NULL rear NULL
}
p else
{
p rear->next=p;
10 NULL 20 NULL
rear=p;
data next }
}
insert(10) insert(20)
49
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Linked Queue
delete Operation void delete()
{
if(front==NULL)
printf(“Queue empty\n”);
else if(front->next==NULL)
{
printf(“Deleted element:%d\n”,front->data);
free(front);
front=NULL;
NULL rear NULL rear=NULL;
front
}
else
{
p p=front;
10 20 NULL
front=front->next;
data next printf(“Deleted element:%d\n”,p->data);
free(p);
delete() delete() p=NULL;
50
}
}
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Recalling Earlier Topics
I. The difficulties with single linked lists is that, it is possible to traverse only in one direction, ie.,
direction of the links.
II. The only way to find the node that precedes p is to start at the beginning of the list. The same
problem arises when one wishes to delete an arbitrary node from a singly linked list. Hence the
solution is to use doubly linked list
51
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Doubly Linked List
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
C Structure Representation of DLL
struct node
{
struct node *prev;
int data; prev data next
struct node *next;
};
typedef struct node node;
node *first=NULL,*last=NULL;
53
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Creation of a DLL with two nodes
node *p1,*p2;
p1=(node *)malloc(sizeof(node));
p1->data =20;
p1->next=NULL;
p1->prev=NULL;
p2=(node *)malloc(sizeof(node));
p2->data =30;
p2->next=NULL;
prev data next prev data next
p2->prev=NULL;
NULL 20 NULL NULL 30 NULL
p1->next=p2;
p2->prev=p1; 54
p1 p2
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Doubly Linked List Operations
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Insertion at beginning
Step1
Create a new node
Step2
Assign data to the data part of new node. Set prev and next part with NULL value
Step3
If first =NULL , then assign first and last with new node address ,goto step5
Step4
If first !=NULL, then assign new node next part with address of first node and first node
prev part with new node address. Then make first to point to new node.
Step5
Exit
56
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
first
Insertion at beginning Cont… last
NULL 30 NULL
first
last
p=(node*)malloc(sizeof(node));
p->data=item;
p->next=NULL;
p->prev = NULL;
prev data next
if(first == NULL)
NULL 80 NULL
first =last =p;
else
{ first->prev=p; p
p->next =first;
first= p;
} } 58
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Insertion at end
Step1
Create a new node
Step2
Assign data to the data part of new node. Set prev and next part with NULL value
Step3
If first =NULL , then assign first and last with new node address ,goto step5
Step4
If first !=NULL, then assign new node prev part with address of last node and last node
next part with new node address. Then make last to point to new node.
Step5
Exit
59
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
first
Insertion at end Cont... last
NULL 30 NULL
first
last
p=(node*)malloc(sizeof(node));
p->data=item;
p->next=NULL;
prev data next
p->prev = NULL;
if(first == NULL) NULL 80 NULL
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Insertion at specified position
Step1
Create a new node
Step2
Assign data to the data part of new node. Set prev and next part with NULL value
Step3
If first =NULL , then assign first and last with new node address ,goto step5
Step4
If we want to insert a node at position k, then traverse the list to find the address of (k-1) position
node and (k+1) position node.
Step5
Make the next part of (k-1) position node to point to the new node and prev Part of new node to
point to the (k-1) position node.
Step 6
Make the next part of new node to point to (k+1) position node and prev part of (k+1) position to
point to new node
Step 7 62
Exit
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
first
Insertion at specified position Cont.. last
NULL 30 NULL
first
last
64
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Deletion at beginning
Step1
Check list is empty or not. If the list is empty print empty list
Step2
If the list contains only one node then delete that node and set first and last
pointer to NULL value, goto step4
Step3
Otherwise set the first pointer to point to the second node in the linked list and
assign prev part of second node to NULL value and delete first node.
Step4
Exit
65
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
first
Deletion at beginning Cont.. last
last
first
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
C Function last
first
void deleteFront(){ prev data next prev data next
node * t;
if(first ==NULL) NULL
7 20 NULL
607 707 50 NULL
printf(“Empty\n”); t
else if(first->next ==NULL)
{
printf(“%d”,first->data);
free(first);
first=last=NULL;
}
else
{
t=first;
first=first->next;
printf(“%d”,t->data);
first->prev=NULL;
free(t);
t=NULL; 67
}
} An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Deletion at end
Step1
Check list is empty or not. If the list is empty print empty list
Step2
If the list contains only one node then delete that node and set first and last pointer to
NULL value, goto step4
Step3
Otherwise set the last pointer to point to the second last node in the linked list and assign
next part of second last node to NULL value and delete last node.
Step4
Exit
68
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
first
Deletion at end Cont.. last
last
first
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
C Function last
void deleteend(){ first
node * t; prev data next prev data next
if(first ==NULL)
NULL 10 207 607 NULL 70 NULL
printf(“Empty\n”);
else if(first->next ==NULL)
t
{
printf(“%d”,first->data);
free(first);
first=last=NULL;
}
else
{
t=last;
last=last->prev;
printf(“%d”,t->data);
last->next=NULL;
free(t);
t=NULL;
}
70
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Deletion at Specified Position
Step1
Check list is empty or not. If the list is empty print empty list
Step2
If we want to delete a node from position k ,then traverse the list to find (k-1) and k position
nodes addresses.
Step3
Make the next part of (k-1) position node to point to the (k+1) position node and the prev
part of (k+1) position node to point to (k-1) position node. Delete position k node.
Step4
Exit
71
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
first
Deletion at Specified Position Cont.. last
last
first
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
C Function Pos=3
void deletespecpos(int pos)
{ last
first q
node * t,*p,*q; t p prev
next
int i;
if(first ==NULL) NULL 10 207 707 50 NULL
printf(“Empty\n”);
else
{
p=first;
for(k=1;k<pos;k++)
{
t=p;
p=p->next;
}
q=p->next;
printf(“%d”,p->data);
t->next=q;
q->prev=t;
free(p);
73
p=NULL;
}
} An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Traversing Doubly Linked List
• Visiting each node of the list at least once in order to perform some specific
operation like searching, sorting, display, etc.
Forward Traversal
Step 1: IF first == NULL
WRITE “empty"
GOTO STEP 6
[END OF IF]
Step 2: Set PTR = first
Step 3: Repeat step 4 and 5 while PTR !
= NULL
Step 4: Write PTR → data
Step 5: PTR = PTR → next
Step 6: Exit 74
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
C Function
void forwardtraversal() last
{ first t
NULL
node *t; prev data next prev data next prev data next
if(first==NULL) NULL 207 707 50 NULL
{
printf("Linked list empty\n");
}
else
{
20 70 50
printf("Linked list elements are\n");
t=first;
while(t!=NULL)
{
printf("%d\t",t->data);
t=t->next;
}
}
} 75
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Backward Traversal
Step 1: IF first == NULL
WRITE “empty"
GOTO STEP 6
[END OF IF]
Step 2: Set PTR = last
Step 3: Repeat step 4 and 5 while PTR !=
NULL
Step 4: Write PTR → data
Step 5: PTR = PTR → prev
Step 6: Exit
76
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
C Functions
void backwardtraversal()
{ t last
first
node *t; prev data next prev data next prev data next
NULL
if(last==NULL)
{ NULL 207 707 50 NULL
printf("Linked list empty\n");
}
else
{
printf("Linked list elements are\n");
t=last; 50 70 20
while(t!=NULL)
{
printf("%d\t",t->data);
t=t->prev;
}
}
} 77
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
77
Advantages
78
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Circular Linked lists
• It is just a singly linked list in which the link field of the last node contains
the address of the first node of the list.
• The link field of the last node does not point to NULL rather it points back
to the beginning of the linked list
last
10 20 30
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Circular Singly Linked List
• Every node is accessible from a given node by traversing successively using next field
• Since link field of last node contains address of first node ,there is no special indication to find which is
the last node.
• There are two ways to implement circular linked list
Approach 1:
A pointer variable first can be used to designate the starting point of the list
first data next data next data next
10 20 30
Approach 2:
last
A pointer variable last can be used to designate as the last node
data next data next data next
10 20 30
80
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Circular Singly Linked List
Approach 2:
Structure definition
struct node
{
int data;
struct node *next;
};
typedef struct node node;
node *last=NULL;
An empty list is identified by the variable last is pictorially represented as shown below:
last NULL
List with only one node can be represented as last
data next
60 81
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Circular Singly linked list operations
82
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Circular singly linked list operations
Insert a node at the front end Case 1: empty list
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Circular singly linked list operations
Insert a node at the end
void insertend(int item)
{
node *p,*t;
p=(node *)malloc(sizeof(node));
p->data=item;
Case 2: non empty list last
if(last==NULL)
{
data next data next data next
last=p;
last->next=last; 10 20 30
}
else
{
p->next=last->next; data next
last->next=p;
last=p; 50
84
}
} p
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Circular singly linked list operations
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Circular singly linked list operationsCase 2: List with one node
Delete a node from the front end last
data next
void deletefront()
{ 50
node *first; NULL
if(last==NULL)
printf("Linked list empty\n");
else if(last->next==last)
{ first Case 3: list with more than one node last
free(last);
data next data next data next
last=NULL;
printf("Element deleted successfully\n"); 10 20 30
}
else
{
first=last->next;
last->next=first->next;
free(first);
first=NULL;
printf("Element deleted successfully\n");
86
}
}
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Circular singly linked list operations
Delete a node from end
void deleteend()
{
node *p,*t;
if(last==NULL)
printf("Linked list empty\n");
else if(last->next==last)
{
free(last);
last=NULL;
printf("Element deleted successfully\n"); Case 3: list with more than one node last t
} p
else
{ data next data next data next
p=last->next;
while(p->next!=last) 10 20 30
{
p=p->next;
}
p->next=last->next;
t=last;
last=p;
free(t);
t=NULL;
printf("Element deleted successfully\n"); 87
}
}
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Circular singly linked list operations
Delete a node from specified position
void deletespecifiedpos(int pos)
{
node *p,*t;
int i;
if(last==NULL)
printf("Linked list empty\n");
else
{
p=last->next; // to get first node address
for(i=1;i<pos;i++)
{
t=p;
p=p->next;
}
t->next=p->next;
printf("element deleted:%d\n",p->data);
free(p);
p=NULL; 88
}
}
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
88
Circular singly linked list operations
t last
Display data next data next data next
void display() 10 20 30
{
node *t;
if(last==NULL)
printf("Linked list empty\n");
else
{
t=last->next; 10 20 30
while(t!=last)
{
printf("%d\t ",t->data);
t=t->next;
}
printf("%d\n ",t->data);
}
89
}
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Circular doubly linked list
• Circular doubly linked list is a variation of doubly linked list in which every node in
the list has three fields:
Previous node address part
Data part
Next node address part
and the previous node address part of first node contains address of last node
whereas next node address part of last node contains address of first node
first
10 20 30
90
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Lab Program 7
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.
b) Display the status of DLL and count the number of nodes in it.
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Lab Program 7
void insertfront()
#include <stdio.h> {
#include<stdlib.h> node *p;
struct node p=(node *)malloc(sizeof(node));
{ printf("Enter SSN,Name,dept,designation,salary,Phno\n");
struct node *prev; scanf("%d%s%s%s%f%s",&p->ssn,p->name,p->dept,
int ssn; p->designation,&p->salary,p->phno);
char name[20]; p->next=NULL;
char dept[20]; p->prev=NULL;
char designation[20]; if(first==NULL)
float salary; first=last=p;
char phno[15]; else
struct node *next; {
}; p->next=first;
first->prev=p;
typedef struct node node; first=p;
node *first=NULL,*last=NULL; }
}
92
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
void deletefront()
Lab Program 7 {
node *p;
void insertend() if(first==NULL)
{ printf("Linked list empty\n");
node *p,*t; else if(first->next==NULL)
p=(node *)malloc(sizeof(node)); {
printf("Enter
SSN,Name,dept,designation,salary,Phno\n"); free(first);
scanf("%d%s%s%s%f%s",&p->ssn,p->name, first=last=NULL;
p->dept,p->designation,&p->salary,p->phno); printf("Element deleted
p->next=NULL; successfully\n");
p->prev=NULL; }
if(first==NULL) else
first=last=p; {
else p=first;
{ first=first->next;
last->next=p; first->prev=NULL;
p->prev=last; free(p);
last=p; p=NULL;
} printf("Element deleted
} successfully\n");
}
93
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Lab Program 7
void deleteend()
{ void display()
node *p; {
if(first==NULL) node *t;
printf("Linked list empty\n"); int c=0;
else if(first->next==NULL) if(first==NULL)
{ printf("Linked list empty\n");
free(first); else
first=last=NULL; {
printf("Element deleted t=first;
successfully\n"); while(t!=NULL)
} {
else printf("%d\t%s\t%s\t%s\t%f\t%s --->
{ ",t->ssn,t->name,t->dept,t->designation,t-
p=last; >salary,t->phno);
last=last->prev; c=c+1;
last->next=NULL; t=t->next;
free(p); }
p=NULL; }
printf("Element deleted printf(" NULL\n");
successfully\n"); printf("Number of nodes: %d\n",c);
} }
94
}
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Lab Program 7
void main()
{ case 4:
int ch,n,i; deletefront();
while(1) break;
{ case 5: deleteend();
printf("1.create 2.insertfront 3.insertend break;
4.deletefront 5.deleteend 6.display 7.exit\n");
scanf("%d",&ch); case 6:
switch(ch)
{ display();
case 1: break;
printf("Enter the number of case 7:
nodes to be created\n");
scanf("%d",&n); exit(0);
for(i=0;i<n;i++) break;
insertend(); default:
break; printf("Invalid option\n");
case 2: }
}
insertfront(); }
break;
case 3:
insertend(); 95
break;
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Header Linked List: List with header node
• Header node in a linked list is a special node whose link field(next field) always contains address
of the first node of the list
• Using header node , any node in the list can be accessed.
• Data field of header node usually does not contain any information and such a node does not represent an
item in the list. Sometimes ,useful information such as number of nodes in the list can be stored.
• If the list is empty then the link field of header node contains NULL.
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Advantages of header linked list
• Simplifies insertion and deletion operations
• Avoid the usage of various cases such as “if only one node is present what to do”
• Designing of program will be very simple
• Circular lists with header node are frequently used instead of ordinary linked lists
because many operations can be easily implemented
Structure definition
struct node
{
int data;
struct node *next;
};
typedef struct node node;
node *head;
97
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Header linked list operations
Step1:
Create a header node dynamically to represent empty header linked list.
Step2:
Perform various operations on header linked list.
data next
NULL
98
head
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Header linked list operations head
data next
80 99
p
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Header linked list operations head t
data next
80 100
NULL
p
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Header linked list operations
data next
Deletion at beginning/front Case 1: Linked list
empty NULL
void deletefront()
{
node *first;
head
if(head->next==NULL) Case 2: Linked list non-empty
printf("Linked list empty\n");
first
else data next data next data next data next
head
{ 30 NULL
first=head->next; 10 20
head->next=first->next;
free(first);
first=NULL;
printf("Element deleted successfully\n");
}
}
101
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Header linked list operations
Deletion at end
void deleteend()
{
node *prev,*cur;
if(head->next==NULL) prev cur
printf("Linked list empty\n");
else
{
prev=head; data next data next data next
head
cur=head->next; NULL NULL NULL
while(cur->next!=NULL) 10 20
{
prev=cur;
cur=cur->next;
}
prev->next=NULL;
free(cur);
cur=NULL;
printf("Element deleted successfully\n");
102
}
}
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Header linked list operations
Display t NULL
void display()
data next data next data next data next
{ head
node *t; 30 NULL
10 20
if(head->next==NULL)
printf("Linked list empty\n");
else
{
t=head->next; 10 20 30
while(t!=NULL)
{
printf("%d\t ",t->data);
t=t->next;
}
printf("\n");
}
103
}
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Header linked list operations
void main()
{
int ch,item; case 3:
head=(node *)malloc(sizeof(node)); deletefront();
head->next =NULL; break;
while(1) case 4: deleteend();
{ break;
printf("1.insertfront 2.insertend 3.deletefront 4.deleteend 5.display
6.exit\n"); case 5:
scanf("%d",&ch);
switch(ch) display();
{ break;
case 1: case 6:
printf("Enter the item to be inserted\n");
scanf("%d",&item); exit(0);
insertfront(item); break;
break; default:
printf("Invalid option\n");
case 2: }
}
printf("Enter the item to be inserted\n"); }
scanf("%d",&item);
insertend(item);
break; 104
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Applications of linked list
• Polynomial representation
o Evaluation of polynomial
o Addition of two polynomials
105
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Polynomial Representation
head
coef exp next coef exp next coef exp next
NULL
8 5 3 2 5 0 106
Example: 8x5+3x2+5
head
coef exp next coef exp next coef exp next
8 5 3 2 5 0
Representation of polynomial using circular singly linked list with header node
//Structure definition
struct node
{
int coef;
int expon;
struct node *next; 107
};
typedef struct node node;
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
C functions to store and display polynomial using linked list
(Singly linked list with header node)
void attachnode(node *poly,int co,int ex)
void readpoly(node *poly) {
{ node *p,*t;
int n,i,co,ex; p=(node *)malloc(sizeof(node));
printf("Enter number of terms\n"); p->coef=co;
scanf("%d",&n); p->expon=ex;
for(i=1;i<=n;i++) p->next=NULL;
{ t=poly;
printf("Enter coef and exponent\n"); while(t->next!=NULL)
scanf("%d%d",&co,&ex); {
attachnode(poly,co,ex);
} t=t->next;
}
} t->next=p;
}
poly
head
coef exp next coef exp next
NULL 8 5 NULL 3 2 NULL
108
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
C functions to store and display polynomial using linked list
(Singly linked list with header node)
void display(node *poly) poly
{ head
coef exp next coef exp next
node *t;
if(poly->next==NULL) 8 5 3 2 NULL
{
printf("Zero polynomial\n");
}
else
{ void main()
t=poly->next; {
printf("Polynomial is\n");
while(t->next!=NULL) node *head=(node *)malloc(sizeof(node));
{ head->next=NULL;
readpoly(head);
printf("%dx^%d + ",t->coef,t->expon); display(head);
t=t->next; }
}
printf("%dx^%d\n",t->coef,t->expon);
} 109
}
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
C functions to evaluate polynomial using linked list
printf("Sum : %d\n",sum);
}
110
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Polynomial addition using linked list
void main()
{
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Additional linked list operations
112
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Topic identified to overcome the mismatch in the curriculum
113
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Multiple Choice Questions
114
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Multiple Choice Questions
3. A doubly linked list has .......... pointers with each node.
A. 0
B. 1
C. 2
D. 3
4) A doubly linked list is also called as ..........
A. linked list
B. one way chain
C. two way chain
D. right link
115
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Multiple Choice Questions
5. What is a memory efficient double linked list?
a) Each node has only one pointer to traverse the list back and forth
b) The list has breakpoints for faster traversal
c) An auxiliary singly linked list acts as a helper list to traverse through the doubly
linked list
d) A doubly linked list that uses bitwise AND operator for storing addresses
6. How do you calculate the pointer difference in a memory efficient double linked list?
a) head or tail
b) pointer to previous node or pointer to next node
c) pointer to previous node – pointer to next node
d) pointer to next node – pointer to previous node
7. What differentiates a circular linked list from a normal linked list?
a) You cannot have the ‘next’ pointer point to null in a circular linked list
b) It is faster to traverse the circular linked list
c) You may or may not have the ‘next’ pointer point to null in a circular linked list
d) Head node is known in circular linked list 116
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Multiple Choice Questions
8. Which of the following application makes use of a circular linked list?
a) Undo operation in a text editor
b) Recursive function calls
c) Allocating CPU to resources
d) Implement Hash Tables
9. Which of the following is false about a circular linked list?
a) Every node has a successor
b) Time complexity of inserting a new node at the head of the list is O(1)
c) Time complexity for deleting the last node is O(n)
d) We can traverse the whole circular linked list by starting from any point
10. Consider a small circular linked list. How to detect the presence of cycles in this list
effectively?
a) Keep one node as head and traverse another temp node till the end to check if its
‘next points to head
b) Have fast and slow pointers with the fast pointer advancing two nodes at a time and
slow pointer advancing by one node at a time
c) Cannot determine, you have to pre-define if the list contains cycles 117
d) Circular linked list itself represents a cycle. So no new cycles cannot be generated
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Self Assessment Questions
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
Self Assessment Questions Link
• https://docs.google.com/forms/d/e/1FAIpQLSdhJJiwE5H9IWVYYd8RidrxiB5LQZn
k_Q_je5w7odZ8ZzJ5_g/viewform?usp=sf_link
119
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
UNIVERSITY QUESTIONS WITH RAISED RBT LEVELS
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
NPTEL Links
• https://nptel.ac.in/courses/106106130/
• https://nptel.ac.in/courses/106102064/
• https://nptel.ac.in/courses/106105085
121
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
MCQ Questions Reference Link
• https://www.sanfoundry.com/data-structure-questions-answers-circular-linked-lists
/
• https://www.sanfoundry.com/data-structure-questions-answers-doubly-linked-lists/
122
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.
REFERENCES
1. Ellis Horowitz and Sartaj Sahni, Fundamentals of Data Structures in C, 2nd Ed,
Universities Press, 2014.
2. Seymour Lipschutz, Data Structures Schaum's Outlines, Revised 1st Ed, McGraw Hill,
2014.
123
An Autonomous Institution ,Affiliated to Visvesvaraya Technological University, Belagavi. Approved By AICTE, New Delhi. Recognized by UGC with 2(f) & 12(B)
status. Accredited by NBA and NAAC.