0% found this document useful (0 votes)
2 views14 pages

Dsa Lab Final

The document outlines several C programming tasks, including creating a calendar using structures, performing string pattern matching, implementing stack operations, converting infix expressions to postfix, evaluating suffix expressions, solving the Tower of Hanoi problem, managing a circular queue, and manipulating a singly linked list. Each task includes specific requirements, sample code snippets, and expected outputs. The document serves as a comprehensive guide for various data structure implementations and algorithms in C.

Uploaded by

pranavshekarc
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)
2 views14 pages

Dsa Lab Final

The document outlines several C programming tasks, including creating a calendar using structures, performing string pattern matching, implementing stack operations, converting infix expressions to postfix, evaluating suffix expressions, solving the Tower of Hanoi problem, managing a circular queue, and manipulating a singly linked list. Each task includes specific requirements, sample code snippets, and expected outputs. The document serves as a comprehensive guide for various data structure implementations and algorithms in C.

Uploaded by

pranavshekarc
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

[Link] a Program in C for the following: a) Declare a calendar as an array of 7 elements (A dynamically Created array) to represent 7 days of a week.

Each Element of the array is a structure having


three fields. The first field is the name of the Day (A dynamically allocated String), The second field is the date of the Day (A integer), the third field is the description of the activity for a particular day (A
dynamically allocated String). b) Write functions create(), read() and display(); to create the calendar, to readthe data from the keyboard and to print weeks activity details report on screen.

#include<stdio.h> int main(){


