0% found this document useful (0 votes)
9 views34 pages

DSA Codes

The document contains multiple C code implementations for various data structures and algorithms, including linked lists for student and employee records, polynomial operations, binary search trees, and graph traversal methods (BFS and DFS). Each section provides a menu-driven interface for users to interact with the data structures, allowing for operations such as creation, display, insertion, deletion, and traversal. The code is structured with functions for each operation, demonstrating fundamental programming concepts in C.

Uploaded by

varshithm369
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views34 pages

DSA Codes

The document contains multiple C code implementations for various data structures and algorithms, including linked lists for student and employee records, polynomial operations, binary search trees, and graph traversal methods (BFS and DFS). Each section provides a menu-driven interface for users to interact with the data structures, allowing for operations such as creation, display, insertion, deletion, and traversal. The code is structured with functions for each operation, demonstrating fundamental programming concepts in C.

Uploaded by

varshithm369
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

7 CODE

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct student

char usn[15];

char name[20];

char branch[10];

int sem;

char phno[20];

struct student *link;

};

typedef struct student node;

node *start = NULL;

void create();

void display();

void insert_end();

void del_front();
int main()

int choice;

while (1)

printf("\n--- MAIN MENU ---\n"

"1. Create List\n"

"2. Display List\n"

"3. Insert at End\n"

"4. Delete from Front\n"

"5. Exit\n"

"Enter your choice: ");

scanf("%d", &choice);

switch (choice)

case 1: create(); break;

case 2: display(); break;

case 3: insert_end(); break;

case 4: del_front(); break;

case 5: exit(0);

default: printf("Invalid choice\n");

return 0;
}

void create()

int i, n;

node *p;

printf("Enter number of students: ");

scanf("%d", &n);

for (i = 0; i < n; i++)

p = (node *)malloc(sizeof(node));

printf("Enter USN Name Branch Sem Phone:\n");

scanf("%s %s %s %d %s",p->usn, p->name, p->branch, &p->sem, p-


>phno);

p->link = start;

start = p;

void display()

node *t = start;

int count = 0;

if (start == NULL)
{

printf("List is empty\n");

return;

while (t != NULL)

printf("%s %s %s %d %s\n",t->usn, t->name, t->branch, t->sem, t-


>phno);

t = t->link;

count++;

printf("Total nodes = %d\n", count);

void insert_end()

node *p, *r;

p = (node *)malloc(sizeof(node));

printf("Enter USN Name Branch Sem Phone:\n");

scanf("%s %s %s %d %s",p->usn, p->name, p->branch, &p->sem, p-


>phno);

p->link = NULL;

if (start == NULL)

start = p;
return;

r = start;

while (r->link != NULL)

r = r->link;

r->link = p;

void del_front()

node *q;

if (start == NULL)

printf("List is empty\n");

return;

q = start;

printf("Deleted USN: %s\n", q->usn);

start = start->link;

free(q);

}
8 CODE

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct emp

int ssn;

char name[40],dept[40],desig[40], phno[20];

long int sal;

struct emp *llink;

struct emp *rlink;

};

typedef struct emp node;

node *start = NULL;

void create();

void display();

void insert_front();

void del_front();

int main()
{

int ch;

while (1)

printf("\n--- MAIN MENU ---\n"

"1. Create\n"

"2. Display\n"

"3. Insert Front\n"

"4. Delete Front\n"

"5. Exit\n"

"Enter your choice: ");

scanf("%d", &ch);

switch (ch)

case 1: create(); break;

case 2: display(); break;

case 3: insert_front(); break;

case 4: del_front(); break;

case 5: exit(0);

default: printf("Invalid choice\n");

return 0;

}
void create()

int i, n;

node *p, *t;

printf("Enter number of employees: ");

scanf("%d", &n);

for (i = 0; i < n; i++)

p = (node *)malloc(sizeof(node));

printf("Enter SSN Name Dept Designation Salary Phone:\n");

scanf("%d %s %s %s %ld %s",&p->ssn, p->name, p->dept, p->desig,


&p->sal, p->phno);

p->llink = p->rlink = NULL;

if (start == NULL)

start = p;

else

t = start;

while (t->rlink != NULL)

t = t->rlink;

t->rlink = p;
p->llink = t;

void display()

node *r = start;

if (start == NULL)

printf("List is empty\n");

return;

while (r != NULL)

printf("%d %s %s %s %ld %s\n",

r->ssn, r->name, r->dept, r->desig, r->sal, r->phno);

r = r->rlink;

void insert_front()

node *p = (node *)malloc(sizeof(node));


printf("Enter SSN Name Dept Designation Salary Phone:\n");

scanf("%d %s %s %s %ld %s",&p->ssn, p->name, p->dept, p->desig,


&p->sal, p->phno);

p->llink = NULL;

p->rlink = start;

if (start != NULL)

start->llink = p;

start = p;

void del_front()

node *q;

if (start == NULL)

printf("List is empty\n");

return;

q = start;

printf("Deleted SSN: %d\n", q->ssn);

