Unit 2
Stack and queues
STACK ADT
• A stack is an Abstract Data Type (ADT), that is
popularly used in most programming
languages. It is named stack because it has the
similar operations as the real-world stacks.
• The stack follows the LIFO (Last in - First out)
structure where the last element inserted
would be the first element deleted.
STACK OPERATION
IMPLEMENTATION OF STACK
APPLICATIONS OF STACK
EVALUATING ARITHMETIC EXPRESSION
QUEUE
IMPLEMENTATION
Circular Queue
• A Circular Queue is an extended version of a
normal queue where the last element of the
queue is connected to the first element of the
queue forming a circle.
• The operations are performed based on FIFO
(First In First Out) principle. It is also
called ‘Ring Buffer’.
• In a normal Queue, we can insert elements until
queue becomes full. But once queue becomes
full, we can not insert the next element even if
there is a space in front of queue.
• In Circular Queue, the insertion of a new
element is performed at the very first location
of the queue if the last location of the queue is
full, in which the first element comes just after
the last element.
• Toper form the insertion of an element to the
queue, the position of the element is
calculated by the relation as
• Rear = (Rear + 1) % Maxsize.
then set
• Queue [Rear] = value.
• Step 1: IF FRONT = -1
Write " UNDERFLOW "
Applications of Circular Queue:
• Memory Management: The unused memory
locations in the case of ordinary queues can be
utilized in circular queues.
• Traffic system: In computer controlled traffic system,
circular queues are used to switch on the traffic
lights one by one repeatedly as per the time set.
• CPU Scheduling: Operating systems often maintain a
queue of processes that are ready to execute or that
are waiting for a particular event to occur.
PRIORITY QUEUES
• priority queue, an element with high priority is
served before an element with low priority.
• If two elements have the same priority, they
are served according to their order in the
queue.
• Two types of priority queue
• 1. Max Priority Queue
• 2. Min Priority Queue
STACK AND QUEUES APPLICATIONS, INFIX TO POST FIX

STACK AND QUEUES APPLICATIONS, INFIX TO POST FIX

  • 1.
  • 2.
    STACK ADT • Astack is an Abstract Data Type (ADT), that is popularly used in most programming languages. It is named stack because it has the similar operations as the real-world stacks. • The stack follows the LIFO (Last in - First out) structure where the last element inserted would be the first element deleted.
  • 4.
  • 5.
  • 10.
  • 11.
  • 15.
  • 16.
  • 19.
    Circular Queue • ACircular Queue is an extended version of a normal queue where the last element of the queue is connected to the first element of the queue forming a circle. • The operations are performed based on FIFO (First In First Out) principle. It is also called ‘Ring Buffer’.
  • 20.
    • In anormal Queue, we can insert elements until queue becomes full. But once queue becomes full, we can not insert the next element even if there is a space in front of queue. • In Circular Queue, the insertion of a new element is performed at the very first location of the queue if the last location of the queue is full, in which the first element comes just after the last element.
  • 24.
    • Toper formthe insertion of an element to the queue, the position of the element is calculated by the relation as • Rear = (Rear + 1) % Maxsize. then set • Queue [Rear] = value.
  • 27.
    • Step 1:IF FRONT = -1 Write " UNDERFLOW "
  • 28.
    Applications of CircularQueue: • Memory Management: The unused memory locations in the case of ordinary queues can be utilized in circular queues. • Traffic system: In computer controlled traffic system, circular queues are used to switch on the traffic lights one by one repeatedly as per the time set. • CPU Scheduling: Operating systems often maintain a queue of processes that are ready to execute or that are waiting for a particular event to occur.
  • 29.
    PRIORITY QUEUES • priorityqueue, an element with high priority is served before an element with low priority. • If two elements have the same priority, they are served according to their order in the queue. • Two types of priority queue • 1. Max Priority Queue • 2. Min Priority Queue