Lec 2-DS ALGO (Final)
Lec 2-DS ALGO (Final)
CS-102
Lecture 2
Link List
1
Linked List
Create a structure called a Node.
object next
2
Linked List
Picture of our list (2, 6, 7, 8, 1) stored as a
linked list:
head
2 6 8 7 1 size=5
current
3
Link List
4
Linked List
Note some features of the list:
Need a head to point to the first node of the list.
Otherwise we won’t know where the start of the
list is.
The current here is a pointer, not an index.
The next field in the last node points to nothing.
We will place the memory address NULL which
is guaranteed to be inaccessible.
5
Operations on Linked List:
7
8
Algorithm: (Traversing a Linked List) Let LIST be a
linked list in memory. This algorithm traverses LIST,
applying an operation PROCESS to each element of
list. The variable PTR point to the node currently being
processed.
1. Set PTR:=START.
2. Repeat Step 3 while PTR≠NULL:
3. If ITEM < INFO[PTR], then:
Set PTR:=LINK[PTR]. [PTR now points to the next node]
Else If ITEM=INFO[PTR],then
Set LOC:=PTR, and Exit. [Search is successful.]
Else:
LOC:=NULL , and Exit. [ITEM now exceeds INFO[PTR].]
[End of If structure.]
[End of Step 2 loop.]
4. Set LOC:=NULL.
5. Exit.
11
Search Linked List for insertion and deletion of Nodes:
Both insertion and deletion operations need searching the linked list.
• To add a new node, we must identify the logical predecessor (address of
previous node) where the new node is to be inserting.
• To delete a node, we must identify the location (addresses) of the node to
be deleted and its logical predecessor (previous node).
14
Linked List Operations
add(9): Create a new node in memory to hold ‘9’
Node* newNode = new Node(9); newNode 9
15
Linked List Operations
add(9): Create a new node in memory to hold ‘9’
Node* newNode = new Node(9); newNode 9
head
2 6 8 7 1 size=5 6
current 2 1
3
9
newNode
16
Building a Linked List
List list; headNode size=0
17
Building a Linked List
List list; headNode size=0
currentNode
headNode 2 size=1
list.add(2);
lastcurrentNode
18
Building a Linked List
List list; headNode size=0
currentNode
headNode 2 size=1
list.add(2);
lastcurrentNode
currentNode
lastcurrentNode
19
Building a Linked List
currentNode
headNode 2 6 8 7 1 size=5
lastcurrentNode
20
Insertion into a Linked List:
If a node N is to be inserted into the list between nodes A and B in a
linked list named LIST. Its schematic diagram would be:
21
Inserting at the Beginning of a List:
If the linked list is sorted list and new node has the least low value
already stored in the list i.e. (if New->info < Head->info) then new node
is inserted at the beginning / Top of the list.
22
23
Insertion In Sorted Linked List
Steps:
1.Find the Location of the node after witch new node will be inserted
2.Use Insertion method to insert into the Linked List
Find the Location of the node
3.Search the Item after which insertion needs to be made . As the Linked List is sorted
searching will result in the location of element >given ITEM
4.As Insertion can not be made before a Node therefore another pointer will keep track of
the previous node i.e before moving the PTR , Save := PTR
5.Thus PTR points to current node Element which is > ITEM while SAVE points to
Element<ITEM
6.Thus Insertion can be made after SAVE
24
2
25
Deletion from a Link List
currentNode
headNode
2 6 8 7 1 size=5
lastcurrentNode
26
Deletion from a Linked List
currentNode
headNode
1
2 6 8 7 1 size=5
lastcurrentNode
27
Deletion from a Linked List
currentNode
headNode
1
2 8 7 1 size=5
2
lastcurrentNode
28
Deletion from a Linked List
3
currentNode
headNode
1 4
2 8 7 1 size=4
2
lastcurrentNode
29
30
Analysis of Linked List
add
• we simply insert the new node after the current
node. So add is a one-step operation.
remove
remove is also a one-step operation
find
worst-case: may have to search the entire list
back
moving the current pointer back one node requires
traversing the list from the start until the node whose
next pointer points to current node.
31
Two-Way or Doubly-linked List
Moving forward in a singly-linked list is easy;
moving backwards is not so easy.
To move back one node, we have to start at the
head of the singly-linked list and move forward
until the node before the current.
To avoid this we can use two pointers in a
node: one to point to next node and another to
point to the previous node:
32
Doubly-linked List
Need to be more careful when adding or
removing a node.
Consider add: the order in which pointers are
reorganized is important:
head 2 6 8 7 1 size=5
current
33
34
35
Insertion in Doubly-linked List
current
head 2 6 8 7 1 size=5
newNode 9 1
36
Doubly-linked List
current
head 2 6 8 7 1 size=5
newNode 9 1
37
Doubly-linked List
current
head 2 6 8 7 1 size=5
2 3
newNode 9 1
38
Doubly-linked List
current
head 2 6 8 7 1 size=5
2 4
3
newNode 9 1
39
Doubly-linked List
head 2 6 8 7 1 size=6
2 4 3
newNode 9 1
current
40