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

DS Programs

The document contains implementations of Stack and Queue Abstract Data Types (ADTs) in C. It includes functions for initialization, checking if they are empty or full, pushing/enqueuing, popping/dequeuing, and peeking at the top/front elements. Sample outputs demonstrate the functionality of both data structures.

Uploaded by

Mahaboob Saniya
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)
2 views4 pages

DS Programs

The document contains implementations of Stack and Queue Abstract Data Types (ADTs) in C. It includes functions for initialization, checking if they are empty or full, pushing/enqueuing, popping/dequeuing, and peeking at the top/front elements. Sample outputs demonstrate the functionality of both data structures.

Uploaded by

Mahaboob Saniya
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

STACK ADT PROGRAM

#include <stdio.h>
#include <stdbool.h>

#define MAX 5
struct Stack {
int data[MAX];
int top;
};
void initStack(struct Stack* s);
bool isEmpty(struct Stack* s);
bool isFull(struct Stack* s);
void push(struct Stack* s, int value);
int pop(struct Stack* s);
int peek(struct Stack* s);

int main() {
struct Stack stack;
initStack(&stack);
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
printf("Top element: %d\n", peek(&stack));

printf("Popped element: %d\n", pop(&stack));


printf("Popped element: %d\n", pop(&stack));

if (isEmpty(&stack)) {
printf("Stack is empty now.\n");
} else {
printf("Stack is not empty.\n");
}
return 0;
}
void initStack(struct Stack* s) {
s->top = -1;
}
bool isEmpty(struct Stack* s) {
return s->top == -1;
}
bool isFull(struct Stack* s) {
return s->top == MAX - 1;
}
void push(struct Stack* s, int value) {
if (isFull(s)) {
printf("Stack overflow! Cannot push %d\n", value);
return;
}
s->data[++(s->top)] = value;
printf("Pushed %d onto the stack.\n", value);
}
int pop(struct Stack* s) {
if (isEmpty(s)) {
printf("Stack underflow! Cannot pop an element.\n");
return -1;
}
return s->data[(s->top)--];
}
int peek(struct Stack* s) {
if (isEmpty(s)) {
printf("Stack is empty! Cannot peek.\n");
return -1;
}
return s->data[s->top];
}

OUTPUT:
Pushed 10 onto the stack.
Pushed 20 onto the stack.
Pushed 30 onto the stack.
Top element: 30
Popped element: 30
Popped element: 20
Stack is not empty.

QUEUE ADT PROGRAM


#include <stdio.h>
#include <stdbool.h>

#define MAX 5
struct Queue {
int data[MAX];
int front;
int rear;
};
void initQueue(struct Queue* q);
bool isEmpty(struct Queue* q);
bool isFull(struct Queue* q);
void enqueue(struct Queue* q, int value);
int dequeue(struct Queue* q);
int peek(struct Queue* q);

int main() {
struct Queue queue;
initQueue(&queue);
enqueue(&queue, 10);
enqueue(&queue, 20);
enqueue(&queue, 30);

printf("Front element: %d\n", peek(&queue));


printf("Dequeued element: %d\n", dequeue(&queue));
printf("Dequeued element: %d\n", dequeue(&queue));

if (isEmpty(&queue)) {
printf("Queue is empty now.\n");
} else {
printf("Queue is not empty.\n");
}
return 0;
}
void initQueue(struct Queue* q) {
q->front = -1;
q->rear = -1;
}
bool isEmpty(struct Queue* q) {
return q->front == -1;
}
bool isFull(struct Queue* q) {
return (q->rear + 1) % MAX == q->front;
}
void enqueue(struct Queue* q, int value) {
if (isFull(q)) {
printf("Queue overflow! Cannot enqueue %d\n", value);
return;
}
if (isEmpty(q)) {
q->front = 0;
}
q->rear = (q->rear + 1) % MAX;
q->data[q->rear] = value;
printf("Enqueued %d into the queue.\n", value);
}
int dequeue(struct Queue* q) {
if (isEmpty(q)) {
printf("Queue underflow! Cannot dequeue an element.\n");
return -1;
}
int value = q->data[q->front];
if (q->front == q->rear) {
q->front = -1;
q->rear = -1;
} else {
q->front = (q->front + 1) % MAX;
}
return value;
}
int peek(struct Queue* q) {
if (isEmpty(q)) {
printf("Queue is empty! Cannot peek.\n");
return -1;
}
return q->data[q->front];
}
OUTPUT:
Enqueued 10 into the queue.
Enqueued 20 into the queue.
Enqueued 30 into the queue.
Front element: 10
Dequeued element: 10
Dequeued element: 20
Queue is not empty.

You might also like