Upcoming SlideShare
×

# Lect 11-12 Zaheer Abbas

624 views

Published on

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
624
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
13
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Lect 11-12 Zaheer Abbas

1. 1. Created by Zaheer Abbas Aghani
2. 2. Data Structure & Algorithms ITC-306 Lecture 11 &12
3. 3. 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
4. 4. Linked List Operations: <ul><li>The operation normally performed on linked list are: </li></ul><ul><li>Traversing: Process each element in the list. </li></ul><ul><li>Searching: Finding the location of an element. </li></ul><ul><li>Insertion: Insert new elements/ nodes in to list. </li></ul><ul><li>Deletion: Delete an elements/ nodes from list. </li></ul>
5. 5. Traversing Operation <ul><li>Suppose we want traverse list in order to process each node exactly once. </li></ul><ul><li>Now here we write an algorithm that shows how to perform traversing operation on linked list. </li></ul><ul><li>Let LIST be a linked list in memory. In linked list start pointing to the first element & null indicating the end of list. </li></ul><ul><li>Our traversing algorithm uses a pointer variable PTR which points to the node currently being processed. </li></ul><ul><li>Accordingly LINK[PTR] points to the next node to be processed. Thus the assignment </li></ul><ul><li>PTR:=LINK[PTR] </li></ul><ul><li>Moves the pointer to the next node in the list. </li></ul>
6. 6. Traversing algorithm <ul><li>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. </li></ul><ul><li>Step1: Start. </li></ul><ul><li>Step2: set PTR:=START [initialize pointer PTR] </li></ul><ul><li>Step3: Repeat step4 and 5 while PTR!=NULL. </li></ul><ul><li>Step4: Print INFO[PTR]. </li></ul><ul><li>Step5: set PTR:=LINK[PTR]. (PTR now points to the next node) </li></ul><ul><li>End of step3 loop. </li></ul><ul><li>Step6: Exit. </li></ul>
7. 7. Searching into a linked list: <ul><li>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: </li></ul><ul><li>Step1: Start. </li></ul><ul><li>Step2: Set PTR to START [initialize pointer PTR] </li></ul><ul><li>Step3: Repeat step4 while PTR!=NULL. </li></ul><ul><li>Step4: if INFO[PTR]==ITEM then </li></ul><ul><li> print element found </li></ul><ul><li>Else: </li></ul><ul><li> Set PTR:=LINK[PTR]. (PTR now points to the next node) </li></ul><ul><li>(End of step3 loop) </li></ul><ul><li>Step5: Exit. </li></ul>
8. 8. Insertion into a linked list: <ul><li>Insertion into linked list may be possible in two ways: </li></ul><ul><li>Insertion at beginning </li></ul><ul><li>Insertion in between. </li></ul>
9. 9. Insertion Algorithm <ul><li>CASE 1 (insertion at beginning) </li></ul><ul><li>Suppose temp is node that has to be inserted. </li></ul><ul><li>In linked list START points to the first element of linked list. </li></ul><ul><li>Step1: Start </li></ul><ul><li>Step2: For insertion at beginning we assign the value of START to the link part of inserted node. </li></ul><ul><li>Now inserted node points to the next node which was beginning node of list as Temp[PTR]=START. </li></ul><ul><li>Step3: assign the address of inserted node to the start pointer variable as START=temp. </li></ul><ul><li>Now START will point to the inserted node(temp) which is first node of linked list. </li></ul><ul><li>Step4: exit </li></ul>Start Null temp 10 10 70 40 70 40 42 Start Null temp 40 70 70 40 10 10 42 Start Null temp 40 70 70 40 10 10 42 42
10. 10. CASE 2 (insertion in between) <ul><li>In case 2 first we traverse the list for obtaining the node after which we want to insert the element. </li></ul><ul><li>Here we have a linked list with 3 nodes (A, C & D) </li></ul><ul><li>Now here suppose we want to insert the node B in b/w A and C node. Then </li></ul><ul><li>Step1: First we assign the link part of node A to link part of node B as B[PTR]=A[PTR] </li></ul><ul><li>Now node B points to the node C </li></ul><ul><li>Step2: Secondly assign the address of node B to the link part of node A as A[PTR]=B. </li></ul><ul><li>Now node A points to node B and node B points to node C. </li></ul><ul><li>Step3: exit </li></ul>10 42 30 42 30 10 10 10 42 42 30 30 22 22 10 10 42 42 30 30 start A C D start A C D B start A C D B
11. 11. Deletion from a linked list: <ul><li>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. </li></ul><ul><li>Deletion at beginning </li></ul><ul><li>Deletion in between. </li></ul>
12. 12. Deletion Algorithm: <ul><li>Case 1 (deletion at beginning) </li></ul><ul><li>Suppose temp is node that has to be deleted. If the element to be deleted is the first node of linked list then first </li></ul><ul><li>Step1: assign the linked part of deleted node (temp) to the start. </li></ul><ul><li>After that start points to the second node of list and now we should free the temp node to be deleted </li></ul><ul><li>Step2: delete node temp. </li></ul><ul><li>Step3: exit. </li></ul>temp start A B C 21 21 55 55 42 42 Null Null temp start A B C 21 55 55 55 42 42 temp start Null 55 55 42 42 A B C
13. 13. Deletion Algorithm: <ul><li>Case 1 (deletion at beginning) </li></ul><ul><li>Step1: Start </li></ul><ul><li>Step2: Set PTR=START </li></ul><ul><li>Step3: Assign START=LINK[PTR] </li></ul><ul><li>now Start points out the second node in list. </li></ul><ul><li>Step4: Delete first Node.(A node) </li></ul><ul><li>Step5: Exit/End </li></ul>
14. 14. Case 2 deletion in between <ul><li>In case 2 first we traverse the list for obtaining the node which we want to delete. </li></ul><ul><li>Now here suppose we want to delete node B which is in b/w A and C node. Then </li></ul><ul><li>Step1: assign the link part of deleted node (node B) to the pervious node (node A) </li></ul><ul><li>After step1 node A is pointed to the node C. </li></ul><ul><li>Now we should delete the node B. </li></ul><ul><li>Step2: delete node B. </li></ul><ul><li>Step3: exit. </li></ul>start A B C 20 20 42 42 111 111 start A B C 111 111 20 20 42 start A B C 20 20 42 111 111
15. 15. Case 2 deletion in between <ul><li>Step1: Start </li></ul><ul><li>Step2: set PTR=START (PTR points to 1 st node) </li></ul><ul><li>Step3: set PTR1=LINK[PTR] (LINK[PTR] points to 2 nd node) </li></ul><ul><li>Step4: Assign LINK[PTR]=LINK[PTR1] or A[PTR]=B[PTR] </li></ul><ul><li>Step5: Delete Node. </li></ul><ul><li>Step6: Exit/End </li></ul>
16. 16. Deletion at last. <ul><li>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. </li></ul><ul><li>Step 1: </li></ul><ul><li>Step 2: </li></ul>start A B C start A B C start A B C
17. 17. Deletion at last. <ul><li>Step1: Start </li></ul><ul><li>Step2: set PTR=START </li></ul><ul><li>Step3: set PTR1=LINK[PTR] </li></ul><ul><li>Step4: Assign LINK[PTR1]= NULL </li></ul><ul><li>Step5: Delete last Node </li></ul><ul><li>Step6: Exit/End </li></ul>