You are on page 1of 10

Linked List

Course Code: CSC 2106 Course Title: Data Structure (Theory)

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 8.2 Week No: 8 Semester:


Lecturer: Name & email
Lecture Outline

1. Linked List
• Search
• Insertion
• Deletion
2. Doubly Linked List
Linked List
Searching (Algorithm and simulation)

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

Curr Curr Curr Curr Curr Curr


Linked List
Insertion (Algorithm and simulation)

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

Each NODE of the list contains –


• The DATA item
• A LINK/POINTER to store the previous NODE’s address
• A LINK/POINTER to store the next NODE’s address

Representation of a NODE in C/C++


struct ListNode{ node
int data; *prev data *next
ListNode *prev;
ListNode *next;
};
ListNode node;
Doubly Linked List
Introduction

The *prev link of the head node and the *next link of the last node is NULL.

While creating a list, *prev must be assigned to previous created node.


In InsertNode and DeleteNode operations, parameters will be current node
because, current node contains both previous and next node address.

In Searching, one parameter is sufficient to get both *prev and *next values.
Doubly Linked List
Exercise

1. Write algorithm for insertion, deletion for Doubly Linked List

2. Difference between Singly Linked List and Doubly Linked List (Advantage and
Disadvantage)

3. Can STACK and QUEUE be implemented using LINKED LIST? How?


References

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,

You might also like