top of page
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.
- C Programming
- •
- Basics
- •
- Stack
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