0% found this document useful (0 votes)
81 views4 pages

ADts Worksheet

This document contains an abstract data types worksheet that assesses understanding of common data structures. It includes the following: 1) Identifying the most appropriate data structure (queue, stack, linked list) for different scenarios like call centers and undo functions. 2) Describing three common stack operations: push, pop, and top. 3) Drawing diagrams to show the contents of a queue after different enqueue and dequeue operations. 4) Providing C code to reverse the order of items in a queue. 5) Explaining how to delete the first, last, and middle nodes of a singly linked list.

Uploaded by

Fitness Purpose
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views4 pages

ADts Worksheet

This document contains an abstract data types worksheet that assesses understanding of common data structures. It includes the following: 1) Identifying the most appropriate data structure (queue, stack, linked list) for different scenarios like call centers and undo functions. 2) Describing three common stack operations: push, pop, and top. 3) Drawing diagrams to show the contents of a queue after different enqueue and dequeue operations. 4) Providing C code to reverse the order of items in a queue. 5) Explaining how to delete the first, last, and middle nodes of a singly linked list.

Uploaded by

Fitness Purpose
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

CAPE COMPUTER SCIENCE

UNIT TWO

ABSTRACT DATA TYPES WORKSHEET

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

2.Describe THREE operations that can be executed on a stack.

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.

Line 1 Q.enqueue (‘A’);

Line 2 Q.enqueue (‘B’);

Line 2 Q.enqueue (‘C’);

Line 4 Char c = Q.dequeue ();

Line 5 Q.enqueue (‘A’);


3 marks

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>

void enqueue(int, int);


void show();
void reverse();

int front = -1,back = -1;


int q[50];

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

printf("\nQueue you entered:-");


show();
reverse();
printf("\nQueue after reversing:-");
show();

return 0;
}

void enqueue(int data,int l)

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

Explain how the first node can be deleted.


Ans: Shift the head location to point to a temporary variable, then detach the first node from the
second node relation, making the first node the second node.

3 marks

Explain how the last node can be deleted.


Ans: Find the node before the last node, and make that node's pointer null. This deletes the
system's last node.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

You might also like