Upcoming SlideShare
×

# Lecture 5 data structures and algorithms

771 views
638 views

Published on

Published in: Education, Technology
1 Like
Statistics
Notes
• Full Name
Comment goes here.

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

Views
Total views
771
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
62
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Lecture 5 data structures and algorithms

1. 1. Data Structure & Algorithm CS 102 Ashok K Turuk
5. 5. List Traversal Let Head be a pointer to a linked list in memory. Write an algorithm to print the contents of each node of the list 5
6. 6. List Traversal Algorithm 1. set PTR = Head 2. Repeat step 3 and 4 while PTR NULL 3. Print PTR->DATA 4. Set PTR = PTR -> LINK 5. Stop 6
7. 7. Search for an ITEM • Let Head be a pointer to a linked list in memory. Write an algorithm that finds the location LOC of the node where ITEM first appears in the list, or sets LOC = NULL if search is unsuccessful. 7
8. 8. Search for an ITEM Algorithm 1. Set PTR = Head 2. Repeat step 3 while PTR NULL 3. if ITEM == PTR -> DATA, then Set LOC = PTR, and Exit else Set PTR = PTR -> LINK 4. Set LOC = NULL /*search unsuccessful */ 5. Stop 8
9. 9. Search for an ITEM Algorithm [Sorted ] 1. Set PTR = Head 2. Repeat step 3 while PTR NULL 3. if ITEM < PTR -> DATA, then Set PTR = PTR->LINK, else if ITEM == PTR->DATA, then Set LOC = PTR, and Exit else Set LOC = NULL, and Exit 4. Set LOC = NULL /*search unsuccessful */ 5. Stop 9
11. 11. Insertion to Beginning Head PTR Data Link New Node 11
14. 14. Overflow and Underflow • Overflow : A new Data to be inserted into a data structure but there is no available space. • Underflow: A situation where one wants to delete data from a data structure that is empty. 14
15. 15. Insertion to Beginning Head PTR Data Link New Node Overflow , PTR == NULL 15
16. 16. Insertion at the Beginning Let Head be a pointer to a linked list in memory. Write an algorithm to insert node PTR at the beginning of the List. 16
17. 17. Insertion at the Beginning Algorithm 1. If PTR == NULL , then Write Overflow and Exit 2. Set PTR -> DATA = ITEM 3. Set PTR -> LINK = Head -> LINK 4. Set Head = PTR 5. Exit 17
18. 18. Insertion After a Given Node Let Head be a pointer to a linked list in memory. Write an algorithm to insert ITEM so that ITEM follows the node with location LOC or insert ITEM as the first node when LOC == NULL 18
19. 19. Insertion at a Given Node Head Data Link LOC A PTR B New Node 19
21. 21. Insertion at a Given Node Head Data Link LOC B A PTR New Node LOC->Link = PTR 21
22. 22. Insertion After a Given Node Algorithm 1. If PTR == NULL , then Write Overflow and Exit 2. Set PTR -> DATA = ITEM 3. If LOC == NULL Set PTR -> LINK = Head Set Head = PTR Else Set PTR->Link = LOC->Link Set LOC->Link = PTR 4. Exit 22
23. 23. Insertion into a Sorted Linked List Head Data Link 1000 2000 3500 3000 4000 5000 3000 < 3500 < 4000 23
24. 24. Insertion into a Sorted Linked List To Insert Between Node A and B We have to Remember the Pointer to Node A which is the predecessor Node of B 1000 2000 3000 4000 A 5000 B 3000 < 3500 < 4000 24
25. 25. Insertion into a Sorted Linked List Steps to Find the LOC of Insertion 1. If Head == NULL, then Set LOC = NULL and Return 2. If ITEM < Head ->Data , then Set LOC = NULL and Return 3. Set SAVE = Head and PTR = Head -> Link 4. Repeat Steps 5 and 6 while PTR NULL 5. If ITEM < PTR -> Data then LOC = SAVE and Return 6. Set SAVE = PTR and PTR = PTR->Link 7. Set LOC = SAVE 8. Return 25
26. 26. Deletion Algorithm A 26
27. 27. Deletion Algorithm A 27
28. 28. Delete the Node Following a Given Node • Write an Algorithm that deletes the Node N with location LOC. LOCP is the location of the node which precedes N or when N is the first node LOCP = NULL 28
29. 29. Delete the Node Following a Given Node • Algorithm: Delete(Head, LOC, LOCP) 1 If LOCP = NULL then Set Head = Head ->Link. [Deletes the 1st Node] Else Set LOCP->Link = LOC->Link [Deletes Node N] 2. Exit 29
30. 30. Delete an Item Let Head be a pointer to a linked list in memory that contains integer data. Write an algorithm to delete node which contains ITEM. 30
31. 31. Delete an Item To delete a Node [Node B] We have to Remember the Pointer to its predecessor [Node A] 1000 2000 3000 4000 A 5000 B 4000 31
32. 32. Deletion of an ITEM Algorithm 1. 2. 3. Set PTR=Head and TEMP = Head Repeat step 3 while PTR NULL If PTR->DATA == ITEM, then Set TEMP->LINK = PTR -> LINK, exit else TEMP = PTR PTR = PTR -> LINK 4. Stop 32
33. 33. Deletion of an ITEM Algorithm 1. If Head == NULL , then Write ITEM is not in the List, Exit 2. If Head ->Data == Item Head = Head -> Link 3. Set PTR=Head and TEMP = Head 33
34. 34. Deletion of an ITEM Algorithm 4. Repeat step 4 while PTR NULL If PTR->DATA == ITEM, then Set TEMP->LINK = PTR -> LINK, exit else TEMP = PTR PTR = PTR -> LINK 5. Stop 34
38. 38. Header Linked Lists • The first node in a header list is the node following the header node • Circular Header list are frequently used instead of ordinary linked list – Null pointer are not used, hence all pointer contain valid addresses – Every node has a predecessor, so the first node may not require a special case. 38
39. 39. Traversing a Circular Header List • Let Head be a circular header list in memory. Write an algorithm to print Data in each node in the list. 39
40. 40. Traversing a Circular Header List Algorithm 1. 2. 3. 4. 5. Set PTR = Head->Link; Repeat Steps 3 and 4 while PTR Print PTR->Data Set PTR = PTR ->Link Exit Head 40
41. 41. Locating an ITEM • Let Head be a circular header list in memory. Write an algorithm to find the location LOC of the first node in Head which contains ITEM or return LOC = NULL when the item is not present. 41
42. 42. Locating an ITEM Algorithm 1. Set PTR = Head->Link 2. Repeat while PTR ->Data PTR Head ITEM and Set PTR = PTR ->Link 3. If PTR->Data == ITEM then Set LOC = PTR else Set LOC = NULL 4. Exit 42
43. 43. Other variation of Linked List • A linked list whose last node points back to the first node instead of containing a NULL pointer called a circular list 43
44. 44. Other variation of Linked List • A linked list which contains both a special header node at the beginning of the list and a special trailer node at the end of list Header Node Trailer Node 44
45. 45. Applications of Linked Lists 1. Polynomial Representation and operation on Polynomials Ex: 10 X 6 +20 X 3 + 55 2. Sparse Matrix Representation 0 22 0 0 0 0 11 0 66
46. 46. Polynomials A( x) am 1 x coef em 1 am 2 x expon em 2 ... link Representation of Node a0 x e0
47. 47. Example 14 a 3x a 3 14 14 b 8x b 2x 8 8 14 1 1 0 null 2 8 10 3x 10x -3 10 6 10 6 null
48. 48. Polynomial Operation 1. 2. 3. 4. Addition Subtraction Multiplication Scalar Division
49. 49. Polynomial Addition 3 14 a 8 14 b 2 8 1 0 -3 10 10 6 11 14 d 3 14 8 14 11 14 a->expon == b->expon 2 8 a -3 10 b -3 10 d 1 0 10 6 a->expon < b->expon
50. 50. Polynomial Addition 3 14 2 8 a 8 14 -3 10 11 14 -3 10 a->expon > b->expon 1 0 10 6 b 2 8 d
51. 51. Polynomial Addition (cont’d) 3 2 8 11 14 14 14 -3 10 -3 10 8 2 8 a->expon > b->expon 1 0 10 6 10 6 a b 1 0 d
52. 52. Polynomial Subtraction 3 14 a 8 14 b 2 8 1 0 -3 10 10 6 -5 14 d 3 14 8 14 -5 14 a->expon == b->expon 2 8 a -3 10 b 3 10 d 1 0 10 6 a->expon < b->expon
53. 53. Polynomial Subtraction (cont’d) 3 14 2 8 a 8 14 -3 10 -5 14 3 10 a->expon > b->expon 1 0 10 6 b 2 8 d
54. 54. Polynomial Addition (cont’d) 3 2 8 -5 14 14 14 -3 10 3 10 8 2 8 a->expon > b->expon 1 0 10 6 -10 6 a b 1 0 d
55. 55. Sparse Matrix A sparse matrix is a very large matrix that contains lot of zero’s. In order to save storage space, a sparse matrix stores only the nonzero elements.
56. 56. Linked list representation Sparse Matrix m n -- i j val i j val Null 0 2 5 3 2 8 Null 0 0 5 0 A 2 7 0 9 0 0 0 0 0 3 8 0 4 4 --