CHAPTER 10 - DATA STRUCTURE-II - STACKS AND QUEUES Notes For Computer Science Class 12
CHAPTER 10 - DATA STRUCTURE-II - STACKS AND QUEUES Notes For Computer Science Class 12
1
10.1 STACKS:
• In computer science, a stack is a Last in, First out (LIFO) data structure.
• It simply means that an element that is inserted at the end will be deleted first. To manage
a stack all the insertion and deletion takes place from one position called “top”.
1. Data can only be removed from the top, the element at the top of the stack. The
removal of element from a stack is technically called pop operation.
2. A new data element can only be added to the top of the stack. The insertion of
element in a stack is push operation.
• Stack is a dynamic data structure as it can grow or shrink. A stack is also a static data
structure, as it is the one that has fixed size.
1. Peek:
Refers to inspecting the value at the stack’s top without removing it. It is also some
2. Overflow:
Refers to condition when one tries to push an item in stack that is full. This situation
occurs when the size of the stack is fixed and cannot grow more or there is no
memory left to put up new item.
3. Underflow:
Refers to situation when one tries to pop an item from an empty stack. Stack is
2
10.1.3 Implementing stack in python:
1. Peek:
• We can use
<stack> [top]
2.Push:
• We can use
<stack>.append(<item>)
• We can use
<stack>.pop( )
• It removes the last value from the stack and returns it.
• If we want to create stack that may contain logically group information such as
member detail like member no, member name, age. Then for such stack the
item-node will be a list containing the member details and then this list will
be entered as an item to the stack.
• Example:
3 'Z'
Item - node Item - node
8 Being Pushed
[31,'Zubin',28]
'C' Being Pushed
3
• From the above fig,
1. fig(a) the stack will be implemented as stack of integers as item-node is of integer type.
2. fig(b) the stack will be implemented as stack of strings as item-node is of string type.
3. Fig© the stack will be implemented as stack of lists as item-node is of list type.
1. Reverse a line:
• When the line is finished, character are then popped off the stack, and they will
come off in the reverse order.
2. Polish string:
• Arithmetic operations can be converted into polish string using stacks which then
can be executed in two operands and operator form.
• Polish string refers to the notation in which the operator symbol is placed either
before its operand or after its operands in contrast to usual form where operator is
placed in between the operands.
1. Brackets or parentheses
2. Exponentiations
3. Multiplication or division
4. Addition or subtraction
4
• The operator with same priority are evaluated from left to right. To convert an infix into
postfix expression, order must be followed.
• An infix may be converted into postfix from either manually or using stack.
2. Convert the expression in the innermost braces into postfix notation by putting the
operator after the operand.
• Algorithm:
1. Read the next element '''First element for the first time'''
5. Evaluate the Expression formed by the two operands and the operator
else
go to step 1.
8. END
5
10.2 QUEUE:
• In computer science, a Queue is a First in, First out (FIFO) data structure.
• It simply means that an element that is inserted at the beginning will be deleted
first.
• To manage a queue all the insertion and deletion takes place from two different
positions called “front” and “rear”.
• Every element is inserted from the rear position and deleted from the front position
in the queue.
• The operations of adding and removing items are called enqueuer and dequeuer.
1. Data can only be removed from the front-end, the element at the front-end
of the queue. The removal of element is called dequeuer operation.
2. A new data element can only be added to the rear of the queue.
The insertion of element is called enqueuer operation.
1. Peek:
Refers to inspecting the value at the queue’s front without removing it.
It is also sometimes referred as inspection.
2. Overflow:
Refers to condition when one tries to enqueue an item in queue that is full.
This situation occurs when the size of the queue is fixed and cannot
grow more or there is no memory left to put up new item.
3. Underflow:
6
10.2.2 Implementing queue in python:
1. Peek:
• We can use
<queue>[front]
• Where <queue> is a list, front is an integer storing the position of first value in the
queue.
2. Push:
• We can use
<queue>.append(<item>)
• Where <item> is the item being pushed in the queue. The item will be added at
the rear-end of the queue.
3. Pop:
• We can use
<Queue>.pop(0)
• It removes the last value from the queue and returns it.
1. Circular queue:
• Circular queue are the queues implemented in circular form rather than a straight line.
• In linear type of queues after some insertion and deletions, some unutilized space
lies in the beginning of the queue.
7
Rear
Utilised
Space
Rear 4 12
Front Front 3 0 3 9
1 2
5 7
Occupied
Space (b) Circular Queue at any random
(a)
20 18
21 8 7 6 4 12
3 0 3 9
Rear 1 2
5 7
Front
(b) Circular Queue (full to the Capacity) at any random time
2. Deque:
• Deques are the refined queues in which elements can be added or removed either
end but not in the middle.
1. An input restricted deque is a deque which allows insertions at only one end
but allows deletions at both ends of the list
2. An output restricted deque which allows deletions at only one end of the list
but allows insertions at both ends of the list.
8
10.2.4 Queue applications:
1. Sharing of one resource among multiple users or seekers such as shared printer
among multiple computers, call center executives response among waiting callers.