Stack Data Structure Practice Questions

  1. Write an algorithm for Push and Pop operations on Stack using Linked list. (8)
  2. Explain the linked list implementation of stack ADT in detail?
  3. Define an efficient representation of two stacks in a given area of memory with n words and explain.
  4. Explain linear linked implementation of Stack and Queue?
    a. Write an ADT to implement stack of size N using an array. The elements in the stack are to be integers. The operations to be supported are PUSH, POP and DISPLAY. Take into account the exceptions of stack overflow and stack underflow. (8)
    b. A circular queue has a size of 5 and has 3 elements 10,20 and 40 where F=2 and R=4. After inserting 50 and 60, what is the value of F and R. Trying to insert 30 at this stage what happens? Delete 2 elements from the queue and insert 70, 80 & 90. Show the sequence of steps with necessary diagrams with the value of F & R. (8 Marks)
  5. Write the algorithm for converting infix expression to postfix (polish) expression?
  6. Explain in detail about priority queue ADT in detail?
  7. Write a function called ‘push’ that takes two parameters: an integer variable and a stack into which it would push this element and returns a 1 or a 0 to show success of addition or failure.
  8. What is a DeQueue? Explain its operation with example?
  9. Explain the array implementation of queue ADT in detail?
  10. Explain the addition and deletion operations performed on a circular queue with necessary algorithms.(8)

List any four applications of stack.

  • Parsing context free languages
  • Evaluating arithmetic expressions
  • Function call
  • Traversing trees and graph
  • Tower of Hanoi

Define Circular Queue.

Another representation of a queue, which prevents an excessive use of memory by arranging elements/ nodes Q1,Q2,…Qn in a circular fashion. That is, it is the queue, which wraps around upon reaching the end of the queue

Define Dequeue.

Deque stands for Double ended queue. It is a linear list in which insertions and deletion are made from either end of the queue structure.

Write down the function to insert an element into a queue, in which the queue isimplemented as an array.

Q – Queue

X – element to added to the queue Q IsFull(Q)
– Checks and true if Queue Q is full Q->Size – Number of elements in the queue Q Q->Rear – Points to last element of the queue Q Q->Array – array used to store queue elements void
enqueue (int X, Queue Q) {
if(IsFull(Q))

Error (“Full queue”);
else {

Q->Size++;

Q->Rear = Q->Rear+1;

Q->Array[ Q->Rear ]=X;

}

}

How do you test for an empty Queue?

The condition for testing an empty queue is rear=front-1. In linked list implementation of queue the condition for an empty queue is the header node link field is NULL.

What are the various operations performed on the Queue?

The various operations performed on the queue are

CREATE(Q) Creates Q as an empty Queue.

Enqueue(Q,X) Adds the element X to the Queue.

Dequeue(Q) Deletes a element from the Queue.

ISEMTPTY(Q) returns true if Queue is empty else false. ISFULL(Q) – returns true if Queue is full else false.

Define Queue.

A Queue is an ordered list in which all insertions take place at one end called the rear, while all deletions take place at the other end called the front. Rear is initialized to -1 and front is initialized to 0. Queue is also referred as First In First Out (FIFO) list.

Explain the usage of stack in recursive algorithm implementation?

In recursive algorithms, stack data structures is used to store the return address when a recursive call is encountered and also to store the values of all the parameters essential to the current state of the function.