ADts Worksheet
ADts Worksheet
UNIT TWO
1. For each of the following scenarios, identify which data structure would be MOST appropriate
when writing software.
For a call centre; ensure that calls are answered in first- in, first – out order
Ans: Queue
To allow the undo button to work correctly in a word processing application (for example, MS
Word)
Ans: Stack
To store an unknown number of data items and subsequently search this data structure for
different keys
Ans: Linked List
3 marks
1. Push - If the stack is not complete, this action adds a new element to the stack.
2. Pop - This operation removes from the stack the last element entered if the stack is not
empty.
3. Top - This returns the stack's top element.
6 marks
3.Queue is an empty queue of size 5. In the space provided below, draw diagrams to show the
contents of the queue after each of the following lines is executed. Lines 3, 4 and 5. Show all
elements in the queue for each illustration.
4.In the space provided below, write C code which will reverse the order of a set of items in a
queue. For example, if initially the queue contains the elements x1, x2, x3 …xN (with x1 at the
front of the queue)
then your code should reverse this order to xN… x3, x2, x1 (where xN is at the front of the
queue).
Ans:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int f,i=0,d;
printf("Enter size of the Queue");
scanf("%d",&f);
printf("\nEnter data for the Queue");
while(i<f)
{
scanf("%d",&d);
enqueue(d,f);
i++;
}
return 0;
}
{
if(back==l-1)
{
printf("Queue is full");
}
else if((front==-1)&&(back==-1))
{
front = back= 0;
q[back] = data;
}
else
{
back++;
q[back] = data;
}
}
void show()
{
int i;
for(i=front;i<=back;i++)
{
printf("\n%d",q[i]);
}
}
void reverse()
{
int i,k,d;
for(i=front,k=back;i<k;i++,k--)
{
d = q[i];
q[i] = q[k];
q[k] = d;
}
}
10 marks
5.A certain singly linked list is loaded with five integers. The head of the list is accessed via top.
3 marks
Explain how any node between the first and last node can be deleted.
Ans: We need to provide a pointer to the node next to the node to be removed in order to
remove the middle node first. To do this, create a 1-time loop location and get a pointer to the
previous node.
4 marks