0% found this document useful (0 votes)
12 views22 pages

Dsa Codes

The document contains multiple C code snippets demonstrating various data structures and algorithms, including a weekly calendar, string replacement, stack operations, infix to postfix conversion, postfix expression evaluation, Tower of Hanoi, circular queue, singly linked list, and doubly linked list implementations. Each code snippet is designed to showcase specific functionalities such as memory management, user input handling, and algorithmic logic. The document serves as a collection of practical examples for learning and understanding data structures in C.

Uploaded by

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

Dsa Codes

The document contains multiple C code snippets demonstrating various data structures and algorithms, including a weekly calendar, string replacement, stack operations, infix to postfix conversion, postfix expression evaluation, Tower of Hanoi, circular queue, singly linked list, and doubly linked list implementations. Each code snippet is designed to showcase specific functionalities such as memory management, user input handling, and algorithmic logic. The document serves as a collection of practical examples for learning and understanding data structures in C.

Uploaded by

Rager Vs God
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 22

code 1 (week)

#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;
}

code 2 ( string replace)

#include <stdio.h>

char STR[100], PAT[100], REP[100], ANS[100];


int s, p, r, a, flag = 0;

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);
}
}

code 3 (push pop)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 5

int s[MAX], top = -1, item;

int IsFull() {
return top >= MAX - 1;
}

int IsEmpty() {
return top == -1;
}

void push(int item) {


if (IsFull()) {
printf("Stack Overflow\n");
return;
}
s[++top] = item;
}

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 num = 0, revnum = 0, k = 0;


while (!IsEmpty()) {
pop();
num = num * 10 + item;
revnum += item * pow(10, k);
k++;
}

printf("\nNumber: %d\nReverse Number: %d\n", num, revnum);


if (num == revnum) {
printf("The stack contains a Palindrome number\n");
} else {
printf("The stack does not contain a Palindrome number\n");
}
}

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;
}

code 4 ( infix to post)

#include <stdio.h>
#include <ctype.h> // for isalnum() function

#define SIZE 20
char s[SIZE];
int top = -1;

void push(char elem) {


if (top < SIZE - 1) {
s[++top] = elem;
} else {
printf("Stack overflow!\n");
}
}

char pop() {
if (top == -1) {
printf("Stack underflow!\n");
return -1; // Return an error value when stack is empty
}
return s[top--];
}

int precedence(char elem) {


switch (elem) {
case '#': return 0;
case '(': return 1;
case '+':
case '-': return 2;
case '*':
case '/':
case '%': return 3;
case '$':
case '^': return 4;
default: return -1; // In case of an invalid operator
}
}

void infixToPostfix(char infix[], char postfix[]) {


char ch, elem;
int i = 0, k = 0;

push('#'); // Push a sentinel value to mark the end of the expression

while ((ch = infix[i++]) != '\0') {


if (ch == '(') {
push(ch); // Push '(' onto stack
} else if (isalnum(ch)) {
postfix[k++] = ch; // If the character is an operand, add it to the
postfix expression
} else if (ch == ')') {
while (s[top] != '(') {
postfix[k++] = pop(); // Pop operators till '(' is encountered
}
pop(); // Remove '(' from the stack
} else { // If the character is an operator
while (precedence(s[top]) >= precedence(ch)) {
postfix[k++] = pop(); // Pop operators of higher or equal
precedence
}
push(ch); // Push the current operator to the stack
}
}

while (s[top] != '#') {


postfix[k++] = pop(); // Pop all remaining operators from the stack
}

postfix[k] = '\0'; // Null-terminate the postfix expression


}

int main() {
char infix[50], postfix[50];

printf("\nEnter the Infix Expression: ");


scanf("%s", infix); // Input the infix expression

infixToPostfix(infix, postfix); // Convert infix to postfix

printf("\nGiven Infix Expression is: %s\n", infix);


printf("Postfix Expression is: %s\n", postfix);

return 0;
}

code 5a (suffix operands)

