DS Codes
DS Codes
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
if (isdigit(ch)) {
stack[++top] = ch - '0';
} else {
int b = stack[top--];
int a = stack[top--];
switch (ch) {
case '+': stack[++top] = a + b; break;
case '-': stack[++top] = a - b; break;
case '*': stack[++top] = a * b; break;
case '/': stack[++top] = a / b; break;
default:
printf("Invalid operator: %c\n", ch);
exit(1);
}
}
}
return stack[top];
}
int main() {
char exp[MAX];
printf("Enter a postfix expression: ");
scanf("%s", exp);
printf("The result is: %d\n", evaluatePostfix(exp));
return 0;
}
Input :
Enter a postfix expression: 53+62/*
Output :
The result is: 8
To implement stack using array
#include <stdio.h>
#define SIZE 10
// Function prototypes
void push(int value);
void pop();
void display();
void peek();
int main() {
int value, choice;
do {
printf("\n\n***** MENU *****\n");
printf("1. Push\n2. Pop\n3. Display\n4. Peek\n5. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to be inserted: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
peek();
break;
case 5:
printf("\nExiting...\n");
break;
default:
printf("\nInvalid choice! Please try again.\n");
}
} while (choice != 5);
return 0;
}
void pop() {
if (top == -1) {
printf("\nStack is Empty! Deletion is not possible!\n");
} else {
printf("\nDeleted: %d\n", stack[top]);
top--;
}
}
void peek() {
if (top == -1) {
printf("\nStack is Empty!\n");
} else {
printf("\nStack top is %d\n", stack[top]);
}
}
void display() {
if (top == -1) {
printf("\nStack is Empty!\n");
} else {
printf("\nStack elements are:\n");
for (int i = top; i >= 0; i--) {
printf("%d\n", stack[i]);
}
}
}
Array implementation of queue
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
int queue_array[MAX];
int rear = -1;
int front = -1;
void insert();
void delete();
void display();
int main() {
int choice;
while (1) {
printf("\nQueue Operations:\n");
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(0);
default:
printf("Wrong choice. Please try again.\n");
}
}
return 0;
}
void insert() {
int add_item;
if (rear == MAX - 1) {
printf("Queue Overflow\n");
} else {
if (front == -1) {
front = 0;
}
printf("Insert the element in queue: ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
printf("%d added to the queue.\n", add_item);
}
}
void delete() {
if (front == -1) {
printf("Queue Underflow\n");
return;
}
printf("Element deleted from queue is: %d\n", queue_array[front]);
if (front == rear) {
front = -1;
rear = -1;
} else {
front = front + 1;
}
}
void display() {
if (front == -1) {
printf("Queue is empty\n");
} else {
printf("Queue elements are: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue_array[i]);
}
printf("\n");
}
}