top of page

A stack can be implemented using two queues in C. The basic idea is to use one queue for enqueue operations and another for dequeue operations. When an element is pushed onto the stack, it is enqueued into the enqueue queue. When an element is popped from the stack, it is dequeued from the dequeue queue along with all other elements, except the last one, and enqueued back into the enqueue queue in reverse order. The last element is then dequeued from the dequeue queue and returned.


#include <stdio.h>

#include <stdlib.h>


#define MAX_SIZE 100


// Define the queue data structure

struct queue {

int data[MAX_SIZE];

int front;

int rear;

};


// Initialize a queue

void init_queue(struct queue *q) {

q->front = -1;

q->rear = -1;

}


// Check if the queue is empty

int is_empty(struct queue *q) {

return q->front == -1;

}


// Check if the queue is full

int is_full(struct queue *q) {

return (q->rear + 1) % MAX_SIZE == q->front;

}


// Enqueue an element

void enqueue(struct queue *q, int element) {

if (is_full(q)) {

printf("Error: Queue is full\n");

exit(EXIT_FAILURE);

}

if (is_empty(q)) {

q->front = 0;

}

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

q->data[q->rear] = element;

}


// Dequeue an element

int dequeue(struct queue *q) {

if (is_empty(q)) {

printf("Error: Queue is empty\n");

exit(EXIT_FAILURE);

}

int element = q->data[q->front];

if (q->front == q->rear) {

q->front = -1;

q->rear = -1;

} else {

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

}

return element;

}


// Define the stack data structure

struct stack {

struct queue enqueue_queue;

struct queue dequeue_queue;

};


// Initialize a stack

void init_stack(struct stack *s) {

init_queue(&s->enqueue_queue);

init_queue(&s->dequeue_queue);

}


// Push an element onto the stack

void push(struct stack *s, int element) {

enqueue(&s->enqueue_queue, element);

}


// Pop an element from the stack

int pop(struct stack *s) {

if (is_empty(&s->enqueue_queue)) {

printf("Error: Stack is empty\n");

exit(EXIT_FAILURE);

}

while (!is_empty(&s->enqueue_queue) && s->enqueue_queue.front != s->enqueue_queue.rear) {

int element = dequeue(&s->enqueue_queue);

enqueue(&s->dequeue_queue, element);

}

int element = dequeue(&s->enqueue_queue);

struct queue temp = s->enqueue_queue;

s->enqueue_queue = s->dequeue_queue;

s->dequeue_queue = temp;

return element;

}


// Main function to test the stack

int main() {

struct stack s;

init_stack(&s);

push(&s, 1);

push(&s, 2);

push(&s, 3);

printf("%d\n", pop(&s));

printf("%d\n", pop(&s));

printf("%d\n", pop(&s));

return 0;

}


In this implementation, the queue struct represents a queue and the stack struct represents a stack. The push_queue is used to enqueue elements onto the stack, and the pop_queue is used to dequeue elements from the stack. When an element is popped, the implementation checks if the pop_queue is empty. If it is, it dequeues all the elements except the last one from the push_queue and enqueues them onto the pop_queue. The last element is then dequeued from the push_queue and returned as the popped element. The implementation uses the enqueue() and dequeue() functions to enqueue and dequeue elements onto and from the queues.


5 views0 comments

A queue can be implemented using two stacks in C. The basic idea is to use one stack for enqueue operations and another for dequeue operations. When an element is enqueued, it is pushed onto the enqueue stack. When an element is dequeued, it is popped from the dequeue stack. If the dequeue stack is empty, all the elements from the enqueue stack are popped and pushed onto the dequeue stack, effectively reversing the order of the elements.


#include <stdio.h>

#include <stdlib.h>


#define MAX_SIZE 100


// Define the stack data structure

struct stack {

int data[MAX_SIZE];

int top;

};


// Initialize a stack

void init stack(struct stack *s) {

s->top = -1;

}


// Check if the stack is empty

int is empty(struct stack *s) {

return s->top == -1;

}


// Check if the stack is full

