Linked List: Dept. of Computer Science Faculty of Science and Technology
Linked List: Dept. of Computer Science Faculty of Science and Technology
1. Linked List
• Search
• Insertion
• Deletion
Algorithm
Input: Head (the address of first node)
Curr = Head
Step 1: if Curr == NULL print not found and exit
If Curr->data = item print found and exit
Step 2: move Curr to next node and go to step 1 Item = 15
Item = 5 Not Found!
Found!
Head
12 3 14 5 16 NULL
Algorithm
Head Node
Input:
Head (the address of first node),
Node (inserting node), Node->next
Prev (address of previous node)
Case 1:
if Prev != NULL then go to Case 2
Make a link from Node to first node Prev->next
Prev
Make Node as the Head
Exit
Case 1: 2: Inserting at
after a given
Head node
Case 2:
Make a link from Node to the node next to Prev
Make another link from Prev to Node
NEW
67 1001 CURR
head
5000
5000
LOCB
LOCA
1001 2005
FORW[LOC]=3010 node3
BACK[LOC]=1001
PREV[FORW]=LOC[FORW] 1001 20 5000
BACK[FORW [LOC]]=1001
3010
Linked List
Deletion (Algorithm and simulation)
Algorithm
Input: Prev->next
Curr->next Curr->next
Head (the address of first node), Head
Prev (address of previous node)
Case 1:
if Prev = NULL then go to Case 2 Curr Prev Curr
Curr = Prev->next
Make a link from Prev to the node next to Curr
Exit Case 1:
2: Delete the
afterfirst nodenode
a given
Case 2:
Make the node next to Head as new Head
Doubly Linked List
Introduction
The *prev link of the head node and the *next link of the last node is NULL.
In Searching, one parameter is sufficient to get both *prev and *next values.
Doubly Linked List
Exercise
2. Difference between Singly Linked List and Doubly Linked List (Advantage and
Disadvantage)
1. https://en.wikipedia.org/wiki/Linked_list
Books
“Schaum's Outline of Data Structures with C++”. By John R. Hubbard
“Data Structures and Program Design”, Robert L. Kruse, 3rd Edition, 1996.
“Data structures, algorithms and performance”, D. Wood, Addison-Wesley, 1993
“Advanced Data Structures”, Peter Brass, Cambridge University Press, 2008
“Data Structures and Algorithm Analysis”, Edition 3.2 (C++ Version), Clifford A.
Shaffer, Virginia Tech, Blacksburg, VA 24061 January 2, 2012
“C++ Data Structures”, Nell Dale and David Teague, Jones and Bartlett Publishers,
2001.
“Data Structures and Algorithms with Object-Oriented Design Patterns in C++”,
Bruno R. Preiss,