0% found this document useful (0 votes)
9 views

Sss Stackqueue

Uploaded by

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

Sss Stackqueue

Uploaded by

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

Subject: Data Structure Using C

Chapter 3: Stack & Queue

Name of Instructor: Prof. Suyog. S .Dhoot


Lecturer in IT Dept, K. K. Wagh Polytechnic,
Nashik
STACK

Linear data structure in which elements are added
and removed in particular order


It follow the First in Last out (FILO) principle. The first
item to be inserted into a stack is the last one to be
deleted from it.


Insertions and deletions are permitted only at one
end of the list.
Representation of stack in
memory

Stack can be represented as 1
dimentional array

When new element added in stack
then it stored in computer memory
at appropriate location


Exampl
e
Stack operations-
PUSH
● Adding/inserting new element in stack called
as push operation
● New element always added at top position of stack
● If stack size is full then new element can not
be added in stack
Stack operations-
POP
● removing existing element from stack called as pop
operation
● element always removed from top position of stack
● If stack size is empty then element can not
be removed from stack
Stack as

ADT
Operations performed on stack as follows
➢ Stack() -creates a new stack that is empty. It needs no parameters and returns an empty
stack.
➢ push(item)- adds a new item to the top of the stack. It needs the item and returns
nothing.
➢ pop()- removes the top item from the stack. It needs no parameters and returns the item.
The stack is modified.
➢ peek()- returns the top item from the stack but does not remove it. It needs no
parameters. The stack is not modified.
➢ isEmpty()- tests to see whether the stack is empty. It needs no parameters and returns a
boolean value.
➢ IsFull()- tests to see whether the stack is full. It needs no parameters and returns a boolean
value.
➢ size() -returns the number of items on the stack. It needs no parameters and returns an
integer.
Stack operation
conditions
● Stack Overflow : When the stack is full and user still
try to push an element in, the condition is called
stack overflow/ stack full.

● Stack Underflow : When the stack is empty and an


user try to pop element from the stack, the
condition is called stack underflow/ stack empty.
Algorithm for push

operation
Step 1 − Checks if the stack is full.
● Step 2 − If the stack is full, produces an error and exit.

Step 3 − If the stack is not full, increments top to point next empty space.

Step 4 − Adds data element to the stack location, where top is pointing.
● Step 5 − Returns success.

● Algo:
if stack is full
return null
else
top ← top + 1
stack[top] ← data
Algorithm for pop

operation
Step 1 − Checks if the stack is empty.
● Step 2 − If the stack is empty, produces an error and exit.
● Step 3 − If the stack is not empty, accesses the data element at which top is
pointing.
● Step 4 − Decreases the value of top by 1.
● Step 5 − Returns success.

● Algorithm:
if stack is empty
return null
else
data ← stack[top]
top ← top – 1
return data
Expression
types
● Infix: Operator comes in between
operands a+b, a*6, 5/9

● Prefix: Operator comes before


