7a
#include
#include
#define MAX_SIZE 5
int queue[MAX_SIZE];
int front = -1;
int rear = -1;
int isEmpty() {
return front == -1;
}
int isFull() {
return rear == MAX_SIZE - 1;
}
void enqueue(int data) {
if (isFull()) {
printf("Queue Full.\n");
return;
}
if (isEmpty()) {
front = 0;
}
queue[++rear] = data;
}
int dequeue() {
if (isEmpty()) {
printf("Queue Empty.\n");
return -1;
}
int data = queue[front];
if (front == rear) {
front = -1;
rear = -1;
} else {
front++;
}
return data;
}
int peek() {
if (isEmpty()) {
printf("Queue Empty.\n");
return -1;
}
return queue[front];
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50);
if (isFull()) printf("Is Full.\n");
enqueue(60);
printf("Front: %d\n", peek());
printf("Dequeued: %d\n", dequeue());
printf("Dequeued: %d\n", dequeue());
printf("Front: %d\n", peek());
if (!isEmpty()) printf("Not Empty.\n");
return 0;
}
7b
#include
#include
struct Node {
int data;
struct Node* next;
};
struct Node* front = NULL;
struct Node* rear = NULL;
int isEmpty() {
return front == NULL;
}
void enqueue(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) return;
newNode->data = data;
newNode->next = NULL;
if (isEmpty()) {
front = newNode;
rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
}
int dequeue() {
if (isEmpty()) {
printf("Queue Empty.\n");
return -1;
}
struct Node* temp = front;
int data = temp->data;
front = front->next;
if (front == NULL) {
rear = NULL;
}
free(temp);
return data;
}
int peek() {
if (isEmpty()) {
printf("Queue Empty.\n");
return -1;
}
return front->data;
}
int main() {
enqueue(100);
enqueue(200);
enqueue(300);
printf("Front: %d\n", peek());
printf("Dequeued: %d\n", dequeue());
printf("Dequeued: %d\n", dequeue());
printf("Front: %d\n", peek());
if (!isEmpty()) printf("Not Empty.\n");
printf("Dequeued: %d\n", dequeue());
if (isEmpty()) printf("Is Empty.\n");
return 0;
}
Cool stories
Sunday, 5 October 2025
Queue 7
Stack 6
7a
#include
#include
#define MAX_SIZE 5
int stack[MAX_SIZE];
int top = -1;
int isEmpty() {
return top == -1;
}
int isFull() {
return top == MAX_SIZE - 1;
}
void push(int data) {
if (isFull()) {
printf("Stack Full.\n");
return;
}
stack[++top] = data;
}
int pop() {
if (isEmpty()) {
printf("Stack Empty.\n");
return -1;
}
return stack[top--];
}
int peek() {
if (isEmpty()) {
printf("Stack Empty.\n");
return -1;
}
return stack[top];
}
int main() {
push(10);
push(20);
push(30);
push(40);
push(50);
if (isFull()) printf("Is Full.\n");
push(60);
printf("Top: %d\n", peek());
printf("Popped: %d\n", pop());
printf("Popped: %d\n", pop());
printf("Top: %d\n", peek());
if (!isEmpty()) printf("Not Empty.\n");
return 0;
}
7b
#include
#include
struct Node {
int data;
struct Node* next;
};
struct Node* top = NULL;
int isEmpty() {
return top == NULL;
}
void push(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) return;
newNode->data = data;
newNode->next = top;
top = newNode;
}
int pop() {
if (isEmpty()) {
printf("Stack Empty.\n");
return -1;
}
struct Node* temp = top;
int data = temp->data;
top = top->next;
free(temp);
return data;
}
int peek() {
if (isEmpty()) {
printf("Stack Empty.\n");
return -1;
}
return top->data;
}
int main() {
push(100);
push(200);
push(300);
printf("Top: %d\n", peek());
printf("Popped: %d\n", pop());
printf("Popped: %d\n", pop());
printf("Top: %d\n", peek());
if (!isEmpty()) printf("Not Empty.\n");
printf("Popped: %d\n", pop());
if (isEmpty()) printf("Is Empty.\n");
return 0;
}
Imp
stack prefix:
#include
#include
// Function to check if a pair of opening and closing symbols match
int is_match(char open, char close) {
if (open == '(' && close == ')') return 1;
if (open == '{' && close == '}') return 1;
if (open == '[' && close == ']') return 1;
return 0;
}
int main() {
char exp[100];
char stack[100];
int top = -1; // Stack pointer
int i;
printf("Enter expression: ");
scanf("%s", exp);
for (i = 0; exp[i] != '\0'; i++) {
char current_char = exp[i];
// Push opening symbols
if (current_char == '(' || current_char == '{' || current_char == '[') {
if (top < 99) {
stack[++top] = current_char; // PUSH
}
}
// Process closing symbols
else if (current_char == ')' || current_char == '}' || current_char == ']') {
// Check for closing symbol with empty stack
if (top == -1) {
printf("Expression is Invalid\n");
return 0;
}
// Check for correct match
if (is_match(stack[top], current_char)) {
top--; // POP
} else {
printf("Expression is Invalid\n"); // Mismatch (e.g., {[}] )
return 0;
}
}
}
// Final check: Stack must be empty
if (top == -1) {
printf("Expression is Valid\n");
} else {
printf("Expression is Invalid\n"); // Unmatched opening symbol (e.g., (a+b)
}
return 0;
}
hanoi problem:
#include
void tower_of_hanoi(int n, char source, char destination, char auxiliary) {
if (n == 1) {
printf("Move disk 1 from rod %c to rod %c\n", source, destination);
return;
}
tower_of_hanoi(n - 1, source, auxiliary, destination);
printf("Move disk %d from rod %c to rod %c\n", n, source, destination);
tower_of_hanoi(n - 1, auxiliary, destination, source);
}
int main() {
int num_disks = 3;
// Start with 3 disks on 'A', moving to 'C', using 'B' as auxiliary
tower_of_hanoi(num_disks, 'A', 'C', 'B');
return 0;
}
linked list prefix:
#include
#include
#define MAX 50
char stack[MAX];
int top = -1;
void push(char op) {
if (top < MAX - 1) {
stack[++top] = op;
}
}
char pop() {
if (top != -1) {
return stack[top--];
}
return '\0';
}
char peek() {
if (top != -1) {
return stack[top];
}
return '\0';
}
int is_empty() {
return (top == -1);
}
int precedence(char op) {
switch(op) {
case '+':
case '-': return 1;
case '*':
case '/': return 2;
case '^': return 3;
default: return 0;
}
}
void infix_to_postfix(char *infix) {
char postfix[MAX];
int i = 0, j = 0;
char next_char;
top = -1;
for (i = 0; infix[i] != '\0'; i++) {
next_char = infix[i];
if ((next_char >= 'a' && next_char <= 'z') || (next_char >= 'A' && next_char <= 'Z') || (next_char >= '0' && next_char <= '9')) {
postfix[j++] = next_char;
}
else if (next_char == '(') {
push(next_char);
}
else if (next_char == ')') {
while (!is_empty() && peek() != '(') {
postfix[j++] = pop();
}
if (!is_empty() && peek() == '(') {
pop();
}
}
else {
while (!is_empty() && precedence(peek()) >= precedence(next_char)) {
postfix[j++] = pop();
}
push(next_char);
}
}
while (!is_empty()) {
postfix[j++] = pop();
}
postfix[j] = '\0';
printf("Postfix expression: %s\n", postfix);
}
int main() {
// Example expression: (a+b)*c-d
char expression[] = "(a+b)*c-d";
infix_to_postfix(expression);
return 0;
}
Linked list 5
5a
#include
#include
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) return NULL;
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
void traverseForward(struct Node* head) {
struct Node* current = head;
if (head == NULL) {
printf("List Empty.\n");
return;
}
printf("Forward: NULL <-> ");
while (current != NULL) {
printf("%d <-> ", current->data);
current = current->next;
}
printf("NULL\n");
}
void insertBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (newNode == NULL) return;
newNode->next = *head;
if (*head != NULL) {
(*head)->prev = newNode;
}
*head = newNode;
}
void insertEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (newNode == NULL) return;
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* last = *head;
while (last->next != NULL) {
last = last->next;
}
last->next = newNode;
newNode->prev = last;
}
void insertAfter(struct Node* head, int key, int data) {
struct Node* current = head;
while (current != NULL && current->data != key) {
current = current->next;
}
if (current == NULL) return;
struct Node* newNode = createNode(data);
if (newNode == NULL) return;
newNode->next = current->next;
newNode->prev = current;
if (current->next != NULL) {
current->next->prev = newNode;
}
current->next = newNode;
}
void deleteBeginning(struct Node** head) {
if (*head == NULL) return;
struct Node* temp = *head;
*head = (*head)->next;
if (*head != NULL) {
(*head)->prev = NULL;
}
free(temp);
}
void deleteEnd(struct Node** head) {
if (*head == NULL) return;
struct Node* temp = *head;
if (temp->next == NULL) {
free(*head);
*head = NULL;
return;
}
while (temp->next != NULL) {
temp = temp->next;
}
temp->prev->next = NULL;
free(temp);
}
void deleteByKey(struct Node** head, int key) {
struct Node* current = *head;
while (current != NULL && current->data != key) {
current = current->next;
}
if (current == NULL) return;
if (current->prev != NULL) {
current->prev->next = current->next;
} else {
*head = current->next;
}
if (current->next != NULL) {
current->next->prev = current->prev;
}
free(current);
}
int main() {
struct Node* head = NULL;
insertEnd(&head, 10);
insertBeginning(&head, 5);
insertEnd(&head, 30);
traverseForward(head);
insertAfter(head, 5, 7);
traverseForward(head);
deleteBeginning(&head);
traverseForward(head);
deleteEnd(&head);
traverseForward(head);
insertEnd(&head, 40);
insertEnd(&head, 50);
traverseForward(head);
deleteByKey(&head, 40);
traverseForward(head);
return 0;
}
5b
#include
#include
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) return NULL;
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
void insertEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (newNode == NULL) return;
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* last = *head;
while (last->next != NULL) {
last = last->next;
}
last->next = newNode;
newNode->prev = last;
}
void traverseForward(struct Node* head) {
struct Node* current = head;
if (head == NULL) {
printf("List Empty.\n");
return;
}
printf("Forward: NULL <-> ");
while (current != NULL) {
printf("%d <-> ", current->data);
current = current->next;
}
printf("NULL\n");
}
void traverseReverse(struct Node* head) {
struct Node* current = head;
if (head == NULL) {
printf("List Empty.\n");
return;
}
while (current->next != NULL) {
current = current->next;
}
printf("Reverse: NULL <-> ");
while (current != NULL) {
printf("%d <-> ", current->data);
current = current->prev;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
insertEnd(&head, 10);
insertEnd(&head, 20);
insertEnd(&head, 30);
insertEnd(&head, 40);
traverseForward(head);
traverseReverse(head);
return 0;
}
Linked list 4
4a
#include
#include
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) return NULL;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (newNode == NULL) return;
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* last = *head;
while (last->next != NULL) {
last = last->next;
}
last->next = newNode;
}
void traverseReverse(struct Node* head) {
if (head == NULL) return;
traverseReverse(head->next);
printf("%d ", head->data);
}
void traverseList(struct Node* head) {
struct Node* current = head;
if (head == NULL) {
printf("List Empty.\n");
return;
}
printf("List: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
insertEnd(&head, 10);
insertEnd(&head, 20);
insertEnd(&head, 30);
insertEnd(&head, 40);
traverseList(head);
printf("Reverse Traversal: ");
traverseReverse(head);
printf("\n");
return 0;
}
4b
#include
#include
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) return NULL;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (newNode == NULL) return;
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* last = *head;
while (last->next != NULL) {
last = last->next;
}
last->next = newNode;
}
void traverseList(struct Node* head, const char* name) {
struct Node* current = head;
printf("%s: ", name);
if (head == NULL) {
printf("Empty.\n");
return;
}
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
void mergeLists(struct Node* head1, struct Node* head2) {
if (head1 == NULL) return;
if (head2 == NULL) return;
struct Node* current = head1;
while (current->next != NULL) {
current = current->next;
}
current->next = head2;
}
int main() {
struct Node* list1 = NULL;
struct Node* list2 = NULL;
insertEnd(&list1, 10);
insertEnd(&list1, 20);
insertEnd(&list2, 100);
insertEnd(&list2, 200);
insertEnd(&list2, 300);
traverseList(list1, "List 1");
traverseList(list2, "List 2");
mergeLists(list1, list2);
printf("\nAfter Merging:\n");
traverseList(list1, "Merged List");
return 0;
}
Linked list 3
3.a
#include
#include
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) return NULL;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void traverseList(struct Node* head) {
struct Node* current = head;
if (head == NULL) {
printf("List Empty.\n");
return;
}
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
void insertBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (newNode == NULL) return;
newNode->next = *head;
*head = newNode;
}
void insertEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (newNode == NULL) return;
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* last = *head;
while (last->next != NULL) {
last = last->next;
}
last->next = newNode;
}
void insertAfter(struct Node* head, int key, int data) {
if (head == NULL) return;
struct Node* current = head;
while (current != NULL && current->data != key) {
current = current->next;
}
if (current == NULL) return;
struct Node* newNode = createNode(data);
if (newNode == NULL) return;
newNode->next = current->next;
current->next = newNode;
}
void deleteBeginning(struct Node** head) {
if (*head == NULL) return;
struct Node* temp = *head;
*head = (*head)->next;
free(temp);
}
void deleteEnd(struct Node** head) {
if (*head == NULL) return;
if ((*head)->next == NULL) {
free(*head);
*head = NULL;
return;
}
struct Node* secondLast = *head;
while (secondLast->next->next != NULL) {
secondLast = secondLast->next;
}
free(secondLast->next);
secondLast->next = NULL;
}
void deleteByKey(struct Node** head, int key) {
struct Node *temp = *head, *prev = NULL;
if (temp != NULL && temp->data == key) {
*head = temp->next;
free(temp);
return;
}
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp);
}
int main() {
struct Node* head = NULL;
insertEnd(&head, 10);
insertBeginning(&head, 5);
insertEnd(&head, 30);
traverseList(head);
insertAfter(head, 10, 20);
traverseList(head);
deleteBeginning(&head);
traverseList(head);
deleteEnd(&head);
traverseList(head);
insertEnd(&head, 40);
insertEnd(&head, 50);
traverseList(head);
deleteByKey(&head, 40);
traverseList(head);
return 0;
}
3.b
#include
#include
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) return NULL;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (newNode == NULL) return;
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* last = *head;
while (last->next != NULL) {
last = last->next;
}
last->next = newNode;
}
void traverseList(struct Node* head) {
struct Node* current = head;
if (head == NULL) {
printf("List Empty.\n");
return;
}
printf("List: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int searchList(struct Node* head, int key) {
struct Node* current = head;
int position = 1;
while (current != NULL) {
if (current->data == key) {
return position;
}
current = current->next;
position++;
}
return -1;
}
int main() {
struct Node* head = NULL;
int searchKey = 30;
int position;
insertEnd(&head, 10);
insertEnd(&head, 20);
insertEnd(&head, 30);
insertEnd(&head, 40);
insertEnd(&head, 50);
traverseList(head);
position = searchList(head, searchKey);
if (position != -1) {
printf("Element %d found at position %d.\n", searchKey, position);
} else {
printf("Element %d not found.\n", searchKey);
}
searchKey = 99;
position = searchList(head, searchKey);
if (position != -1) {
printf("Element %d found at position %d.\n", searchKey, position);
} else {
printf("Element %d not found.\n", searchKey);
}
return 0;
}
Array 2
2a
#include
#define SIZE 3
void readMatrix(int matrix[SIZE][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
scanf("%d", &matrix[i][j]);
}
}
}
void printMatrix(int matrix[SIZE][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
void addMatrices(int a[SIZE][SIZE], int b[SIZE][SIZE], int result[SIZE][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
result[i][j] = a[i][j] + b[i][j];
}
}
}
void multiplyMatrices(int a[SIZE][SIZE], int b[SIZE][SIZE], int result[SIZE][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
result[i][j] = 0;
for (int k = 0; k < SIZE; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
}
void transposeMatrix(int a[SIZE][SIZE], int result[SIZE][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
result[j][i] = a[i][j];
}
}
}
void showMenu() {
printf("\n--- Matrix Menu ---\n");
printf("1. Addition\n");
printf("2. Multiplication\n");
printf("3. Transpose\n");
printf("4. Exit\n");
printf("Choice: ");
}
int main() {
int A[SIZE][SIZE], B[SIZE][SIZE], R[SIZE][SIZE];
int choice;
printf("Enter matrix A (9 elements):\n");
readMatrix(A);
printf("Enter matrix B (9 elements):\n");
readMatrix(B);
do {
showMenu();
if (scanf("%d", &choice) != 1) {
while (getchar() != '\n');
choice = 0;
}
switch (choice) {
case 1:
addMatrices(A, B, R);
printf("\nMatrix A + B:\n");
printMatrix(R);
break;
case 2:
multiplyMatrices(A, B, R);
printf("\nMatrix A * B:\n");
printMatrix(R);
break;
case 3:
transposeMatrix(A, R);
printf("\nTranspose of A:\n");
printMatrix(R);
break;
case 4:
break;
default:
printf("Invalid.\n");
}
} while (choice != 4);
return 0;
}
2b
#include
#define DEPTH 2
#define ROWS 3
#define COLS 4
int main() {
int arr[DEPTH][ROWS][COLS];
int count = 1;
for (int i = 0; i < DEPTH; i++) {
for (int j = 0; j < ROWS; j++) {
for (int k = 0; k < COLS; k++) {
arr[i][j][k] = count++;
}
}
}
printf("Three Dimensional Array:\n");
for (int i = 0; i < DEPTH; i++) {
printf("--- Depth Layer %d ---\n", i);
for (int j = 0; j < ROWS; j++) {
for (int k = 0; k < COLS; k++) {
printf("%d\t", arr[i][j][k]);
}
printf("\n");
}
}
return 0;
}
Subscribe to:
Comments (Atom)