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

Sheet1,2 Stack+Que Solution

The document discusses data structures, particularly stacks and queues, explaining their definitions, operations, and scenarios for insertion in circular queues. It includes C++ code for implementing a circular queue and evaluates various expressions and stack operations. Additionally, it addresses the state of stacks and queues after specific operations and provides examples of testing for balanced parentheses.

Uploaded by

eslamdawam2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Sheet1,2 Stack+Que Solution

The document discusses data structures, particularly stacks and queues, explaining their definitions, operations, and scenarios for insertion in circular queues. It includes C++ code for implementing a circular queue and evaluates various expressions and stack operations. Additionally, it addresses the state of stacks and queues after specific operations and provides examples of testing for balanced parentheses.

Uploaded by

eslamdawam2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Sheet (1) Stack and Queue

1- What is Data Structure? Explain.

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++

The results are 31, 31 and 35.

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 enQueue(int element)


{
if(isFull())
{
cout << "Queue is full"<<endl;
}
else {
if(front == -1)
front = 0;
rear = (rear + 1) % SIZE;
data[rear] = element;
cout <<"data in queue"<<data[rear]<<endl;

}
}

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;
}
}

8- i) What are the values of front and rear?


front = 1
rear = 5
ii) Given this queue, suppose we call Dequeue twice and Enqueue once. What
would be the new values of front and rear?
front = 3
rear = 6
iii) How many elements can this queue hold?
the array has 13 elements but one is reserved for the empty spot before the first element

9- Consider the following sequence of stack operations: push(d), push(h), pop(),


push(f), push(s), pop(), pop(), push(m).
(a) Assume the stack is initially empty, what is the sequence of popped values, and
what is the final state of the stack? (Identify which end is the top of the stack.)
(b) Suppose you were to replace the push and pop operations with enqueue and
dequeue respectively. What would be the sequence of dequeued values, and what
would be the final state of the queue? (Identify which end is the front of the queue.)
(a) Sequence of popped values: h,s,f. State of stack (from top to bottom): m, d
(b) Sequence of dequeued values: d,h,f. State of queue (from front to rear): s,m.

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.

You might also like