QUIZ 5
  1. 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>
    }
    
    
    1. What is the order for the queue when the following code is used.
      for(i=0;i<n;i++)
      	Q.QInsert(a[i]);
      
      1. reverses the order
      2. maintains the same order
      3. moves the first element to the rear.
      4. moves the last element to the front.
    2. What is the order for the queue when the following code is used.
      for(i=n-1;i>=0;i--)
      	Q.QInsert(a[i])
      
      1. reverses the order
      2. maintains the same order
      3. moves the first element to the rear.
      4. moves the last element to the front.
  2. 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.

    1. 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();
      }
      

      1. creates an ordered array in ascending order
      2. creates an ordered array in descending order
      3. reverses the order
      4. maintains the same order.

    2. 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();
      }
      

      1. creates an ordered array in ascending order
      2. creates an ordered array in descending order
      3. reverses the order
      4. maintains the same order.
  3. 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
    

  4. 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 ______
    

  5. 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);
    }
    
    1. What is printed by PrintStack
      1. 3 1 2
      2. 2 1 3 7 4
      3. 2 1 3
      4. 3 4 7 1 2
    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.
      1. 5 30 25 10 20
      2. 20 10 5
      3. 20 25 30
      4. 5 10

  6. 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
    

    1. For a stack
      1. 1 and 2
      2. 2 and 4
      3. 1 and 4
      4. none of the above
    2. For a queue
      1. 1 and 2
      2. 1 and 4
      3. 1 and 3
      4. none of the above
  7. 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);
    }
    

    1. What is the output _____
    2. Assume array vals has initial values {20, 50, 30, 20, 10, 40}
      What is the resulting output _____
  8. Write the postfix expression in infix form
    8a*bb*+cd+/e+
    1. (8a+b2)/(c+d)+e
    2. (8a+b)*b/(c+d)+e
    3. 8a/b2*/(c+d)+e
    4. (8a+b2)/c+d+e

  9. Convert the infix expression to postfix form.
    (a2+4*b)/c+d2
    1. aa*4b+*c*dd/+
    2. aa4b*+/cdd+*
    3. aa*4b*+c/dd*+
    4. aa*4b+*/*+cd
  10. 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_____
    
  11. 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(): _____

  12. 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);
    		}
    }
    		
    

  13. 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;
    }
    
    1. What is the output A _____
    2. What is the output B ____

  14. 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:
    1. "Robinson,Beth" $45000
    2. "Herbert,Ronald" $35000
    3. "Wells, Earl" $55000
    4. "Barnes, William" $25000
    5. "Stowe, Harold" $15000
    6. "Carter, Donna" $65000
    1. 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;
      }
      

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