0% found this document useful (0 votes)
17 views

Dsa Assignment 1

The document compares and contrasts stacks, queues, and deques which are abstract data types (ADTs) that store data elements. Stacks follow LIFO, queues follow FIFO, and deques allow insertion and deletion from both ends. Key operations for each are described along with diagrams and C code implementing queue operations.

Uploaded by

Dilan Sandeepa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Dsa Assignment 1

The document compares and contrasts stacks, queues, and deques which are abstract data types (ADTs) that store data elements. Stacks follow LIFO, queues follow FIFO, and deques allow insertion and deletion from both ends. Key operations for each are described along with diagrams and C code implementing queue operations.

Uploaded by

Dilan Sandeepa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

i) Comparison of Stacks, Queues, and Deques:

- Construction: All three ADTs have been implemented contiguously with the same number of
elements, n = 6. They store data elements of type integer. Each ADT has its own particular
pointers for operation.
- Operation:
1. Stacks: Follow the Last-In-First-Out (LIFO) principle. Elements can be added or removed only
from one end, known as the top of the stack.
2. Queues: Follow the First-In-First-Out (FIFO) principle. Elements are appended at one end
called the rear, and they are served or removed from the other end called the front.
3. Deques: Allow insertion and deletion of elements from both ends. Elements can be
appended or served from either the front or rear.

ii) Key Operations of the three ADTs:


1. Stacks:
- Push(S, item): Adds an item to the top of the stack.
- Pop(S): Removes and returns the item from the top of the stack.
- Peek(S): Returns the item at the top of the stack without removing it.

2. Queues:
- Append(Q, item): Adds an item to the rear of the queue.
- Serve(Q): Removes and returns the item from the front of the queue.
- Peek(Q): Returns the item at the front of the queue without removing it.

3. Deques:
- AppendFront(DQ, item): Adds an item to the front of the deque.
- AppendRear(DQ, item): Adds an item to the rear of the deque.
- ServeFront(DQ): Removes and returns the item from the front of the deque.

iii) Diagrams illustrating the ADTs after each operation:


Stack (S):

Queue (Q):

Deque (DQ):
iv) C language code to implement the Queue (Q) operations:

```c
#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 6

typedef struct {
int items[MAX_SIZE];
int front;
int rear;
int size;
} Queue;
void create(Queue* q) {
q->front = 0;
q->rear = -1;
q->size = 0;
}

void append(Queue* q, int item) {


if (q->size >= MAX_SIZE) {
printf("Queue is full. Unable to append.\n");
return;
}
q->rear = (q->rear + 1) % MAX_SIZE;
q->items[q->rear] = item;
q->size++;
}

int serve(Queue* q) {
if (q->size == 0) {
printf("Queue is empty. Unable to serve.\n");
return -1;
}
int item = q->items[q->front];
q->front = (q->front + 1) % MAX_SIZE;
q->size--;
return item;
}
int peek(Queue* q) {
if (q->size == 0) {
printf("Queue is empty. Unable to peek.\n");
return -1;
}
return q->items[q->front];
}

int main() {
Queue q;
create(&q);

append(&q, 50);
append(&q, 100);
append(&q, 125);

printf("Front of the queue: %d\n", peek(&q));

int servedItem = serve(&q);


printf("Served item: %d\n", servedItem);

append(&q, 15);

printf("Front of the queue after serving: %d\n", peek(&q));

return 0;
}

You might also like