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. 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. 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
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
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
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. 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
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
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. 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
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
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. 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
40. A queue is a linear list in which insertions take
place from rear end and deletions take place
from front end.
40
41. Railway reservation counters.
Soda vending machines.
Normally at all service centers.
What other example queues can you think of?
41
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
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
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. 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
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. 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. 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
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. 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. Same as arrayStack, except push operation.
56
57. May be implemented 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