0% found this document useful (0 votes)
10 views5 pages

Practical 8

Uploaded by

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

Practical 8

Uploaded by

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

Practical 8

Write a program to implement a stack using two stack such that the enqueue operations runs in constant
time and dequeue operations runs in linear time.

Input:

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#define QUEUE_EMPTY_MAGIC 0xdeadbeef

typedef struct queue_t {

int *arr;

int rear, front, count, max;

} queue_t;

// Function prototypes

queue_t *queue_allocate(int n);

void queue_insert(queue_t *q, int v);

int queue_remove(queue_t *q);

int queue_count(queue_t *q);

int queue_is_empty(queue_t *q);

void queue_display(queue_t *q);

void stack_push(queue_t *q, int v);

int stack_pop(queue_t *q);

int stack_is_empty(queue_t *q);

int stack_count(queue_t *q);

#define MAX 128

int main(void) {

queue_t *q1;
int x, select;

// Static allocation

q1 = queue_allocate(MAX);

do {

printf("\n[1] Push\n[2] Pop\n[0] Exit");

printf("\nChoice: ");

scanf("%d", &select);

switch (select) {

case 1:

printf("\nEnter value to Push: ");

scanf("%d", &x);

stack_push(q1, x);

printf("\n\nIn Current Queue:\n");

queue_display(q1);

printf("\n\nPushed Value: %d\n", x);

break;

case 2:

x = stack_pop(q1);

printf("\n\nIn Current Queue:\n");

queue_display(q1);

if (x == QUEUE_EMPTY_MAGIC)

printf("\nNo values removed\n");

else

printf("\n\nPopped Value: %d\n", x);

break;

case 0:

printf("\nQuitting.\n");

return 0;

default:

printf("\nQuitting.\n");

return 0;
}

} while (1);

return 0;

// Function definitions for the missing functions

queue_t *queue_allocate(int n) {

queue_t *q = (queue_t *)malloc(sizeof(queue_t));

q->arr = (int *)malloc(n * sizeof(int));

q->rear = -1;

q->front = 0;

q->count = 0;

q->max = n;

return q;

void queue_insert(queue_t *q, int v) {

if (q->count < q->max) {

q->rear = (q->rear + 1) % q->max;

q->arr[q->rear] = v;

q->count++;

int queue_remove(queue_t *q) {

if (q->count > 0) {

int removed_value = q->arr[q->front];

q->front = (q->front + 1) % q->max;

q->count--;

return removed_value;

} else {

return QUEUE_EMPTY_MAGIC;

}
}

int queue_count(queue_t *q) {

return q->count;

int queue_is_empty(queue_t *q) {

return q->count == 0;

void queue_display(queue_t *q) {

if (queue_is_empty(q)) {

printf("Queue is empty\n");

} else {

printf("Front -> ");

int i;

for (i = 0; i < q->count; i++) {

int index = (q->front + i) % q->max;

printf("%d ", q->arr[index]);

printf(" <- Rear\n");

void stack_push(queue_t *q, int v) {

queue_insert(q, v);

int stack_pop(queue_t *q) {

return queue_remove(q);

int stack_is_empty(queue_t *q) {

return queue_is_empty(q);
}

int stack_count(queue_t *q) {

return queue_count(q);

Output:

You might also like