Stack and Queue Program
Stack and Queue Program
Input:
The infix expression. x^y/(5*z)+2
Output:
Postfix Form Is: xy^5z*/2+
Algorithm
infixToPostfix(infix)
Begin
initially push some special character say # into the stack
for each character ch from infix expression, do
if ch is alphanumeric character, then
add ch to postfix expression
else if ch = opening parenthesis (, then
push ( into stack
else if ch = ^, then //exponential operator of higher precedence
push ^ into the stack
else if ch = closing parenthesis ), then
while stack is not empty and stack top ≠ (,
do pop and add item from stack to postfix expression
done
#include<bits/stdc++.h>
class Queue
// Circular Queue
int size;
int *arr;
public:
Queue(int s)
size = s;
int deQueue();
void displayQueue();
};
printf("\nQueue is Full");
return;
front = rear = 0;
arr[rear] = value;
rear = 0;
arr[rear] = value;
else
rear++;
arr[rear] = value;
int Queue::deQueue()
if (front == -1)
printf("\nQueue is Empty");
return INT_MIN;
arr[front] = -1;
if (front == rear)
front = -1;
rear = -1;
}
else if (front == size-1)
front = 0;
else
front++;
return data;
// of Circular Queue
void Queue::displayQueue()
if (front == -1)
printf("\nQueue is Empty");
return;
{
for (int i = front; i <= rear; i++)
printf("%d ",arr[i]);
else
int main()
Queue q(5);
q.enQueue(14);
q.enQueue(22);
q.enQueue(13);
q.enQueue(-6);
q.displayQueue();
q.displayQueue();
q.enQueue(9);
q.enQueue(20);
q.enQueue(5);
q.displayQueue();
q.enQueue(20);
return 0;
Output
Elements in Circular Queue are: 14 22 13 -6
Deleted value = 14
Deleted value = 22
Elements in Circular Queue are: 13 -6
Elements in Circular Queue are: 13 -6 9 20 5
Queue is Full
#include <bits/stdc++.h>
class Stack {
public:
int top;
unsigned capacity;
char* array;
};
stack->capacity = capacity;
stack->top = -1;
stack->array
return stack;
}
// Stack is empty when top is equal to -1
// It increases top by 1
if (isFull(stack))
return;
stack->array[++stack->top] = item;
// It decreases top by 1
if (isEmpty(stack))
return -1;
return stack->array[stack->top--];
}
int n = strlen(str);
int i;
push(stack, str[i]);
str[i] = pop(stack);
}
// Driver code
int main()
reverse(str);
return 0;
Output
Reversed string is ziuQskeeG