#include<stdlib.h> int n;
printf("Enter the number of days in OUTPUT:-
struct d{char *n,*a;int dt;}; the week: "); Enter the number of days in the week: 7
scanf("%d",&n); Enter details for Day 1:
struct d* create(int n){ struct d *cal=create(n); Enter the day name: Sunday
Enter the date: 1 Day 3:
return malloc(n*sizeof(struct d)); read(cal,n); Enter the activity for the day: Learning Day Name: Tuesday
} display(cal,n); Enter details for Day 2: Date: 3
Enter the day name: Monday Activity: Testing
} Day 4:
Enter the date: 2
void read(struct d *c,int n){ Enter the activity for the day: Coding Day Name: Wednesday
int i; Enter details for Day 3: Date: 4
Enter the day name: Tuesday Activity: Debugging
for(i=0;i<n;i++){ Day 5:
Enter the date: 3
c[i].n=malloc(20); Enter the activity for the day: Testing Day Name: Thursday
c[i].a=malloc(30); Enter details for Day 4: Date: 5
Enter the day name: Wednesday Activity: Publishing
printf("Enter details for Day %d:\n",i+1); Day 6:
Enter the date: 4
printf("Enter the day name: "); Enter the activity for the day: Debugging Day Name: Friday
scanf("%s",c[i].n); Enter details for Day 5: Date: 6
Enter the day name: Thursday Activity: Marketing
printf("Enter the date: "); Day 7:
Enter the date: 5
scanf("%d",&c[i].dt); Enter the activity for the day: Publishing Day Name: Saturday
printf("Enter the activity for the day: "); Enter details for Day 6: Date: 7
Enter the day name: Friday Activity: Earning
scanf("%s",c[i].a);
Enter the date: 6
} Enter the activity for the day: Marketing
} Enter details for Day 7:
Enter the day name: Saturday
Enter the date: 7
void display(struct d *c,int n){ Enter the activity for the day: Earning
int i; Week's Activity
printf("Week's Activity\n"); Day 1:
Day Name: Sunday
for(i=0;i<n;i++) Date: 1
printf("Day %d:\nDay Name: %s\nDate: %d\nActivity: %s\n",i+1,c[i].n,c[i].dt,c[i].a); Activity: Learning
} Day 2:
Day Name: Monday
Date: 2
Activity: Coding
2. Develop a Program in C for the following operations on Strings. a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP) b. Perform Pattern Matching
Operation: Find and Replace all occurrences of PAT in STR with REP if PAT exists in STR. Report suitable messages in case PAT does not exist in STR Support the program with
functions for each of the above operations. Don't use Built-in functions.
#include<stdio.h> printf("Enter the replace string:");
gets(rep);
OUTPUT:-
char str[100],pat[50],rep[50],res[200]; printf("The string before pattern match Enter the main string:Designed by vtucode
int flag=0; is:%s",str); Enter the pat string:vtucode
stringmatch(); Enter the replace string:Braham
int len(char s[]){ if(flag) The string before pattern match is:Designed by vtucode
int i=0; while(s[i]) i++; return i; printf("\nThe string after pattern match The string after pattern match and replace is:Designed by Braham
} and replace is:%s",res);
else
int match(int i){ printf("\nPattern string is not found");
int j=0; }
while(pat[j]){
if(str[i+j]!=pat[j]) return 0;
j++;
}
return 1;
}

void stringmatch(){
int i=0,j=0,k,lp=len(pat),lr=len(rep);
while(str[i]){
if(match(i)){
for(k=0;k<lr;k++) res[j++]=rep[k];
i+=lp; flag=1;
}else res[j++]=str[i++];
}
res[j]=0;
}
int main(){
printf("Enter the main string:");
gets(str);
printf("Enter the pat string:");
gets(pat);
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

#include<stdio.h> void palindrome(){ OUPTUT:-


#define MAX 3 int n,r,t[20],i=0,flag=1;
~~~~~Menu~~~~~
printf("Enter number: "); =>[Link] an Element to Stack and Overflow demo
int s[MAX],top=-1; scanf("%d",&n); =>[Link] an Element from Stack and Underflow
while(n){ t[i++]=n%10; n/=10; } demo
void push(){ for(int j=0;j<i/2;j++) =>[Link] demo
int x; if(t[j]!=t[i-j-1]) flag=0; =>[Link]
if(top==MAX-1){ printf("Stack overflow\n"); return; } if(flag) printf("It is palindrome number\n"); =>[Link]
printf("Enter an element to be pushed: "); else printf("It is not palindrome number\n"); Enter your choice: 1
} Enter an element to be pushed: 11
scanf("%d",&x);
Enter your choice: 1
s[++top]=x; Enter an element to be pushed: 12
} int main(){
Enter your choice: 1
int ch;
Enter an element to be pushed: 13
void pop(){ do{ Enter your choice: 1
if(top==-1){ printf("Stack underflow\n"); return; } printf("~~~~~Menu~~~~~\n"); Stack overflow
printf("Element popped is: %d\n",s[top--]); printf("=>[Link] an Element to Stack and Overflow demo\n"); Enter your choice: 4
} printf("=>[Link] an Element from Stack and Underflow demo\n"); Stack elements are:
printf("=>[Link] demo\n"); | 13 |
void display(){ printf("=>[Link]\n"); | 12 |
int i; printf("=>[Link]\n"); | 11 |
printf("Enter your choice: "); Enter your choice: 2
if(top==-1){ printf("Stack empty\n"); return; }
scanf("%d",&ch); Element popped is: 13
printf("Stack elements are:\n"); Enter your choice: 5
for(i=top;i>=0;i--) printf("| %d |\n",s[i]); if(ch==1) push();
} else if(ch==2) pop();
else if(ch==3) palindrome();
else if(ch==4) display();
}while(ch!=5);
}
4. Develop a Program in C for converting an Infix Expression to Postfix Expression. Program should support for both parenthesized and free parenthesized expressions with the operators: +,
-, *, /, % (Remainder), ^ (Power) and alphanumeric operands.
#include<stdio.h> while((c=inf[i++])!='\0')
#include<ctype.h> {
OUTPUT:-
if(isalnum(c)) Enter the valid infix expression:(a+b)*c/d^5%1
char s[50]; post[j++]=c; The entered infix expression is :
int top=-1; (a+b)*c/d^5%1
else if(c=='(') The corresponding postfix expression is :
void push(char c) push(c); ab+c*d5^/1%
{
s[++top]=c; else if(c==')')
} {
while(s[top]!='(')
char pop() post[j++]=pop();
{ pop();
return s[top--]; }
} else
{
int priority(char c) while(top!=-1 && priority(s[top])>=priority(c))
{ post[j++]=pop();
if(c=='^') return 3; push(c);
if(c=='*'||c=='/'||c=='%') return 2; }
if(c=='+'||c=='-') return 1; }
return 0;
} while(top!=-1)
int main() post[j++]=pop();
{
char inf[50],post[50],c; post[j]='\0';
int i=0,j=0; printf("%s",post);
printf("Enter the valid infix expression:"); }
scanf("%s",inf);
printf("The entered infix expression is :\n%s",inf);
printf("\nThe corresponding postfix expression is :\n");
5. Develop a Program in C for the following Stack Applications
a. Evaluation of Suffix expression with single digit operands and operators:
+, -, *, /, %, ^
OUTPUT:-
while((c=post[i++])!='\0') Enter the postfix expression:23*54*+9-
#include<stdio.h> {
#include<ctype.h>
Result = 17
if(isdigit(c))
push(c-'0');
int s[50]; else
int top=-1; {
b=pop();
void push(int x) a=pop();
{ switch(c)
s[++top]=x; {
} case '+': push(a+b); break;
case '-': push(a-b); break;
int pop() case '*': push(a*b); break;
{ case '/': push(a/b); break;
return s[top--]; case '%': push(a%b); break;
} }
}
int main() }
{
char post[50],c; printf("Result = %d",pop());
int i=0,a,b; }

