Stack From Queue Infix Postfix Prefix
Stack From Queue Infix Postfix Prefix
struct Queue { }
if (q->size == MAX_SIZE) {
struct Stack { }
struct Queue q; }
};
exit(EXIT_FAILURE);
q->size--;
push(&stack, 1); }
push(&stack, 2); }
push(&stack, 3);
// Function to check if the queue is empty #define MAX_SIZE 100
} struct Stack {
int top;
// 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;
exit(EXIT_FAILURE);
int main() {
}
struct Queue queue;
queue.stack1.top = -1;
return dequeue(&(s->q));
queue.stack2.top = -1;
}
enqueue(&queue, 1);
// queue from stack
enqueue(&queue, 2);
#include <stdio.h>
enqueue(&queue, 3);
#include <stdlib.h>
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)));
}
}
}
}
printf("Stack underflow\n");
exit(EXIT_FAILURE);
} else {
return s->items[(s->top)--];
}
// C code to convert infix to postfix expression
// to postfix expression
#include <stdlib.h> {
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);
// 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;
}
}
int i, j = 0;
// Function call
for (i = 0; infix[i] != '\0'; i++) { int main() {
infix[i] = ')';
if (isalnum(infix[i])) {
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
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);
}
}
// 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;
operand2[op1_len] = infix[op2_len];
#include <stdio.h>
op1_len++;
#include <stdlib.h>
op2_len++;
#include <string.h>
}
op1_len++;
op1_len++;
// Function to convert postfix expression to prefix
op2_len++;
expression
}
void postfixToPrefix(char postfix[], char 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; }
#include <stdio.h> }
#include <stdlib.h>
operand2[op2_len] = infix[op2_len];
}
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';
int main() {
int op1_len = strlen(infix);
char prefix[MAX_SIZE], infix[MAX_SIZE];
int op2_len = 0;
char result[MAX_SIZE];
printf("Enter the prefix expression: "); for (int i = len - 1; i >= 0; 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
//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
}
while (postfix[op1_len] != '\0') {
operand1[op2_len] = postfix[op1_len];
// Function to convert prefix expression to postfix
expression op1_len++;
operand1[op2_len] = '\0';
}
// Function prototypes
#include <stdlib.h>
return 0;
#include <string.h>
}
// Function to push an item onto the stack }
}
int i, j = 0;
}
return 1; }
return 2;
} push(&stack2, 'A');
push(&stack2, 'B');
postfix[j] = '\0';
if (isEqual(&stack1, &stack2)) {
return 0;
struct Stack {
}; if (s->top == MAX_SIZE - 1) {
printf("Stack overflow\n");
printf("Stack underflow\n");
push(&stack1, 'C'); }
}
char pop(struct Stack *s);
int isEmpty(struct Stack *s) { int areStacksEqual(struct Stack *s1, struct Stack *s2);
} int main() {
} push(&stack1, 'B');
push(&stack1, 'C');
} push(&stack2, 'B');
} push(&stack2, 'C');
} if (areStacksEqual(&stack1, &stack2)) {
return 0;
struct Stack {
}; if (s->top == MAX_SIZE - 1) {
printf("Stack overflow\n");
} #include <string.h>
} else { };
return s->items[(s->top)--];
} // Function prototypes
int isEmpty(struct Stack *s) { int areQueuesEqual(struct Queue *q1, struct Queue
*q2);
return (s->top == -1);
}
int main() {
enqueue(&queue1, 'B');
for (int i = 0; i <= s1->top; i++) {
enqueue(&queue1, 'C');
if (s1->items[i] != s2->items[i]) {
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
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
return value;
// Structure to represent a queue
}
struct Queue {
}
int front, rear, size;
char items[MAX_SIZE];
q->size++;
int main() {
}
struct Queue queue1, queue2;
}
queue1.front = queue1.rear = queue1.size = 0;
}
// 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");
}
}
if (q1->items[index1] != q2->items[index2]) {