Dsa Manual SH
Dsa Manual SH
struct Day {
char* name; // Day name
int date; // Date
char* activity; // Activity description
};
void create() {
for (int i = 0; i < 7; i++) {
calendar[i] = (struct Day*)malloc(sizeof(struct Day));
calendar[i]->name = (char*)malloc(20);
calendar[i]->activity = (char*)malloc(100);
printf("Enter day name: ");
scanf("%s", calendar[i]->name);
printf("Enter date: ");
scanf("%d", &calendar[i]->date);
printf("Enter activity: ");
scanf(" %[^\n]", calendar[i]->activity);
printf("\n");
}
}
void read() {
printf("\nReading Calendar:\n");
for (int i = 0; i < 7; i++) {
printf("Day %d\n", i + 1);
printf("Day name: %s\n", calendar[i]->name);
printf("Date: %d\n", calendar[i]->date);
printf("Activity: %s\n\n", calendar[i]->activity);
}
}
void display() {
printf("\nWeekly Activity Details:\n");
for (int i = 0; i < 7; i++) {
printf("Day %d: %s - %d, Activity: %s\n", i + 1, calendar[i]->name,
calendar[i]->date, calendar[i]->activity);
}
}
int main() {
printf("Creating Calendar:\n");
create();
read();
printf("Displaying Calendar:\n");
display();
// Free memory
for (int i = 0; i < 7; i++) {
free(calendar[i]->name);
free(calendar[i]->activity);
free(calendar[i]);
}
return 0;
}
void stringmatch() {
while(str[c] !='\0') {
if(str[m] == pat[i]) {
i++;
m++;
if(pat[i] == '\0') {
flag = 1;
for(k = 0; rep[k] != '\0'; k++, j++) {
ans[j] = rep[k];
}
i = 0;
c = m;
}
} else {
ans[j] = str[c];
j++;
c++;
m = c;
i = 0;
}
}
ans[j] = '\0';
}
int main() {
printf("\nEnter the main string: ");
gets(str); // Use of gets() is unsafe, consider using fgets() for safety.
printf("\nEnter the pattern string: ");
gets(pat);
printf("\nEnter the replace string: ");
gets(rep);
stringmatch();
if(flag == 1) {
printf("\nResultant string is: %s", ans);
} else {
printf("\nPattern string is not found");
}
return 0;
}
3. Develop a menu driven Program in C for the following operations on
STACK of Integers (Array Implementation of Stack with maximum size
MAX) a. Push an Element on to Stack b. Pop an Element from Stack c.
Demonstrate how Stack can be used to check Palindrome d.
Demonstrate Overflow and Underflow situations on Stack e. Display
the status of Stack f. Exit Support the program with appropriate
functions for each of the above operations.
Code:
#include <stdio.h>
#define MAX 5
int stack[MAX], top = -1;
void push(int element);
int pop();
void checkPalindrome();
void display();
int main() {
int choice, element;
while (1) {
// Display menu
printf("\n~~~~~~Menu~~~~~~ :\n");
printf("1. Push an Element to Stack\n2. Pop an Element from
Stack\n3. Palindrome demo\n4. Display\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter an element to be pushed: ");
scanf("%d", &element);
push(element);
break;
case 2:
element = pop();
if (element != -1) printf("Popped element: %d\n", element);
break;
case 3:
checkPalindrome();
break;
case 4:
display();
break;
case 5:
return 0;
default:
printf("Invalid choice. Try again.\n");
}
}
}
// Push operation
void push(int element) {
if (top == MAX - 1)
printf("Stack is full, can't push %d\n", element); // Stack full check
else
stack[++top] = element;
}
// Pop operation
int pop() {
if (top == -1) {
printf("Stack is empty, can't pop.\n"); // Stack empty check
return -1; // Return -1 if empty
}
return stack[top--];
}
int main() {
char infix[MAX], postfix[MAX];
return 0;
}
5. Develop a Program in C for the following Stack Applications a.
Evaluation of Suffix expression with single digit operands and
operators: +, -, *, /, %, ^ b. Solving Tower of Hanoi problem with n
disks
Code:
a) #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
int pop() {
return s[top--];
}
int main() {
char postfix[90], symb;
int op1, op2, res;
// If the symbol is a digit, push the integer value onto the stack
if (isdigit(symb)) {
push(symb - '0'); // Converts char to int
}
else {
// Pop two operands from the stack
op2 = pop();
op1 = pop();
// The final result will be the only item left on the stack
res = pop();
printf("\nResult = %d\n", res);
return 0;
}
b) #include <stdio.h>
#define SIZE 5
// Insert an element
void enQueue(int element) {
if (isFull())
printf("\nQueue is full cannot push!!\n");
else {
rear = (rear + 1) % SIZE;
if (front == -1) front = 0; // If the queue is empty, initialize front
items[rear] = element;
printf("\nInserted -> %d\n", element);
}
}
// Remove an element
int deQueue() {
if (isEmpty()) {
printf("\nQueue is empty cannot pop!!\n");
return -1;
}
int element = items[front];
if (front == rear) front = rear = -1; // Reset if queue becomes empty
else front = (front + 1) % SIZE;
printf("\nDeleted element -> %d\n", element);
return element;
}
int i = front;
while (i != rear) {
printf("%d ", items[i]);
i = (i + 1) % SIZE; // Circular increment
}
printf("%d ", items[rear]); // Print the rear element
printf("\nRear -> %d\n", rear);
}
int main() {
int choice, x;
do {
printf("\n1: Insert\n2: Delete\n3: Display\n0: Exit\nEnter choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter element: ");
scanf("%d", &x);
enQueue(x);
break;
case 2:
deQueue();
break;
case 3:
display();
break;
case 0:
printf("\nExiting.\n");
break;
default:
printf("\nInvalid choice\n");
}
} while (choice != 0);
return 0;
}
7. Develop a menu driven Program in C for the following operations on
Singly Linked List (SLL) of Student Data with the fields: USN, Name,
Programme, Sem, PhNo
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion / Deletion at End of SLL
d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
e. Exit
code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void display() {
if (!head) {
printf("The list is empty.\n");
return;
}
Student *temp = head;
int count = 0;
while (temp) {
printf("USN: %s, Name: %s, Programme: %s, Sem: %d, PhNo:
%s\n",temp->usn, temp->name, temp->prog, temp->sem, temp->phno);
temp = temp->next;
count++;
}
printf("Total Students: %d\n", count);
}
void insertEnd() {
Student *newNode = (Student *)malloc(sizeof(Student));
printf("Enter USN, Name, Programme, Sem, PhNo: \n");
scanf("%s %s %s %d %s", newNode->usn, newNode->name, newNode-
>prog, &newNode->sem, newNode->phno);
newNode->next = NULL;
if (!head) head = newNode;
else {
Student *temp = head;
while (temp->next) temp = temp->next;
temp->next = newNode;
}
}
void deleteFront() {
if (head) {
Student *temp = head;
head = head->next;
free(temp);
} else printf("List is empty\n");
}
void deleteEnd() {
if (!head) printf("List is empty\n");
else if (!head->next) {
free(head);
head = NULL;
} else {
Student *temp = head;
while (temp->next->next) temp = temp->next;
free(temp->next);
temp->next = NULL;
}
}
int main() {
int choice;
do {
printf("\n1. Create SLL of students Nodes\n2. Display\n3. Insert
End\n4. Delete Front\n5. Delete End\n6. Exit\nEnter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: createSLL(); break;
case 2: display(); break;
case 3: insertEnd(); break;
case 4: deleteFront(); break;
case 5: deleteEnd(); break;
}
} while (choice != 6);
return 0;
}
8. Develop a menu driven Program in C for the following operations on
Doubly Linked List (DLL) of Employee Data with the fields: SSN, Name,
Dept, Designation, Sal, PhNo
a. Create a DLL of N Employees Data by using end insertion.
b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue.
f. Exit
code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
switch (ch) {
case 1:
insertFront();
break;
case 2:
deleteFront();
break;
case 3:
insertEnd();
break;
case 4:
deleteEnd();
break;
case 5:
displayDLL();
break;
case 6:
return;
default:
printf("Invalid choice! Exiting demo.\n");
return;
}
}
}
int main() {
menu();
return 0;
}
9. Develop a Program in C for the following operationson Singly Circular
Linked List (SCLL) with header nodes
a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-
4yz5+3x3yz+2xy5z-2xyz3
b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store
the result in POLYSUM(x,y,z) Support the program with appropriate
functions for each of the above operations
code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct node {
int coef, xexp, yexp, zexp;
struct node* link;
};
NODE getnode() {
NODE x = (NODE)malloc(sizeof(struct node));
if (!x) exit(1);
return x;
}
NODE attach(NODE head, int coef, int xexp, int yexp, int zexp) {
NODE temp = getnode();
temp->coef = coef;
temp->xexp = xexp;
temp->yexp = yexp;
temp->zexp = zexp;
NODE cur = head;
while (cur->link != head) cur = cur->link;
cur->link = temp;
temp->link = head;
return head;
}
int main() {
NODE head1 = getnode(), head2 = getnode(), head3 = getnode();
head1->link = head1; head2->link = head2; head3->link = head3;
int ch;
while (1) {
printf("\n~~~Menu~~~\n1. Represent and Evaluate a Polynomial\n2.
Find the sum of two polynomials\nEnter your choice: ");
scanf("%d", &ch);
if (ch == 1) {
printf("\nPolynomial evaluation\n");
head1 = read_poly(head1);
printf("Polynomial: "); display(head1);
printf("Result: %d\n", evaluate(head1));
}
else if (ch == 2) {
printf("\nEnter POLY1(x, y, z):\n");
head1 = read_poly(head1);
printf("POLY1: "); display(head1);
return root;
}
// Main function
int main() {
NODE root = NULL;
int choice, key, n, i;
do {
// Display menu options
printf("\n~~~~BST MENU~~~~\n");
printf("1. Create a BST\n");
printf("2. Search\n");
printf("3. BST Traversals:\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: // Create BST
printf("Enter the number of elements: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter The value: ");
scanf("%d", &key);
root = insert(root, key);
}
break;
case 2: // Search
printf("Enter the value to search: ");
scanf("%d", &key);
NODE found = search(root, key);
if (found != NULL) {
printf("Element %d found in the BST.\n", key);
} else {
printf("Element %d not found in the BST.\n", key);
}
break;
case 3: // Traversals
printf("The Preorder display: ");
preorder(root);
printf("\nThe Inorder display: ");
inorder(root);
printf("\nThe Postorder display: ");
postorder(root);
printf("\n");
break;
case 4: // Exit
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 4);
return 0;
}
11. Develop a Program in C for the following operations on Graph(G) of
Cities a. Create a Graph of N cities using Adjacency Matrix. b. Print all the
nodes reachable from a given starting node in a digraph using DFS/BFS
method
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 20
void resetVisited(int n) {
for (int i = 0; i < n; i++) visited[i] = 0;
}
int main() {
int n, start, choice;
while (1) {
printf("\n==>1. BFS: Print all nodes reachable from a given starting
node\n");
printf("==>2. DFS: Print all nodes reachable from a given starting
node\n");
printf("==>3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
resetVisited(n);
BFS(start, n);
for (int i = 0; i < n; i++) if (!visited[i]) printf("The vertex that is not
reachable is %d\n", i + 1);
break;
case 2:
resetVisited(n);
printf("Nodes reachable from starting vertex %d are: ", start + 1);
DFS(start, n);
printf("\n");
break;
case 3:
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}
12. Given a File of N employee records with a set K of Keys (4-digit) which
uniquely determine the records in file F. Assume that file F is maintained in
memory by a Hash Table (HT) of m memory locations with L as the set of
memory addresses (2-digit) of locations in HT. Let the keys in K and
addresses in L are Integers. Develop a Program in C that uses Hash
function H: K →L as H(K)=K mod m (remainder method), and implement
hashing technique to map a given key K to the address space L. Resolve the
collision (if any) using linear probing.
Code:
#include <stdio.h>
#include <stdlib.h>
// Function to insert a key into the hash table using linear probing
void insert(int key, int m) {
int index = hashFunction(key, m);
int originalIndex = index;
int main() {
int n, key, m;
// Take the key values for N employee records and insert into hash table
printf("Enter the four digit key values (K) for %d Employee Records:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &key);
insert(key, m);
}
return 0;
}
11) alter
#include <stdio.h>
#include <stdlib.h>
#define MAX 20
int graph[MAX][MAX], visited[MAX];
printf("BFS: Nodes reachable from starting vertex %d: ", start + 1);
while (front < rear) {
int v = queue[front++];
printf("%d ", v + 1);
for (int i = 0; i < n; i++) {
if (graph[v][i] && !visited[i]) {
visited[i] = 1;
queue[rear++] = i;
}
}
}
printf("\n");
}
void resetVisited(int n) {
for (int i = 0; i < n; i++) visited[i] = 0;
}
int main() {
int n, start, choice;
resetVisited(n);
switch (choice) {
case 1:
BFS(start, n);
break;
case 2:
printf("DFS: Nodes reachable from starting vertex %d: ", start + 1);
DFS(start, n);
printf("\n");
break;
case 3:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}