printf("Enter the postfix expression:");


scanf("%s",post);
5b. Solving Tower of Hanoi problem with n disks OUTPUT:-
#include <stdio.h>
#include <math.h> Enter the number of
void tower(int n, char source, char temp, char destination) discs: 3
{ Move disc 1 from A to C
if (n == 0)
return; Move disc 2 from A to B
tower(n - 1, source, destination, temp); Move disc 1 from C to B
printf("\nMove disc %d from %c to %c", n, source, destination);
tower(n - 1, temp, source, destination); Move disc 3 from A to C
} Move disc 1 from B to A
int main() Move disc 2 from B to C
{ Move disc 1 from A to C
int n;
printf("\nEnter the number of discs:\n"); Total Number of moves
scanf("%d", &n); are: 7
tower(n, 'A', 'B', 'C');
printf("\n\nTotal Number of moves are: %d", (int)pow(2, n) - 1);
}
6. Develop a menu driven Program in C for the following operations on Circular QUEUE of Characters (Array Implementation of Queue with maximum size MAX) a. Insert an Element on to
Circular QUEUE b. Delete an Element from Circular QUEUE c. Demonstrate Overflow and Underflow situations on Circular QUEUE d. Display the status of Circular QUEUE e. Exit Support the
program with appropriate functions for each of the above operations

#include<stdio.h> void display() OUTPUT:-