int is full(struct stack *s) {

return s->top == MAX_SIZE - 1;

}


// Push an element onto the stack

void push(struct stack *s, int element) {

if (is full(s)) {

printf("Error: Stack is full\n");

exit(EXIT_FAILURE);

}

s->top++;

s->data[s->top] = element;

}


// Pop an element from the stack

int pop(struct stack *s) {

if (is empty(s)) {

printf("Error: Stack is empty\n");

exit(EXIT_FAILURE);

}

int element = s->data[s->top];

s->top--;

return element;

}


// Define the queue data structure

struct queue {

struct stack enqueue stack;

struct stack dequeue stack;

};


// Initialize a queue

void init queue(struct queue *q) {

init stack(&q->enqueue stack);

init stack(&q->dequeue stack);

}


// Enqueue an element

void enqueue(struct queue *q, int element) {

push(&q->enqueue stack, element);

}


// Dequeue an element

int dequeue(struct queue *q) {

if (is empty(&q->dequeue stack)) {

while (!is empty(&q->enqueue stack)) {

int element = pop(&q->enqueue stack);

push(&q->dequeue stack, element);

}

}

return pop(&q->dequeue stack);

}


// Main function to test the queue

int main() {

struct queue q;

init queue(&q);

enqueue(&q, 1);

enqueue(&q, 2);

enqueue(&q, 3);

printf("%d\n", dequeue(&q));

printf("%d\n", dequeue(&q));

printf("%d\n", dequeue(&q));

return 0;

}


In this implementation, the stack struct represents a stack and the queue struct represents a queue. The enqueue_stack is used to push elements onto the queue, and the dequeue_stack is used to pop elements from the queue. When an element is dequeued, the implementation checks if the dequeue_stack is empty. If it is, it pops all the elements from the enqueue_stack and pushes them onto the dequeue_stack in reverse order, effectively reversing the order of the elements. The implementation uses the push() and pop() functions to push and pop elements onto and from the stacks.


3 views0 comments

#include <stdio.h>

#include<stdlib.h>

#include<math.h>


#define size 5

int queue[size];

int front = -1;

int rear = -1;


void enque(void);

void deque(void);

void peek(void);

void isEmpty(void);

void isFull(void);

void display(void);


void main() {

int c,e;

do{

switch(c)

{

case 1:

enque();

break;

case 2:

deque();

break;

case 3:

peek();

break;

case 4:

isEmpty();

break;

case 5:

isFull();

break;

case 6:

display();

break;

case 7:

printf("\n\n----Exit from program successfully----");

printf("\n--------------Thank you--------------");

exit(0);

}

printf("\n1:Enque\t 2:Deque\t 3:Peek\t 4:isEmpty\t 5:isFull\t 6:Display\t 7:Exit");

printf("\nEnter your choice : ");

scanf("%d",&c);

}while(c);

}


void enque()

{

int v;

printf("\nEnter value : ");

scanf("%d",&v);

if(rear==size-1)

{

printf("\nQueue is full!!");

}

else if(front=-1 && rear==-1)

{

front=rear=0;

queue[rear]=v;

}

else

{

rear++;

queue[rear]=v;

}

}


void display()

{

int i=0;

if(front==-1 && rear==-1)

{

printf("\nQueue is empty");

}

else

{

for(i=front;i<rear+1;i++)

{

printf("--%d",queue[i]);

}

}

}


void deque()

{

if(front==-1 && rear==-1)

{

printf("\nQueue is empty!!");

}

else if(front==rear)

{

front=rear=-1;

}

else

{

int i;

front++;

}

}


void peek()

{

printf("\nTop : %d",rear);

printf("\nTop Value: %d",queue[rear]);

}


void isEmpty()

{

if(front==-1 && rear==-1)

{

printf("\nQueue is empty!!");

}

else

{

printf("\nQueue is not empty!!");

}

}


void isFull()

{

if(rear==size-1)

{

printf("\nQueue is full!!");

}

else

{

printf("\nQueue is not full!!");

}

}

3 views0 comments
bottom of page