Review of basic data structures

802 views

Published on

Published in: Education, Technology, Business
  • Be the first to comment

  • Be the first to like this

Review of basic data structures

  1. 1. Data Structures through C++
  2. 2.  Review of Basic Data Structures  The List ADT  Stack ADT  Queue ADT  Array and Linked Implementations using Template Classes in C++. 2
  3. 3.  To learn the implementation of list, stack, and queueADTs in C++ using templates. 3
  4. 4. Array Representation 4
  5. 5.  A data object is a set of instances or values.  Examples:  An instance may be primitive or may be composed of other instances. (Eg: 123, total) 5
  6. 6.  A data structure is a data object together with the relationships that exist among the instances and among the individual elements that compose an instance.  These relationships are provided by specifying the operations of interest.  Most frequently used data objects and their operations are already implemented in C++ as primitive data types. ▪ Boolean (bool) ▪ Integer (int) 6
  7. 7.  Each instance of linear list (or ordered list) is an ordered collection of elements.  e0, e1, e2, e3, e4, …, en-1  Index of ei is i.  n is the list length or size.  When n = 0, the list is empty.  When n > 0, e0 is the zeroth element, en-1 is the last.  e0 comes before e1, e1 comes before e2, and so on.  Examples:  List of students in a class in alphabetic order.  List of percentages of students in decreasing order. 7
  8. 8.  Operations on a Linear List 8
  9. 9.  ADT provides a specification of  Instances, and  Operations that are to be performed.  It is independent of any programming language.  All representations of ADT must satisfy the specification. 9
  10. 10. 10
  11. 11. 11
  12. 12.  An array is used to store the list of elements.  If we use one dimensional array, the elements of the array are accessed as:  A[0] accesses zeroth element.  A[1] accesses first element.  A[n] accesses nth element.  Elements are mapped to positions in the array.  location (i) = i is the most natural mapping.  other ways of mapping are also possible. 12
  13. 13.  Different ways of mapping [5, 2, 4, 8, 1] 13
  14. 14. 14
  15. 15. 15
  16. 16. Linked Representation 16
  17. 17.  Each element of an instance of a data object is represented in a cell or a node.  Each node keeps the location of next node, called a link or a pointer.  As each node links to only one other node, the structure is called as a singly linked list or a chain. 17
  18. 18.  Removing the third element from the list involves:  Locating the second node.  Linking the second node to the fourth node.  Similar steps are followed for removing any element. 18
  19. 19. 19
  20. 20.  To insert an element as ith element,  Find the location on i-1th element.  Insert the new node after that element. 20
  21. 21. 21
  22. 22.  Defines a data type called chainNode for storing nodes. 22
  23. 23.  Implements a linear list as singly linked list of nodes. 23
  24. 24. Array and Linked List Representation 24
  25. 25.  A stack is a linear list in which insertions and removals take place at the same end, called top.The other end is called bottom.  Insertions are also called as pushes.  Removals are also called as pops.  A stack is a LIFO (Last In First Out) list. 25
  26. 26.  Adding element E to the following stack. 26
  27. 27.  Stack of books in a library.  Stack of CDs.  Stack of papers in a printer.  Identify more examples of stacks from real world? 27
  28. 28.  For implementing function calls.  For implementing recursive functions.  For converting infix expression to postfix.  For evaluating postfix expressions.  For construction of compilers.  For depth first search of a graph. 28
  29. 29. 29
  30. 30. 30
  31. 31.  Can be implemented in two ways:  derivedArrayStack ▪ Derived from arrayList and stack classes. ▪ Uses the methods of arrayList for performing push and pop operations. ▪ Efficiency of this class is low due to the usage of methods of arrayList for push and pop.  arrayStack ▪ Derived from stack class. ▪ Improves the run-time performance. 31
  32. 32. 32
  33. 33.  Better in performance than derivedArrayStack 33
  34. 34. 34
  35. 35. 35
  36. 36.  Left end of the chain or the right end of the chain can be used as a stack top.  Using right end as top takes more time.  So, left end of the stack is used as top.  Push and pop operations are done at the left end. 36
  37. 37.  Can be implemented in two ways:  derivedLinkedStack ▪ Derived from chain and stack classes. ▪ Can be obtained by changing derivedArrayStack.  linkedStack ▪ Derived from stack. ▪ Improves the run-time performance. 37
  38. 38. 38
  39. 39. Array and Linked Representations 39
  40. 40.  A queue is a linear list in which insertions take place from rear end and deletions take place from front end. 40
  41. 41.  Railway reservation counters.  Soda vending machines.  Normally at all service centers.  What other example queues can you think of? 41
  42. 42.  For job processing in operating systems.  For printing documents in printers.  For breadth-first search of a graph.  For file handling in distributed file systems. 42
  43. 43. 43
  44. 44. 44
  45. 45.  ith element may be stored in ith location.  location(i) = i  queueFront and queueBack are used to denote front and rear of the queue.  queueFront = 0.  Queue size = queueBack+1.  queueBack = -1, for empty queue. 45
  46. 46. 46
  47. 47.  Insertion  Increments queueBack by 1.  Places new element in queue[queueBack].  Takes O(1) time.  Deletion  Shifts all the elements one position to the left.  Takes O(n) time, where n is number of elements.  Very time consuming. 47
  48. 48.  We can improve the deletion operation by the following method:  Use the equation ▪ location(i) = location(front element) + i.  Does not require shifting of elements by one position to the left.  Increment queueFront by 1 for every deletion.  Queue is empty, if queueBack < queueFront.  Takes O(1) time. 48
  49. 49. 49
  50. 50.  When the second equation is used, every deletion increments queueFront by 1.  This may result in a situation shown below, where no new elements can be inserted even when space is available. 50
  51. 51.  One method is  To shift all the elements to the left end, which leaves space at the right end allowing insertions.  Deletion time is O(1).  Insertion time is O(arrayLength) in worst case. 51
  52. 52.  Another method is  To insert the elements from the queueFront when no space is available at the queueBack.  Both insertion and deletion take O(1) time.  In this case, the array is viewed as a circle.  The queue is called as a circular queue. 52
  53. 53. 53
  54. 54.  Position 0 is preceded by arrayLength-1.  When queueBack = arrayLength-1, then the new element is inserted into position 0.  Uses the following equation:  location(i) = (location(front element) + i) % arrayLength  Queue is empty iff  queueFront = queueBack = 0 (initially)  queueFront = queueBack (otherwise) 54
  55. 55.  Even when Queue is full, the condition queueFront = queueBack becomes true.  To avoid this confusion , a queue is never made full, and the size is doubled whenever such a situation arises. 55
  56. 56.  Same as arrayStack, except push operation. 56
  57. 57.  May be implemented in the following two ways:  But, which is better? 57
  58. 58.  Initial values  queueFront = queueBack = NULL.  Boundary value  queueFront = NULL iff queue is empty.  All operations require O(1) time. 58
  59. 59. 59
  60. 60. 60
  61. 61. 61
  62. 62. 62
  63. 63.  Linear Lists  Array lists  Linked lists  Stacks  Array implementation  Linked implementation  Queues  Array implementation  Linked implementation 63
  64. 64. Review of Basic Data Structures 64

×