#define MAX 3 {
~~~~~Menu~~~~~
int i; =>[Link]
char q[MAX]; if(f==-1)
=>[Link]
int f=-1,r=-1; { =>[Link]
printf("Queue empty\n"); =>[Link]
return;
void insert() Enter your choice: 1
} Enter element to insert: A
{ printf("Queue elements are:\n");
char x; Enter your choice: 1
i=f; Enter element to insert: B
if((f==0 && r==MAX-1) || (f==r+1)) while(1){ Enter your choice: 1
{ printf("%c ",q[i]);
Enter element to insert: C
printf("Queue overflow\n"); if(i==r) break; Enter your choice: 1
return; i=(i+1)%MAX;
Queue overflow
} } Enter your choice: 3
printf("Enter element to insert: "); printf("\n"); Queue elements are:
scanf(" %c",&x); }
ABC
int main() Enter your choice: 2
if(f==-1) f=0;
{
r=(r+1)%MAX; Deleted element is: A
int ch; Enter your choice: 2
q[r]=x; do
} Deleted element is: B
{
Enter your choice: 2
printf("~~~~~Menu~~~~~\n"); Deleted element is: C
void delete() printf("=>[Link]\n");
Enter your choice: 2
{ printf("=>[Link]\n"); Queue underflow
if(f==-1) printf("=>[Link]\n");
{ printf("=>[Link]\n");
printf("Queue underflow\n"); printf("Enter your choice: ");
return; scanf("%d",&ch);
switch(ch)
}
{
printf("Deleted element is: %c\n",q[f]); case 1: insert(); break;
if(f==r) f=r=-1; case 2: delete(); break;
else f=(f+1)%MAX; case 3: display(); break;
} }
}
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 void display() if(!head->next)
#include<stdio.h> { {
#include<stdlib.h> struct node *t=head; free(head);
int c=0; head=NULL;
if(!head) return;
struct node { }
{ printf("SLL empty\n"); while(t->next->next) t=t->next;
char usn[20],name[20],branch[20]; return; free(t->next);
int sem; } t->next=NULL;
long ph; printf("The contents of SLL:\n"); }
struct node *next; while(t) void stack_demo()
}*head=NULL; { {
printf("| USN:%s Name:%s Branch:%s Sem:%d Ph:%ld struct node *p=create();
|\n", p->next=head;
struct node* create() t->usn,t->name,t->branch,t->sem,t->ph); head=p;
{ c++; t=t->next; printf("Deleted from stack\n");
struct node *p=malloc(sizeof(struct node)); } p=head;
scanf("%s%s%s%d%ld",p->usn,p->name,p- printf("No of student nodes is %d\n",c); head=head->next;
>branch,&p->sem,&p->ph); } free(p);
p->next=NULL; }
return p; int main()
} void insert_end() {
{ int ch,n;
struct node *p=create(),*t; do
void create_sll(int n) if(!head) head=p;
{ {
else printf("~~~Menu~~~\n");
struct node *p,*t; {
while(n--) printf("1:Create 2:Display 3:InsertEnd 4:DeleteEnd
for(t=head;t->next;t=t->next); 5:StackDemo6:Exit\n");
{ t->next=p;
printf("Enter the usn,Name,Branch, scanf("%d",&ch);
}
sem,PhoneNo of the student:\n"); }
p=create(); void delete_end() if(ch==1){ scanf("%d",&n); create_sll(n); }
if(!head) head=p; { else if(ch==2) display();
else struct node *t=head; else if(ch==3) insert_end();
{ if(!head) else if(ch==4) delete_end();
for(t=head;t->next;t=t->next); { else if(ch==5) stack_demo();
t->next=p; printf("SLL empty\n"); }
} return; while(ch!=6);
}
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
void display()
#include<stdio.h> void insert_front() { switch(ch)
#include<stdlib.h> { struct node *t=first; {
struct node *p; int c=0; case 1:
struct node printf("Enter SSN Name Dept Designation if(first==NULL) printf("Enter the no of Employees:");
{ Salary PhoneNo:\n"); { scanf("%d",&n);
char ssn[10],name[20],dept[20],desg[20]; p=create(); printf("DLL empty\n"); for(i=0;i<n;i++) insert_end();
int sal; if(first) first->l=p; return; break;
long ph; p->r=first; }
struct node *l,*r; first=p; while(t) case 2: display(); break;
}; } { case 3: insert_end(); break;
printf("|SSN:%s|Name:%s|Dept:%s|Desg:%s|Sa case 4: delete_end(); break;
struct node *first=NULL; void delete_end() l:%d|Ph:%ld|\n", case 5: insert_front(); break;
{ t->ssn,t->name,t->dept,t->desg,t->sal,t->ph); case 6: delete_front(); break;
struct node* create() struct node *t; c++; case 7: deque_demo(); break;
{ if(first==NULL) t=t->r; case 8: exit(0);
struct node *p; { } default: printf("Please enter valid
p=(struct node*)malloc(sizeof(struct node)); printf("DLL empty\n"); printf("No of employee nodes is %d\n",c); choice\n");
scanf("%s%s%s%s%d%ld",p->ssn,p->name,p->dept,p- return; } }
>desg,&p->sal,&p->ph); } }
p->l=p->r=NULL; t=first; void deque_demo() while(ch!=8);
return p; while(t->r) t=t->r; { }
} if(t->l) t->l->r=NULL; insert_front();
else first=NULL; delete_end();
void insert_end() free(t); }
{ } int main()
struct node *p,*t; {
printf("Enter SSN Name Dept Designation Salary PhoneNo:\n"); void delete_front() int ch,n,i;
p=create(); { do
if(first==NULL) first=p; struct node *t; {
else if(first==NULL) printf("~~~Menu~~~\n");
{ { printf("1:Create DLL of Employee Nodes\n");
t=first; printf("DLL empty\n"); printf("2:DisplayStatus\n");
while(t->r) t=t->r; return; printf("3:InsertAtEnd\n");
t->r=p; } printf("4:DeleteAtEnd\n");
p->l=t; t=first; printf("5:InsertAtFront\n");
} first=first->r; printf("6:DeleteAtFront\n");
} if(first) first->l=NULL; printf("7:Double Ended Queue Demo using
free(t); DLL\n");
} printf("8:Exit\n");
printf("Enter your choice:");
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) = 6x 2 y 2 z-4yz 5 +3x 3 yz+2xy 5 z-
2xyz 3 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 int main()
void show(struct node *h) {
#include<stdio.h> int ch,x,y,z;
{
#include<stdlib.h> struct node *h1=malloc(sizeof(struct node)),
struct node *t=h->n;
#include<math.h> *h2=malloc(sizeof(struct node)),
while(t!=h)
{ *h3=malloc(sizeof(struct node));
struct node h1->n=h1; h2->n=h2; h3->n=h3;
printf("%dx^%dy^%dz^%d",t->c,t->x,t->y,t->z);
{ do
if((t=t->n)!=h) printf(" + ");
int c,x,y,z; {
}
struct node *n; printf("~~~Menu~~~\n");
printf("\n");
}; printf("[Link] and Evaluate a Polynomial P(x,y,z)\n");
}
int eval(struct node *h,int x,int y,int z) printf("[Link] the sum of two polynomials POLY1(x,y,z) and
struct node* new() POLY2(x,y,z)\n");
{
{ printf("[Link]\n");
int s=0;
struct node *p=malloc(sizeof(struct node)); printf("Enter your choice: ");
struct node *t=h->n;
scanf("%d%d%d%d",&p->c,&p->x,&p->y,&p->z); scanf("%d",&ch);
while(t!=h)
p->n=p; if(ch==1)
{
return p; {
s+=t->c*pow(x,t->x)*pow(y,t->y)*pow(z,t->z);
} h1=read(h1);
t=t->n;
} printf("Polynomial is:\n");
struct node* read(struct node *h) show(h1);
return s;
{ printf("Enter the value of x,y and z: ");
}
int n; scanf("%d%d%d",&x,&y,&z);
struct node* add(struct node *a,struct node *b,struct node *c)
struct node *t=h,*p; printf("Result of polynomial evaluation is:
{
printf("Enter the no of terms in the polynomial: "); %d\n",eval(h1,x,y,z));
struct node *p=a->n,*q=b->n,*t=c,*r;
scanf("%d",&n); }
while(p!=a && q!=b)
while(n--) else if(ch==2)
{
{ {
if(p->x==q->x && p->y==q->y && p->z==q->z)
printf("Enter coef and Pow(x) Pow(y) Pow(z): "); printf("Enter the POLY1(x,y,z):\n");
{
p=new(); h1=read(h1);
r=malloc(sizeof(struct node));
p->n=h; printf("Enter the POLY2(x,y,z):\n");
r->c=p->c+q->c;
t->n=p; h2=read(h2);
r->x=p->x; r->y=p->y; r->z=p->z;
t=p; h3=add(h1,h2,h3);
r->n=c; t->n=r; t=r;
} printf("Polynomial addition result:\n");
p=p->n; q=q->n;
return h; show(h3);
}
} }
else p=p->n;
} }
return c; while(ch!=3);
}
PROGRAM 8:-
PROGRAM 7:-
PROGRAM 9:-
10. Develop a menu driven Program in C for the following operations on Binary Search Tree (BST) of Integers . a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2 b. Traverse the BST
in Inorder, Preorder and Post Order c. Search the BST for a given element (KEY) and report the appropriate message d. Exit
#include <stdio.h>
void postorder(struct node *r)
{ switch (ch) OUTPUT:-
#include <stdlib.h> {
struct node
if (r) [Link] [Link] [Link] [Link] [Link]
{ case 1: Choice: 1 2 5 6 7 8 9 14 15 24
{
postorder(r->left); inorder(root); [Link] [Link] [Link] [Link] [Link]
int data;
struct node *left, *right; postorder(r->right); printf("\n"); Choice: 2 6 5 2 9 8 7 15 14 24
}; printf("%d ", r->data); break; [Link] [Link] [Link] [Link] [Link]
} case 2: Choice: 3 2 5 7 8 14 24 15 9 6
preorder(root); [Link] [Link] [Link] [Link] [Link]
struct node* insert(struct node *root, int v) }
{ printf("\n"); Choice: 4
Enter key: 14 Key 14 found
if (!root) void search(struct node *r, int key) break;
{ [Link] [Link] [Link] [Link] [Link]
{ case 3: Choice: 4
root = (struct node*)malloc(sizeof(struct node)); postorder(root);
root->data = v;
if (!r) Enter key: 10 Key 10 NOT found
printf("Key %d NOT found\n", key); printf("\n"); [Link] [Link] [Link] [Link] [Link]
root->left = root->right = NULL;
else if (key == r->data) break; Choice: 5
}
else if (v < root->data) printf("Key %d found\n", key); case 4:
root->left = insert(root->left, v); else if (key < r->data) printf("Enter key: ");
else if (v > root->data) search(r->left, key); scanf("%d", &key);
root->right = insert(root->right, v); else search(root, key);
search(r->right, key); break;
return root; } case 5:
} exit(0);
int main() default:
void inorder(struct node *r) printf("Invalid\n");
{ {
struct node *root = NULL; }
if (r)
{ int ch, key; }
inorder(r->left); int arr[] = {6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2}; }
printf("%d ", r->data);
inorder(r->right); for (int i = 0; i < 12; i++)
} root = insert(root, arr[i]);
}
void preorder(struct node *r)
while (1)
{
if (r)
{
{ printf("\[Link]\[Link]\[Link]\[Link]\[Link]\nChoice: ");
printf("%d ", r->data); scanf("%d", &ch);
preorder(r->left);
preorder(r->right);
}
}
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.
int main()
#include<stdio.h> {

int a[10][10],v[10],n;
int i,j,s,ch; OUTPUT:-
do
{ Enter the number of vertices in graph:4
void bfs(int s) printf("Enter the number of vertices in graph:"); Enter the adjacency matrix:
{ scanf("%d",&n); 0101
int q[10],f=0,r=-1,i; 0010
q[++r]=s; printf("Enter the adjacency matrix:\n"); 0001
for(i=1;i<=n;i++) 0000
v[s]=1;
for(j=1;j<=n;j++) Enter the starting vertex: 1
while(f<=r) ==>1. BFS: Print all nodes reachable from a given starting node
scanf("%d",&a[i][j]);
{ ==>2. DFS: Print all nodes reachable from a given starting node
s=q[f++]; printf("Enter the starting vertex: "); ==>3. Exit
for(i=1;i<=n;i++) scanf("%d",&s); Enter your choice: 1
if(a[s][i] && !v[i]) Nodes reachable from starting vertex 1 are: 2 4
{ for(i=1;i<=n;i++) v[i]=0;
printf("%d ",i); Enter your choice: 2
q[++r]=i; printf("==>1. BFS\n==>2. DFS\n==>3. Exit\n"); Nodes reachable from starting vertex 1 are: 2 3
v[i]=1; printf("Enter your choice: ");
} scanf("%d",&ch);
}
if(ch==1)
}
{
printf("Nodes reachable from starting vertex %d are: ",s);
void dfs(int s) bfs(s);
{ printf("\n");
int i; }
v[s]=1; else if(ch==2)
for(i=1;i<=n;i++) {
if(a[s][i] && !v[i]) printf("Nodes reachable from starting vertex %d are: ",s);
{ dfs(s);
printf("%d ",i); printf("\n");
dfs(i); }
}
}
while(ch!=3);
} }
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.
#include <stdio.h>
#define MAX 20
int main()
{
OUTPUT:-
int m,n,key; Enter hash table size: 10
int HT[MAX]; printf("Enter hash table size: "); Enter number of keys: 5
scanf("%d",&m); Enter keys:
void init(int m)
{ 1234
init(m);
for(int i=0;i<m;i++) HT[i]=-1; 2345
} printf("Enter number of keys: "); 3456
scanf("%d",&n); 4567
void insertKey(int key,int m) 5678
{ printf("Enter keys:\n"); Hash Table:
int h=key%m; for(int i=0;i<n;i++) 0 : -1
for(int i=0;i<m;i++) { 1 : -1
{ scanf("%d",&key); 2 : -1
int p=(h+i)%m; insertKey(key,m);
if(HT[p]==-1) 3 : 1234
} 4 : 2345
{
HT[p]=key; 5 : 3456
display(m);
return; 6 : 4567
return 0;
} } 7 : 5678
} 8 : -1
printf("Table Full! Cannot insert %d\n",key); 9 : -1
}

void display(int m)
{
printf("\nHash Table:\n");
for(int i=0;i<m;i++)
printf("%d : %d\n",i,HT[i]);
}

You might also like