Dsa Assignment 1
Dsa Assignment 1
- 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.
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.
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;
}
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);
append(&q, 15);
return 0;
}