Data Structures Lab Programs Fibonacci
Data Structures Lab Programs Fibonacci
#include<stdio.h>
#include<stdlib.h>
struct day {
char *dayname;
int date;
char *activity;
};
typedef struct day DAY;
1|Page
printf("\nDay\t\tName of the day\tDate\tActivity\n");
printf("\n___________________________________________________");
for(int i = 0;i<size;i++){
printf("\n%d\t\t%s\t\t%d\t\t%s\n",i+1,day[i].dayname,day[i].date,day[i].activity);
}
}
int main(){
int n;
printf("Enter the number of days in the week:");
scanf("%d",&n);
DAY *calender;
calender = (DAY*)malloc(n*sizeof(DAY));
if(calender==NULL){
printf("Memory Allocation Falied!!");
return 1;
}
read(calender,n);
display(calender,n);
freememory(calender,n);
free(calender);
return 0;
}
OUTPUT
2|Page
PROGRAM 2
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char str[50],pat[50],rep[50],ans[50];
int i,j,k,m,c,flag=0;
void stringmatch(){
i=m=c=j=0;
while(str[c]!='\0'){
if(str[m]==pat[i]){
i++;
m++;
if(pat[i]=='\0'){
flag=1;
3|Page
for(k=0;rep[k]!='\0';k++)
ans[j++]=rep[k];
c=m;
i=0;
}
}
else{
ans[j]=str[c];
j++;
c++;
m=c;
i=0;
}
}
int main(){
printf("\nEnter a Main String:\n");
gets(str);
printf("\nEnter a Patter String:\n");
gets(pat);
printf("\nEnter a Replacement String:\n");
gets(rep);
stringmatch();
if(flag==1)
printf("\nThe resultant string is :\n%s",ans);
else
printf("\nPattern String not found!!\n");
return 0;
}
OUTPUT
4|Page
PROGRAM 3
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>
#include<stdlib.h>
#define max 4
int stack[max],i,itemdel;
int top = -1;
int flag=1;
int status = 0;
5|Page
printf("\nItem deleted = %d",itemdel);
}
}
for(i=0;i<=top/2;i++){
if(stack[i]!=stack[top-i])
flag=0;
break;
}
if(flag==1)
printf("\nStack is a Pallindrome.");
else
printf("\nStack is Not a Pallinrome.");
}
int main(){
int ch,item;
while(ch!=5){
printf("\n1.Push\t2.Pop\t3.Display\t4.Pallindrome\t5.Exit\nEnter your choice:");
scanf("%d",&ch);
switch(ch){
case 1:
printf("\nEnter the element to be inserted:");
scanf("%d",&item);
push(stack,item);
break;
case 2:
pop(stack);
break;
6|Page
case 3:
display(stack);
break;
case 4:
pallindrome(stack);
break;
case 5:
break;
default :
printf("\nInvalid Choice!!");
}
}
return 0;
}
OUTPUT
1.Push 2.Pop 3.Display 4.Pallindrome 5.Exit
Enter your choice:1
7|Page
Stack is Full!
1.Push 2.Pop 3.Display 4.Pallindrome 5.Exit
Enter your choice:3
Stack is a Pallindrome.
1.Push 2.Pop 3.Display 4.Pallindrome 5.Exit
Enter your choice:2
Item deleted = 1
1.Push 2.Pop 3.Display 4.Pallindrome 5.Exit
Enter your choice:3
Item deleted = 2
1.Push 2.Pop 3.Display 4.Pallindrome 5.Exit
Enter your choice:3
8|Page
|2|
|1|
PROGRAM 4
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
9|Page
case '*':
case '/':
return 3;
case '$':
case '^':
return 6;
case '(':
return 9;
case ')':
return 0;
default : return 7;
}
}
for(i=0;i<strlen(infix);i++){
symbol = infix[i];
while(stkpre(s[top])>inpre(symbol))
postfix[j++]=s[top--];
if(stkpre(s[top])!=inpre(symbol))
s[++top]=symbol;
else
top--;
}
while(s[top]!='#'){
postfix[j++] = s[top--];
}
postfix[j] = '\0';
}
int main(){
char infix[30],postfix[30];
printf("\nEnter Infix Expression:");
scanf("%s",infix);
printf("\nEquivalent Postfix Expression is:");
infix_postfix(infix,postfix);
printf("\n%s",postfix);
return 0;
}
10 | P a g e
OUTPUT
11 | P a g e
PROGRAM 5A
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
double compute(char symbol, double op1, double op2) {
switch (symbol) {
case '+':
return op1 + op2;
case '-':
return op1 - op2;
case '*':
return op1 * op2;
case '/':
return op1 / op2;
case '$':
case '^':
return pow(op1, op2);
case '%':
return fmod(op1 , op2);
default:
return 0;
}
}
int main() {
double s[100];
double res, op1, op2;
int top = -1, i;
char postfix[100], symbol;
printf("\nEnter the postfix expression:\n");
12 | P a g e
fgets(postfix, sizeof(postfix), stdin);
for (i = 0; i < strlen(postfix); i++) {
symbol = postfix[i];
if (isspace(symbol))
continue;
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("\nThe result is : %f\n", res);
return 0;
}
OUTPUT
Enter the postfix expression:
231*+9-
13 | P a g e
PROGRAM 5B
int main(){
int n;
printf("Enter the Number of Disks:");
scanf("%d",&n);
hanoi(n,'A','B','C');
printf("\nTotal number of moves : %d",count);
return 0;
}
OUTPUT
14 | P a g e
PROGRAM 6
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>
#include<stdlib.h>
#define max 4
int item,q[max],i;
int count=0;
int front=0;
int rear=-1;
15 | P a g e
printf("\nCircular Queue is Empty!");
}
else{
int itemdel = q[*front];
*front = (*front+1)%max;
(*count)--;
printf("\nItem Deleted : %d",itemdel);
}
}
int main(){
int ch;
while(1){
printf("\n1.Insert\t2.Delete\t3.Display\t4.Exit\nEnter your choice:");
scanf("%d",&ch);
switch(ch){
case 1:
printf("\nEnter item to be inserted:");
scanf("%d",&item);
insert(item,&count,q,&rear);
break;
case 2:
del(&count,q,&front);
break;
case 3:
display(front,count,q);
break;
case 4:
exit(0);
default:printf("\nInvalid Choice!");
}
}
return 0;
}
16 | P a g e
OUTPUT
Item Deleted : 1
1.Insert 2.Delete 3.Display 4.Exit
Enter your choice:2
Item Deleted : 2
1.Insert 2.Delete 3.Display 4.Exit
Enter your choice:3
17 | P a g e
PROGRAM 7
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, Branch, 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
#include<stdio.h>
#include<stdlib.h>
#define max 5
struct node {
char usn[11];
char name[15];
char branch[5];
int sem;
char phone[15];
struct node *next;
};
p=p->next;
18 | P a g e
count++;
}
return count;
}
NODE* getnode(){
NODE *newnode;
newnode = (NODE*)malloc(sizeof(NODE));
newnode->next=NULL;
if(newnode==NULL){
printf("Memory allocation failed!!");
return 0;
}
else{
printf("Enter USN:");
scanf("%s",newnode->usn);
printf("Enter Name:");
scanf("%s",newnode->name);
printf("Enter Branch:");
scanf("%s",newnode->branch);
printf("Enter Sem:");
scanf("%d",&newnode->sem);
printf("Enter Phone No:");
scanf("%s",newnode->phone);
}
return newnode;
}
19 | P a g e
return head;
}
NODE *newnode;
newnode = getnode();
newnode->next = head;
return newnode;
}
20 | P a g e
}
NODE *temp = head;
while (temp->next->next != NULL) {
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
printf("Data Deleted!!\n");
return head;
}
21 | P a g e
do{
printf("\nSSL used as Stack...");
printf("\n 1.Insert at Front(PUSH) \t 2.Delete from Front(POP))\t3.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch){
case 1: head=insert_front(head);
head = display(head);
break;
case 2: head=del_front(head);
head = display(head);
break;
case 3: break;
}
}while(ch!=3);
return head;
}
int main(){
int ch, i, n;
NODE *head;
head=NULL;
printf("\n*----------StudentDatabase-----------*");
do{
printf("\n 1.Create\t 2.Display\t 3.Insert\t 4.Delete\t 5.Stack\t 6.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch){
case 1: printf("\nHow many student data you want to create: ");
scanf("%d", &n);
for(i=0;i<n;i++){
printf("Enter Student%d Details:--\n",i+1);
head = create(head);
}
break;
case 2: head=display(head);
break;
case 3: head=insert(head);
22 | P a g e
break;
case 4: head=del(head);
break;
case 5: head=stack(head);
break;
case 6: break;
}
}while(ch!=6);
return 0;
}
OUTPUT:
*----------StudentDatabase-----------*
1.Create 2.Display 3.Insert 4.Delete 5.Stack 6.Exit
Enter your choice: 1
23 | P a g e
Enter your choice: 1
Enter USN:1BI22IS0FF
Enter Name:FFFFFFF
Enter Branch:ISE
Enter Sem:3
Enter Phone No:9191919191
24 | P a g e
The number of nodes in the list is : 5
25 | P a g e
1.Create 2.Display 3.Insert 4.Delete 5.Stack 6.Exit
Enter your choice: 2
PROGRAM 8
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
#include<stdio.h>
#include<stdlib.h>
#define max 5
struct node{
int ssn;
char name[15];
char dept[10];
char desig[20];
int salary;
char phno[15];
struct node *prev;
struct node *next;
};
typedef struct node NODE;
26 | P a g e
temp=head;
while(temp){
count++;
temp=temp->next;
}
}
return count;
}
NODE* getnode(){
NODE *newnode;
newnode = (NODE*)malloc(sizeof(NODE));
if(newnode==NULL){
printf("\nMemory allocation failed!!\n");
}
else{
newnode->prev=newnode->next=NULL;
printf("Enter SSN:");
scanf("%d",&newnode->ssn);
printf("Enter Name:");
scanf("%s",newnode->name);
printf("Enter Department:");
scanf("%s",newnode->dept);
printf("Enter Designation:");
scanf("%s",newnode->desig);
printf("Enter Salary:");
scanf("%d",&newnode->salary);
printf("Enter PhoneNo.:");
scanf("%s",newnode->phno);
}
return newnode;
}
NODE* display(NODE *head){
if(countnodes(head)==0){
printf("Database Empty!!");
return head;
}
else{
printf("SSN\tNAME\t\tDEPARTMENT\tDESIGNATION\tSALARY\tPHONENO\n");
NODE *temp;
temp = head;
while(temp!=NULL){
printf("%d\t%s\t\t%s\t\t%s\t%d\t%s\n",temp->ssn,temp->name,temp->dept,temp-
>desig,temp->salary,temp->phno);
temp=temp->next;
}
}
printf("Total number of nodes : %d\n",countnodes(head));
return head;
}
27 | P a g e
NODE* insertrear(NODE *head){
NODE *temp;
NODE *newnode;
newnode = getnode();
if(head==NULL){
return newnode;
}
else if (countnodes(head)==max){
printf("Database Full!!");
return head;
}
else{
temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next = newnode;
newnode->prev = temp;
return head;
}
}
NODE* insertfront(NODE *head){
NODE *newnode;
newnode = getnode();
if(head==NULL){
return newnode;
}
else if (countnodes(head)==max){
printf("Database Full!!");
return head;
}
else{
newnode->next=head;
head->prev=newnode;
return newnode;
}
}
NODE* insert(NODE *head){
int ch=0 ;
while(ch!=3){
printf("1insert_front\t2.insert_rear\t3.Exit\nEnter your choice:");
scanf("%d",&ch);
switch(ch){
case 1:
head = insertfront(head);
head=display(head);
break;
case 2:
head = insertrear(head);
head=display(head);
28 | P a g e
break;
case 3:
break;
}
}
return head;
}
NODE* del_rear(NODE *head){
if(head==NULL){
printf("Database empty!\n");
return head;
}
if(head->next==NULL){
free(head->next);
printf("Data Deleted!\n");
return head;
}
NODE *temp;
temp=head;
while(temp->next->next!=NULL){
temp=temp->next;
}
free(temp->next);
temp->next=NULL;
printf("Data Deleted!\n");
return head;
}
NODE* del_front(NODE *head){
if(head==NULL){
printf("Database Empty\n");
return head;
}
NODE *temp;
temp=head;
head=head->next;
free(temp);
printf("\nData Deleted!");
return head;
}
NODE* del(NODE *head){
int ch = 0; // Initialize ch
while(ch!=3){
printf("1.del_front\t2del_rear\t3.Exit\nEnter your choice:");
scanf("%d",&ch);
switch(ch){
case 1:
head = del_front(head);
head=display(head);
break;
case 2:
29 | P a g e
head =del_rear(head);
head=display(head);
break;
case 3:
break;
}
}
return head;
}
NODE* dqueue(NODE *head) {
int ch;
while (1) {
printf("\n DLL used as Double Ended Queue");
printf("\n 1. Insert at Rear\n 2. Delete from Front\n 3. Insert at Front\n 4. Delete from
Rear\n 5. Display\n 6. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
head = insertrear(head);
head=display(head);
break;
case 2:
head = del_front(head);
head=display(head);
break;
case 3:
head = insertfront(head);
head=display(head);
break;
case 4:
head = del_rear(head);
head=display(head);
break;
case 5:
head = display(head);
break;
case 6:
return head;
}
}
}
30 | P a g e
head = insertfront(head);
}
return head;
}
int main(){
NODE *head;
head = NULL;
int ch ,n;
while(1){
printf("-------EMPLOYEE DATABASE SYSTEM ------");
printf("\n1.Create\t2.Display\t3.Insert\t4.Delete\t5.Queue\t6.Exit\nEnter your choice:");
scanf("%d",&ch);
switch(ch){
case 1:
printf("How many records you want to create? : ");
scanf("%d",&n);
for(int i=0;i<n;i++){
printf("\nEnter Employee%d Details:--\n",i+1);
head = create(head);
}
break;
case 2 :
head = display(head);
break;
case 3:
head = insert(head);
break;
case 4:
head = del(head);
break;
case 5:
head = dqueue(head);
break;
case 6:
exit(0);
default:
printf("Invalid choice!!");
}
}
return 0;
}
OUTPUT
-------EMPLOYEE DATABASE SYSTEM ------
1.Create 2.Display 3.Insert 4.Delete 5.Queue 6.Exit
Enter your choice:1
How many records you want to create? : 2
Enter Employee1 Details:--
Enter SSN:1
31 | P a g e
Enter Name:AAAAA
Enter Department:ISE
Enter Designation:INSTRUCTOR
Enter Salary:20000
Enter PhoneNo.:1234567890
32 | P a g e
1inset_front 2.insert_rear 3Exit
Enter your choice:3
-------EMPLOYEE DATABASE SYSTEM ------
1.Create 2.Display 3.Insert 4.Delete 5.Queue 6.Exit
Enter your choice:5
33 | P a g e
Enter your choice: 3
Enter SSN:6
Enter Name:INSERTQ
Enter Department:ISE
Enter Designation:INSTRUCTOR
Enter Salary:50000
Enter PhoneNo.:4536987120
SSN NAME DEPARTMENT DESIGNATION SALARY PHONENO
6 INSERTQ ISE INSTRUCTOR 50000 4536987120
2 BBBBB ISE INSTRUCTOR 25000 4569832170
1 AAAAA ISE INSTRUCTOR 20000 1234567890
4 RRRRR ISE INSTRUCTOR 35000 4563271890
5 INSERTF ISE INSTRUCTOR 40000 1237894560
Total number of nodes : 5
DLL used as Double Ended Queue
1. Insert at Rear
2. Delete from Front
3. Insert at Front
4. Delete from Rear
5. Display
6. Exit
Enter your choice: 4
Data Deleted!
SSN NAME DEPARTMENT DESIGNATION SALARY PHONENO
6 INSERTQ ISE INSTRUCTOR 50000 4536987120
2 BBBBB ISE INSTRUCTOR 25000 4569832170
1 AAAAA ISE INSTRUCTOR 20000 1234567890
4 RRRRR ISE INSTRUCTOR 35000 4563271890
Total number of nodes : 4
34 | P a g e
6. Exit
Enter your choice: 6
-------EMPLOYEE DATABASE SYSTEM ------
1.Create 2.Display 3.Insert 4.Delete 5.Queue 6.Exit
Enter your choice:4
1del_front 2del_rear 3.Exit
Enter your choice:1
Data Deleted!
SSN NAME DEPARTMENT DESIGNATION SALARY PHONENO
2 BBBBB ISE INSTRUCTOR 25000 4569832170
1 AAAAA ISE INSTRUCTOR 20000 1234567890
4 RRRRR ISE INSTRUCTOR 35000 4563271890
Total number of nodes : 3
1del_front 2del_rear 3.Exit
Enter your choice:2
Data Deleted!
SSN NAME DEPARTMENT DESIGNATION SALARY PHONENO
2 BBBBB ISE INSTRUCTOR 25000 4569832170
1 AAAAA ISE INSTRUCTOR 20000 1234567890
Total number of nodes : 2
1del_front 2del_rear 3.Exit
Enter your choice:3
-------EMPLOYEE DATABASE SYSTEM ------
1.Create 2.Display 3.Insert 4.Delete 5.Queue 6.Exit
Enter your choice:6
35 | P a g e
PROGRAM 9
9. Develop a Program in C for the following operations on 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.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct node {
int cf, px, py, pz;
int flag;
struct node *link;
};
NODE* getnode() {
NODE *x;
x = (NODE*)malloc(sizeof(NODE));
if(x == NULL) {
printf("Insufficient memory\n");
exit(0);
}
return x;
}
36 | P a g e
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");
}
37 | P a g e
}
while(p1 != h1) {
x1 = p1->px;
y1 = p1->py;
z1 = p1->pz;
cf1 = p1->cf;
p2 = h2->link;
while(p2 != h2) {
x2 = p2->px;
y2 = p2->py;
z2 = p2->pz;
cf2 = p2->cf;
if(x1 == x2 && y1 == y2 && z1 == z2) break;
p2 = p2->link;
}
if(p2 != h2) {
cf = cf1 + cf2;
p2->flag = 1;
if(cf != 0)
h3 = insert_rear(cf, x1, y1, z1, h3);
} else
h3 = insert_rear(cf1, x1, y1, z1, 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;
}
38 | P a g e
printf("\nPolynomial result is: %f", result);
}
int main() {
NODE *h, *h1, *h2, *h3;
int ch;
while(1) {
printf("\n\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");
h = getnode();
h->link = h;
h = read_poly(h);
display(h);
evaluate(h);
break;
case 2:
printf("\nEnter the first polynomial:");
h1 = getnode();
h1->link = h1;
h1 = read_poly(h1);
printf("\nEnter the second polynomial:");
h2 = getnode();
h2->link = h2;
h2 = read_poly(h2);
h3 = getnode();
h3->link = h3;
h3 = add_poly(h1, h2, h3);
printf("\nFirst polynomial is: ");
display(h1);
printf("\nSecond polynomial is: ");
display(h2);
printf("\nThe sum of 2 polynomials is: ");
display(h3);
break;
case 3:
exit(0);
break;
default:
printf("\nInvalid entry");
break;
}
}
return 0;
}
39 | P a g e
OUTPUT
1.Evaluate polynomial
2.Add two polynomials
3.Exit
Enter your choice: 1
Enter coeff: 6
Enter coeff: -4
Enter coeff: 3
Enter coeff: 2
Enter coeff: -2
6 x^2 y^2 z^1 + -4 x^0 y^1 z^5 + 3 x^3 y^1 z^1 + 2 x^1 y^5 z^1 + -2 x^1 y^1 z^3
1.Evaluate polynomial
2.Add two polynomials
3.Exit
Enter your choice: 2
40 | P a g e
Enter the first polynomial:
Enter coeff: 4
Enter coeff: 3
41 | P a g e
PROGRAM 10
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>
#include<stdlib.h>
struct node{
int data ;
struct node *llink;
struct node *rlink;
};
typedef struct node NODE;
42 | P a g e
}
if(item < prev->data){
prev->llink = temp;
}
else{
prev->rlink = temp;
}
return root;
}
NODE* construct_bsf(NODE*root){
int n , a;
printf("Enter the number of element:");
scanf("%d",&n);
printf("Enter Elements : ");
for(int i = 0 ; i<n ; i++ ){
scanf("%d",&a);
root = insert(a,root);
}
printf("Tree created Successfully !\n");
return root ;
}
int search_item(NODE *root , int key){
if(root == NULL) {
printf("Tree Empty!");
return root;
}
NODE *cur ;
cur = root;
while(cur){
if(cur->data == key){
printf("Key found!");
break;
}
else{
if(key < cur->data) cur = cur->llink;
else cur=cur->rlink;
}
}
if(cur == NULL) printf("KEY not found!");
}
void preorder(NODE *root){
if(root != NULL){
printf("%d\t",root->data);
preorder(root->llink);
preorder(root->rlink);
}
}
void postorder(NODE *root){
if(root != NULL){
43 | P a g e
postorder(root->llink);
postorder(root->rlink);
printf("%d\t",root->data);
}
}
void inorder(NODE *root){
if(root != NULL){
inorder(root->llink);
printf("%d\t",root->data);
inorder(root->rlink);
}
}
void main(){
NODE *root;
root = NULL;
int ch , key;
while(1){
printf("\n1.Construct Tree\n2.Preorder\n3.Postorder\n4.Inorder\n5.Search\n6.Exit\nEnter
your Choice:");
scanf("%d",&ch);
switch(ch){
case 1:root = construct_bsf(root);
break;
case 2:preorder(root);
break;
case 3:postorder(root);
break;
case 4:inorder(root);
break;
case 5:printf("Enter key to search:");
scanf("%d",&key);
search_item(root,key);
break;
case 6:exit(0);
default:printf("Invalid Choice");
}
}
}
OUTPUT
1.Construct Tree
2.Preorder
3.Postorder
4.Inorder
5.Search
6.Exit
Enter your Choice:1
44 | P a g e
Enter the number of element:4
Enter Elements : 1 2 3 4
Tree created Successfully !
1.Construct Tree
2.Preorder
3.Postorder
4.Inorder
5.Search
6.Exit
Enter your Choice:2
1 2 3 4
1.Construct Tree
2.Preorder
3.Postorder
4.Inorder
5.Search
6.Exit
Enter your Choice:3
4 3 2 1
1.Construct Tree
2.Preorder
3.Postorder
4.Inorder
5.Search
6.Exit
Enter your Choice:4
1 2 3 4
1.Construct Tree
2.Preorder
3.Postorder
4.Inorder
5.Search
6.Exit
Enter your Choice:5
Enter key to search:1
Key found!
1.Construct Tree
2.Preorder
3.Postorder
4.Inorder
5.Search
6.Exit
Enter your Choice:5
Enter key to search:6
KEY not found!
45 | P a g e
1.Construct Tree
2.Preorder
3.Postorder
4.Inorder
5.Search
6.Exit
Enter your Choice:6
PROGRAM 11A
#include<stdio.h>
#include<stdlib.h>
int f,r,a[10][10],q[20],v,u,n;
int s[10]={0};
void bfs(){
r=-1,f=0;
printf("Nodes visited from %d\n",u);
s[u]=1;
q[++r]=u;
printf("%d\t",u);
while(f<=r){
u=q[f++];
for(v=1;v<=n;v++){
if(a[u][v]==1){
if(s[v]==0){
printf("%d\t",v);
q[++r]=v;
s[v]=1;
}
}
}
}
}
int main(){
46 | P a g e
printf("Enter the number of nodes:");
scanf("%d",&n);
printf("Enter adjacency matrix:\n");
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%d",&a[i][j]);
}
}
printf("Enter the starting node:");
scanf("%d",&u);
bfs();
return 0;
}
OUTPUT
47 | P a g e
PROGRAM 11B
b. Print all the nodes reachable from a given starting node in a digraph using DFS/BFS
method.
#include<stdio.h>
#include<stdlib.h>
int f,r,a[10][10],q[20],v,u,n;
int visited[10]={0};
48 | P a g e
printf("Enter the starting node:");
scanf("%d",&u);
printf("Nodes visited from %d \n ",u);
printf("%d\t",u);
dfs(u);
return 0;
}
OUTPUT
49 | P a g e
PROGRAM 12
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. Design and 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>
#include<stdlib.h>
#include<string.h>
int H(k){
return k % hashsize;
50 | P a g e
}
int main(){
EMPLOYEE a[hashsize];
int ch , id ;
char name[30];
create_hash_table(a);
while(1){
printf("HASH_TABLE\n1.INSERT\n2.DISPLAY\n3EXIT\nEnter Your Choice:");
scanf("%d",&ch);
switch(ch){
case 1:
printf("\nEnter name :");
scanf("%s",name);
printf("Enter id:");
scanf("%d",&id);
insert_table(a,id,name);
break;
case 2:
display(a);
break;
51 | P a g e
case 3:
exit(0);
}
}
return 0;
}
OUTPUT
52 | P a g e
53 | P a g e