- In function F, a place holder identifies missing code. In parts A and B use the specified code substitutions to answer the questions. Assume Data Type =int.
void F(Queue& Q)
{
int a[64], n=0,i;
int elt;
while(!Q.QEmpty())
a[n++]=Q.QDelete();
<code here>
}
- What is the order for the queue when the following code is used.
for(i=0;i<n;i++)
Q.QInsert(a[i]);
- reverses the order
- maintains the same order
- moves the first element to the rear.
- moves the last element to the front.
- What is the order for the queue when the following code is used.
for(i=n-1;i>=0;i--)
Q.QInsert(a[i])
- reverses the order
- maintains the same order
- moves the first element to the rear.
- moves the last element to the front.
- In parts A and B a collection class is used to reorder an array ofintegers. Describe the ordering in integer array A after executing function F.
- Use a Stack object
void F(int A[], int n)
{
Stack S;
int i;
for(i=0;i<n;i++)
S.Push(A[i]);
i=0;
while(!S.StackEmpty())
A[i++]=S.Pop();
}
- creates an ordered array in ascending order
- creates an ordered array in descending order
- reverses the order
- maintains the same order.
- Use a Priority Queue object.
void F(int A[], int n)
{
PQueue PQ;
int i;
for(i=0;i<n;i++)
PQ.PQInsert(A[i])
while(!PQ.PQEmpty())
A[--i]=PQ.PQDelete();
}
- creates an ordered array in ascending order
- creates an ordered array in descending order
- reverses the order
- maintains the same order.
- What is the output from the following sequence of stack operations? (Data Type=int)
Stack S;
int x=3; y=8;
S.Push(9);
S.Push(x);
S.Push(y);
x=S.Pop();
y=S.Pop();
cout<<y<<endl; //Output 1 _____
S.Push(22);
while(!S.StackEmpty())
{
y=S.Pop();
cout <<y<<endl; //Output 2 ____
}
cout <<x<<endl; //Output 3
- Trace the instructions and give the resulting output (Data Type=int)
Queue Q;
int x=5, y=3;
Q.QInsert(x);
Q.Qinsert(15)
Q.Qinsert(y);
x=Q.QDelete();
Q.Qinsert(22);
x=Q.Qdelete();
Q.Qinsert(45);
while(!Q.QEmpty())
{
y=Q.QDelete(); //Output 1 _____
cout << y << endl;
}
cout <<x<<endl; //Output 2 ______
- Trace the program and answer parts A through C. Assume that the function PrintStack pops succesive elements from the stack and prints them.
typedef int DataType;
#include "astack.h"
void F(Stack& S, Stack &T, int n)
{
int item;
T.ClearStack();
while(!S.StackEmpty())
{
item=S.Pop();
if(item<n)
T.Push(item);
}
}
void main(void)
{
int A[5]={2, 1, 7, 4, 3}, i;
Stack S,T; //Declare two stack objects.
for(i=0;i<5;i++)
S.Push(A[i]);
F(S,T,4); PrintStack(T);
}
- What is printed by PrintStack
- 3 1 2
- 2 1 3 7 4
- 2 1 3
- 3 4 7 1 2
- Assume array A has initial values {5, 30, 25, 10, 20} and F is called with parameters F(S,T, 20). What is printed by PrintStack.
- 5 30 25 10 20
- 20 10 5
- 20 25 30
- 5 10
- Assume that the character "F" implies first, "L" implies last. "I" implies in, and "O" implies out. Select the sequence that describes the action of collection classes in parts A and B.
1 2 3 4
LILO FILO FIFO LIFO
- For a stack
- 1 and 2
- 2 and 4
- 1 and 4
- none of the above
- For a queue
- 1 and 2
- 1 and 4
- 1 and 3
- none of the above
- Trace the Program and use for parts A and B.
#include<iostream.h>
typedef int DataType;
#include "astack.h"; //For the stack class
#include "apqueue.h" //for the priority queue class
//function uses both a priority queue and a stack object
void Ques(PQueues& pq)
{
Stack S; //declare a local stack to store data
int elt;
while(!pq.PQEmpty())
{
elt=pq.PQDelete();
S.Push(elt);
}
while(!S.StackEmpty())
cout<<S.Pop()<<" ";
cout << endl;
}
void main(void)
{
PQueue PQ;
int vals[]={2, 8, 55, 3, 12, 9} //declare an array of 6 integers
for(int i=0;i<6;i++)
PQ.PQInsert(vals[i]);
Ques(PQ);
}
- What is the output _____
- Assume array vals has initial values {20, 50, 30, 20, 10, 40}
What is the resulting output _____
- Write the postfix expression in infix form
8a*bb*+cd+/e+
- (8a+b2)/(c+d)+e
- (8a+b)*b/(c+d)+e
- 8a/b2*/(c+d)+e
- (8a+b2)/c+d+e
- Convert the infix expression to postfix form.
(a2+4*b)/c+d2
- aa*4b+*c*dd/+
- aa4b*+/cdd+*
- aa*4b*+c/dd*+
- aa*4b+*/*+cd
- What is the output from the sequence of priority operations? (DataType=int)
PQueue PQ;
DataType x=5,y=3;
PQ.PQInsert(8);
PQ.PQInsert(9);
PQ.PQInsert(y);
x=PQ.PQDelete();
PQ.PQInsert(22);
while(!PQ.PQEmpty())
{
y=PQ.PQDelete(); //output 1(sequence)_____
cout << y << endl
}
cout << x << endl; //output 2_____
- For each Print statement, give the list of elements in the priority queue. (Data Type=int)
PQueue PQ; //Initailly the list contains 10 50 30 40 60
PQ.PQInsert(25);
PQ.PQDelete(); Print(): _____
PQ.PQInsert(35)
PQ.PQDelete(); Print(): _____
PQ.PQDelete(); Print(): _____
- A second stack can be used to modify the contents of the initial stack. After executing function F, describe the contents of Stack S.
void F(Stack &S, DataType item)
{
Stack T;
DataType val;
while(!S.StackEmpty()&&(val=S.Pop())!=item)
T.Push(val);
if(!S.StackEmpty())
{
while(!T.StackEmpty())
S.Push(T.Pop());
S.Push(val);
}
}
- Trace the code and give the output for #1 and #2
#include<iostream.h>
#include<string.h>
#include<ctype.h>
typedef char DataType;
#include "aqueue.h"
#include "astack.h"
#include "apqueue.h"
void main(void)
{
Stack S;
Queue Q;
PQueue PQ;
char ch;
char sentence[]="StackQueue";
for(int i=0; i<strlen(sentence);i++)
PQ.PQInsert(sentence[i]);
while(!PQ.PQEmpty())
{
ch=PQ.PQDelete();
if(isupper(ch))
S.Push(ch);
else
Q.QInsert(ch);
}
while(!S.StackEmpty()) //A Prints the stack
{
ch=S.Pop();
cout << ch;
}
cout << endl;
while(!Q.QEmpty()) //B prints the queue
{
ch=Q.Qdelete();
cout<<ch;
}
cout <<endl;
}
- What is the output A _____
- What is the output B ____
- A priority queue contains a list of items of record type Person:
struct Person
{
char name[32];
float salary;
};
The following items in the priority queue:
- "Robinson,Beth" $45000
- "Herbert,Ronald" $35000
- "Wells, Earl" $55000
- "Barnes, William" $25000
- "Stowe, Harold" $15000
- "Carter, Donna" $65000
- Indicate how the items will be deleted from the queue if the following overloaded "<" operator is used. List the ordering using the labels 1 to 6.
int operator < (Person a, Person b)
{
return strcmp(a.name, b.name)<0;
}
- List the ordering using the labels 1 to 6 when the following overloaded operator is used
int operator< (Person a, Person b)
{
return a.salary>b.salary;
}