SlideShare a Scribd company logo
Lectures 13-14 linked lists
Chapter 6 of textbook
1. Concepts of linked lists
2. Simple linked lists
3. Circular linked lists
4. Double linked lists
5. Circular double linked lists
© Oxford University Press 2014. All rights reserved.
1. Concepts of linked list
• A linked list is a linear collection of data elements
called nodes in which linear representation is given by
links from one node to the next node.
• Similar to array, it is a linear collection of data
elements of the same type.
• Different from array, data elements of linked list are
generally not lined in consecutive memory space;
instead they are dispersed in various locations
© Oxford University Press 2014. All rights reserved.
Concepts of linked list
• Linked list is a data structure which in turn can be
used to implement other data structures. Thus, it acts
as building block to implement data structures like
stacks, queues and their variations.
• A linked list can be perceived as a train or a sequence
of nodes in which each node contains one or more
data fields and a pointer to the next node.
© Oxford University Press 2014. All rights reserved.
Element of linked list
• Linked list element (node) is user defined structure
data type, typically contains two parts
 data variables
 pointers to next elements, hold the addresses of
next elements
Example:
struct node {
int data; // data
struct node *next; // pointer to next element
};
© Oxford University Press 2014. All rights reserved.
Simple Linked List
1 2 3 4 5 6 7 X
START
• In the above linked list, every node contains two parts - one
integer and the other a pointer to the next node.
• The data part of the node which contains data may include a
simple data type, an array or a structure.
• The pointer part of the node contains a pointer to the next node
(or address of the next node in sequence).
• The last node will have no next node connected to it, so it will
store a NULL value.
• A linked list is defined by the pointer pointing to the first node,
e.g START
© Oxford University Press 2014. All rights reserved.
Linked List Operations
1. Create a node and linked list
2. Traversal, e.g. display all elements
3. Search node
4. Insert node
at beginning, at end, after/before a node
5. Delete node
at beginning, at end, after/before a node
6. Sort
How to create nodes
A node is a structure data type, can be created in
two methods, statically and dynamically.
• Static method
– use array of structures
– declared as globally outside functions
– declared locally within a function
• Dynamic method (mostly used for linked list)
– use stdlib function malloc(size) get memory space
struct node *np = (struct node*) malloc(sizeof(struct node));
How to create nodes
struct node *np = (struct node*) malloc(sizeof(struct node));
At run time, OS allocates consecutive sizeof(struct node)
bytes in the heap region,
return the address of the address of the first memory cell,
store the address to struct node type pointer np.
Need (struct node*) to cast the return address to struct
node pointer value.
Need use free(np) to release when deleting the node!!!
Otherwise cause memory leaking
Traversing Linked Lists
• We can traverse the entire linked list using a
single pointer variable called START.
• The START node contains the address of the first
node; the next part of the first node in turn
stores the address of its succeeding node.
• Using this technique the individual nodes of the
list will form a chain of nodes.
• If START = NULL, this means that the linked list is
empty and contains no nodes.
2. Singly Linked Lists
• A singly linked list is the simplest type of linked list in which
every node contains some data and a pointer to the next node
of the same data type.
Example:
struct node {
int data;
struct node *next;
};
1 2 3 4 5 6 7 X
START
Traversal Singly Linked Lists
ALGORITHM FOR TRAVERSING A LINKED LIST
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Steps 3 and 4 while PTR != NULL
Step 3: Apply Process to PTR->DATA
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: EXIT
void display(struct node *ptr) {
while(ptr != NULL) {
printf("%d ", ptr->data); // process
ptr = ptr->next;
}
}
Call as display(START);
Searching for Val 4 in Linked List
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
Searching a Linked List
ALGORITHM TO SEARCH A LINKED LIST
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Step 3 while PTR != NULL
Step 3: IF VAL = PTR->DATA
SET POS = PTR
Go To Step 5
ELSE
SET PTR = PTR->NEXT
[END OF IF]
[END OF LOOP]
Step 4: SET POS = NULL // not found
Step 5: EXIT // found, output POS
struct node* search(struct node *ptr, int num) {
while((ptr != NULL) && (ptr->data != num)) {
ptr = ptr->next;
}
return ptr;
}
// call example, search(START, 4)
Inserting a Node at the Beginning
1 7 3 4 2 6 5 X
START
START
9 1 7 3 4 2 6 5 X
ALGORITHM: INSERT A NEW NODE IN THE BEGINNING OF THE LINKED LIST
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = START
Step 6: SET START = New_Node
Step 7: EXIT
See example
Inserting a Node at the End
1 7 3 4 2 6 5 X
START, PTR
1 7 3 4 2 6 5 9 X
START
ALGORITHM TO INSERT A NEW NODE AT THE END OF THE LINKED LIST
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 10
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = NULL
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR->NEXT != NULL
Step 8: SET PTR = PTR ->NEXT
[END OF LOOP]
Step 9: SET PTR->NEXT = New_Node
Step 10: EXIT
Inserting a Node after Node that ahs Value NUM
ALGORITHM TO INSERT A NEW NODE AFTER A NODE THAT HAS VALUE
NUM
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: SET PREPTR = PTR
Step 7: Repeat Steps 8 and 9 while PREPTR->DATA != NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR->NEXT
[END OF LOOP]
Step 10: PREPTR->NEXT = New_Node
Step 11: SET New_Node->NEXT = PTR
Step 12: EXIT
Deleting the First Node
1 7 3 4 2 6 5 X
7 3 4 2 6 5 X
START
START
Algorithm to delete the first node from the linked
list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 5
[END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START->NEXT
Step 4: FREE PTR
Step 5: EXIT
Deleting the Last Node
1 7 3 4 2 6 5 X
START, PREPTR, PTR
1 7 3 4 2 6 X 5 X
PREPTR PTR
START
ALGORITHM TO DELETE THE LAST NODE OF THE LINKED LIST
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Steps 4 and 5 while PTR->NEXT != NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR->NEXT
[END OF LOOP]
Step 6: SET PREPTR->NEXT = NULL
Step 7: FREE PTR
Step 8: EXIT
Deleting the Node After a Given Node
1 7 3 4 2 6 5 X
START, PREPTR, PTR
1 7 3 4 2 6 5 X
PREPTR PTR
START
1 7 3 4 2 6 5 X
START
1 7 3 4 6 5 X
Deleting the Node After a Given Node
ALGORITHM TO DELETE THE NODE AFTER A GIVEN NODE FROM THE
LINKED LIST
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 10
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PREPTR = PTR
Step 4: Repeat Step 5 and 6 while PRETR->DATA != NUM
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR->NEXT
[END OF LOOP]
Step7: SET TEMP = PTR->NEXT
Step 8: SET PREPTR->NEXT = TEMP->NEXT
Step 9: FREE TEMP
Step 10: EXIT
3. Circular Linked List
• In a circular linked list, the last node contains a pointer to the first
node of the list. We can have a circular singly listed list as well as
circular doubly linked list. While traversing a circular linked list,
we can begin at any node and traverse the list in any direction
forward or backward until we reach the same node where we had
started. Thus, a circular linked list has no beginning and no
ending.
1 2 3 4 5 6 7
START
Circular Linked List
Algorithm to insert a new node in the beginning of
circular the linked list
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: Repeat Step 7 while PTR->NEXT != START
Step 7: PTR = PTR->NEXT
Step 8: SET New_Node->Next = START
Step 8: SET PTR->NEXT = New_Node
Step 6: SET START = New_Node
Step 7: EXIT
Circular Linked List
1 7 3 4 2 6 5
START, PTR
1 7 3 4 2 6 5
START
PTR
9 1 7 3 4 2 6 5
START
Circular Linked List
Algorithm to insert a new node at the end of the circular
linked list
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = START
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR->NEXT != START
Step 8: SET PTR = PTR ->NEXT
[END OF LOOP]
Step 9: SET PTR ->NEXT = New_Node
Step 10: EXIT
Circular Linked List
1 7 3 4 2 6 5
START, PTR
1 7 3 4 2 6 5 9
START PTR
Circular Linked List
Algorithm to insert a new node after a node that has
value NUM
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: SET PREPTR = PTR
Step 7: Repeat Step 8 and 9 while PTR->DATA != NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR->NEXT
[END OF LOOP]
Step 10: PREPTR->NEXT = New_Node
Step 11: SET New_Node->NEXT = PTR
Step 12: EXIT
Circular Linked List
Algorithm to delete the first node from the circular linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR->NEXT != START
Step 4: SET PTR = PTR->NEXT
[END OF IF]
Step 5: SET PTR->NEXT = START->NEXT
Step 6: FREE START
Step 7: SET START = PTR->NEXT
Step 8: EXIT
Circular Linked List
1 7 3 4 2 6 5
START, PTR
1 7 3 4 2 6 5
START
PTR
7 3 4 2 6 5
START
Circular Linked List
Algorithm to delete the last node of the circular linked
list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR->NEXT != START
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR->NEXT
[END OF LOOP]
Step 6: SET PREPTR->NEXT = START
Step 7: FREE PTR
Step 8: EXIT
Circular Linked List
1 7 3 4 2 6 5
START, PREPTR, PTR
1 7 3 4 2 6 5
START
PTR
PREPTR
1 7 3 4 2 6
START
Circular Linked List
Algorithm to delete the node after a given node from the circular linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PREPTR = PTR
Step 4: Repeat Step 5 and 6 while PREPTR->DATA != NUM
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR->NEXT
[END OF LOOP]
Step 7: SET PREPTR->NEXT = PTR->NEXT
Step 8: FREE PTR
Step 9: EXIT
Circular Linked List
1 7 3 4 2 6 5
START, PREPTR, PTR
1 7 3 4 2 6 5
START PREPTR PTR
1 7 3 4 6 5
START
4. Doubly Linked List
 A doubly linked list or a two way linked list is a more complex
type of linked list which contains a pointer to the next as well
as previous node in the sequence. Therefore, it consists of
three parts and not just two. The three parts are data, a
pointer to the next node and a pointer to the previous node
1
X 1 2 3 4 X
START
Doubly Linked List
• In C language, the structure of a doubly linked list is given as,
struct node
{ struct node *prev;
int data;
struct node *next;
};
• The prev field of the first node and the next field of the last
node will contain NULL. The prev field is used to store the
address of the preceding node. This would enable to traverse
the list in the backward direction as well.
Doubly Linked List
1 7 3 4 2 X
X
9 1 7 3 4
X 2 X
START
START
Algorithm to insert a new node in the beginning of the
doubly linked list
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 8
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->PREV = NULL
Step 6: SET New_Node->Next = START
Step 7: SET START = New_Node
Step 8: EXIT
Doubly Linked List
1 7 3 4 2 X
X
START, PTR
1 7 3 4 2
X 9 X
PTR
Algorithm to insert a new node at the end of the doubly linked list
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = NULL
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR->NEXT != NULL
Step 8: SET PTR = PTR->NEXT
[END OF LOOP]
Step 9: SET PTR->NEXT = New_Node
Step 10: New_Node->PREV = PTR
Step 11: EXIT
Doubly Linked List
Algorithm to insert a new node after a node that has
value NUM
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: Repeat Step 8 while PTR->DATA != NUM
Step 7: SET PTR = PTR->NEXT
[END OF LOOP]
Step 8: New_Node->NEXT = PTR->NEXT
Step 9: SET New_Node->PREV = PTR
Step 10: SET PTR->NEXT = New_Node
Step 11: EXIT
Doubly Linked List
1 7 3 4 2 X
X
START, PTR
1 7 3 4 2 X
X
9
1 7 3 9 4
X 2 X
START
START PTR
Doubly Linked List
1 7 3 4 2 X
X
START, PTR
7 3 4 2 X
Algorithm to delete the first node from the doubly linked
list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 6
[END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START->NEXT
Step 4: SET START->PREV = NULL
Step 5: FREE PTR
Step 6: EXIT
Doubly Linked List
1 3 5 7 8
X 9
1
X
START, PTR
1 3 5 7 8
X 9
1
X
START PTR
1 3 5 7 8 X
X
START
Algorithm to delete the last node of the doubly linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 7
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 and 5 while PTR->NEXT != NULL
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: SET PTR->PREV->NEXT = NULL
Step 6: FREE PTR
Step 7: EXIT
Doubly Linked List
Algorithm to delete the node after a given node from the
doubly linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR->DATA != NUM
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: SET TEMP = PTR->NEXT
Step 6: SET PTR->NEXT = TEMP->NEXT
Step 7: SET TEMP->NEXT->PREV = PTR
Step 8: FREE TEMP
Step 9: EXIT
Doubly Linked List
1 3 4 7 8
X 9
1
X
1 3 4 7 8
X 9
1
X
1 3 4 8 9 X
X
START, PTR
START
PTR
START
5. Circular Doubly Linked List
• A circular doubly linked list or a circular two way linked list is a
more complex type of linked list which contains a pointer to
the next as well as previous node in the sequence.
• The difference between a doubly linked and a circular doubly
linked list is same as that exists between a singly linked list
and a circular linked list. The circular doubly linked list does
not contain NULL in the previous field of the first node and
the next field of the last node. Rather, the next field of the last
node stores the address of the first node of the list, i.e; START.
Similarly, the previous field of the first field stores the address
of the last node.
Circular Doubly Linked List
• Since a circular doubly linked list contains three parts in its
structure, it calls for more space per node and for more
expensive basic operations. However, it provides the ease to
manipulate the elements of the list as it maintains pointers to
nodes in both the directions . The main advantage of using a
circular doubly linked list is that it makes searches twice as
efficient.
1 1 2 3 4
START
Circular Doubly Linked List
Algorithm to insert a new node in the beginning of the circular doubly linked list
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 6: SET START->PREV->NEXT = new_node;
Step 7: SET New_Node->PREV = START->PREV;
Step 8: SET START->PREV= new_Node;
Step 9: SET new_node->next = START;
Step 10: SET START = New_Node
Step 11: EXIT
Circular Doubly Linked List
1 7 3 4 2
START
9 1 7 3 4 2
START
Circular Doubly Linked List
1 7 3 4 2
1 7 3 4 2 9
START
START
Algorithm to insert a new node at the end of the circular
doubly linked list
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = START
Step 6: SET New_Node->PREV = START->PREV
Step 7: EXIT
Circular Doubly Linked List
Algorithm to insert a new node after a node that has
value NUM
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: Repeat Step 8 while PTR->DATA != NUM
Step 7: SET PTR = PTR->NEXT
[END OF LOOP]
Step 8: New_Node->NEXT = PTR->NEXT
Step 9: SET PTR->NEXT->PREV = New_Node
Step 9: SET New_Node->PREV = PTR
Step 10: SET PTR->NEXT = New_Node
Step 11: EXIT
Circular Doubly Linked List
1 7 3 4 2
START, PTR
1 7 3 4 2
9
START
PTR
1 7 3 9 4 2
START
Circular Doubly Linked List
1 3 5 7 8 9
1
START, PTR
3 5 7 8 9
1
START
Algorithm to delete the first node from the circular doubly
linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PTR->PREV=>NEXT= PTR->NEXT
Step 4: SET PTR->NEXT->PREV = PTR->PREV
Step 5: SET START = START->NEXT
Step 6: FREE PTR
Step 7: EXIT
Circular Doubly Linked List
1 3 5 7 8 9
1
START PTR
1 3 5 7 8
X
START
Algorithm to delete the last node of the circular doubly
linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START->PREV
Step 5: SET PTR->PREV->NEXT = START
Step 6: SET START->PREV = PTR->PREV
Step 7: FREE PTR
Step 8: EXIT
Circular Doubly Linked List
Algorithm to delete the node after a given node from the
circular doubly linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR->DATA != NUM
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: SET TEMP = PTR->NEXT
Step 6: SET PTR->NEXT = TEMP->NEXT
Step 7: SET TEMP->NEXT->PREV = PTR
Step 8: FREE TEMP
Step 9: EXIT
Circular Doubly Linked List
1 3 5 7 8 9
1
START
1 3 5 7 8 9
1
START PTR
1 3 4 8 9
START
Circular Doubly Linked List
 A header linked list is a special type of linked list which
contains a header node at the beginning of the list. So, in a
header linked list START will not point to the first node of the
list but START will contain the address of the header node.
There are basically two variants of a header linked list-
 Grounded header linked list which stores NULL in the next
field of the last node
 Circular header linked list which stores the address of the
header node in the next field of the last node. Here, the
header node will denote the end of the list.
Circular Doubly Linked List
1 2 3 4 5 6 X
Header Node
START
1 2 3 4 5 6
Header Node
START
Circular Doubly Linked List
Algorithm to traverse a Circular Header Linked List
Step 1: SET PTR = START->NEXT
Step 2: Repeat Steps 3 and 4 while PTR != START
Step 3: Apply PROCESS to PTR->DATA
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: EXIT
Circular Doubly Linked List
Algorithm to insert a new node after a given node
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 10
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET PTR = START->NEXT
Step 5: SET New_Node->DATA = VAL
Step 6: Repeat step 4 while PTR->DATA != NUM
Step 7: SET PTR = PTR->NEXT
[END OF LOOP]
Step 8: New_Node->NEXT = PTR->NEXT
Step 9: SET PTR->NEXT = New_Node
Step 10: EXIT

More Related Content

What's hot

DATABASE CONSTRAINTS
DATABASE CONSTRAINTSDATABASE CONSTRAINTS
DATABASE CONSTRAINTS
sunanditaAnand
 
Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point
University of Science and Technology Chitttagong
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
NITISH KUMAR
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
Hossain Md Shakhawat
 
Dfs presentation
Dfs presentationDfs presentation
Dfs presentationAlizay Khan
 
header, circular and two way linked lists
header, circular and two way linked listsheader, circular and two way linked lists
header, circular and two way linked lists
student
 
Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking 
Hasanain Alshadoodee
 
Functions & Recursion
Functions & RecursionFunctions & Recursion
Functions & Recursion
Nishant Munjal
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
Reazul Islam
 
B+ trees and height balance tree
B+ trees and height balance treeB+ trees and height balance tree
B+ trees and height balance tree
Jasleen Kaur (Chandigarh University)
 
Avl tree detailed
Avl tree detailedAvl tree detailed
Avl tree detailed
Dr Sandeep Kumar Poonia
 
Dbms lab Manual
Dbms lab ManualDbms lab Manual
Dbms lab Manual
Vivek Kumar Sinha
 
Closest pair problems (Divide and Conquer)
Closest pair problems (Divide and Conquer)Closest pair problems (Divide and Conquer)
Closest pair problems (Divide and Conquer)
Gem WeBlog
 
03 Linear Arrays Memory Representations .pdf
03 Linear Arrays Memory Representations .pdf03 Linear Arrays Memory Representations .pdf
03 Linear Arrays Memory Representations .pdf
KkSingh64
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
KristinaBorooah
 

What's hot (20)

Heap sort
Heap sortHeap sort
Heap sort
 
trigger dbms
trigger dbmstrigger dbms
trigger dbms
 
DATABASE CONSTRAINTS
DATABASE CONSTRAINTSDATABASE CONSTRAINTS
DATABASE CONSTRAINTS
 
Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Dfs presentation
Dfs presentationDfs presentation
Dfs presentation
 
header, circular and two way linked lists
header, circular and two way linked listsheader, circular and two way linked lists
header, circular and two way linked lists
 
Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking 
 
Functions & Recursion
Functions & RecursionFunctions & Recursion
Functions & Recursion
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
 
B+ trees and height balance tree
B+ trees and height balance treeB+ trees and height balance tree
B+ trees and height balance tree
 
binary tree
binary treebinary tree
binary tree
 
Trigger
TriggerTrigger
Trigger
 
Heap tree
Heap treeHeap tree
Heap tree
 
Avl tree detailed
Avl tree detailedAvl tree detailed
Avl tree detailed
 
Dbms lab Manual
Dbms lab ManualDbms lab Manual
Dbms lab Manual
 
Closest pair problems (Divide and Conquer)
Closest pair problems (Divide and Conquer)Closest pair problems (Divide and Conquer)
Closest pair problems (Divide and Conquer)
 
03 Linear Arrays Memory Representations .pdf
03 Linear Arrays Memory Representations .pdf03 Linear Arrays Memory Representations .pdf
03 Linear Arrays Memory Representations .pdf
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
 

Similar to cp264_lecture13_14_linkedlist.ppt

Introduction Linked Lists - Singly Linked List,
Introduction Linked Lists - Singly Linked List,Introduction Linked Lists - Singly Linked List,
Introduction Linked Lists - Singly Linked List,
JayaKamal
 
358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8
sumitbardhan
 
Ch-17-Data Structures.ppt.pptx
Ch-17-Data Structures.ppt.pptxCh-17-Data Structures.ppt.pptx
Ch-17-Data Structures.ppt.pptx
coderasylem
 
ds 4Linked lists.ppt
ds 4Linked lists.pptds 4Linked lists.ppt
ds 4Linked lists.ppt
AlliVinay1
 
5.Linked list
5.Linked list 5.Linked list
5.Linked list
Mandeep Singh
 
DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
ChrisSosaJacob
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocation
kiran Patel
 
Dounly linked list
Dounly linked listDounly linked list
Dounly linked list
NirmalPandey23
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
Dabbal Singh Mahara
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptx
msohail37
 
Bca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiBca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothi
Sowmya Jyothi
 
unit 2- PPT.pdf
unit 2- PPT.pdfunit 2- PPT.pdf
unit 2- PPT.pdf
PranavMakwana6
 
Lecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsLecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsAakash deep Singhal
 
linkrd_list.pdf
linkrd_list.pdflinkrd_list.pdf
linkrd_list.pdf
ISHAN194169
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
Afaq Mansoor Khan
 
4.linked list(contd.)
4.linked list(contd.)4.linked list(contd.)
4.linked list(contd.)
Chandan Singh
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
SonaPathak5
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
Khuram Shahzad
 
VCE Unit 02 (1).pptx
VCE Unit 02 (1).pptxVCE Unit 02 (1).pptx
VCE Unit 02 (1).pptx
skilljiolms
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
Akila Krishnamoorthy
 

Similar to cp264_lecture13_14_linkedlist.ppt (20)

Introduction Linked Lists - Singly Linked List,
Introduction Linked Lists - Singly Linked List,Introduction Linked Lists - Singly Linked List,
Introduction Linked Lists - Singly Linked List,
 
358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8
 
Ch-17-Data Structures.ppt.pptx
Ch-17-Data Structures.ppt.pptxCh-17-Data Structures.ppt.pptx
Ch-17-Data Structures.ppt.pptx
 
ds 4Linked lists.ppt
ds 4Linked lists.pptds 4Linked lists.ppt
ds 4Linked lists.ppt
 
5.Linked list
5.Linked list 5.Linked list
5.Linked list
 
DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocation
 
Dounly linked list
Dounly linked listDounly linked list
Dounly linked list
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptx
 
Bca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiBca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothi
 
unit 2- PPT.pdf
unit 2- PPT.pdfunit 2- PPT.pdf
unit 2- PPT.pdf
 
Lecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsLecture 5 data structures and algorithms
Lecture 5 data structures and algorithms
 
linkrd_list.pdf
linkrd_list.pdflinkrd_list.pdf
linkrd_list.pdf
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
4.linked list(contd.)
4.linked list(contd.)4.linked list(contd.)
4.linked list(contd.)
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
VCE Unit 02 (1).pptx
VCE Unit 02 (1).pptxVCE Unit 02 (1).pptx
VCE Unit 02 (1).pptx
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 

Recently uploaded

Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
veerababupersonal22
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
BrazilAccount1
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 

Recently uploaded (20)

Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 

cp264_lecture13_14_linkedlist.ppt

  • 1. Lectures 13-14 linked lists Chapter 6 of textbook 1. Concepts of linked lists 2. Simple linked lists 3. Circular linked lists 4. Double linked lists 5. Circular double linked lists
  • 2. © Oxford University Press 2014. All rights reserved. 1. Concepts of linked list • A linked list is a linear collection of data elements called nodes in which linear representation is given by links from one node to the next node. • Similar to array, it is a linear collection of data elements of the same type. • Different from array, data elements of linked list are generally not lined in consecutive memory space; instead they are dispersed in various locations
  • 3. © Oxford University Press 2014. All rights reserved. Concepts of linked list • Linked list is a data structure which in turn can be used to implement other data structures. Thus, it acts as building block to implement data structures like stacks, queues and their variations. • A linked list can be perceived as a train or a sequence of nodes in which each node contains one or more data fields and a pointer to the next node.
  • 4. © Oxford University Press 2014. All rights reserved. Element of linked list • Linked list element (node) is user defined structure data type, typically contains two parts  data variables  pointers to next elements, hold the addresses of next elements Example: struct node { int data; // data struct node *next; // pointer to next element };
  • 5. © Oxford University Press 2014. All rights reserved. Simple Linked List 1 2 3 4 5 6 7 X START • In the above linked list, every node contains two parts - one integer and the other a pointer to the next node. • The data part of the node which contains data may include a simple data type, an array or a structure. • The pointer part of the node contains a pointer to the next node (or address of the next node in sequence). • The last node will have no next node connected to it, so it will store a NULL value. • A linked list is defined by the pointer pointing to the first node, e.g START
  • 6. © Oxford University Press 2014. All rights reserved. Linked List Operations 1. Create a node and linked list 2. Traversal, e.g. display all elements 3. Search node 4. Insert node at beginning, at end, after/before a node 5. Delete node at beginning, at end, after/before a node 6. Sort
  • 7. How to create nodes A node is a structure data type, can be created in two methods, statically and dynamically. • Static method – use array of structures – declared as globally outside functions – declared locally within a function • Dynamic method (mostly used for linked list) – use stdlib function malloc(size) get memory space struct node *np = (struct node*) malloc(sizeof(struct node));
  • 8. How to create nodes struct node *np = (struct node*) malloc(sizeof(struct node)); At run time, OS allocates consecutive sizeof(struct node) bytes in the heap region, return the address of the address of the first memory cell, store the address to struct node type pointer np. Need (struct node*) to cast the return address to struct node pointer value. Need use free(np) to release when deleting the node!!! Otherwise cause memory leaking
  • 9. Traversing Linked Lists • We can traverse the entire linked list using a single pointer variable called START. • The START node contains the address of the first node; the next part of the first node in turn stores the address of its succeeding node. • Using this technique the individual nodes of the list will form a chain of nodes. • If START = NULL, this means that the linked list is empty and contains no nodes.
  • 10. 2. Singly Linked Lists • A singly linked list is the simplest type of linked list in which every node contains some data and a pointer to the next node of the same data type. Example: struct node { int data; struct node *next; }; 1 2 3 4 5 6 7 X START
  • 11. Traversal Singly Linked Lists ALGORITHM FOR TRAVERSING A LINKED LIST Step 1: [INITIALIZE] SET PTR = START Step 2: Repeat Steps 3 and 4 while PTR != NULL Step 3: Apply Process to PTR->DATA Step 4: SET PTR = PTR->NEXT [END OF LOOP] Step 5: EXIT void display(struct node *ptr) { while(ptr != NULL) { printf("%d ", ptr->data); // process ptr = ptr->next; } } Call as display(START);
  • 12. Searching for Val 4 in Linked List 1 7 3 4 2 6 5 X PTR 1 7 3 4 2 6 5 X PTR 1 7 3 4 2 6 5 X PTR 1 7 3 4 2 6 5 X PTR
  • 13. Searching a Linked List ALGORITHM TO SEARCH A LINKED LIST Step 1: [INITIALIZE] SET PTR = START Step 2: Repeat Step 3 while PTR != NULL Step 3: IF VAL = PTR->DATA SET POS = PTR Go To Step 5 ELSE SET PTR = PTR->NEXT [END OF IF] [END OF LOOP] Step 4: SET POS = NULL // not found Step 5: EXIT // found, output POS struct node* search(struct node *ptr, int num) { while((ptr != NULL) && (ptr->data != num)) { ptr = ptr->next; } return ptr; } // call example, search(START, 4)
  • 14. Inserting a Node at the Beginning 1 7 3 4 2 6 5 X START START 9 1 7 3 4 2 6 5 X ALGORITHM: INSERT A NEW NODE IN THE BEGINNING OF THE LINKED LIST Step 1: IF AVAIL = NULL, then Write OVERFLOW Go to Step 7 [END OF IF] Step 2: SET New_Node = AVAIL Step 3: SET AVAIL = AVAIL->NEXT Step 4: SET New_Node->DATA = VAL Step 5: SET New_Node->Next = START Step 6: SET START = New_Node Step 7: EXIT See example
  • 15. Inserting a Node at the End 1 7 3 4 2 6 5 X START, PTR 1 7 3 4 2 6 5 9 X START ALGORITHM TO INSERT A NEW NODE AT THE END OF THE LINKED LIST Step 1: IF AVAIL = NULL, then Write OVERFLOW Go to Step 10 [END OF IF] Step 2: SET New_Node = AVAIL Step 3: SET AVAIL = AVAIL->NEXT Step 4: SET New_Node->DATA = VAL Step 5: SET New_Node->Next = NULL Step 6: SET PTR = START Step 7: Repeat Step 8 while PTR->NEXT != NULL Step 8: SET PTR = PTR ->NEXT [END OF LOOP] Step 9: SET PTR->NEXT = New_Node Step 10: EXIT
  • 16. Inserting a Node after Node that ahs Value NUM ALGORITHM TO INSERT A NEW NODE AFTER A NODE THAT HAS VALUE NUM Step 1: IF AVAIL = NULL, then Write OVERFLOW Go to Step 12 [END OF IF] Step 2: SET New_Node = AVAIL Step 3: SET AVAIL = AVAIL->NEXT Step 4: SET New_Node->DATA = VAL Step 5: SET PTR = START Step 6: SET PREPTR = PTR Step 7: Repeat Steps 8 and 9 while PREPTR->DATA != NUM Step 8: SET PREPTR = PTR Step 9: SET PTR = PTR->NEXT [END OF LOOP] Step 10: PREPTR->NEXT = New_Node Step 11: SET New_Node->NEXT = PTR Step 12: EXIT
  • 17. Deleting the First Node 1 7 3 4 2 6 5 X 7 3 4 2 6 5 X START START Algorithm to delete the first node from the linked list Step 1: IF START = NULL, then Write UNDERFLOW Go to Step 5 [END OF IF] Step 2: SET PTR = START Step 3: SET START = START->NEXT Step 4: FREE PTR Step 5: EXIT
  • 18. Deleting the Last Node 1 7 3 4 2 6 5 X START, PREPTR, PTR 1 7 3 4 2 6 X 5 X PREPTR PTR START ALGORITHM TO DELETE THE LAST NODE OF THE LINKED LIST Step 1: IF START = NULL, then Write UNDERFLOW Go to Step 8 [END OF IF] Step 2: SET PTR = START Step 3: Repeat Steps 4 and 5 while PTR->NEXT != NULL Step 4: SET PREPTR = PTR Step 5: SET PTR = PTR->NEXT [END OF LOOP] Step 6: SET PREPTR->NEXT = NULL Step 7: FREE PTR Step 8: EXIT
  • 19. Deleting the Node After a Given Node 1 7 3 4 2 6 5 X START, PREPTR, PTR 1 7 3 4 2 6 5 X PREPTR PTR START 1 7 3 4 2 6 5 X START 1 7 3 4 6 5 X
  • 20. Deleting the Node After a Given Node ALGORITHM TO DELETE THE NODE AFTER A GIVEN NODE FROM THE LINKED LIST Step 1: IF START = NULL, then Write UNDERFLOW Go to Step 10 [END OF IF] Step 2: SET PTR = START Step 3: SET PREPTR = PTR Step 4: Repeat Step 5 and 6 while PRETR->DATA != NUM Step 5: SET PREPTR = PTR Step 6: SET PTR = PTR->NEXT [END OF LOOP] Step7: SET TEMP = PTR->NEXT Step 8: SET PREPTR->NEXT = TEMP->NEXT Step 9: FREE TEMP Step 10: EXIT
  • 21. 3. Circular Linked List • In a circular linked list, the last node contains a pointer to the first node of the list. We can have a circular singly listed list as well as circular doubly linked list. While traversing a circular linked list, we can begin at any node and traverse the list in any direction forward or backward until we reach the same node where we had started. Thus, a circular linked list has no beginning and no ending. 1 2 3 4 5 6 7 START
  • 22. Circular Linked List Algorithm to insert a new node in the beginning of circular the linked list Step 1: IF AVAIL = NULL, then Write OVERFLOW Go to Step 7 [END OF IF] Step 2: SET New_Node = AVAIL Step 3: SET AVAIL = AVAIL->NEXT Step 4: SET New_Node->DATA = VAL Step 5: SET PTR = START Step 6: Repeat Step 7 while PTR->NEXT != START Step 7: PTR = PTR->NEXT Step 8: SET New_Node->Next = START Step 8: SET PTR->NEXT = New_Node Step 6: SET START = New_Node Step 7: EXIT
  • 23. Circular Linked List 1 7 3 4 2 6 5 START, PTR 1 7 3 4 2 6 5 START PTR 9 1 7 3 4 2 6 5 START
  • 24. Circular Linked List Algorithm to insert a new node at the end of the circular linked list Step 1: IF AVAIL = NULL, then Write OVERFLOW Go to Step 7 [END OF IF] Step 2: SET New_Node = AVAIL Step 3: SET AVAIL = AVAIL->NEXT Step 4: SET New_Node->DATA = VAL Step 5: SET New_Node->Next = START Step 6: SET PTR = START Step 7: Repeat Step 8 while PTR->NEXT != START Step 8: SET PTR = PTR ->NEXT [END OF LOOP] Step 9: SET PTR ->NEXT = New_Node Step 10: EXIT
  • 25. Circular Linked List 1 7 3 4 2 6 5 START, PTR 1 7 3 4 2 6 5 9 START PTR
  • 26. Circular Linked List Algorithm to insert a new node after a node that has value NUM Step 1: IF AVAIL = NULL, then Write OVERFLOW Go to Step 12 [END OF IF] Step 2: SET New_Node = AVAIL Step 3: SET AVAIL = AVAIL->NEXT Step 4: SET New_Node->DATA = VAL Step 5: SET PTR = START Step 6: SET PREPTR = PTR Step 7: Repeat Step 8 and 9 while PTR->DATA != NUM Step 8: SET PREPTR = PTR Step 9: SET PTR = PTR->NEXT [END OF LOOP] Step 10: PREPTR->NEXT = New_Node Step 11: SET New_Node->NEXT = PTR Step 12: EXIT
  • 27. Circular Linked List Algorithm to delete the first node from the circular linked list Step 1: IF START = NULL, then Write UNDERFLOW Go to Step 8 [END OF IF] Step 2: SET PTR = START Step 3: Repeat Step 4 while PTR->NEXT != START Step 4: SET PTR = PTR->NEXT [END OF IF] Step 5: SET PTR->NEXT = START->NEXT Step 6: FREE START Step 7: SET START = PTR->NEXT Step 8: EXIT
  • 28. Circular Linked List 1 7 3 4 2 6 5 START, PTR 1 7 3 4 2 6 5 START PTR 7 3 4 2 6 5 START
  • 29. Circular Linked List Algorithm to delete the last node of the circular linked list Step 1: IF START = NULL, then Write UNDERFLOW Go to Step 8 [END OF IF] Step 2: SET PTR = START Step 3: Repeat Step 4 while PTR->NEXT != START Step 4: SET PREPTR = PTR Step 5: SET PTR = PTR->NEXT [END OF LOOP] Step 6: SET PREPTR->NEXT = START Step 7: FREE PTR Step 8: EXIT
  • 30. Circular Linked List 1 7 3 4 2 6 5 START, PREPTR, PTR 1 7 3 4 2 6 5 START PTR PREPTR 1 7 3 4 2 6 START
  • 31. Circular Linked List Algorithm to delete the node after a given node from the circular linked list Step 1: IF START = NULL, then Write UNDERFLOW Go to Step 9 [END OF IF] Step 2: SET PTR = START Step 3: SET PREPTR = PTR Step 4: Repeat Step 5 and 6 while PREPTR->DATA != NUM Step 5: SET PREPTR = PTR Step 6: SET PTR = PTR->NEXT [END OF LOOP] Step 7: SET PREPTR->NEXT = PTR->NEXT Step 8: FREE PTR Step 9: EXIT
  • 32. Circular Linked List 1 7 3 4 2 6 5 START, PREPTR, PTR 1 7 3 4 2 6 5 START PREPTR PTR 1 7 3 4 6 5 START
  • 33. 4. Doubly Linked List  A doubly linked list or a two way linked list is a more complex type of linked list which contains a pointer to the next as well as previous node in the sequence. Therefore, it consists of three parts and not just two. The three parts are data, a pointer to the next node and a pointer to the previous node 1 X 1 2 3 4 X START
  • 34. Doubly Linked List • In C language, the structure of a doubly linked list is given as, struct node { struct node *prev; int data; struct node *next; }; • The prev field of the first node and the next field of the last node will contain NULL. The prev field is used to store the address of the preceding node. This would enable to traverse the list in the backward direction as well.
  • 35. Doubly Linked List 1 7 3 4 2 X X 9 1 7 3 4 X 2 X START START Algorithm to insert a new node in the beginning of the doubly linked list Step 1: IF AVAIL = NULL, then Write OVERFLOW Go to Step 8 [END OF IF] Step 2: SET New_Node = AVAIL Step 3: SET AVAIL = AVAIL->NEXT Step 4: SET New_Node->DATA = VAL Step 5: SET New_Node->PREV = NULL Step 6: SET New_Node->Next = START Step 7: SET START = New_Node Step 8: EXIT
  • 36. Doubly Linked List 1 7 3 4 2 X X START, PTR 1 7 3 4 2 X 9 X PTR Algorithm to insert a new node at the end of the doubly linked list Step 1: IF AVAIL = NULL, then Write OVERFLOW Go to Step 11 [END OF IF] Step 2: SET New_Node = AVAIL Step 3: SET AVAIL = AVAIL->NEXT Step 4: SET New_Node->DATA = VAL Step 5: SET New_Node->Next = NULL Step 6: SET PTR = START Step 7: Repeat Step 8 while PTR->NEXT != NULL Step 8: SET PTR = PTR->NEXT [END OF LOOP] Step 9: SET PTR->NEXT = New_Node Step 10: New_Node->PREV = PTR Step 11: EXIT
  • 37. Doubly Linked List Algorithm to insert a new node after a node that has value NUM Step 1: IF AVAIL = NULL, then Write OVERFLOW Go to Step 11 [END OF IF] Step 2: SET New_Node = AVAIL Step 3: SET AVAIL = AVAIL->NEXT Step 4: SET New_Node->DATA = VAL Step 5: SET PTR = START Step 6: Repeat Step 8 while PTR->DATA != NUM Step 7: SET PTR = PTR->NEXT [END OF LOOP] Step 8: New_Node->NEXT = PTR->NEXT Step 9: SET New_Node->PREV = PTR Step 10: SET PTR->NEXT = New_Node Step 11: EXIT
  • 38. Doubly Linked List 1 7 3 4 2 X X START, PTR 1 7 3 4 2 X X 9 1 7 3 9 4 X 2 X START START PTR
  • 39. Doubly Linked List 1 7 3 4 2 X X START, PTR 7 3 4 2 X Algorithm to delete the first node from the doubly linked list Step 1: IF START = NULL, then Write UNDERFLOW Go to Step 6 [END OF IF] Step 2: SET PTR = START Step 3: SET START = START->NEXT Step 4: SET START->PREV = NULL Step 5: FREE PTR Step 6: EXIT
  • 40. Doubly Linked List 1 3 5 7 8 X 9 1 X START, PTR 1 3 5 7 8 X 9 1 X START PTR 1 3 5 7 8 X X START Algorithm to delete the last node of the doubly linked list Step 1: IF START = NULL, then Write UNDERFLOW Go to Step 7 [END OF IF] Step 2: SET PTR = START Step 3: Repeat Step 4 and 5 while PTR->NEXT != NULL Step 4: SET PTR = PTR->NEXT [END OF LOOP] Step 5: SET PTR->PREV->NEXT = NULL Step 6: FREE PTR Step 7: EXIT
  • 41. Doubly Linked List Algorithm to delete the node after a given node from the doubly linked list Step 1: IF START = NULL, then Write UNDERFLOW Go to Step 9 [END OF IF] Step 2: SET PTR = START Step 3: Repeat Step 4 while PTR->DATA != NUM Step 4: SET PTR = PTR->NEXT [END OF LOOP] Step 5: SET TEMP = PTR->NEXT Step 6: SET PTR->NEXT = TEMP->NEXT Step 7: SET TEMP->NEXT->PREV = PTR Step 8: FREE TEMP Step 9: EXIT
  • 42. Doubly Linked List 1 3 4 7 8 X 9 1 X 1 3 4 7 8 X 9 1 X 1 3 4 8 9 X X START, PTR START PTR START
  • 43. 5. Circular Doubly Linked List • A circular doubly linked list or a circular two way linked list is a more complex type of linked list which contains a pointer to the next as well as previous node in the sequence. • The difference between a doubly linked and a circular doubly linked list is same as that exists between a singly linked list and a circular linked list. The circular doubly linked list does not contain NULL in the previous field of the first node and the next field of the last node. Rather, the next field of the last node stores the address of the first node of the list, i.e; START. Similarly, the previous field of the first field stores the address of the last node.
  • 44. Circular Doubly Linked List • Since a circular doubly linked list contains three parts in its structure, it calls for more space per node and for more expensive basic operations. However, it provides the ease to manipulate the elements of the list as it maintains pointers to nodes in both the directions . The main advantage of using a circular doubly linked list is that it makes searches twice as efficient. 1 1 2 3 4 START
  • 45. Circular Doubly Linked List Algorithm to insert a new node in the beginning of the circular doubly linked list Step 1: IF AVAIL = NULL, then Write OVERFLOW Go to Step 12 [END OF IF] Step 2: SET New_Node = AVAIL Step 3: SET AVAIL = AVAIL->NEXT Step 4: SET New_Node->DATA = VAL Step 6: SET START->PREV->NEXT = new_node; Step 7: SET New_Node->PREV = START->PREV; Step 8: SET START->PREV= new_Node; Step 9: SET new_node->next = START; Step 10: SET START = New_Node Step 11: EXIT
  • 46. Circular Doubly Linked List 1 7 3 4 2 START 9 1 7 3 4 2 START
  • 47. Circular Doubly Linked List 1 7 3 4 2 1 7 3 4 2 9 START START Algorithm to insert a new node at the end of the circular doubly linked list Step 1: IF AVAIL = NULL, then Write OVERFLOW Go to Step 11 [END OF IF] Step 2: SET New_Node = AVAIL Step 3: SET AVAIL = AVAIL->NEXT Step 4: SET New_Node->DATA = VAL Step 5: SET New_Node->Next = START Step 6: SET New_Node->PREV = START->PREV Step 7: EXIT
  • 48. Circular Doubly Linked List Algorithm to insert a new node after a node that has value NUM Step 1: IF AVAIL = NULL, then Write OVERFLOW Go to Step 11 [END OF IF] Step 2: SET New_Node = AVAIL Step 3: SET AVAIL = AVAIL->NEXT Step 4: SET New_Node->DATA = VAL Step 5: SET PTR = START Step 6: Repeat Step 8 while PTR->DATA != NUM Step 7: SET PTR = PTR->NEXT [END OF LOOP] Step 8: New_Node->NEXT = PTR->NEXT Step 9: SET PTR->NEXT->PREV = New_Node Step 9: SET New_Node->PREV = PTR Step 10: SET PTR->NEXT = New_Node Step 11: EXIT
  • 49. Circular Doubly Linked List 1 7 3 4 2 START, PTR 1 7 3 4 2 9 START PTR 1 7 3 9 4 2 START
  • 50. Circular Doubly Linked List 1 3 5 7 8 9 1 START, PTR 3 5 7 8 9 1 START Algorithm to delete the first node from the circular doubly linked list Step 1: IF START = NULL, then Write UNDERFLOW Go to Step 8 [END OF IF] Step 2: SET PTR = START Step 3: SET PTR->PREV=>NEXT= PTR->NEXT Step 4: SET PTR->NEXT->PREV = PTR->PREV Step 5: SET START = START->NEXT Step 6: FREE PTR Step 7: EXIT
  • 51. Circular Doubly Linked List 1 3 5 7 8 9 1 START PTR 1 3 5 7 8 X START Algorithm to delete the last node of the circular doubly linked list Step 1: IF START = NULL, then Write UNDERFLOW Go to Step 8 [END OF IF] Step 2: SET PTR = START->PREV Step 5: SET PTR->PREV->NEXT = START Step 6: SET START->PREV = PTR->PREV Step 7: FREE PTR Step 8: EXIT
  • 52. Circular Doubly Linked List Algorithm to delete the node after a given node from the circular doubly linked list Step 1: IF START = NULL, then Write UNDERFLOW Go to Step 9 [END OF IF] Step 2: SET PTR = START Step 3: Repeat Step 4 while PTR->DATA != NUM Step 4: SET PTR = PTR->NEXT [END OF LOOP] Step 5: SET TEMP = PTR->NEXT Step 6: SET PTR->NEXT = TEMP->NEXT Step 7: SET TEMP->NEXT->PREV = PTR Step 8: FREE TEMP Step 9: EXIT
  • 53. Circular Doubly Linked List 1 3 5 7 8 9 1 START 1 3 5 7 8 9 1 START PTR 1 3 4 8 9 START
  • 54. Circular Doubly Linked List  A header linked list is a special type of linked list which contains a header node at the beginning of the list. So, in a header linked list START will not point to the first node of the list but START will contain the address of the header node. There are basically two variants of a header linked list-  Grounded header linked list which stores NULL in the next field of the last node  Circular header linked list which stores the address of the header node in the next field of the last node. Here, the header node will denote the end of the list.
  • 55. Circular Doubly Linked List 1 2 3 4 5 6 X Header Node START 1 2 3 4 5 6 Header Node START
  • 56. Circular Doubly Linked List Algorithm to traverse a Circular Header Linked List Step 1: SET PTR = START->NEXT Step 2: Repeat Steps 3 and 4 while PTR != START Step 3: Apply PROCESS to PTR->DATA Step 4: SET PTR = PTR->NEXT [END OF LOOP] Step 5: EXIT
  • 57. Circular Doubly Linked List Algorithm to insert a new node after a given node Step 1: IF AVAIL = NULL, then Write OVERFLOW Go to Step 10 [END OF IF] Step 2: SET New_Node = AVAIL Step 3: SET AVAIL = AVAIL->NEXT Step 4: SET PTR = START->NEXT Step 5: SET New_Node->DATA = VAL Step 6: Repeat step 4 while PTR->DATA != NUM Step 7: SET PTR = PTR->NEXT [END OF LOOP] Step 8: New_Node->NEXT = PTR->NEXT Step 9: SET PTR->NEXT = New_Node Step 10: EXIT