0% found this document useful (0 votes)
37 views

Stack From Queue Infix Postfix Prefix

The document describes how to implement a stack using a single queue. It defines a Stack structure containing a Queue. It provides functions to push and pop elements onto/from the stack by enqueueing and dequeuing from the underlying queue. It also rotates elements in the queue after pushing so the newest element is at the front. This allows simulating LIFO behavior of a stack using the FIFO queue.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Stack From Queue Infix Postfix Prefix

The document describes how to implement a stack using a single queue. It defines a Stack structure containing a Queue. It provides functions to push and pop elements onto/from the stack by enqueueing and dequeuing from the underlying queue. It also rotates elements in the queue after pushing so the newest element is at the front. This allows simulating LIFO behavior of a stack using the FIFO queue.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

//stack from queue

#include <stdio.h> // Example: Pop elements from the stack

#include <stdlib.h> printf("Popped element: %d\n", pop(&stack));

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

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

// Structure to represent a queue return 0;

struct Queue { }

int front, rear, size;

int items[MAX_SIZE]; // Function to enqueue an item into the queue

}; void enqueue(struct Queue *q, int value) {

if (q->size == MAX_SIZE) {

// Function prototypes for queue operations printf("Queue overflow\n");

void enqueue(struct Queue *q, int value); exit(EXIT_FAILURE);

int dequeue(struct Queue *q); } else {

int isEmpty(struct Queue *q); q->items[q->rear] = value;

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

// Structure to represent a stack using a single queue q->size++;

struct Stack { }

struct Queue q; }

};

// Function to dequeue an item from the queue

// Function prototypes for stack operations int dequeue(struct Queue *q) {

void push(struct Stack *s, int value); if (isEmpty(q)) {

int pop(struct Stack *s); printf("Queue underflow\n");

exit(EXIT_FAILURE);

int main() { } else {

struct Stack stack; int value = q->items[q->front];

stack.q.front = stack.q.rear = stack.q.size = 0; q->front = (q->front + 1) % MAX_SIZE;

q->size--;

// Example: Push elements onto the stack return value;

push(&stack, 1); }

push(&stack, 2); }

push(&stack, 3);
// Function to check if the queue is empty #define MAX_SIZE 100

int isEmpty(struct Queue *q) {

return (q->size == 0); // Structure to represent a stack

} struct Stack {

int top;

// Function to push an item onto the stack int items[MAX_SIZE];

void push(struct Stack *s, int value) { };

// Enqueue the new element

enqueue(&(s->q), value); // Function prototypes for stack operations

void push(struct Stack *s, int value);

// Rotate the elements in the queue so that the int pop(struct Stack *s);
newly added element is at the front
int isEmpty(struct Stack *s);
int size = s->q.size;

for (int i = 0; i < size - 1; i++) {


// Structure to represent a queue using two stacks
int frontElement = dequeue(&(s->q));
struct Queue {
enqueue(&(s->q), frontElement);
struct Stack stack1;
}
struct Stack stack2;
}
};

// Function to pop an item from the stack


