Professional Documents
Culture Documents
Expression Evaluation
#include <iostream>
#include <string.h>
// Stack type
struct Stack
{
int top;
unsigned capacity;
int* array;
};
// Stack Operations
struct Stack* createStack( unsigned capacity )
{
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
stack->top = -1;
stack->capacity = capacity;
stack->array = (int*) malloc(stack->capacity * sizeof(int));
return stack;
}
if (isdigit(exp[i]))
push(stack, exp[i] - '0');
else
{
int val1 = pop(stack);
int val2 = pop(stack);
switch (exp[i])
{
case '+': push(stack, val2 + val1); break;
case '-': push(stack, val2 - val1); break;
case '*': push(stack, val2 * val1); break;
case '/': push(stack, val2/val1); break;
}
}
}
return pop(stack);
}
int main()
{
int size;
cin >> size;
char exp[size];
for(int i =0; i<size; i++){
cin >> exp[i];
}
cout << evaluatePostfix(exp);
return 0;
}
Infix To Postfix
#include <iostream>
#include <stack>
#include <cstring>
using namespace std;
int prec(char c)
{
if (c == '^')
return 3;
else if (c == '/' || c == '*')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return -1;
}
void infixToPostfix(string s)
{
stack<char> st;
string result;
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
|| (c >= '0' && c <= '9')){
result += c;
result += " ";
}
else if (c == '(')
st.push('(');
else if (c == ')') {
while (st.top() != '(') {
result += st.top();
result += " ";
st.pop();
}
st.pop();
}
else {
while (!st.empty()
&& prec(s[i]) <= prec(st.top())) {
result += st.top();
result += " ";
st.pop();
}
st.push(c);
}
}
while (!st.empty()) {
result += st.top();
result += " ";
st.pop();
}
int main(){
string exp;
cin >> exp;
infixToPostfix(exp);
return 0;
}
C program for array implementation of stack
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
push(stack, 10);
push(stack, 20);
push(stack, 30);
return 0;
}
Infix to Prefix
i) Using Arrays
#include<stdio.h>
#include<string.h>
#include<limits.h>
#include<stdlib.h>
# define MAX 100
int top = -1;
char stack[MAX];
// Push function here, inserts value in stack and increments stack top by 1
void push(char item) {
if (isFull())
return;
top++;
stack[top] = item;
}
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
// Here, if we scan character is an ‘)’, we need to pop and print from the stack
// do this until an ‘(‘ is encountered in the stack.
else if (expression[i] == ')')
{
while (!isEmpty(stack) && peek(stack) != '(')
expression[++j] = pop(stack);
if (!isEmpty(stack) && peek(stack) != '(')
return -1; // invalid expression
else
pop(stack);
}
else // if an opertor
{
while (!isEmpty(stack) && precedence(expression[i]) <= precedence(peek(stack)))
expression[++j] = pop(stack);
push(expression[i]);
}
expression[++j] = '\0';
temp[j--]='\0';
while(exp[i]!='\0')
{
temp[j] = exp[i];
j--;
i++;
}
strcpy(exp,temp);
}
void brackets(char* exp){
int i = 0;
while(exp[i]!='\0')
{
if(exp[i]=='(')
exp[i]=')';
else if(exp[i]==')')
exp[i]='(';
i++;
}
}
void InfixtoPrefix(char *exp){
// reverse string
reverse(exp);
//change brackets
brackets(exp);
//get postfix
getPostfix(exp);
// reverse string again
reverse(exp);
}
int main()
{
printf("The infix is: ");
return 0;
}
#include<string.h>
#include<limits.h>
#include<stdio.h>
#include<stdlib.h>
# define MAX 100
// Push function here, inserts value in stack and increments stack top by 1
void push(struct Stack* stack, char item)
{
if (isFull(stack))
return;
stack->array[++stack->top] = item;
}
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
// Here, if we scan character is an ‘)’, we need to pop and print from the stack
// do this until an ‘(‘ is encountered in the stack.
else if (expression[i] == ')')
{
while (!isEmpty(stack) && peek(stack) != '(')
expression[++j] = pop(stack);
if (!isEmpty(stack) && peek(stack) != '(')
return -1; // invalid expression
else
pop(stack);
}
else // if an opertor
{
while (!isEmpty(stack) && precedence(expression[i]) <= precedence(peek(stack)))
expression[++j] = pop(stack);
push(stack, expression[i]);
}
}
expression[++j] = '\0';
temp[j--]='\0';
while(exp[i]!='\0')
{
temp[j] = exp[i];
j--;
i++;
}
strcpy(exp,temp);
}
void brackets(char* exp){
int i = 0;
while(exp[i]!='\0')
{
if(exp[i]=='(')
exp[i]=')';
else if(exp[i]==')')
exp[i]='(';
i++;
}
}
void InfixtoPrefix(char *exp){
// reverse string
reverse(exp);
//change brackets
brackets(exp);
//get postfix
getPostfix(exp);
// reverse string again
reverse(exp);
}
int main()
{
printf("The infix is: ");
return 0;
}
Double Stack – 2 Stacks in a single array
#include <stdio.h>
#define SIZE 20
int array[SIZE]; // declaration of array type variable.
int top1 = -1;
int top2 = SIZE;
int main()
{
int ar[SIZE];
int i;
int num_of_ele;
return 0;
}
QUEUE
Queue in C
#include <stdio.h>
#include<stdlib.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
int main()
{
int choice;
while (1)
{
printf("1.Insert element to queue n");
printf("2.Delete element from queue n");
printf("3.Display all elements of queue n");
printf("4.Quit n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice n");
}
}
}
void insert()
{
int item;
if(rear == MAX - 1)
printf("Queue Overflow n");
else
{
if(front== - 1)
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &item);
rear = rear + 1;
queue_array[rear] = item;
}
}
void delete()
{
if(front == - 1 || front > rear)
{
printf("Queue Underflow n");
return;
}
else
{
printf("Element deleted from queue is : %dn", queue_array[front]);
front = front + 1;
}
}
void display()
{
int i;
if(front == - 1)
printf("Queue is empty n");
else
{
printf("Queue is : n");
for(i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("n");
}
}
Queue in C++
#include<iostream>
class Queue {
private:
int front;
int rear;
int arr[5];
public:
Queue() {
front = -1;
rear = -1;
for (int i = 0; i < 5; i++) {
arr[i] = 0;
}
}
bool isEmpty() {
if (front == -1 && rear == -1)
return true;
else
return false;
}
bool isFull() {
if (rear == 4)
return true;
else
return false;
}
void enqueue(int val) {
if (isFull()) {
cout << "Queue full" << endl;
return;
} else if (isEmpty()) {
rear = 0;
front = 0;
arr[rear] = val;
} else {
rear++;
arr[rear] = val;
}
int dequeue() {
int x = 0;
if (isEmpty()) {
cout << "Queue is Empty" << endl;
return x;
} else if (rear == front) {
x = arr[rear];
rear = -1;
front = -1;
return x;
} else {
cout << "front value: " << front << endl;
x = arr[front];
arr[front] = 0;
front++;
return x;
}
}
int count() {
return (rear - front + 1);
}
void display() {
cout << "All values in the Queue are - " << endl;
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
}
};
int main() {
Queue q1;
int value, option;
do {
cout << "\n\nWhat operation do you want to perform? Select Option number. Enter 0 to exit."
<< endl;
cout << "1. Enqueue()" << endl;
cout << "2. Dequeue()" << endl;
cout << "3. isEmpty()" << endl;
cout << "4. isFull()" << endl;
cout << "5. count()" << endl;
cout << "6. display()" << endl;
cout << "7. Clear Screen" << endl << endl;
switch (option) {
case 0:
break;
case 1:
cout << "Enqueue Operation \nEnter an item to Enqueue in the Queue" << endl;
cin >> value;
q1.enqueue(value);
break;
case 2:
cout << "Dequeue Operation \nDequeued Value : " << q1.dequeue() << endl;
break;
case 3:
if (q1.isEmpty())
cout << "Queue is Empty" << endl;
else
cout << "Queue is not Empty" << endl;
break;
case 4:
if (q1.isFull())
cout << "Queue is Full" << endl;
else
cout << "Queue is not Full" << endl;
break;
case 5:
cout << "Count Operation \nCount of items in Queue : " << q1.count() << endl;
break;
case 6:
cout << "Display Function Called - " << endl;
q1.display();
break;
case 7:
system("cls");
break;
default:
cout << "Enter Proper Option number " << endl;
}
return 0;
}
Circular Queue in C++
#include<iostream>
class CircularQueue {
private:
int front;
int rear;
int arr[5];
int itemCount;
public:
CircularQueue() {
itemCount = 0;
front = -1;
rear = -1;
for (int i = 0; i < 5; i++) {
arr[i] = 0;
}
}
bool isEmpty() {
if (front == -1 && rear == -1)
return true;
else
return false;
}
bool isFull() {
if ((rear + 1) % 5 == front)
return true;
else
return false;
}
void enqueue(int val) {
if (isFull()) {
cout << "Queue full" << endl;
return;
} else if (isEmpty()) {
rear = 0;
front = 0;
arr[rear] = val;
} else {
rear = (rear + 1) % 5;
arr[rear] = val;
}
itemCount++;
}
int dequeue() {
int x = 0;
if (isEmpty()) {
cout << "Queue is Empty" << endl;
return x;
} else if (rear == front) {
x = arr[rear];
rear = -1;
front = -1;
itemCount--;
return x;
} else {
cout << "front value: " << front << endl;
x = arr[front];
arr[front] = 0;
front = (front + 1) % 5;
itemCount--;
return x;
}
}
int count() {
return (itemCount);
}
void display() {
cout << "All values in the Queue are - " << endl;
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
}
};
int main() {
CircularQueue q1;
int value, option;
do {
cout << "\n\nWhat operation do you want to perform? Select Option number. Enter 0 to exit."
<< endl;
cout << "1. Enqueue()" << endl;
cout << "2. Dequeue()" << endl;
cout << "3. isEmpty()" << endl;
cout << "4. isFull()" << endl;
cout << "5. count()" << endl;
cout << "6. display()" << endl;
cout << "7. Clear Screen" << endl << endl;
return 0;
}
Double Ended Queue (Dequeue) in C
#include <stdio.h>
#define size 5
int deque[size];
int f = -1, r = -1;
// insert_front function will insert the value from the front
void insert_front(int x)
{
if((f==0 && r==size-1) || (f==r+1))
{
printf("Overflow");
}
else if((f==-1) && (r==-1))
{
f=r=0;
deque[f]=x;
}
else if(f==0)
{
f=size-1;
deque[f]=x;
}
else
{
f=f-1;
deque[f]=x;
}
}
while(i!=r)
{
printf("%d ",deque[i]);
i=(i+1)%size;
}
printf("%d",deque[r]);
}
}
else if(f==(size-1))
{
printf("\nThe deleted element is %d", deque[f]);
f=0;
}
else
{
printf("\nThe deleted element is %d", deque[f]);
f=f+1;
}
}
}
else if(r==0)
{
printf("\nThe deleted element is %d", deque[r]);
r=size-1;
}
else
{
printf("\nThe deleted element is %d", deque[r]);
r=r-1;
}
}
int main()
{
insert_front(20);
insert_front(10);
insert_rear(30);
insert_rear(50);
insert_rear(80);
display(); // Calling the display function to retrieve the values of deque
getfront(); // Retrieve the value at front-end
getrear(); // Retrieve the value at rear-end
delete_front();
delete_rear();
display(); // calling display function to retrieve values after deletion
return 0;
}
TREE TRAVERSAL
//Tree Traversal in C
#include <stdio.h>
#include <stdlib.h>
struct node {
int item;
struct node* left;
struct node* right;
};
// Inorder traversal
void inorderTraversal(struct node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}
// preorderTraversal traversal
void preorderTraversal(struct node* root) {
if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
// postorderTraversal traversal
void postorderTraversal(struct node* root) {
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
}
return newNode;
}
// Insert on the left of the node
struct node* insertLeft(struct node* root, int value) {
root->left = createNode(value);
return root->left;
}
int main() {
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);
insertLeft(root->left, 5);
insertRight(root->left, 6);
#include<stdio.h>
#include<stdlib.h>
struct node
{
int key;
struct node *left;
struct node *right;
};
newNode->key = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
return root;
}
int main()
{
struct node *root = NULL;
int data;
char ch;
/* Do while loop to display various options to select from to decide the input */
do
{
printf("\nSelect one of the operations::");
printf("\n1. To insert a new node in the Binary Tree");
printf("\n2. To display the nodes of the Binary Tree(via Inorder Traversal).\n");
int choice;
scanf("%d",&choice);
switch (choice)
{
case 1 :
printf("\nEnter the value to be inserted\n");
scanf("%d",&data);
root = insertNode(root,data);
break;
case 2 :
printf("\nInorder Traversal of the Binary Tree::\n");
inorder(root);
break;
default :
printf("Wrong Entry\n");
break;
}
return 0;
}