Sei sulla pagina 1di 3

El-Shourouk Academy Higher Institute for Computer & Information Technology Department of Computer Science

Acad. Year : 2013 / 2014 Term : First Year : Second

2102 Data Structures Sheet #7- Answer

Queue
1. What is the difference between a queue and a stack?
Stack is defined as a list of elements in which we can insert or delete the element only at the top of the stack. Queue is defined as a list of elements in which we can add elements at one end, called the rear of the queue and delete elements only at the other end called the front of the queue.

2. Assume the following basic queue operations: - enqueue operation inserts an element at the rear of the queue, - dequeue operation removes an element at the front of the queue, - queuerear operation examines (tests) the element at the rear of the queue without deleting it. What would be the exact contents of the queue q after the following pseudo code (algorithm) is executed and the following data are entered? Q = createqueue( ) /* queue is created */ loop (not end of file) read number if (number not 0) enqueue (Q, number) else x = queuerear (Q) enqueue (Q, x) end if end loop The data is: 5, 7, 12, 4, 0, 4 6, 8, 0, 67, 34, 23, 0, 5, 0, 44
Answer: 5, 7, 12, 4, 4, 4, 6, 8, 8, 67, 34, 23, 23, 5, 5, 44 3. In the following problems, display each program fragment's output. Show your work. Assume that initially, the queue Q and the stack S are empty. 1.listQueue ( Q); /* List the elements in the queue in FIFO order*/ enqueue(Q,5); enqueue(Q,6); 1

enqueue(Q,7); 2. enqueue(Q,8); 3. int x= peekFront( Q); /* access the element at the front without removing it*/ int y; 4. y=dequeue(Q); 5. enqueue(Q,x); 6. enqueue(Q,++y); 7. dequeue(Q); 8. y=dequeue(Q); 9. while( ! Q_isEmpty(Q ) ) /* check if queue is not empty*/ x=dequeue(Q); printf(%d\n,x);

Answer: 1. Q: is empty 2. Q: 5,6,7,8 3. x= 5 4. y=5, Q: 6,7,8 5. Q: 6,7,8, 5 6. Q : 6, 7, 8, 5, 6 7. Q : 7,8,5,6 8. y=7, Q : 8,5, 6 9. x=8, x=5, x=6 , Q : is empty ------------------------------------------------------------4.
1.listQueue ( Q); 2. int x = 2; 3. enqueue(Q,4); 4. enqueue(Q,x); 5. enqueue(peekFront(Q )); 6. enqueue(3+peekFront(Q )); 7. Q.enqueue(Q,0); 8. dequeue(); 9. while(!Q_isEmpty(Q)) { x=dequeue(Q); printf(%d\n,x);

Answer 1. Q : is empty 2. x=2 3. Q: 4 4. Q: 4,2 5. Q: 4,2,4 6. Q: 4,2,4,7 7. Q: 4,2,4,7,0 8. Q: 2,4,4,7 9. x=2, x=4, x=4, x=7, Q: is empty -----------------------------------------------

5.
1. listQueue ( Q); 2. int a=0, b=1; 3. int c=a+b; 4. while (c<10) { 5. if (c%2==0) 6. push(S,c); 7. else enqueue(Q,c); 8. a=b; 9. b=c; 10. c=a+b; } 11. while ( !S_isEmpty(S ) ) /* chek if stack is not empty*/ { c= pop(S); printf(%d\n,c); } 12. while ( !Q_isEmpty(Q ) ) { c=dequeue(Q); printf(%d\n,c); }

Answer 1. Q: is empty 2. a=0, b=1, 3. c=1 4. c<10 5. c%2 ==0 is false 7. Q: 1 8. a=1, 9. b=1, 10. c=2 4. c<10 5 c%2==0 is true 6. S: 2 8. a=1, 9. b=2, 10. c=3 4. c<10 5. c%2 ==0 is false 7. Q: 1, 3 8. a=2, 9. b=3, 10. c=5 4. c<10 5. c%2 ==0 is false 7. Q: 1, 3, 5 8. a=3, 9. b=5, 10. c=8 4. c<10 5 c%2==0 is true 6. S: 8, 2 8. a=5, 9. b=8, 10. c=13 4. c<10 is false 11. c= 8, c=2, S: is empty 12. c=1, c=3, c=5, Q: is empty

Potrebbero piacerti anche