operands
+ab, *a6
● Postfix: operator comes after
operands ab+, a5*
Operator precedance
and
● associativity
Priority of operator goes like
this
(
^ precedance right to
left
*,/ precedance left to
right
+,- precedance left to
● e.g.
right 2- 9+7*
4^2^2, 4+7, 4
Evaluation of Postfix
expression
● Algorith
m:
1.scan the expression from left to
right direction
2.If character is operand then push the
element into stack
3. If character is operator then pop
elements
two from stack and perform
operation output result pushed into
stack
e.g. 2 3 1 * + 9 -, 5 3 + 8 2
-*
Evaluation of Prefix
expression
● Algorith
m:
1.scan the expression from right to
left direction
2.If character is operand then push the
element into stack
3. If character is operator then pop
elements
two from stack and perform
operation output result pushed into
stack
e.g. +9*26, -*+ABCD let A=4,
B=3, C=2, D=5,
Infix to
Postfix
Infix to
Postfix
Scan the infix expresion from left to right


If character is operand then push the operand in postfix string

If charcter is '(' then push it to stack
● If charcter is operator then perform following things:
1. if stack is empty then push operator into stack
2.If stack is not empty and scanned character having more priority than
operator present in stack at top position then push the scan operator into
stack
3.If stack is not empty and scanned character having less or same priority than
operator present in stack at top position then pop the operator present at top
position of stack and push it in postfix string as well as push the scanned
character into stack
4.If scanned character is ')' then pop all the operator from stack untill ')'
character obtained and push all operators into postfix string by discarding ')'
character

After all character scanned if any operator remains in stack then pop it and
push into postfix string
Infix to
Scan the infix expresion from Right to Left
Prefix

● Write '(' as ')' & ')' as '('


● If charcter is '(' then push it to stack
● If charcter is operator then perform following things:
1. if stack is empty then push operator into stack
2.If stack is not empty and scanned character having more priority than
operator present in stack at top position then push the scan operator into
stack
3.If stack is not empty and scanned character having less or same priority than
operator present in stack at top position then pop the operator present at top
position of stack and push it in postfix string as well as push the scanned
character into stack
4.If scanned character is ')' then pop all the operator from stack untill ')'
character obtained and push all operators into postfix string by discarding ')'
character

After all character scanned if any operator remains in stack then pop it and
push into postfix string
● Reverse the postfix string to get infix string
QUEUE

Linear data structure in which elements are added
and removed in particular order


It follow the First in First out (FI|FO) principle. The
first item to be inserted into a queue is the first one
to be deleted from it.


Insertions done from rear side and deletions are
done from front side of list
Example of
QUEUE
Representation of queue in
memory

Queue can be represented as 1
dimentional array

When new element added in Queue
then it stored in computer memory
at appropriate location

While deleting element from memory
the element which added first will be
romove first
Exampl
e
Queue operations-
Enqueue
● Adding/inserting new element in queue called
as enqueue operation
● New element always added at rear position
of queue
● If queue size is full then new element can not
be added in queue
Enqueu
operation
Queue operations-
Dequeue
● removing existing element from queue called
as dequeue operation
● element always removed from front
position of queue
● If queue size is empty then
element can not be removed from queue
Dequeue
operation
Queue as
● Operations ADT
performed on Queue as
➢ follows
Queue() creates a new queue that is
➢ empty.It needs no parameters and returns an empty queue.
enqueue(item) adds a new item to the rear of the queue. It needs
➢ the item and returns nothing.
dequeue() removes the front item from the queue. It needs no
➢ parameters and returns the item. The queue is modified.
isEmpty() tests to see whether the queue is empty. It needs no
➢ parameters and returns a boolean value.
size() returns the number of items in the queue. It needs no
parameters and returns an integer.
Queue operation
conditions
● Queue Overflow/Full : When the Queue is full and
user still try to enqueue an element in, the
condition is called queue overflow/ queue full.

● Queue Underflow/Empty : When the queue is


empty and an user try to dequeue element from
the queue, the condition is called queue underflow/
queue empty.
Algorithm for enque

operation
Step 1 − Checks if the queue is full.
● Step 2 − If the queue is full, produces an error and exit.

Step 3 − If the queue is not full, increments rear to point next empty space.

Step 4 − Adds data element to the queue location, where rear is pointing.
● Step 5 − Returns success.

● Algo:
if queue is full
return null
else
rear ← rear + 1
queue[rear] ← data
Algorithm for dequeue

operation
Step 1 − Checks if the queue is empty.
● Step 2 − If the queue is empty, produces an error and exit.
● Step 3 − If the queue is not empty, increments front index to point next element
● Step 4 − remove element where front is pointing.
● Step 5 − Returns success.

● Algorithm:
if queue is empty
return null
else
front =front+1
data ← queue[front]
return data
Types of
Queue
● Linear Queue:
Linear queue defines the simple
operation of queue in which insertion
occurs at the rear of the list and
deletion occurs at the front of the list.
Types of

Queue
Circular Queue:

In a circular queue, all nodes are treated
as circular. Last node is connected back
to the first node.

This implies that even if the last element
is occupied, a new value can be inserted
behind it in the first empty element of
● the array
Circular Queue-Example

Consider a Queue of size 8. Initially the queue is empty. Perform following operations
on queue.

Types
Priority Queue:
of

Queue
In priority queues while inserting an element we assign prority to that
element.
● While deleting we check two conditions.
1. Highest priority element deleted first.
2.While two elements have same priority the element entered first in
to the queue is the deleted first then next.

Types of
Deque (Double Ended Queue):
● Queue
in which insertion and deletion operations are
performed from both the ends
TYPES:
a.Input restricted deques : Input restricted deques allows
insertions at only one end of the array or list but deletions
allows at both ends.
b.Output restricted deques : Output restricted deques
allows deletions at only one end of the array or list but
insertions allow at both ends.
Applications of
Queue

In computer system to allocate cpu for
process execution

In printerto print request made by
multiple terinals

In genral to issue ticket or to provide
service in bank,reservation counter,ticket
issue etc.

You might also like