start = start->rlink;

if (start != NULL)

start->llink = NULL;

free(q);

}
9 CODE

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

struct node

int cf, px, py, pz;

int flag;

struct node *link;

};

typedef struct node NODE;

NODE* getnode()

NODE *x = (NODE *)malloc(sizeof(NODE));

if (x == NULL)

printf("Insufficient memory\n");

exit(0);

x->flag = 0;

return x;
}

void display(NODE *head)

NODE *temp;

if (head->link == head)

printf("Polynomial does not exist\n");

return;

temp = head->link;

while (temp != head)

printf("%d x^%d y^%d z^%d",

temp->cf, temp->px, temp->py, temp->pz);

if (temp->link != head)

printf(" + ");

temp = temp->link;

printf("\n");

NODE* insert_rear(int cf, int x, int y, int z, NODE *head)

{
NODE *temp = getnode();

NODE *cur;

temp->cf = cf;

temp->px = x;

temp->py = y;

temp->pz = z;

cur = head->link;

while (cur->link != head)

cur = cur->link;

cur->link = temp;

temp->link = head;

return head;

NODE* read_poly(NODE *head)

int cf, px, py, pz, ch;

do

printf("Enter coefficient: ");

scanf("%d", &cf);

printf("Enter x y z powers: ");

scanf("%d %d %d", &px, &py, &pz);

head = insert_rear(cf, px, py, pz, head);


printf("Press 1 to continue, 0 to stop: ");

scanf("%d", &ch);

} while (ch != 0);

return head;

NODE* add_poly(NODE *h1, NODE *h2, NODE *h3)

NODE *p1 = h1->link, *p2;

int cf;

while (p1 != h1)

p2 = h2->link;

while (p2 != h2)

if (p1->px == p2->px &&

p1->py == p2->py &&

p1->pz == p2->pz)

cf = p1->cf + p2->cf;

p2->flag = 1;

if (cf != 0)

h3 = insert_rear(cf, p1->px, p1->py, p1->pz, h3);

break;
}

p2 = p2->link;

if (p2 == h2)

h3 = insert_rear(p1->cf, p1->px, p1->py, p1->pz, h3);

p1 = p1->link;

p2 = h2->link;

while (p2 != h2)

if (p2->flag == 0)

h3 = insert_rear(p2->cf, p2->px, p2->py, p2->pz, h3);

p2 = p2->link;

return h3;

void evaluate(NODE *head)

NODE *p = head->link;

int x, y, z;

float result = 0.0;

printf("Enter x y z values: ");


scanf("%d %d %d", &x, &y, &z);

while (p != head)

result += p->cf *

pow(x, p->px) *

pow(y, p->py) *

pow(z, p->pz);

p = p->link;

printf("Polynomial result = %.2f\n", result);

int main()

NODE *h1, *h2, *h3;

int ch;

h1 = getnode();

h2 = getnode();

h3 = getnode();

h1->link = h1;

h2->link = h2;

h3->link = h3;

while (1)

{
printf("\n1. Evaluate Polynomial\n"

"2. Add Two Polynomials\n"

"3. Exit\n"

"Enter your choice: ");

scanf("%d", &ch);

switch (ch)

case 1:

h1->link = h1;

printf("Enter polynomial:\n");

h1 = read_poly(h1);

display(h1);

evaluate(h1);

break;

case 2:

h1->link = h1;

h2->link = h2;

h3->link = h3;

printf("Enter first polynomial:\n");

h1 = read_poly(h1);

printf("Enter second polynomial:\n");

h2 = read_poly(h2);

h3 = add_poly(h1, h2, h3);

printf("First polynomial: ");


display(h1);

printf("Second polynomial: ");

display(h2);

printf("Sum polynomial: ");

display(h3);

break;

case 3:

exit(0);

default:

printf("Invalid choice\n");

return 0;

}
10 CODE

#include <stdio.h>

#include <stdlib.h>

struct BST

int data;

struct BST *left;

struct BST *right;

};

typedef struct BST NODE;

NODE* createtree(NODE *node, int data)

if (node == NULL)

NODE *temp = (NODE *)malloc(sizeof(NODE));

temp->data = data;

temp->left = temp->right = NULL;

return temp;

if (data < node->data)

node->left = createtree(node->left, data);


else if (data > node->data)

node->right = createtree(node->right, data);

return node;

NODE* search(NODE *node, int data)

if (node == NULL)

printf("Element not found\n");

return NULL;

if (data < node->data)

return search(node->left, data);

else if (data > node->data)

return search(node->right, data);

else

printf("Element found: %d\n", node->data);

return node;

void inorder(NODE *node)