#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <string.h>

int compute(char symbol, int op1, int op2) {


switch(symbol) {
case '+': return op1 + op2;
case '-': return op1 - op2;
case '*': return op1 * op2;
case '/': return op1 / op2;
case '%': return op1 % op2;
case '$':
case '^': return pow(op1, op2);
}
}

void main() {
int s[20];
int res;
int op1;
int op2;
int top;
int i;
char postfix[20];
char symbol;

printf("Enter the postfix expression\n");


scanf("%s", postfix);
top = -1;

for(i = 0; i < strlen(postfix); i++) {


symbol = postfix[i];
if(isdigit(symbol))
s[++top] = symbol - '0';
else {
op2 = s[top--];
op1 = s[top--];
res = compute(symbol, op1, op2);
s[++top] = res;
}
}
res = s[top--];
printf("The result is %d\n", res);
}

code 5b (tower of Hanoi)

#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);
}

code 6 (circular queue)

#include <stdio.h>
#include <stdlib.h>
#define QSIZE 4

int q[QSIZE], r = -1, f = 0, count = 0, item;

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--;
}

void display(int front) {


if (count == 0) {
printf("Queue is Empty\n");
return;
}
printf("Contents of the queue:\n");
for (int i = 0; i < count; i++) {
printf("%d\n", q[front]);
front = (front + 1) % QSIZE;
}
}

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 display(NODE *head) {


NODE *temp;
if (head->link == head) {
printf("Polynomial does not exist\n");
return;
}
temp = head->link;
printf("\n");
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, *cur;
temp = getnode();
temp->cf = cf;
temp->px = x;
temp->py = y;
temp->pz = z;
temp->flag = 0;
cur = head;
while (cur->link != head)
cur = cur->link;
cur->link = temp;
temp->link = head;
return head;
}

NODE* read_poly(NODE *head) {


int px, py, pz, cf, ch;
do {
printf("\nEnter coefficient: ");
scanf("%d", &cf);
printf("Enter powers of x, y, z (0 indicates no term): ");
scanf("%d %d %d", &px, &py, &pz);
head = insert_rear(cf, px, py, pz, head);
printf("If you wish to continue, press 1. Otherwise, press 0: ");
scanf("%d", &ch);
} while (ch != 0);
return head;
}

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


NODE *p1, *p2;
int cf;
p1 = h1->link;
while (p1 != h1) {
p2 = h2->link;
while (p2 != h2) {
if (p1->px == p2->px && p1->py == p2->py && p1->pz == p2->pz)
break;
p2 = p2->link;
}
if (p2 != h2) {
cf = p1->cf + p2->cf;
p2->flag = 1;
if (cf != 0)
h3 = insert_rear(cf, p1->px, p1->py, p1->pz, h3);
} else {
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 *h1 = head->link;
int x, y, z;
float result = 0.0;
printf("\nEnter values of x, y, z for evaluation:\n");
scanf("%d %d %d", &x, &y, &z);
while (h1 != head) {
result += h1->cf * pow(x, h1->px) * pow(y, h1->py) * pow(z, h1->pz);
h1 = h1->link;
}
printf("\nPolynomial result: %.2f\n", result);
}

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");
}
}
}

code 10 (binary search tree)

#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;
}
}
}

code 11 (operations on graph)

#include<stdio.h>
#include<stdlib.h>

int n, a[20][20], visited1[20], visited2[20], source;

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 print_data(int visited[]) {


int i;
for(i = 0; i < n; i++) {
if(visited[i] == 0)
printf("\nVertex %d is not reachable\n", i);
else
printf("\nVertex %d is reachable\n", i);
}
}

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;
}
}
}
}

void DFS(int src, int *cnt) {


int i, j;
printf("--%d--", src);
visited2[src] = 1;
for(j = 0; j < n; j++) {
if(a[src][j] == 1 && visited2[j] == 0) {
(*cnt)++;
DFS(j, cnt);
}
}
}

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);
}

You might also like