// Function prototypes for queue operations
int pop(struct Stack *s) {
void enqueue(struct Queue *q, int value);
if (isEmpty(&(s->q))) {
int dequeue(struct Queue *q);
printf("Stack underflow\n");

exit(EXIT_FAILURE);
int main() {
}
struct Queue queue;

queue.stack1.top = -1;
return dequeue(&(s->q));
queue.stack2.top = -1;
}

// Example: Enqueue elements into the queue

enqueue(&queue, 1);
// queue from stack
enqueue(&queue, 2);
#include <stdio.h>
enqueue(&queue, 3);
#include <stdlib.h>

// Example: Dequeue elements from the queue


printf("Dequeued element: %d\n", // Function to enqueue an item into the queue
dequeue(&queue));
void enqueue(struct Queue *q, int value) {
printf("Dequeued element: %d\n",
push(&(q->stack1), value);
dequeue(&queue));
}
printf("Dequeued element: %d\n",
dequeue(&queue));

// Function to dequeue an item from the queue


return 0; int dequeue(struct Queue *q) {
} if (isEmpty(&(q->stack1)) && isEmpty(&(q->stack2)))
{

printf("Queue underflow\n");
// Function to push an item onto the stack
exit(EXIT_FAILURE);
void push(struct Stack *s, int value) {
}
if (s->top == MAX_SIZE - 1) {

printf("Stack overflow\n");
if (isEmpty(&(q->stack2))) {
exit(EXIT_FAILURE);
// Transfer elements from stack1 to stack2
} else {
while (!isEmpty(&(q->stack1))) {
s->items[++(s->top)] = value;
push(&(q->stack2), pop(&(q->stack1)));
}
}
}
}

// Function to pop an item from the stack


return pop(&(q->stack2));
int pop(struct Stack *s) {
}
if (isEmpty(s)) {

printf("Stack underflow\n");

exit(EXIT_FAILURE);

} else {

return s->items[(s->top)--];

// Function to check if the stack is empty

int isEmpty(struct Stack *s) {

return (s->top == -1);

}
// C code to convert infix to postfix expression
// to postfix expression

#include <stdio.h> char* infixToPostfix(char* infix)

#include <stdlib.h> {

#include <string.h> int i, j;

int len = strlen(infix);

#define MAX_EXPR_SIZE 100 char* postfix = (char*)malloc(sizeof(char) *


(len + 2));

char stack[MAX_EXPR_SIZE];
// Function to return precedence of operators
int top = -1;
int precedence(char operator)

{
for (i = 0, j = 0; i < len; i++) {
switch (operator) {
if (infix[i] == ' ' || infix[i] == '\t')
case '+':
continue;
case '-':

return 1;
// If the scanned character is operand
case '*':
// add it to the postfix expression
case '/':
if (isalnum(infix[i])) {
return 2;
postfix[j++] = infix[i];
case '^':
}
return 3;

default:
// if the scanned character is '('
return -1;
// push it in the stack
}
else if (infix[i] == '(') {
}
stack[++top] = infix[i];

}
// Function to check if the scanned character

// is an operator
// if the scanned character is ')'
int isOperator(char ch)
// pop the stack and add it to the
{
// output string until empty or '('
return (ch == '+' || ch == '-' || ch == '*' || ch
found
== '/'
else if (infix[i] == ')') {
|| ch == '^');
while (top > -1 && stack[top] !
}
= '(')

postfix[j++] =
// Main functio to convert infix expression stack[top--];
top--; char* postfix = infixToPostfix(infix);

} printf("%s\n", postfix);

free(postfix);

// If the scanned character is an return 0;


operator
}
// push it in the stack
//infix to prefix
else if (isOperator(infix[i])) {
#include <stdio.h>
while (top > -1
#include <string.h>
&&
precedence(stack[top])
#define MAX_SIZE 100
>=
precedence(infix[i]))

postfix[j++] = // Function to check if a symbol is an operator


stack[top--];
int isOperator(char symbol) {
stack[++top] = infix[i];
return (symbol == '+' || symbol == '-' || symbol ==
} '*' || symbol == '/');
} }

// Pop all remaining elements from the stack // Function to determine the precedence of an
operator
while (top > -1) {
int precedence(char operator) {
if (stack[top] == '(') {
if (operator == '+' || operator == '-') {
return "Invalid Expression";
return 1;
}
} else if (operator == '*' || operator == '/') {
postfix[j++] = stack[top--];
return 2;
}
}
postfix[j] = '\0';
return 0;
return postfix;
}
}

// Function to convert infix expression to prefix