{

if (node != NULL)

inorder(node->left);

printf("%d ", node->data);

inorder(node->right);

void preorder(NODE *node)

if (node != NULL)

printf("%d ", node->data);

preorder(node->left);

preorder(node->right);

void postorder(NODE *node)

if (node != NULL)

postorder(node->left);
postorder(node->right);

printf("%d ", node->data);

int main()

int ch, data, n, i;

NODE *root = NULL;

while (1)

printf("\n--- BST MENU ---\n1. Insert\n2. Inorder Traversal\n3.


Preorder Traversal\n4. Postorder Traversal\n5. Search\n6. Exit\nEnter
your choice: ");

scanf("%d", &ch);

switch (ch)

case 1:

printf("Enter number of nodes: ");

scanf("%d", &n);

printf("Enter the elements:\n");

for (i = 0; i < n; i++)

scanf("%d", &data);

root = createtree(root, data);


}

break;

case 2:

printf("Inorder Traversal:\n");

inorder(root);

printf("\n");

break;

case 3:

printf("Preorder Traversal:\n");

preorder(root);

printf("\n");

break;

case 4:

printf("Postorder Traversal:\n");

postorder(root);

printf("\n");

break;

case 5:

printf("Enter element to search: ");

scanf("%d", &data);

search(root, data);

break;

case 6:
exit(0);

default:

printf("Invalid choice\n");

return 0;

}
1 1 CODE

#include <stdio.h>

#include <stdlib.h>

int a[10][10], n, m, i, j, source;

int visited[10], s[10], b[10];

void create()

printf("Enter the number of vertices of the digraph: ");

scanf("%d", &n);

printf("Enter the adjacency matrix:\n");

for (i = 1; i <= n; i++)

for (j = 1; j <= n; j++)

scanf("%d", &a[i][j]);

void bfs()

int q[10], u, front = 0, rear = -1;

for (i = 1; i <= n; i++)

visited[i] = 0;

printf("Enter the source vertex: ");


scanf("%d", &source);

q[++rear] = source;

visited[source] = 1;

printf("Reachable vertices:\n");

while (front <= rear)

u = q[front++];

for (i = 1; i <= n; i++)

if (a[u][i] == 1 && visited[i] == 0)

q[++rear] = i;

visited[i] = 1;

printf("%d ", i);

printf("\n");

void dfs(int source)

int v;
b[source] = 1;

for (v = 1; v <= n; v++)

if (a[source][v] == 1 && b[v] == 0)

printf("%d -> %d\n", source, v);

dfs(v);

int main()

int ch;

while (1)

printf("\n--- GRAPH MENU ---\n"

"1. Create Graph\n"

"2. BFS\n"

"3. Check Graph Connected (DFS)\n"

"4. Exit\n"

"Enter your choice: ");

scanf("%d", &ch);

switch (ch)
{

case 1:

create();

break;

case 2:

bfs();

for (i = 1; i <= n; i++)

if (visited[i] == 0)

printf("Vertex not reachable: %d\n", i);

break;

case 3:

for (i = 1; i <= n; i++)

b[i] = 0;

printf("Enter the source vertex: ");

scanf("%d", &source);

dfs(source);

m = 1;

for (i = 1; i <= n; i++)

if (b[i] == 0)

m = 0;

if (m == 1)

printf("Graph is Connected\n");
else

printf("Graph is not Connected\n");

break;

case 4:

exit(0);

default:

printf("Invalid choice\n");

return 0;

}
1 2 CODE

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX 10

struct employee

int id;

char name[15];

};

typedef struct employee EMP;

EMP emp[MAX];

int a[MAX];

int create(int num)

return num % MAX;

void getemp(int key)


{

printf("Enter emp id: ");

scanf("%d", &emp[key].id);

printf("Enter emp name: ");

scanf("%s", emp[key].name);

void display()

int i, ch;

printf("\n1. Display ALL\n2. Filtered Display\n");

printf("Enter the choice: ");

scanf("%d", &ch);

printf("\nHTKey\tEmpID\tEmpName\n");

if (ch == 1)

for (i = 0; i < MAX; i++)

printf("%d\t%d\t%s\n", i, emp[i].id, emp[i].name);

else

for (i = 0; i < MAX; i++)

if (a[i] != -1)

printf("%d\t%d\t%s\n", i, emp[i].id, emp[i].name);


}

void linear_prob(int key)

int i, flag = 0, count = 0;

if (a[key] == -1)

a[key] = key;

getemp(key);

return;

printf("Collision detected… using Linear Probing\n");

for (i = 0; i < MAX; i++)

if (a[i] != -1)

count++;

if (count == MAX)

printf("Hash table is full\n");

display();

exit(0);

for (i = key + 1; i < MAX; i++)

{
if (a[i] == -1)

a[i] = i;

getemp(i);

flag = 1;

break;

if (!flag)

for (i = 0; i < key; i++)

if (a[i] == -1)

a[i] = i;

getemp(i);

break;

int main()

{
int num, key, i, ans = 1;

printf("Collision Handling using Linear Probing\n");

for (i = 0; i < MAX; i++)

a[i] = -1;

emp[i].id = 0;

strcpy(emp[i].name, "-");

do

printf("\nEnter the data (key value): ");

scanf("%d", &num);

key = create(num);

linear_prob(key);

printf("Do you wish to continue? (1/0): ");

scanf("%d", &ans);

} while (ans);

display();

return 0;

You might also like