DS Practical solution
DS Practical solution
Write algorithm and Perform PUSH operation on stack using Array implementation
#include<stdio.h>
#define MAX 5
// Function prototype
void push();
int stack[MAX];
int top = -1;
void main() {
int option;
do {
printf("\n__________ MENU __________\n");
printf("1. PUSH\n");
printf("2. Exit\n");
printf("__________________________\n");
printf("Enter your option: ");
scanf("%d", &option);
switch(option) {
case 1:
push();
break;
case 2:
printf("Exiting program...\n");
break;
default:
printf("INVALID OPTION\n");
}
} while(option != 2); // Add semicolon here
}
void push() {
int element;
if (top >= MAX - 1) {
printf("STACK OVERFLOW\n");
return;
}
top = top + 1;
stack[top] = element;
2. Write algorithm and Perform POP operation on stack using Array implementation
#include<stdio.h>
#define MAX 5
// Function prototypes
void push();
void pop();
void display();
int stack[MAX];
int top = -1;
void main() {
int option;
do {
printf("\n__________ MENU __________\n");
printf("1. PUSH\n");
printf("2. POP\n");
printf("3. EXIT\n");
printf("__________________________\n");
printf("Enter your option: ");
scanf("%d", &option);
switch(option) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
printf("Exiting program...\n");
break;
default:
printf("INVALID OPTION\n");
}
} while(option != 3);
}
void push() {
int element;
if (top >= MAX - 1) {
printf("STACK OVERFLOW\n");
return;
}
top = top + 1;
stack[top] = element;
void pop() {
int element;
if (top == -1) {
printf("STACK UNDERFLOW\n");
return;
}
element = stack[top];
top = top - 1;
#include<stdio.h>
#define MAX 5
// Function prototypes
void push();
void pop();
void display();
int stack[MAX];
int top = -1;
void main() {
int option;
do {
printf("\n__________ MENU __________\n");
printf("1. PUSH\n");
printf("2. POP\n");
printf("3. DISPLAY\n");
printf("4. EXIT\n");
printf("__________________________\n");
printf("Enter your option: ");
scanf("%d", &option);
switch(option) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Exiting program...\n");
break;
default:
printf("INVALID OPTION\n");
}
} while(option != 4);
}
void push() {
int element;
if (top >= MAX - 1) {
printf("STACK OVERFLOW\n");
return;
}
top = top + 1;
stack[top] = element;
void pop() {
int element;
if (top == -1) {
printf("STACK UNDERFLOW\n");
return;
}
element = stack[top];
top = top - 1;
void display() {
int i;
if (top == -1) {
printf("STACK IS EMPTY\n");
return;
}
#include <stdio.h>
#define MAX 5 // Maximum size of Queue
int queue[MAX];
int front = -1, rear = -1;
// Function prototypes
void insert();
void delete_element();
void display();
void main() {
int choice;
do {
printf("\n------ MENU ------\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("------------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insert();
break;
case 2:
delete_element();
break;
case 3:
display();
break;
case 4:
printf("Exiting program...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != 4);
}
void insert() {
int item;
if (rear == MAX - 1) {
printf("Queue Overflow! Cannot insert.\n");
return;
}
printf("Enter the element to insert: ");
scanf("%d", &item);
void delete_element() {
if (front == -1 || front > rear) {
printf("Queue Underflow! Cannot delete.\n");
return;
}
printf("Deleted element: %d\n", queue[front]);
front++;
}
void display() {
int i;
if (front == -1 || front > rear) {
printf("Queue is empty.\n");
return;
}
printf("Queue elements are: ");
for (i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
5. Write algorithm and Perform Insert Operation on Queue using Array
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void main() {
int option;
do {
printf("\n_____________MENU___________\n");
printf("1. Insert a node at the beginning\n");
printf("2. Delete a node at the end\n");
printf("3. Exit the program\n");
printf("____________________________\n");
printf("ENTER YOUR OPTION: ");
scanf("%d", &option);
switch(option) {
case 1:
insert_at_begining();
break;
case 2:
delete_at_end();
break;
case 3:
printf("Exiting the program...\n");
break;
default:
printf("INVALID CHOICE\n");
}
} while(option != 3); // ✅ CORRECTED
}
void insert_at_begining() {
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
if (temp == NULL) {
printf("Out of memory\n");
return;
}
void delete_at_end() {
struct node *temp, *ptr;
if (start == NULL) {
printf("EMPTY LIST\n");
return;
} else if (start->next == NULL) {
ptr = start;
start = NULL;
printf("ELEMENT DELETED IS => %d\n", ptr->data);
free(ptr);
} else {
ptr = start;
while (ptr->next != NULL) {
temp = ptr;
ptr = ptr->next;
}
temp->next = NULL;
printf("ELEMENT DELETED IS => %d\n", ptr->data);
free(ptr);
}
}
7. Perform following on Linked list
i. Create a node
ii. Insert at the end
iii. Display
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct node {
int data;
struct node *next;
};
// Function declarations
void create_node();
void insert_at_end();
void display();
void main() {
int option;
do {
printf("\n_________ MENU _________\n");
printf("1. Create a node (if list is empty)\n");
printf("2. Insert at the end\n");
printf("3. Display the list\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &option);
switch(option) {
case 1:
create_node();
break;
case 2:
insert_at_end();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
}
} while(option != 4);
}
if (temp == NULL) {
printf("Memory allocation failed.\n");
return;
}
if (start == NULL) {
start = temp;
} else {
ptr = start;
while (ptr->next != NULL) {
ptr = ptr->next;
}
ptr->next = temp;
}
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *start = NULL; // Global start pointer
// Function prototypes
void create_node();
void insert_at_position();
void delete_at_beginning();
void display();
void main() {
int choice;
do {
printf("\n------ MENU ------\n");
printf("1. Create First Node\n");
printf("2. Insert at Any Position\n");
printf("3. Delete at Beginning\n");
printf("4. Display List\n");
printf("5. Exit\n");
printf("------------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
create_node();
break;
case 2:
insert_at_position();
break;
case 3:
delete_at_beginning();
break;
case 4:
display();
break;
case 5:
printf("Exiting program...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != 5);
}
void create_node() {
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
if (temp == NULL) {
printf("Memory allocation failed!\n");
return;
}
printf("Enter data for first node: ");
scanf("%d", &temp->data);
temp->next = NULL;
if (start == NULL) {
start = temp;
printf("First node created successfully.\n");
} else {
printf("List already created. Use Insert option.\n");
free(temp);
}
}
void insert_at_position() {
struct node *temp, *ptr;
int pos, i = 1;
if (pos == 1) {
temp->next = start;
start = temp;
printf("Node inserted at beginning.\n");
} else {
ptr = start;
while (ptr != NULL && i < pos - 1) {
ptr = ptr->next;
i++;
}
if (ptr == NULL) {
printf("Position out of bounds!\n");
free(temp);
} else {
temp->next = ptr->next;
ptr->next = temp;
printf("Node inserted at position %d.\n", pos);
}
}
}
void delete_at_beginning() {
struct node *temp;
if (start == NULL) {
printf("List is empty, nothing to delete.\n");
return;
}
temp = start;
start = start->next;
printf("Deleted element: %d\n", temp->data);
free(temp);
}
void display() {
struct node *ptr;
if (start == NULL) {
printf("List is empty.\n");
return;
}
ptr = start;
printf("Linked List Elements: ");
while (ptr != NULL) {
printf("%d -> ", ptr->data);
ptr = ptr->next;
}
printf("NULL\n");
}
9. Perform insert operation on circular queue using array implementation and display result
#include <stdio.h>
#define MAX 5
// Function prototypes
void insert();
void display();
int queue[MAX];
int front = -1, rear = -1;
void main() {
int option;
do {
printf("\n_____ CIRCULAR QUEUE MENU _____\n");
printf("1. Insert\n");
printf("2. Display\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &option);
switch(option) {
case 1:
insert();
break;
case 2:
display();
break;
case 3:
printf("Exiting program...\n");
break;
default:
printf("Invalid option!\n");
}
} while(option != 3);
}
// First insertion
if (front == -1) {
front = 0;
rear = 0;
}
else if (rear == MAX - 1 && front != 0) {
rear = 0; // wrap around
}
else {
rear = rear + 1;
}
queue[rear] = item;
printf("Inserted %d successfully.\n", item);
}
if (front == -1) {
printf("Queue is empty.\n");
return;
}
printf("\n");
}
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct node {
int data;
struct node *next;
};
// Function declarations
void enqueue();
void dequeue();
void peek();
void display();
void main() {
int option;
do {
printf("\n_________ QUEUE MENU _________\n");
printf("1. Enqueue (Insert)\n");
printf("2. Dequeue (Delete)\n");
printf("3. Peek (Front element)\n");
printf("4. Display Queue\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &option);
switch(option) {
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
printf("Exiting program...\n");
break;
default:
printf("Invalid option!\n");
}
} while(option != 5);
}
// i. Enqueue operation
void enqueue() {
int value;
struct node *temp;
temp->data = value;
temp->next = NULL;
if (front == NULL) {
front = rear = temp;
} else {
rear->next = temp;
rear = temp;
}
if (front == NULL) {
printf("Queue Underflow! (Empty queue)\n");
return;
}
temp = front;
front = front->next;
if (front == NULL) {
rear = NULL;
}
}
if (front == NULL) {
printf("Queue is empty.\n");
return;
}
printf("\n");
}
11. Perform following operations on Stack using linked list implementation
i. PUSH
ii. POP
iii. PEAK
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
// Function prototypes
void push();
void pop();
void peek();
void display();
void main() {
int choice;
do {
printf("\n------ MENU ------\n");
printf("1. PUSH (Insert)\n");
printf("2. POP (Delete)\n");
printf("3. PEEK (Top Element)\n");
printf("4. Display Stack\n");
printf("5. Exit\n");
printf("------------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
printf("Exiting program...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != 5);
}
void push() {
struct node *temp;
int item;
temp->data = item;
temp->next = top;
top = temp;
printf("%d pushed successfully.\n", item);
}
void pop() {
struct node *temp;
if (top == NULL) {
printf("Stack Underflow! Cannot pop.\n");
return;
}
temp = top;
printf("Popped element: %d\n", temp->data);
top = top->next;
free(temp);
}
void peek() {
if (top == NULL) {
printf("Stack is empty. No top element.\n");
return;
}
printf("Top element is: %d\n", top->data);
}
void display() {
struct node *temp;
if (top == NULL) {
printf("Stack is empty.\n");
return;
}
printf("Stack elements: ");
temp = top;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int stack[MAX];
int top = -1;
// Function prototypes
void push(int);
int pop();
int evaluatePostfix(char[]);
void main() {
char expr[MAX];
int result;
result = evaluatePostfix(expr);
int pop() {
if (top == -1) {
printf("Stack Underflow!\n");
exit(1);
}
return stack[top--];
}
switch (expr[i]) {
case '+':
result = op1 + op2;
break;
case '-':
result = op1 - op2;
break;
case '*':
result = op1 * op2;
break;
case '/':
result = op1 / op2;
break;
default:
printf("Invalid operator %c\n", expr[i]);
exit(1);
}
push(result);
}
}
return pop();
}