0% found this document useful (0 votes)
49 views24 pages

Stacks and Queus

Uploaded by

Cherryyy
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)
49 views24 pages

Stacks and Queus

Uploaded by

Cherryyy
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

Stacks

Stacks
• A stack is a list of elements in which an element may be inserted or
deleted only at one end, called the top of the stack.
• The elements are removed from a stack in the reverse order of that in
which they were inserted into the stack,
• Stack is also known as a LIFO (Last in Fast out) list or Push down list.
Basic Stack Operations
• PUSH: It is the term used to insert an element into a stack.
• POP: It is the term used to delete an element from a stack.
• PEEK: Peek operation allows the user to see the element on the top of
the stack. The stBasic Stack Operations
• ack is not modified in any manner in this operation.
• isEmpty: Check if stack is empty or not
Basic Stack Operations
Standard Error Messages in Stack
• Two standard error messages of stack are
– Stack Overflow: If we attempt to add new element beyond the
maximum size, we will encounter a stack overflow condition.
– Stack Underflow: If we attempt to remove elements beyond the
base of the stack, we will encounter a stack underflow condition.
Stack Operations
• This procedure pushes an ITEM
onto a stack
1. If TOP = MAXSIZE, then Print: OVERFLOW, and Return.
2. Set TOP := TOP + 1 [Increases TOP by 1]
3. SePUSH (STACK, TOP, MAXSTR, ITEM): t STACK [TOP] := ITEM. [Insert
ITEM in TOP position]
4. Return
POP
• POP (STACK, TOP, ITEM): This procedure deletes the top element of
STACK and assign it to the variable ITEM
1. If TOP = 0, then Print: UNDERFLOW, and Return.
2. Set ITEM := STACK[TOP]
3. Set TOP := TOP - 1 [Decreases TOP by 1]
4. Return
• PEEK (STACK, TOP): This procedure returns the position of top of a
stack
1. If TOP = 0, then Print: STACK EMPTY.
2. Return TOP
• ISEMPTY (STACK, TOP): This procedure checks whether stack is empty
or not
1. If TOP = 0, then Print: STACK IS EMPTY and Return.
2. Print: STACK IS NOT EMPTY and Return
• ISFULL (STACK,MAXSIZE, TOP): This procedure checks whether stack
is full or not
1. If TOP = MAXSIZE, then Print: STACK IS FULL and Return.
2. Print: STACK IS NOT FULL and Return
Applications of Stack
• Converting algebraic expressions from one form to another.
E.g. Infix to Postfix, Infix to Prefix, Prefix to Infix, Prefix to Postfix,
Postfix to Infix and Postfix to prefix.
• Evaluation of Postfix expression.
• Parenthesis Balancing in Compilers.
• Depth First Search Traversal of Graph.
• Recursive Applications.
Arithmetic Expression
• Infix: It is the form of an arithmetic expression in which we fix (place) the
arithmetic operator in between the two operands. E.g.: (A + B) * (C - D)
• Prefix: It is the form of an arithmetic notation in which we fix (place) the
arithmetic operator before (pre) its two operands. The prefix notation is
called as polish notation. E.g.: * + A B – C D
• Postfix: It is the form of an arithmetic expression in which we fix (place)
the arithmetic operator after (post) its two operands. The postfix notation
is called as suffix notation and is also referred to reverse polish notation.
E. g: A B + C D - *
Algorithm to convert Infix To Postfix
Let, X is an arithmetic expression written in infix notation. This
algorithm finds the equivalent postfix expression Y.
1. Push “(“onto Stack.
2. Scan X from left to right and repeat Step 3 to 6 for each element of X
until the Stack is empty.
3. If an operand is encountered, add it to Y.
4. If a left parenthesis is encountered, push it onto Stack.
5. If an operator is encountered ,then:
1. Repeatedly pop from Stack and add to Y each operator (on the top of
Stack) which has the same precedence as or higher precedence than
operator.
2. Add operator to Stack.
[End of If]
6. If a right parenthesis is encountered ,then:
1. Repeatedly pop from Stack and add to Y each operator (on the top of
Stack) until a left parenthesis is encountered.
2. Remove the left Parenthesis.
[End of If]
7. END
Conversion from Infix to Postfix
• Convert the following infix expression into its equivalent postfix
expression using stacks.
1. (a * b) / 2 – (c / d – e)
2. ( A + B ) * ( C – D / E ) * G + H
3. x + y * z + ( p * q + r ) * s
4. A + ( B * C – D / E* G ) + H
5. A + ( B * C) - ( ( D * E + F ) / G )
6. ( ( A + B) * C – ( D – E ) ^ ( F + G ))
Evaluation of Postfix Expression
• Postfix expression: 6 5 2 3 + 8 * + 3 + *
Queue
• A queue is a data structure where items are inserted at one end called
the rear and deleted at the other end called the front.
• Another name for a queue is ―FIFO or―First-in-first-out list.
• Operations of a Queue:
• enqueue: which inserts an element at the end of the queue.
• dequeue: which deletes an element at the front of the queue
Representation of Queue
Representation of Queue
Queue Operations using Array
• Various operations of Queue are:
• insertQ(): inserts an element at the end of queue Q.
• deleteQ(): deletes the first element of Q.
• displayQ(): displays the elements in the queue.
• There are two problems associated with linear queue. They are:
• Time consuming: linear time to be spent in shifting the elements to
the beginning of the queue.
• Signaling queuefull: even if the queue is having vacant position
Applications of Queue
• It is used to schedule the jobs to be processed by the CPU.
• When multiple users send print jobs to a printer, each printing job is
kept in the printing queue. Then the printer prints those jobs
according to first in first out (FIFO) basis.
• Breadth first search uses a queue data structure to find an element
from a graph
Circular Queue
• A circular queue is one in which the insertion of new element is done
at the very first location of the queue if the last location of the queue
is full.
• Suppose if we have a Queue of n elements then after adding the
element at the last index i.e. (n-1)th , as queue is starting with 0
index, the next element will be inserted at the very first location of
the queue which was not possible in the simple linear queue
Circular Queue operations
• The Basic Operations of a circular queue are
• InsertionCQ: Inserting an element into a circular queue results in
Rear = (Rear + 1) % MAX
where MAX is the maximum size of the array.
• DeletionCQ : Deleting an element from a circular queue results in
Front = (Front + 1) % MAX,
where MAX is the maximum size of the array.
• TraversCQ: Displaying the elements of a circular Queue.
Circular Queue Empty: Front=Rear=0.
Circular Queue Representation Using
Arrays
• Let us consider a circular queue, which can hold maximum (MAX) of
six elements. Initially the queue is empty.
Insertion and Deletion operations on a
Circular Queue
Insertion and Deletion operations on a
Circular Queue

You might also like