Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

ALGORITH TO INFIX TO POST FIX CONVERSION.

Input and Output

Input:
The infix expression. x^y/(5*z)+2
Output:
Postfix Form Is: xy^5z*/2+

Algorithm

infixToPostfix(infix)

Input − Infix expression.

Output − Convert infix expression to postfix form.

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

pop ( also from the stack


else
while stack is not empty AND precedence of ch <= precedence of stack top
element, do
pop and add into postfix expression
done
push the newly coming character.
done

while the stack contains some remaining characters, do


pop and add to the postfix expression
done
return postfix
End

write a program to insert and delete operation in a circular queue

// C or C++ program for insertion and

// deletion in Circular Queue

#include<bits/stdc++.h>

using namespace std;

class Queue

// Initialize front and rear

int rear, front;

// Circular Queue

int size;
int *arr;

public:

Queue(int s)

front = rear = -1;

size = s;

arr = new int[s];

void enQueue(int value);

int deQueue();

void displayQueue();

};

/* Function to create Circular queue */

void Queue::enQueue(int value)

if ((front == 0 && rear == size-1) ||

((rear+1) % size == front))


{

printf("\nQueue is Full");

return;

else if (front == -1) /* Insert First Element */

front = rear = 0;

arr[rear] = value;

else if (rear == size-1 && front != 0)

rear = 0;

arr[rear] = value;

else

rear++;
arr[rear] = value;

// Function to delete element from Circular Queue

int Queue::deQueue()

if (front == -1)

printf("\nQueue is Empty");

return INT_MIN;

int data = arr[front];

arr[front] = -1;

if (front == rear)

front = -1;

rear = -1;

}
else if (front == size-1)

front = 0;

else

front++;

return data;

// Function displaying the elements

// of Circular Queue

void Queue::displayQueue()

if (front == -1)

printf("\nQueue is Empty");

return;

printf("\nElements in Circular Queue are: ");

if (rear >= front)

{
for (int i = front; i <= rear; i++)

printf("%d ",arr[i]);

else

for (int i = front; i < size; i++)

printf("%d ", arr[i]);

for (int i = 0; i <= rear; i++)

printf("%d ", arr[i]);

/* Driver of the program */

int main()

Queue q(5);

// Inserting elements in Circular Queue

q.enQueue(14);
q.enQueue(22);

q.enQueue(13);

q.enQueue(-6);

// Display elements present in Circular Queue

q.displayQueue();

// Deleting elements from Circular Queue

printf("\nDeleted value = %d", q.deQueue());

printf("\nDeleted value = %d", q.deQueue());

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

write a program to reverse a string using a stack

// C++ program to reverse a string using stack

#include <bits/stdc++.h>

using namespace std;

// A structure to represent a stack

class Stack {

public:

int top;

unsigned capacity;
char* array;

};

// function to create a stack of given

// capacity. It initializes size of stack as 0

Stack* createStack(unsigned capacity)

Stack* stack = new Stack();

stack->capacity = capacity;

stack->top = -1;

stack->array

= new char[(stack->capacity * sizeof(char))];

return stack;

// Stack is full when top is equal to the last index

int isFull(Stack* stack)

return stack->top == stack->capacity - 1;

}
// Stack is empty when top is equal to -1

int isEmpty(Stack* stack) { return stack->top == -1; }

// Function to add an item to stack.

// It increases top by 1

void push(Stack* stack, char item)

if (isFull(stack))

return;

stack->array[++stack->top] = item;

// Function to remove an item from stack.

// It decreases top by 1

char pop(Stack* stack)

if (isEmpty(stack))

return -1;

return stack->array[stack->top--];
}

// A stack based function to reverse a string

void reverse(char str[])

// Create a stack of capacity

// equal to length of string

int n = strlen(str);

Stack* stack = createStack(n);

// Push all characters of string to stack

int i;

for (i = 0; i < n; i++)

push(stack, str[i]);

// Pop all characters of string and

// put them back to str

for (i = 0; i < n; i++)

str[i] = pop(stack);

}
// Driver code

int main()

char str[] = "GeeksQuiz";

reverse(str);

cout << "Reversed string is " << str;

return 0;

// This code is contributed by rathbhupendra

Output
Reversed string is ziuQskeeG

You might also like