Dsa Codes
Dsa Codes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct calElement {
char *day;
int date;
char *activity;
};
struct calElement* create() {
return (struct calElement *)malloc(sizeof(struct calElement) * 7);
}
void read(struct calElement *calendar) {
char day[10], activity[100];
for (int i = 0; i < 7; i++) {
printf("Enter the day: ");
scanf("%s", day);
calendar[i].day = strdup(day);
printf("Enter the date: ");
scanf("%d", &calendar[i].date);
getchar();
printf("Enter the activity: ");
fgets(activity, sizeof(activity), stdin);
activity[strcspn(activity, "\n")] = 0;
calendar[i].activity = strdup(activity);
}
}
void display(struct calElement *calendar) {
printf("\nYour Calendar\n");
printf("Day\t\tDate\t\tActivity\n");
for (int i = 0; i < 7; i++) {
printf("%-10s\t%-10d\t%s\n", calendar[i].day, calendar[i].date,
calendar[i].activity);
}
}int main() {
struct calElement *calendar = create();
read(calendar);
display(calendar);
return 0;
}
#include <stdio.h>
void read();
void replace();
void display();
int main() {
read();
replace();
display();
return 0;
}
void read() {
printf("Enter the MAIN string: \n");
scanf(" %[^\n]s", STR); // Added space before % to consume any leftover newline
printf("Enter a PATTERN string: \n");
scanf("%s", PAT);
printf("Enter a REPLACE string: \n");
scanf("%s", REP);
}
void replace() {
s = p = a = 0;
while (STR[s] != '\0') {
if (STR[s] == PAT[p]) {
p++;
s++;
if (PAT[p] == '\0') {
flag = 1;
for (r = 0; REP[r] != '\0'; r++, a++) {
ANS[a] = REP[r];
}
p = 0;
}
} else {
ANS[a++] = STR[s++];
p = 0;
}
}
ANS[a] = '\0';
}
void display() {
if (flag == 0) {
printf("Pattern not found!\n");
} else {
printf("\nThe RESULTANT string is:\n%s\n", ANS);
}
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 5
int IsFull() {
return top >= MAX - 1;
}
int IsEmpty() {
return top == -1;
}
void pop() {
if (IsEmpty()) {
printf("Stack Underflow\n");
return;
}
item = s[top--];
}
void display() {
if (IsEmpty()) {
printf("Stack is Empty\n");
return;
}
printf("\nThe elements of the stack are:\n");
for (int i = top; i >= 0; i--) {
printf("%d\n", s[i]);
}
}
void check_pal() {
if (IsEmpty()) {
printf("Stack is empty\n");
return;
}
int main() {
int ch;
do {
printf("\n1. Push\n2. Pop\n3. Display\n4. Check Palindrome\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter the element to be inserted: ");
scanf("%d", &item);
push(item);
break;
case 2:
if (IsEmpty()) {
printf("Stack Underflow\n");
} else {
pop();
printf("The item deleted is %d\n", item);
}
break;
case 3:
display();
break;
case 4:
check_pal();
break;
case 5:
printf("Program Terminated\n");
exit(0);
default:
printf("Invalid choice\n");
}
} while (ch != 5);
return 0;
}
#include <stdio.h>
#include <ctype.h> // for isalnum() function
#define SIZE 20
char s[SIZE];
int top = -1;
char pop() {
if (top == -1) {
printf("Stack underflow!\n");
return -1; // Return an error value when stack is empty
}
return s[top--];
}
int main() {
char infix[50], postfix[50];
return 0;
}
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
void main() {
int s[20];
int res;
int op1;
int op2;
int top;
int i;
char postfix[20];
char symbol;
#include<stdio.h>
void towers(int, char, char, char);
void main()
{
int num;
printf("Enter the number of disks : ");
scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
towers(num, 'A', 'B', 'C');
}
void towers(int n, char source, char temp, char dest)
{
if (n == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", source, dest);
return;
}
towers(n - 1, source, dest, temp);
printf("\n Move disk %d from peg %c to peg %c", n, source, dest);
towers(n - 1, temp, source, dest);
}
#include <stdio.h>
#include <stdlib.h>
#define QSIZE 4
void insert() {
if (count == QSIZE) {
printf("Queue Overflow\n");
return;
}
r = (r + 1) % QSIZE;
q[r] = item;
count++;
}
void del() {
if (count == 0) {
printf("Queue Underflow\n");
return;
}
printf("The item deleted is: %d\n", q[f]);
f = (f + 1) % QSIZE;
count--;
}
int main() {
int choice;
do {
printf("***********************\n");
printf("Circular Queue Operations\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Quit\n");
printf("Enter your choice:\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the item to be inserted:\n");
scanf("%d", &item);
insert();
break;
case 2:
del();
break;
case 3:
display(f);
break;
case 4:
printf("Program Terminated\n");
exit(0);
default:
printf("Invalid choice\n");
}
} while (choice != 4);
return 0;
}
code 7 (singly)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int count = 0;
struct node {
int sem;
long int phno;
char name[20], branch[10], usn[20];
struct node *next;
} *first = NULL, *last = NULL, *temp = NULL, *temp1 = NULL;
void create() {
int sem;
long int phno;
char name[20], branch[10], usn[20];
temp = (struct node *)malloc(sizeof(struct node));
if (temp == NULL) {
printf("Memory allocation failed\n");
return;
}
printf("\nEnter USN, NAME, BRANCH, SEMESTER, PHNUM of student: ");
scanf("%s %s %s %d %ld", usn, name, branch, &sem, &phno);
strcpy(temp->usn, usn);
strcpy(temp->name, name);
strcpy(temp->branch, branch);
temp->sem = sem;
temp->phno = phno;
temp->next = NULL;
count++;
}
void insert_atfirst() {
create();
if (first == NULL) {
first = temp;
last = first;
} else {
temp->next = first;
first = temp;
}
}
void insert_atlast() {
create();
if (first == NULL) {
first = temp;
last = first;
} else {
last->next = temp;
last = temp;
}
}
void display() {
temp1 = first;
if (temp1 == NULL) {
printf("List is empty\n");
return;
}
printf("\nLinked list elements from beginning:\n");
printf("USN\t\tNAME\t\tBRANCH\tSEMESTER\tPH.NUM\n");
while (temp1 != NULL) {
printf("%s\t\t%s\t\t%s\t%d\t\t%ld\n", temp1->usn, temp1->name, temp1-
>branch, temp1->sem, temp1->phno);
temp1 = temp1->next;
}
printf("Number of students = %d\n", count);
}
void delete_end() {
if (first == NULL) {
printf("List is empty\n");
return;
}
if (first->next == NULL) {
printf("Deleted: %s %s %s %d %ld\n", first->usn, first->name, first-
>branch, first->sem, first->phno);
free(first);
first = last = NULL;
} else {
temp = first;
while (temp->next != last) {
temp = temp->next;
}
printf("Deleted: %s %s %s %d %ld\n", last->usn, last->name, last->branch,
last->sem, last->phno);
free(last);
temp->next = NULL;
last = temp;
}
count--;
}
void delete_front() {
if (first == NULL) {
printf("List is empty\n");
return;
}
temp = first;
printf("Deleted: %s %s %s %d %ld\n", temp->usn, temp->name, temp->branch, temp-
>sem, temp->phno);
if (first->next == NULL) {
free(first);
first = last = NULL;
} else {
first = first->next;
free(temp);
}
count--;
}
int main() {
int ch, n, i;
printf("-----------------MENU----------------------\n");
printf("1 - Create a SLL of n Students\n");
printf("2 - Display from beginning\n");
printf("3 - Insert at end\n");
printf("4 - Delete at end\n");
printf("5 - Insert at beginning\n");
printf("6 - Delete at beginning\n");
printf("7 - Exit\n");
printf("-------------------------------------------\n");
while (1) {
printf("\nEnter choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter number of students: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
insert_atfirst();
}
break;
case 2:
display();
break;
case 3:
insert_atlast();
break;
case 4:
delete_end();
break;
case 5:
insert_atfirst();
break;
case 6:
delete_front();
break;
case 7:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
code 8 (doubly)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int count = 0;
struct node {
struct node *prev;
int ssn;
long int phno;
float sal;
char name[20], dept[10], desg[20];
struct node *next;
} *first = NULL, *temp = NULL, *last = NULL;
void create() {
int ssn;
long int phno;
float sal;
char name[20], dept[10], desg[20];
temp = (struct node *)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\nEnter SSN, Name, Department, Designation, Salary, and Phone Number:
");
scanf("%d %s %s %s %f %ld", &ssn, name, dept, desg, &sal, &phno);
temp->ssn = ssn;
strcpy(temp->name, name);
strcpy(temp->dept, dept);
strcpy(temp->desg, desg);
temp->sal = sal;
temp->phno = phno;
count++;
}
void display() {
temp = first;
if (temp == NULL) {
printf("List is Empty\n");
return;
}
printf("\nLinked list elements from beginning:\n");
while (temp != NULL) {
printf("%d %s %s %s %.2f %ld\n", temp->ssn, temp->name, temp->dept, temp-
>desg, temp->sal, temp->phno);
temp = temp->next;
}
printf("Number of employees = %d\n", count);
}
void insert_front() {
if (first == NULL) {
create();
first = temp;
last = first;
} else {
create();
temp->next = first;
first->prev = temp;
first = temp;
}
}
void delete_front() {
struct node *cur = first;
if (first == NULL) {
printf("List is Empty\n");
return;
}
if (first->next == NULL) {
printf("%d %s %s %s %.2f %ld\n", first->ssn, first->name, first->dept,
first->desg, first->sal, first->phno);
free(first);
first = NULL;
} else {
first = first->next;
printf("%d %s %s %s %.2f %ld\n", cur->ssn, cur->name, cur->dept, cur->desg,
cur->sal, cur->phno);
free(cur);
first->prev = NULL;
}
count--;
}
void insert_rear() {
if (first == NULL) {
create();
first = temp;
last = first;
} else {
create();
last->next = temp;
temp->prev = last;
last = temp;
}
}
void delete_rear() {
if (first == NULL) {
printf("List is Empty\n");
return;
}
if (first->next == NULL) {
printf("%d %s %s %s %.2f %ld\n", first->ssn, first->name, first->dept,
first->desg, first->sal, first->phno);
free(first);
first = NULL;
} else {
temp = last->prev;
temp->next = NULL;
printf("%d %s %s %s %.2f %ld\n", last->ssn, last->name, last->dept, last-
>desg, last->sal, last->phno);
free(last);
last = temp;
}
count--;
}
void main() {
int ch, n, i;
printf("-----------------MENU--------------------\n");
printf("\n1 - Create a DLL of n employees");
printf("\n2 - Display from beginning");
printf("\n3 - Insert at front end");
printf("\n4 - Delete at front end");
printf("\n5 - Insert at rear end");
printf("\n6 - Delete at rear end");
printf("\n7 - Exit\n");
printf("------------------------------------------\n");
while (1) {
printf("\nEnter Choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("\nEnter number of employees: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
insert_rear();
break;
case 2:
display();
break;
case 3:
insert_front();
break;
case 4:
delete_front();
break;
case 5:
insert_rear();
break;
case 6:
delete_rear();
break;
case 7:
exit(0);
default:
printf("Wrong Choice\n");
}
}
}
code 9 (scll)
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
struct node {
int cf, px, py, pz, flag;
struct node *link;
};
typedef struct node NODE;
NODE* getnode() {
NODE *x;
x = (NODE*)malloc(sizeof(NODE));
if (x == NULL) {
printf("Insufficient memory\n");
exit(0);
}
return x;
}
void main() {
NODE *h1, *h2, *h3, *eval;
int ch;
h1 = getnode();
h2 = getnode();
h3 = getnode();
eval = getnode();
h1->link = h1;
h2->link = h2;
h3->link = h3;
eval->link = eval;
while (1) {
printf("\n1. Evaluate polynomial\n2. Add two polynomials\n3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("\nEnter polynomial to evaluate:\n");
eval = read_poly(eval);
display(eval);
evaluate(eval);
break;
case 2:
printf("\nEnter the first polynomial:\n");
h1 = read_poly(h1);
printf("\nEnter the second polynomial:\n");
h2 = read_poly(h2);
h3 = add_poly(h1, h2, h3);
printf("\nFirst polynomial:\n");
display(h1);
printf("\nSecond polynomial:\n");
display(h2);
printf("\nSum of the polynomials:\n");
display(h3);
break;
case 3:
exit(0);
default:
printf("\nInvalid choice\n");
}
}
}
#include <stdio.h>
#include <stdlib.h>
struct BST
{
int data;
struct BST *left;
struct BST *right;
};
typedef struct BST NODE;
NODE* createtree(NODE *root, int data)
{
if (root == NULL)
{
NODE *temp;
temp = (NODE*) malloc (sizeof(NODE));
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
if (data < (root->data))
root->left = createtree(root->left, data);
else if (data > root->data)
root -> right = createtree(root->right, data);
return root;
}
void inorder(NODE *root)
{
if(root != NULL)
{
inorder(root->left);
printf("%d\t", root->data);
inorder(root->right);
}
}
void preorder(NODE *root)
{
if(root != NULL)
{
printf("%d\t", root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(NODE *root)
{
if(root != NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d\t", root->data);
}
}
NODE *search(NODE *root, int data)
{
if(root == NULL)
printf("\nElement not found");
else if(data < root->data)
root->left = search(root->left, data);
else if(data > root->data)
root->right = search(root->right, data);
else
printf("\nElement found is: %d", root->data);
return root;
}
void main()
{
int data, ch, i, n;
NODE *root = NULL;
while (1)
{
printf("\n1.Creation of Binary Search Tree");
printf("\n2.Inorder\n3.Preorder\n4.Postorder\n5.Search\n6.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1: printf("\nEnter N value: " );
scanf("%d", &n);
printf("\nEnter the values to create BST like(6,9,5,2,8,15,24,14,7,8,5,2)\n");
for(i=0; i<n; i++)
{
scanf("%d", &data);
root = createtree(root, data);
}
break;
case 2: printf("\nInorder Traversal: \n");
inorder(root);
break;
case 3: printf("\nPreorder Traversal: \n");
preorder(root);
break;
case 4: printf("\nPostorder Traversal: \n");
postorder(root);
break;
case 5: printf("\nEnter the element to Search: ");
scanf("%d", &data);
root=search(root, data);
break;
case 6: exit(0);
default: printf("\nWrong Option");
break;
}
}
}
#include<stdio.h>
#include<stdlib.h>
void read_data() {
int i, j;
printf("Enter the number of nodes:\n");
scanf("%d", &n);
printf("Enter the adjacency matrix:\n");
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
}
void BFS() {
int f = 0, r = 0, q[20], i, j;
q[r] = source;
visited1[source] = 1;
while(f <= r) {
i = q[f++];
printf("--%d--", i);
for(j = 0; j < n; j++) {
if(a[i][j] == 1 && visited1[j] == 0) {
visited1[j] = 1;
q[++r] = j;
}
}
}
}
int main() {
int i, choice, a, *count = &a;
read_data();
printf("\t\t**ADJACENCY MATRIX FOR CITIES HAS BEEN CREATED SUCCESSFULLY**\n");
while(1) {
printf("\n1. BFS\n2. DFS\n3. Exit");
printf("\nEnter Your Choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
for(i = 0; i < n; i++)
visited1[i] = 0;
printf("Enter the source vertex between 0 to %d\n", n-1);
scanf("%d", &source);
BFS();
print_data(visited1);
break;
case 2:
for(i = 0; i < n; i++)
visited2[i] = 0;
printf("Enter the source vertex between 0 to %d\n", n-1);
scanf("%d", &source);
a = 0;
DFS(source, count);
print_data(visited2);
if(*count == n-1)
printf("Graph is connected\n");
else
printf("Graph is not connected\n");
break;
case 3:
exit(0);
default:
printf("\nEnter a valid choice\n");
}
}
}
code 12
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
struct employee
{
int id;
char name[15];
};
typedef struct employee EMP;
EMP emp[MAX];
int a[MAX];
int create(int num)
{
int key;
key = num % 100;
return key;
}
int getemp(EMP emp[],int key)
{
printf("\nEnter emp id: ");
scanf("%d",&emp[key].id);
printf("\nEnter emp name: ");
return key;
}
void display()
{
int i, ch;
while(1)
{
printf("\n1.Display ALL\n2.Filtered Display");
printf("\nEnter the choice: ");
scanf("%d",&ch);
if(ch == 1)
{
printf("\nThe hash table is:\n");
printf("\nHTKey\tEmpID\tEmpName");
for(i=0; i<MAX; i++)
printf("\n%d\t%d\t%s", i, emp[i].id, emp[i].name);
}
else if (ch==2)
{
printf("\nThe hash table is:\n");
printf("\nHTKey\tEmpID\tEmpName");
for(i=0; i<MAX; i++)
if(a[i] != -1)
{
printf("\n%d\t%d\t%s", i, emp[i].id, emp[i].name);
continue;
}
}
else
exit(0);
}
}
void linear_prob(int key, int num)
{
int flag, i, count = 0; flag = 0;
if(a[key] == -1)
a[key]=getemp(emp, key);
else
{
printf("\nCollision Detected...!!!\n");
i = 0;
while(i < MAX)
{
if (a[i] != -1)
count++;
else
i++;
}
printf("\nCollision avoided successfully using LINEAR PROBING\n");
if(count == MAX)
{
printf("\n Hash table is full");
display(emp);
exit(1);
}
for(i=key; i<MAX; i++)
{
if(a[i] == -1)
{
a[i] = num;
flag = 1;
break;
}
}
i = 0;
while((i < key) && (flag == 0))
{
if(a[i] == -1)
{
a[i] = num;
flag=1;
break;
}
i++;
}
}
}
void main()
{
int num, key, i;
int ans = 1;
printf("\nCollision handling by linear probing: ");
for (i=0; i < MAX; i++)
a[i] = -1;
do
{
printf("\nEnter the data: ");
scanf("%d", &num);
key=create(num);
linear_prob(key,num);
printf("\nDo you wish to continue? (1/0): ");
scanf("%d",&ans);
}while(ans);
display(emp);
}