0% found this document useful (0 votes)
14 views9 pages

CHAPTER 10 - DATA STRUCTURE-II - STACKS AND QUEUES Notes For Computer Science Class 12

Uploaded by

shah.jayvee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views9 pages

CHAPTER 10 - DATA STRUCTURE-II - STACKS AND QUEUES Notes For Computer Science Class 12

Uploaded by

shah.jayvee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

LESSON 10 : DATA STRUCTURE-II : STACKS AND QUEUES

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”.

• One of the common uses of stack is in function call.

• There are two fundamental operations

1. Push:Push means to insert an element

2. Pop: Pop means to delete an element

• Rules that are followed by the stack:

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.

10.1.2 Other stack terms:

1. Peek:

Refers to inspecting the value at the stack’s top without removing it. It is also some

times referred as inspection.

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

currently having no item and still one tries to pop an item.

2
10.1.3 Implementing stack in python:

• Python offers a convenient set of methods to operate lists as stacks.

• Various stack operations are :

1. Peek:

• We can use

<stack> [top]

• Where <stack> is a list, top is an integer having equal value to len(<stack>) – 1.

2.Push:

• We can use

<stack>.append(<item>)

• Where <item> is the item being pushed in the stack.


3. Pop:

• We can use

<stack>.pop( )

• It removes the last value from the stack and returns it.

10.1.4 Types of stack-itemnode:

• An item stored in a stack is also called item-node.

• 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

7 Item - node 'A'


Being Pushed
(a) Stack of integers 29,'Shreya',30 (b) Stack of Characters
items - node type ; integer) (item - node type : string)
13,'Rohan',35
(c) stack og logically related information
(item - node type : a list)

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.

10.1.5 Stack Applications:

1. Reverse a line:

• This can be achieved by pushing each character on to a stack as it is read.

• When the line is finished, character are then popped off the stack, and they will
come off in the reverse order.

2. Polish string:

• Another application of stack is conversion of arithmetic expressions in high-level


programminglanguages into machine readable form.

• 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.

Infix notation Prefix notation Postfix notation


A+B +AB AB +
(A-C)XB X -ACB AC - B X
A+(BXC) +A X BC ABC X +
(A+B)/(C-D) /+ AB - CB AB + CD -/
(A+(B+C))/(C-(DXB)) / + A X BC - C X DB ABC + CDB X -/

10.1. 6 Conversion of infix expression to postfix expression:

• While evaluating an infix expression, there is an evaluation order according to


which takes place in a specified order.

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.

• Steps to convert an infix expression to postfix expression:

1. Determine the actual evaluation order by inserting braces.

2. Convert the expression in the innermost braces into postfix notation by putting the
operator after the operand.

3. Repeat step 2 until entire expression is converted into postfix notation.

• Algorithm:

Evaluvation of postfix Expression

''' Reading of Expression takes place from left to right'''

1. Read the next element '''First element for the first time'''

2. If element is operated then

Push the element in the stack

3. If element is operator then

4. Pop two operands from the stack

''' POP one operand in case of unary operator'''

5. Evaluate the Expression formed by the two operands and the operator

6. Push the results of the expression in the syack end

7. If not - more - element then

POP the result

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.

• Rules followed by the queue:

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.

10.2.1 Other queue terms:

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:

Refers to situation when one tries to dequeue/delete an item from an


empty queue. Queue is currently having no item and still one tries to
dequeue an item.

6
10.2.2 Implementing queue in python:

• Python offers a convenient set of methods to operate lists as queues.

• Various stack operations are :

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.

10.2.3 Variations in queue:

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.

• Circular queues are used to overcome the problem of unutilized spaces in


fixed sized linear queues.

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.

• There are two variations of a deque

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.

2. Airport authorities make use of queues in situation of sharing a single runway of


airport for both landing and take-off.

3. CPU uses queues to implement round-robin scheduling among waiting processes.

4. Queues are used in many computer algorithms also.

You might also like