2. Contents
 Meaning of time complexity analysis
 Queue data structure
 Complexity analysis of queue operations
 Stack data structure
 Complexity analysis of stack operations
3. Time complexity analysis
 Analysis is based on the amount of work done by
the algorithm.
 Time complexity expresses the relationship
between the size of the input and the run time for
the algorithm.
 To simplify analysis, we sometimes ignore work
that takes a constant amount of time, independent
of the problem input size.
4.  Simplified analysis can be based on:
Number of arithmetic operations
performed
Number of comparisons made
Number of times through a critical
loop
Number of array elements accessed
5. Queue Data Structure
 Queue is a linear structure which follows a
particular order in which the operations are
performed.
 The order is First In First Out (FIFO).
6. Queue operations
 Mainly the following 2 operations are performed
on queue:
 Enqueue: Adds an item to the queue.
 Dequeue: Removes an item from the queue.
7.
8. Queue implemented as array
 Array elements are stored contiguously in memory,
so the time required to compute the memory
address of an array element arr[k] is independent of
the array’s size.
 So, storing and retrieving array elements are O(1)
operations.
9.  The time complexity of enqueue
operation is O(1) unless the array size
has to be increased (in which case it’s
O(n)).
• The time complexity of dequeue
operation is O(n) because all the
remaining elements have to be shifted.
10. Queue implemented as linked
list
 As long as we have both a head and a tail pointer in the linked
list, all operations are O(1)
 To achieve O(1) performance, linked queue can be
implemented as:
 a doubly-linked list, which naturally allows you to manipulate
each end as a single operation.
11. Stack data structure
 It is a simple data structure that allows adding and
removing elements in a particular order.
 Every time an element is added, it goes on the top
of the stack and the only element that can be
removed is the element that is at the top of the
stack.
12. Stack operations
 Mainly the following 2 operations are performed on
queue:
 push: Adds an item to the stack.
 pop: Removes an item from the stack.
13.
14. Stack implemented as array
 All operations are O(1), provided that the top of the stack
is always at the highest index currently in use: no shifting
required.
 In push operation you add one element at the top of
the stack so you make one step , so it takes constant
time so push takes O(1).
 In pop operation you remove one element from the
top of the stack so you make one step , so it takes
constant time so push takes O(1)
15. Stack implemented using linked
list
 Stack using an underlying linked list:
All operations are O(1)
Top of stack is the head of the linked list
If a doubly-linked list with a tail pointer is
used, the top of the stack can be the tail of
the list