lect 11-12 zaheer abbas
TRANSCRIPT
Created by Zaheer Abbas Aghani
Data Structure & AlgorithmsITC-306
Lecture 11 &12
REPRESENTATION OF LINKED LIST IN MEMORY
A P P L E
START
1
2
3
4
5
6
7
8
START
6
A
P
P
L
E
2
1
7
4
0
INFO
LINK
INVALID ADDRESS
LINKED LIST ENDS
Linked List Operations:
The operation normally performed on linked list are:
Traversing: Process each element in the list. Searching: Finding the location of an element. Insertion: Insert new elements/ nodes in to list. Deletion: Delete an elements/ nodes from list.
Traversing Operation
Suppose we want traverse list in order to process each node exactly once.
Now here we write an algorithm that shows how to perform traversing operation on linked list.
Let LIST be a linked list in memory. In linked list start pointing to the first element & null indicating the end of list.
Our traversing algorithm uses a pointer variable PTR which points to the node currently being processed.
Accordingly LINK[PTR] points to the next node to be processed. Thus the assignment
PTR:=LINK[PTR]
Moves the pointer to the next node in the list.
Traversing algorithm
The following is an algorithm that perform traversing operation on linked list. This purpose of this algorithm is to print each element of list. The variable PTR points to the node currently being processed.
Step1: Start. Step2: set PTR:=START [initialize pointer PTR] Step3: Repeat step4 and 5 while PTR!=NULL. Step4: Print INFO[PTR]. Step5: set PTR:=LINK[PTR]. (PTR now points to the next node) End of step3 loop. Step6: Exit.
Searching into a linked list:
Searching refers to search an element in a linked list. For searching the element we first traverse the linked list and with traversing we compare the INFO part of each element with the given element. The algorithm for searching operation is written as:
Step1: Start. Step2: Set PTR to START [initialize pointer PTR] Step3: Repeat step4 while PTR!=NULL. Step4: if INFO[PTR]==ITEM then
print element foundElse:
Set PTR:=LINK[PTR]. (PTR now points to the next node)(End of step3 loop)
Step5: Exit.
Insertion into a linked list:
Insertion into linked list may be possible in two ways:
1.Insertion at beginning 2.Insertion in between.
Insertion Algorithm
CASE 1 (insertion at beginning)
Suppose temp is node that has to be inserted.
In linked list START points to the first element of linked list.
Step1: Start Step2: For insertion at beginning
we assign the value of START to the link part of inserted node.
Now inserted node points to the next node which was beginning node of list as Temp[PTR]=START.
Step3: assign the address of inserted node to the start pointer variable as START=temp.
Now START will point to the inserted node(temp) which is first node of linked list.
Step4: exit
Start
Null
temp
10
10 70 40
70 40
42
Start Null
temp
40
70
70
4010
10
42
Start Null
temp
40
70
70
4010
10
42
42
CASE 2 (insertion in between)
In case 2 first we traverse the list for obtaining the node after which we want to insert the element.
Here we have a linked list with 3 nodes (A, C & D)
Now here suppose we want to insert the node B in b/w A and C node. Then
Step1: First we assign the link part of node A to link part of node B as B[PTR]=A[PTR]
Now node B points to the node C Step2: Secondly assign the
address of node B to the link part of node A as A[PTR]=B.
Now node A points to node B and node B points to node C.
Step3: exit
start A C D
start A C D
B
start A C D
B
10 42 30
42 3010
10
10 42
42
30
30
22
2210
10 42
42
30
30
Deletion from a linked list:
For deleting the node from a linked list, first we traverse the linked list and compare with each element. After finding the element there are may be two cases for deletion.
1. Deletion at beginning 2. Deletion in between.
Deletion Algorithm: Case 1 (deletion at
beginning) Suppose temp is node that
has to be deleted. If the element to be deleted is the first node of linked list then first
Step1: assign the linked part of deleted node (temp) to the start.
After that start points to the second node of list and now we should free the temp node to be deleted
Step2: delete node temp. Step3: exit.
tempstart
A B C
21
21
55
55
42
42
Null
Nulltempstar
t
A B C21
55
55
55 4
2
42
tempstart Null
55
55
42
42
A B C
Deletion Algorithm:
Case 1 (deletion at beginning)
Step1: Start Step2: Set PTR=START Step3: Assign START=LINK[PTR]
now Start points out the second node in list.Step4: Delete first Node.(A node)Step5: Exit/End
Case 2 deletion in between In case 2 first we traverse the
list for obtaining the node which we want to delete.
Now here suppose we want to delete node B which is in b/w A and C node. Then
Step1: assign the link part of deleted node (node B) to the pervious node (node A)
After step1 node A is pointed to the node C.
Now we should delete the node B.
Step2: delete node B.
Step3: exit.
start A B C
20
20 4
2
42
111
111
start A B C
111
1112
0
20 4
2
start A B C20 2
042
111 1
11
Case 2 deletion in between
Step1: Start Step2: set PTR=START (PTR points to 1st node)
Step3: set PTR1=LINK[PTR] (LINK[PTR] points to 2nd node)
Step4: Assign LINK[PTR]=LINK[PTR1] or A[PTR]=B[PTR]
Step5: Delete Node. Step6: Exit/End
Deletion at last. If node to be deleted is the last node of list
then first we assign null value to the link part of last node to pervious node then secondly delete the last node.
Step 1:
Step 2:
start A B C
start A B C
start A B C
Deletion at last.
Step1: Start Step2: set PTR=START Step3: set PTR1=LINK[PTR] Step4: Assign LINK[PTR1]=
NULL Step5: Delete last Node Step6: Exit/End