Sheet1,2 Stack+Que Solution
Sheet1,2 Stack+Que Solution
The data structure is a way that specifies how to organize and manipulate the data. It
also defines the relationship between them. Some examples of Data Structures are
arrays, Linked List, Stack, Queue, etc. Data Structures are the central part of many
computer science algorithms as they enable the programmers to handle the data in an
efficient way
3- What are the scenarios in which an element can be inserted into the circular
queue?
o If (rear + 1) = front, the queue is full. In that case, overflow occurs and therefore,
insertion can not be performed in the queue.
o If rear != max - 1, the rear will be incremented to the (maxsize) and the new value
will be inserted at the rear end of the queue.
o If front != 0 and rear = max - 1, it means that queue is not full therefore, set the
value of rear to 0 and insert the new element there.
5- Given the following C++ fragment, what is the state of the stack (recording the
opening delimiters—(, [, {, <—that have not yet been matched) at the end of
this fragment?
Answer
the state of the stack is
6- Evaluate the following expressions:
123+*45*6++
123+*45*+6+
12+3*45*6++
7- #include <iostream>
#define SIZE 100 /* Size of Circular Queue */
using namespace std;
class Queue {
private:
int data[SIZE], front, rear;
public:
Queue()
{
front = -1;
rear = -1;
}
bool isFull()
{
if((front == 0 && rear == SIZE - 1)||(front == rear + 1))
{
return true;
}
return false;
}
bool isEmpty(){
if(front == -1) return true;
else return false;
}
}
}
void deQueue()
{
int element;
if(isEmpty()){
cout << "Queue is empty" << endl;
}
else {
cout<< data[front];
if(front == rear){
front = -1;
rear = -1;
} /* Q has only one element, so we reset the queue after deleting it. */
else {
front=(front+1) % SIZE;
}
}
}
void display()
{
/* Function to display status of Circular Queue */
int i;
if(isEmpty()) {
cout << endl << "Empty Queue" << endl;
}
else
{
if (front > rear)
{for(i=front; i!=rear;i=(i+1)%SIZE)
cout << data[i]<<endl;
cout << endl << "Rear -> " << rear;}
}
}
};
int main()
{
Queue q;
int i;
cin>>i;
while (i != 0)
{
if (i < 35)
q.enQueue(i);
cin>>i;
}
while (!q.isEmpty())
{
q.deQueue();
cout<<i<<endl;
}
}
10- Use a stack to test for balanced parentheses, when scanning the following
expressions. Your solution should show the state of the stack each time it is
modified. The “state of the stack” must indicate which is the top element. Only
consider the parentheses [,],(,),{,} . Ignore the variables and operators.
(a) [ a + { b / ( c - d ) + e / (f + g ) } - h ]
(b) [ a { b + [ c ( d + e ) - f ] + g }
11- Suppose you have a stack in which the values 1 through 5 must be pushed on
the stack in that order, but that an item on the stack can be popped at any time.
Give a sequence of push and pop operations such that the values are popped in
the following order:
(a) 2, 4, 5, 3, 1
(b) 1, 5, 4, 2, 3
(c) 1, 3, 5, 4, 2
It might not be possible in each case.
12-
13- Consider the following sequence of stack commands:
push(a), push(b), push(c), pop(), push(d), push(e), pop(), pop(), pop(), pop().
(a) What is the order in which the elements are popped? (Give a list and indicate
which was popped first.)
(b) Change the position of the pop() commands in the above sequence so that the
items are popped in the following order: b,d,c,a,e.