// Driver code
expression
int main()
void infixToPrefix(char infix[], char prefix[]) {
{
// Reverse the infix expression
char infix[MAX_EXPR_SIZE] = "a+b*(c^d-
strrev(infix);
e)^(f+g*h)-i";

int i, j = 0;
// Function call
for (i = 0; infix[i] != '\0'; i++) { int main() {

if (infix[i] == '(') { char infix[MAX_SIZE], prefix[MAX_SIZE];

infix[i] = ')';

} else if (infix[i] == ')') { // Input the infix expression

infix[i] = '('; printf("Enter the infix expression: ");

} fgets(infix, MAX_SIZE, stdin);

// Remove the newline character from the input

for (i = 0; infix[i] != '\0'; i++) { infix[strcspn(infix, "\n")] = '\0';

if (isalnum(infix[i])) {

prefix[j++] = infix[i]; // Convert infix to prefix

} else if (isOperator(infix[i])) { infixToPrefix(infix, prefix);

while (j > 0 && precedence(prefix[j - 1]) >=


precedence(infix[i])) {
// Display the prefix expression
prefix[j++] = ' ';
printf("Prefix expression: %s\n", prefix);
}

prefix[j++] = infix[i];
return 0;
} else if (infix[i] == '(') {
}
while (j > 0 && prefix[j - 1] != ')') {
//postfix to infix
prefix[j++] = ' ';
#include <stdio.h>
}
#include <stdlib.h>
prefix[j++] = infix[i];
#include <string.h>
} else if (infix[i] == ')') {

prefix[j++] = infix[i];
#define MAX_SIZE 100
}

}
// Function to check if a symbol is an operator

int isOperator(char symbol) {


// Null-terminate the prefix expression
return (symbol == '+' || symbol == '-' || symbol ==
prefix[j] = '\0'; '*' || symbol == '/');

// Reverse the prefix expression to get the final


result
// Function to convert postfix expression to infix
strrev(prefix); expression

} void postfixToInfix(char postfix[], char infix[]) {


int len = strlen(postfix);

char result[MAX_SIZE]; operand2[op1_len] = '\0';

for (int i = 0; i < len; i++) { sprintf(result, "(%s%c%s)", operand1, postfix[i],


operand2);
if (isalnum(postfix[i])) {
strcpy(infix, result);
sprintf(result, "%c", postfix[i]);
}
strcat(infix, result);
}
} else if (isOperator(postfix[i])) {
}
char operand2[MAX_SIZE],
operand1[MAX_SIZE];

int op2_len = strlen(infix); int main() {

int op1_len = 0; char postfix[MAX_SIZE], infix[MAX_SIZE];

while (infix[op2_len - 1] != ' ' && infix[op2_len - // Input the postfix expression
1] != '\0') {
printf("Enter the postfix expression: ");
op2_len--;
fgets(postfix, MAX_SIZE, stdin);
}

// Remove the newline character from the input


if (infix[op2_len - 1] == ' ') {
postfix[strcspn(postfix, "\n")] = '\0';
op2_len--;

}
// Convert postfix to infix

postfixToInfix(postfix, infix);
while (op1_len < op2_len) {

operand1[op1_len] = infix[op1_len];
// Display the infix expression
op1_len++;
printf("Infix expression: %s\n", infix);
}

return 0;
operand1[op1_len] = '\0';
}
op1_len = 0;

// post fix to prefix


while (infix[op2_len] != '\0') {

operand2[op1_len] = infix[op2_len];
#include <stdio.h>
op1_len++;
#include <stdlib.h>
op2_len++;
#include <string.h>
}
op1_len++;

#define MAX_SIZE 100 }

// Function to check if a symbol is an operator operand1[op1_len] = '\0';

int isOperator(char symbol) { op1_len = 0;

return (symbol == '+' || symbol == '-' || symbol ==


'*' || symbol == '/');
while (prefix[op2_len] != '\0') {
}
operand2[op1_len] = prefix[op2_len];

op1_len++;
// Function to convert postfix expression to prefix
op2_len++;
expression
}
void postfixToPrefix(char postfix[], char prefix[]) {

int len = strlen(postfix);


operand2[op1_len] = '\0';
char result[MAX_SIZE];

sprintf(result, "%c%s%s", postfix[i], operand1,


for (int i = 0; i < len; i++) {
operand2);
if (isalnum(postfix[i])) {
strcpy(prefix, result);
sprintf(result, "%c", postfix[i]);
}
strcat(prefix, result);
}
} else if (isOperator(postfix[i])) {
}
char operand2[MAX_SIZE],
operand1[MAX_SIZE];
int main() {
int op2_len = strlen(prefix);
char postfix[MAX_SIZE], prefix[MAX_SIZE];
int op1_len = 0;

// Input the postfix expression


while (prefix[op2_len - 1] != ' ' && op2_len > 0)
{ printf("Enter the postfix expression: ");
op2_len--; fgets(postfix, MAX_SIZE, stdin);
}

// Remove the newline character from the input


if (prefix[op2_len - 1] == ' ') { postfix[strcspn(postfix, "\n")] = '\0';
op2_len--;

} // Convert postfix to prefix

postfixToPrefix(postfix, prefix);
while (op1_len < op2_len) {

operand1[op1_len] = prefix[op1_len];
// Display the prefix expression

printf("Prefix expression: %s\n", prefix); while (infix[op1_len - 1] != ' ' && op1_len > 0) {

op1_len--;

return 0; }

if (infix[op1_len - 1] == ' ') {

//Prefix to infix op1_len--;

#include <stdio.h> }

#include <stdlib.h>

#include <string.h> while (op2_len < op1_len) {

operand2[op2_len] = infix[op2_len];

#define MAX_SIZE 100 op2_len++;

// Function to check if a symbol is an operator

int isOperator(char symbol) { operand2[op2_len] = '\0';

return (symbol == '+' || symbol == '-' || symbol == op2_len = 0;


'*' || symbol == '/');

}
while (infix[op1_len] != '\0') {

operand1[op2_len] = infix[op1_len];
// Function to convert prefix expression to infix
op1_len++;
expression
op2_len++;
void prefixToInfix(char prefix[], char infix[]) {
}
int len = strlen(prefix);

char result[MAX_SIZE];
operand1[op2_len] = '\0';

for (int i = len - 1; i >= 0; i--) {


sprintf(result, "(%s%c%s)", operand1, prefix[i],
if (isalnum(prefix[i])) {
operand2);
sprintf(result, "%c", prefix[i]);
strcpy(infix, result);
strcat(infix, result);
}
} else if (isOperator(prefix[i])) {
}
char operand1[MAX_SIZE],
}
operand2[MAX_SIZE];

int main() {
int op1_len = strlen(infix);
char prefix[MAX_SIZE], infix[MAX_SIZE];
int op2_len = 0;
char result[MAX_SIZE];

// Input the prefix expression

printf("Enter the prefix expression: "); for (int i = len - 1; i >= 0; i--) {

fgets(prefix, MAX_SIZE, stdin); if (isalnum(prefix[i])) {

sprintf(result, "%c", prefix[i]);

// Remove the newline character from the input strcat(postfix, result);

prefix[strcspn(prefix, "\n")] = '\0'; } else if (isOperator(prefix[i])) {

char operand1[MAX_SIZE],
operand2[MAX_SIZE];
// Convert prefix to infix

prefixToInfix(prefix, infix);
int op1_len = strlen(postfix);

int op2_len = 0;
// Display the infix expression

printf("Infix expression: %s\n", infix);


while (postfix[op1_len - 1] != ' ' && op1_len > 0)
{
return 0;
op1_len--;
}
}

//prefix t postfix
if (postfix[op1_len - 1] == ' ') {

op1_len--;
#include <stdio.h>
}
#include <stdlib.h>

#include <string.h>
while (op2_len < op1_len) {

operand2[op2_len] = postfix[op2_len];
#define MAX_SIZE 100
op2_len++;

}
// Function to check if a symbol is an operator

int isOperator(char symbol) {


operand2[op2_len] = '\0';
return (symbol == '+' || symbol == '-' || symbol ==
op2_len = 0;
'*' || symbol == '/');

}
while (postfix[op1_len] != '\0') {

operand1[op2_len] = postfix[op1_len];
// Function to convert prefix expression to postfix
expression op1_len++;

void prefixToPostfix(char prefix[], char postfix[]) { op2_len++;

int len = strlen(prefix); }


#define MAX_SIZE 100

operand1[op2_len] = '\0';

// Structure to represent a stack

sprintf(result, "%s%s%c", operand1, operand2, struct Stack {


prefix[i]);
int top;
strcpy(postfix, result);
char items[MAX_SIZE];
}
};
}

}
// Function prototypes

void push(struct Stack *s, char value);


int main() {
char pop(struct Stack *s);
char prefix[MAX_SIZE], postfix[MAX_SIZE];
int isOperator(char symbol);

int precedence(char operator);


// Input the prefix expression
void infixToPostfix(char infix[], char postfix[]);
printf("Enter the prefix expression: ");

fgets(prefix, MAX_SIZE, stdin);


int main() {

char infix[MAX_SIZE], postfix[MAX_SIZE];


// Remove the newline character from the input

prefix[strcspn(prefix, "\n")] = '\0';


// Input the infix expression

printf("Enter the infix expression: ");


// Convert prefix to postfix
fgets(infix, MAX_SIZE, stdin);
prefixToPostfix(prefix, postfix);

// Remove the newline character from the input


// Display the postfix expression
infix[strcspn(infix, "\n")] = '\0';
printf("Postfix expression: %s\n", postfix);

// Convert infix to postfix


return 0;
infixToPostfix(infix, postfix);
}

// Display the postfix expression


// infix to post with stack
printf("Postfix expression: %s\n", postfix);
#include <stdio.h>

#include <stdlib.h>
return 0;
#include <string.h>
}
// Function to push an item onto the stack }

void push(struct Stack *s, char value) {

if (s->top == MAX_SIZE - 1) { // Function to convert infix expression to postfix


expression
printf("Stack overflow\n");
void infixToPostfix(char infix[], char postfix[]) {
exit(EXIT_FAILURE);
struct Stack stack;
} else {
stack.top = -1;
s->items[++(s->top)] = value;

}
int i, j = 0;
}

for (i = 0; infix[i] != '\0'; i++) {


// Function to pop an item from the stack
if (isalnum(infix[i])) {
char pop(struct Stack *s) {
postfix[j++] = infix[i];
if (s->top == -1) {
} else if (infix[i] == '(') {
printf("Stack underflow\n");
push(&stack, infix[i]);
exit(EXIT_FAILURE);
} else if (infix[i] == ')') {
} else {
while (stack.top != -1 && stack.items[stack.top]
return s->items[(s->top)--];
!= '(') {
}
postfix[j++] = pop(&stack);
}
}

if (stack.top != -1 && stack.items[stack.top] ==


// Function to check if a symbol is an operator '(') {

int isOperator(char symbol) { pop(&stack);

return (symbol == '+' || symbol == '-' || symbol == }


'*' || symbol == '/');
} else if (isOperator(infix[i])) {
}
while (stack.top != -1 &&
precedence(stack.items[stack.top]) >=
precedence(infix[i])) {
// Function to determine the precedence of an
operator postfix[j++] = pop(&stack);

int precedence(char operator) { }

if (operator == '+' || operator == '-') { push(&stack, infix[i]);

return 1; }

} else if (operator == '*' || operator == '/') { }

return 2;

} // Pop any remaining operators from the stack

return 0; while (stack.top != -1) {


postfix[j++] = pop(&stack); // Example: Push elements onto stack2

} push(&stack2, 'A');

push(&stack2, 'B');

// Null-terminate the postfix expression push(&stack2, 'C');

postfix[j] = '\0';

} // Compare the two stacks

if (isEqual(&stack1, &stack2)) {

//Compare stack printf("The stacks are equal.\n");

#include <stdio.h> } else {

#include <string.h> printf("The stacks are not equal.\n");

#define MAX_SIZE 100

return 0;

// Structure to represent a stack }

struct Stack {

int top; // Function to push an item onto the stack

char items[MAX_SIZE]; void push(struct Stack *s, char value) {

}; if (s->top == MAX_SIZE - 1) {

printf("Stack overflow\n");

// Function prototypes return;

void push(struct Stack *s, char value); } else {

char pop(struct Stack *s); s->items[++(s->top)] = value;

int isEmpty(struct Stack *s); }

int isEqual(struct Stack *s1, struct Stack *s2); }

int main() { // Function to pop an item from the stack

struct Stack stack1, stack2; char pop(struct Stack *s) {

stack1.top = stack2.top = -1; if (isEmpty(s)) {

printf("Stack underflow\n");

// Example: Push elements onto stack1 return '\0';

push(&stack1, 'A'); } else {

push(&stack1, 'B'); return s->items[(s->top)--];

push(&stack1, 'C'); }

}
char pop(struct Stack *s);

// Function to check if the stack is empty int isEmpty(struct Stack *s);

int isEmpty(struct Stack *s) { int areStacksEqual(struct Stack *s1, struct Stack *s2);

return (s->top == -1);

} int main() {

struct Stack stack1, stack2;

// Function to check if two stacks are equal stack1.top = stack2.top = -1;

int isEqual(struct Stack *s1, struct Stack *s2) {

if (s1->top != s2->top) { // Example: Push elements onto stack1

return 0; // Stacks have different sizes push(&stack1, 'A');

} push(&stack1, 'B');

push(&stack1, 'C');

for (int i = 0; i <= s1->top; i++) {

if (s1->items[i] != s2->items[i]) { // Example: Push elements onto stack2

return 0; // Stacks have different elements push(&stack2, 'A');

} push(&stack2, 'B');

} push(&stack2, 'C');

return 1; // Stacks are equal // Compare the two stacks

} if (areStacksEqual(&stack1, &stack2)) {

//or printf("The stacks are equal.\n");

#include <stdio.h> } else {

#include <string.h> printf("The stacks are not equal.\n");

#define MAX_SIZE 100

return 0;

// Structure to represent a stack }

struct Stack {

int top; // Function to push an item onto the stack

char items[MAX_SIZE]; void push(struct Stack *s, char value) {

}; if (s->top == MAX_SIZE - 1) {

printf("Stack overflow\n");

// Function prototypes return;

void push(struct Stack *s, char value); } else {


s->items[++(s->top)] = value; #include <stdio.h>

} #include <string.h>

#define MAX_SIZE 100

// Function to pop an item from the stack

char pop(struct Stack *s) { // Structure to represent a queue

if (isEmpty(s)) { struct Queue {

printf("Stack underflow\n"); int front, rear, size;

return '\0'; char items[MAX_SIZE];

} else { };

return s->items[(s->top)--];

} // Function prototypes

} void enqueue(struct Queue *q, char value);

char dequeue(struct Queue *q);

// Function to check if the stack is empty int isEmpty(struct Queue *q);

int isEmpty(struct Stack *s) { int areQueuesEqual(struct Queue *q1, struct Queue
*q2);
return (s->top == -1);

}
int main() {

struct Queue queue1, queue2;


// Function to check if two stacks are equal
queue1.front = queue1.rear = queue1.size = 0;
int areStacksEqual(struct Stack *s1, struct Stack *s2) {
queue2.front = queue2.rear = queue2.size = 0;
if (s1->top != s2->top) {

return 0; // Stacks have different sizes


// Example: Enqueue elements into queue1
}
enqueue(&queue1, 'A');

enqueue(&queue1, 'B');
for (int i = 0; i <= s1->top; i++) {
enqueue(&queue1, 'C');
if (s1->items[i] != s2->items[i]) {

return 0; // Stacks have different elements


// Example: Enqueue elements into queue2
}
enqueue(&queue2, 'A');
}
enqueue(&queue2, 'B');

enqueue(&queue2, 'C');
return 1; // Stacks are equal

}
// Compare the two queues
// queue are equal
if (areQueuesEqual(&queue1, &queue2)) { // Function to check if the queue is empty

printf("The queues are equal.\n"); int isEmpty(struct Queue *q) {

} else { return (q->size == 0);

printf("The queues are not equal.\n"); }

// Function to check if two queues are equal

return 0; int areQueuesEqual(struct Queue *q1, struct Queue


*q2) {
}
if (q1->size != q2->size) {

return 0; // Queues have different sizes


// Function to enqueue an item into the queue
}
void enqueue(struct Queue *q, char value) {

if (q->size == MAX_SIZE) {
int index1 = q1->front, index2 = q2->front;
printf("Queue overflow\n");

return;
for (int i = 0; i < q1->size; i++) {
} else {
if (q1->items[index1] != q2->items[index2]) {
q->items[q->rear] = value;
return 0; // Queues have different elements
q->rear = (q->rear + 1) % MAX_SIZE;
}
q->size++;

}
index1 = (index1 + 1) % MAX_SIZE;
}
index2 = (index2 + 1) % MAX_SIZE;

}
// Function to dequeue an item from the queue

char dequeue(struct Queue *q) {


return 1; // Queues are equal
if (isEmpty(q)) {
}
printf("Queue underflow\n");
//or
return '\0';
#include <stdio.h>
} else {
#include <string.h>
char value = q->items[q->front];

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


#define MAX_SIZE 100
q->size--;

return value;
// Structure to represent a queue
}
struct Queue {
}
int front, rear, size;
char items[MAX_SIZE];

}; // Function to enqueue an item into the queue

void enqueue(struct Queue *q, char value) {

// Function prototypes if (q->size == MAX_SIZE) {

void enqueue(struct Queue *q, char value); printf("Queue overflow\n");

char dequeue(struct Queue *q); return;

int isEmpty(struct Queue *q); } else {

int areQueuesEqual(struct Queue *q1, struct Queue q->items[q->rear] = value;


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

q->size++;
int main() {
}
struct Queue queue1, queue2;
}
queue1.front = queue1.rear = queue1.size = 0;

queue2.front = queue2.rear = queue2.size = 0;


// Function to dequeue an item from the queue

char dequeue(struct Queue *q) {


// Example: Enqueue elements into queue1
if (isEmpty(q)) {
enqueue(&queue1, 'A');
printf("Queue underflow\n");
enqueue(&queue1, 'B');
return '\0';
enqueue(&queue1, 'C');
} else {

char value = q->items[q->front];


// Example: Enqueue elements into queue2
q->front = (q->front + 1) % MAX_SIZE;
enqueue(&queue2, 'A');
q->size--;
enqueue(&queue2, 'B');
return value;
enqueue(&queue2, 'C');
}

}
// Compare the two queues

if (areQueuesEqual(&queue1, &queue2)) {
// Function to check if the queue is empty
printf("The queues are equal.\n");
int isEmpty(struct Queue *q) {
} else {
return (q->size == 0);
printf("The queues are not equal.\n");
}
}

// Function to check if two queues are equal


return 0;
int areQueuesEqual(struct Queue *q1, struct Queue
} *q2) {
if (q1->size != q2->size) {

return 0; // Queues have different sizes

int index1 = q1->front, index2 = q2->front;

for (int i = 0; i < q1->size; i++) {

if (q1->items[index1] != q2->items[index2]) {

return 0; // Queues have different elements

index1 = (index1 + 1) % MAX_SIZE;

index2 = (index2 + 1) % MAX_SIZE;

return 1; // Queues are equal

You might also like