Data Structures through C++
 Review of Basic Data Structures
 The List ADT
 Stack ADT
 Queue ADT
 Array and Linked Implementations using
Template Classes in C++.
2
 To learn the implementation of list, stack,
and queueADTs in C++ using templates.
3
Array Representation
4
 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
 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
 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
 Operations on a Linear List
8
 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
11
 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
 Different ways of mapping [5, 2, 4, 8, 1]
13
14
15
Linked Representation
16
 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
 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
 To insert an element as ith element,
 Find the location on i-1th element.
 Insert the new node after that element.
20
21
 Defines a data type called chainNode for
storing nodes.
22
 Implements a
linear list as
singly linked
list of nodes.
23
Array and Linked List Representation
24
 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
 Adding element E to the following stack.
26
 Stack of books in a library.
 Stack of CDs.
 Stack of papers in a printer.
 Identify more examples of stacks from real
world?
27
 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
30
 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
 Better in
performance than
derivedArrayStack
33
34
35
 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
 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
Array and Linked Representations
39
 A queue is a linear list in which insertions take
place from rear end and deletions take place
from front end.
40
 Railway reservation counters.
 Soda vending machines.
 Normally at all service centers.
 What other example queues can you think of?
41
 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
44
 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
 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
 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
 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
 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
 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
 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
 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
 Same as arrayStack, except push operation.
56
 May be implemented in the following two
ways:
 But, which is better?
57
 Initial values
 queueFront = queueBack = NULL.
 Boundary value
 queueFront = NULL iff queue is empty.
 All operations require O(1) time.
58
59
60
61
62
 Linear Lists
 Array lists
 Linked lists
 Stacks
 Array implementation
 Linked implementation
 Queues
 Array implementation
 Linked implementation
63
Review of Basic Data Structures
64

Review of basic data structures

  • 1.
  • 2.
     Review ofBasic Data Structures  The List ADT  Stack ADT  Queue ADT  Array and Linked Implementations using Template Classes in C++. 2
  • 3.
     To learnthe implementation of list, stack, and queueADTs in C++ using templates. 3
  • 4.
  • 5.
     A dataobject 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.
     A datastructure 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.
     Each instanceof 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.
     Operations ona Linear List 8
  • 9.
     ADT providesa 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.
  • 11.
  • 12.
     An arrayis 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.
     Different waysof mapping [5, 2, 4, 8, 1] 13
  • 14.
  • 15.
  • 16.
  • 17.
     Each elementof 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.
     Removing thethird 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.
  • 20.
     To insertan element as ith element,  Find the location on i-1th element.  Insert the new node after that element. 20
  • 21.
  • 22.
     Defines adata type called chainNode for storing nodes. 22
  • 23.
     Implements a linearlist as singly linked list of nodes. 23
  • 24.
    Array and LinkedList Representation 24
  • 25.
     A stackis 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.
     Adding elementE to the following stack. 26
  • 27.
     Stack ofbooks in a library.  Stack of CDs.  Stack of papers in a printer.  Identify more examples of stacks from real world? 27
  • 28.
     For implementingfunction 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.
  • 30.
  • 31.
     Can beimplemented 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.
  • 33.
     Better in performancethan derivedArrayStack 33
  • 34.
  • 35.
  • 36.
     Left endof 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.
     Can beimplemented 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.
  • 39.
    Array and LinkedRepresentations 39
  • 40.
     A queueis a linear list in which insertions take place from rear end and deletions take place from front end. 40
  • 41.
     Railway reservationcounters.  Soda vending machines.  Normally at all service centers.  What other example queues can you think of? 41
  • 42.
     For jobprocessing in operating systems.  For printing documents in printers.  For breadth-first search of a graph.  For file handling in distributed file systems. 42
  • 43.
  • 44.
  • 45.
     ith elementmay 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.
  • 47.
     Insertion  IncrementsqueueBack 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.
     We canimprove 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.
  • 50.
     When thesecond 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.
     One methodis  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.
     Another methodis  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.
  • 54.
     Position 0is 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.
     Even whenQueue 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.
     Same asarrayStack, except push operation. 56
  • 57.
     May beimplemented in the following two ways:  But, which is better? 57
  • 58.
     Initial values queueFront = queueBack = NULL.  Boundary value  queueFront = NULL iff queue is empty.  All operations require O(1) time. 58
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
     Linear Lists Array lists  Linked lists  Stacks  Array implementation  Linked implementation  Queues  Array implementation  Linked implementation 63
  • 64.
    Review of BasicData Structures 64