lecture 2: linked lists - prakash gautam · deleting an element from a list searching a list ... in...
TRANSCRIPT
Lecture 2: Linked Lists
Prakash Gautamhttps://prakashgautam.com.np/dipit02/[email protected] 15 March, 2018
Objectives● Why Linked Lists?● Basic Concepts of Linked Lists● Difference between Array & Linked Lists● Types of Linked Lists● Operations
2
Agenda● Introduction of Linked List● Arrays Vs Linked Lists● Types of Linked Lists
✔ Singly Linked List✔ Doubly Linked List✔ Circular Linked List
● Operations✔ Insertion✔ Deletion 3
Introduction to Linked List● Linear data structure● It is series of connected nodes● Each node contains at least: A piece of Data (Any Type)
& Pointer to the next node in the list● Head: Pointer to the first node ● Last node to Null
4
1
Tail
2 3Head
5
A
Data Pointer
Node
● Nodes make up Linked List● Nodes are structures made up of data and a pointer to
another node● Usually, the pointer is called next
Null
Head
6
10 12 143 102
Array Vs Linked List
7
Arrays Linked List● Fixed size: Resizing is
expensive● Dynamic size
● Insertions and Deletions are inefficient: Elements are usually shifted
● Insertions and Deletions are efficient: No shifting
● Random access i.e., efficient indexing
● No random access● Not suitable for operations requiring
accessing elements by index, e.g., Sorting
8
Arrays Linked List
● No memory waste if thearray is full otherwise may result in much memory waste
● Memory is allocated Dynamically so, there is no waste of memory
● Sequential access is faster [Elements in contiguous memory locations]
● Sequential access is slow[Elements not in contiguous memory locations]
Types of Linked Lists
● Singly Linked List● Doubly Linked List● Circular Linked List
9
● Singly Linked List✔ Each node has only one link part✔ Each link part contains the address of the next node in
the list✔ Link part of the last node contains NULL value which
signifies the end of the node✔ This is Singly Linked List(SLL)
10Header Pointer
Null10 12 143 102
myList
● Singly Linked List✔ Each node contains a data & a pointer to the next node in
the list
✔ myList is the header pointer which points at the first node in the list
11
Header Pointer
Null10 12 143 102
myList
Basic Operations
● Creating a List● Inserting an element on a List● Deleting an element from a List● Searching a List● Reversing a List
12
13
A
Data Pointer
Node
● Inserting a node in the SLL✔ Insertion at the beginning✔ Insertion at the end✔ Insertion after a particular node
14
● Insertion at the beginning
15
Header Pointer
Null10 12 143 102myList
● Insertion at the beginning
16
Header Pointer
Null10 12 143 102myList
25
● Insertion at the end
17
Header Pointer
Null10 12 143 102myList
● Insertion at the end
18
Header Pointer
Null10 12 143 102myList
73
● Inserting after a particular node
19
Header Pointer
Null10 12 143 102myList
● Inserting after a particular node
20
Header Pointer
Null10 12 143 102myList
11
● Deleting a node in the SLL✔ Deleting the first node✔ Deleting the last node✔ Deleting the intermediate node
21
● Deleting the first node
22
Header Pointer
Null10 12 143 102myList
● Deleting the first node
23
Header Pointer
Null10 12 143 102myList
● Deleting the last node
24
Header Pointer
Null10 12 143 102myList
● Deleting the last node
25
Header Pointer
Null10 12 143 102myList
● Deleting the particular node
26
Header Pointer
Null10 12 143 102myList
● Deleting the particular node
27
Header Pointer
Null10 12 143 102myList
● Searching a node in the SLL✔ Searching involves finding the required element in the list✔ Sequential search or Binary search?✔ In Linked list since random access is not available it would
become complex to do binary search✔ We can perform simple linear search traversal
28
● Reversing a Linked List✔ We can reverse a linked list by reversing the direction of the
links between 2 nodes
29
Header Pointer10 12 143 102myList Null
102 143 12 10myList Null
30
Operation Array SLL
Insert at beginning O(n) O(1)
Insert at end O(1)O(1): Tail Reference
O(n): No Tail Reference
Insert at middle O(n) O(n)
Delete at beginning O(n) O(1)Delete at end O(1) O(n)
SearchO(n)
O(logn)O(n)
Indexing O(1) O(n)
● Doubly Linked List✔ A doubly linked list is a list that has two references, one to
the next node and another to previous node.
Head Tail
31
11 12 17 20
Previous Data Next
NullNull
● A doubly linked list contain three fields✔ An Integer Value✔ The link to the next node✔ The link to the previous node
● Structure
32
Previous Data Next
DLL vs SLL● Advantages
✔ Can be traversed in either direction✔ Some operations, such as deletion and inserting before a
node, become easier
● Disadvantages✔ Requires more space✔ List manipulations are slower✔ Greater chance of having bugs
33
● Inserting at beginning
34
11 12 17 20 Null Null
● Inserting at beginning
35
11 12 17 20 Null
Null 15
Null
● Inserting at the end
36
11 12 17 20 Null Null
● Inserting at the end
37
11 12 17 20 Null
Null 16
Null
● Inserting after a node
38
11 12 17 20 Null Null
● Inserting after a node
39
11 12 17 20 Null Null
13
● Deleting a node
40
11 12 17 20 Null Null
● Deleting a node
41
11 12 17 20 Null Null
● Circular Linked List✔ The last node points to the first node of the list
✔ How do we know when we have finished traversing the list?
Check if the pointer of the current node is equal to the head
42
10 12 143 102myList
Applications● MRU Lists● A linked list of URLs (Back: Chrome)● A linked list of state (Ctrl+Z)● A stack, hash table can be implemented using a DLL● Polynomial Algebra
44
...?
Thank You45