Program 17
Write a C program to convert an infix expression into postfix.
#include <stdio.h>
#define MAX_SIZE 100
struct Queue {
int items[MAX_SIZE];
int front;
int rear;
};
void enqueue(struct Queue *q, int value);
int dequeue(struct Queue *q);
void display(struct Queue *q);
int main() {
struct Queue q;
q.front = -1;
q.rear = -1;
int choice, value;
do {
printf("\nQueue Implementation using Array\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter the value to enqueue: ");
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
value = dequeue(&q);
if (value != -1)
printf("Dequeued element: %d\n", value);
break;
case 3:
display(&q);
break;
case 4:
printf("Exiting program.\n");
break;
Program 17 Page 1
Program 17
default:
printf("Invalid choice! Please enter a valid
option.\n");
}
} while(choice != 4);
return 0;
}
void enqueue(struct Queue *q, int value) {
if (q->rear == MAX_SIZE - 1)
printf("Queue is full. Cannot enqueue.\n");
else {
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
printf("Enqueued: %d\n", value);
}
}
int dequeue(struct Queue *q) {
int value;
if (q->front == -1 || q->front > q->rear) {
printf("Queue is empty. Cannot dequeue.\n");
return -1;
} else {
value = q->items[q->front];
q->front++;
return value;
}
}
void display(struct Queue *q) {
if (q->front == -1)
printf("Queue is empty.\n");
else {
printf("Queue elements: ");
for (int i = q->front; i <= q->rear; i++)
printf("%d ", q->items[i]);
printf("\n");
}
}
Program 17 Page 2
Program 17
Output
Queue Implementation using Array
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the value to enqueue: 5
Enqueued: 5
Queue Implementation using Array
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
Dequeued element: 5
Queue Implementation using Array
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Queue elements:
Queue Implementation using Array
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 4
Exiting program.
Program 17 Page 3