Dsa Lab Finall 2.0
Dsa Lab Finall 2.0
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define NUM_DAYS_IN_WEEK 7
typedef struct
{
char *acDayName;
int iDate;
char *acActivity;
}DAYTYPE;
void fnFreeCal(DAYTYPE*);
void fnDispCal(DAYTYPE*);
void fnReadCal(DAYTYPE*);
DAYTYPE *fnCreateCal();
int main()
{
DAYTYPE *weeklyCalendar=fnCreateCal();
fnReadCal(weeklyCalendar);
fnDispCal(weeklyCalendar);
fnFreeCal(weeklyCalendar);
return 0;
}
DAYTYPE *fnCreateCal()
{
DAYTYPE*Calendar=(DAYTYPE*)malloc(NUM_DAYS_IN_WEEK*sizeof(DAYTYPE))
;
int i;
for(i=0;i<NUM_DAYS_IN_WEEK;i++)
{
Calendar[i].acDayName=NULL;
Calendar[i].iDate=0;
Calendar[i].acActivity=NULL;
}
return Calendar;
}
void fnReadCal(DAYTYPE*Calendar)
{
char cChoice;
int i;
for(i=0;i<NUM_DAYS_IN_WEEK;i++)
{
printf("do you want to enter details for day %d[Y/N]:",i+1);
scanf("%c",&cChoice);
getchar();
if(tolower(cChoice)=='n')
continue;
printf("Day Name:");
char nameBuffer[50];
scanf("%s",nameBuffer);
Calendar[i].acDayName = strdup(nameBuffer);
printf("Date:");
scanf("%d",&Calendar[i].iDate);
printf("Activity:");
char activityBuffer[100];
scanf("%s",activityBuffer);
Calendar[i].acActivity = strdup(activityBuffer);
printf("\n");
getchar();
}
}
void fnDispCal(DAYTYPE*Calendar)
{
printf("\nWeek's Activity Details:\n");
int i;
for(i=0;i<NUM_DAYS_IN_WEEK;i++)
{
printf("Day %d\n",i+1);
if(Calendar[i].iDate==0)
{
printf("No Activity\n\n");
continue;
}
printf("Day Name:%s\n",Calendar[i].acDayName);
printf("Date:%d\n",Calendar[i].iDate);
printf("Activity:%s\n\n",Calendar[i].acActivity);
}
}
void fnFreeCal(DAYTYPE*Calendar)
{
int i;
for(i=0;i<NUM_DAYS_IN_WEEK;i++)
{
free(Calendar[i].acDayName);
free(Calendar[i].acActivity);
}
free(Calendar);
}
OUTPUT:
#include <stdio.h>
#include <string.h>
void main() {
char s[20], pat[20], rep[20], ans[30];
int i, j, k, flag;
printf("\n Enter string: ");
scanf("%s", s);
printf("\n Enter pattern: ");
scanf("%s", pat);
printf("\n Enter replacement: ");
scanf("%s", rep);
if (flag == 1) {
for (j = 0; rep[j] != '\0'; j++, k++) {
ans[k] = rep[j];
}
i += strlen(pat) - 1; // Move past the pattern in the original string
} else {
ans[k++] = s[i];
}
}
ans[k] = '\0';
printf("%s\n", ans);
}
OUTPUT:
#include<stdio.h>
#include<stdlib.h>
#define MAX 3
int top=-1,s[MAX];
void push(int item);
int pop();
void palindrome();
void display();
void main()
{
int choice,item;
printf("\n\n\n\n~~~~~Menu~~~~~:");
printf("\n 1.Push an element to stack and overflow demo");
printf("\n 2.Pop an element from stack and underflow demo");
printf("\n 3.Palindrome demo");
printf("\n 4.Display");
printf("\n 5.Exit");
while(1)
{
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n Enter an element to be pushed:");
scanf("%d",&item);
push(item);
break;
case 2:
item=pop();
if (item!=-1)
printf("\n Element popped is : %d",item);
break;
case 3:
palindrome();
break;
case 4:
display();
break;
case 5:
exit(1);
default:
printf("\n Please enter valid choice");
break;
}
}
}
int pop()
{
int item;
if (top==-1)
{
printf("\n ~~~~Stack underflow~~~~");
return -1;
}
item=s[top];
top=top-1;
return item;
}
void display()
{
int i;
if(top==-1)
{
printf("\n ~~~~Stack is empty~~~~");
return;
}
printf("\n stack elements are:\n");
for(i=top;i>=0;i--)
printf("|%d|\n",s[i]);
}
void palindrome()
{
int flag=1,i;
printf("\n Stack content are:\n");
for(i=top;i>=0;i--)
printf("|%d|\n",s[i]);
printf("\n Reverse of stack content are :\n");
for(i=0;i<=top;i++)
printf("|%d|\n",s[i]);
for(i=0;i<=top/2;i++)
{
if(s[i]!=s[top-i])
{
flag=0;
break;
}
}
if(flag==1)
{
printf("\n It is palindrome number");
}
else
{
printf("\n It is not a palindrome number");
}
}
OUTPUT:
~~~~~Menu~~~~~:
1.Push an element to stack and overflow demo
2.Pop an element from stack and underflow demo
3.Palindrome demo
4.Display
5.Exit
Enter your choice:1
Enter an element to be pushed:2
~~~~~Menu~~~~~:
1.Push an element to stack and overflow demo
2.Pop an element from stack and underflow demo
3.Palindrome demo
4.Display
5.Exit
Enter your choice:1
Enter an element to be pushed:3
OUTPUT:
~~~~~Menu~~~~~:
1.Push an element to stack and overflow demo
2.Pop an element from stack and underflow demo
3.Palindrome demo
4.Display
5.Exit
Enter your choice:1
Enter an element to be pushed:4
~~~~~Menu~~~~~:
1.Push an element to stack and overflow demo
2.Pop an element from stack and underflow demo
3.Palindrome demo
4.Display
5.Exit
Enter your choice:1
Enter an element to be pushed:5
~~~~Stack underflow~~~~
~~~~~Menu~~~~~:
1.Push an element to stack and overflow demo
2.Pop an element from stack and underflow demo
3.Palindrome demo
4.Display
5.Exit
Enter your choice:4
stack elements are:
|4|
|3|
|2|
~~~~~Menu~~~~~:
1.Push an element to stack and overflow demo
2.Pop an element from stack and underflow demo
3.Palindrome demo
4.Display
5.Exit
Enter your choice:3
stack elements are:
|4|
|3|
|2|
Reverse of stack content are:
|2|
|3|
|4|
It is not a palindrome number
~~~~~Menu~~~~~:
1.Push an element to stack and overflow demo
2.Pop an element from stack and underflow demo
3.Palindrome demo
4.Display
5.Exit
Enter your choice:2
Element popped is : 4
~~~~~Menu~~~~~:
1.Push an element to stack and overflow demo
2.Pop an element from stack and underflow demo
3.Palindrome demo
4.Display
5.Exit
Enter your choice:5
_
#include<stdio.h>
#include<stdlib.h>
void evaluate();
void push(char);
char pop();
int prec(char);
int top=-1;
char infix[30],postfix[30],stack[30];
void main()
{
printf("\n Enter the valid infix expression:");
scanf("%s",infix);
evaluate();
printf("\n Entered infix expression is :\n%s\n",infix);
printf("\n The corrresponding postfix expression is :\n%s\n",postfix);
}
void evaluate()
{
int i=0,j=0;
char symb,temp;
push('#');
for(i=0;infix[i]!='\0';i++)
{
symb=infix[i];
switch(symb)
{
case '(':
push(symb);
break;
case ')':
temp=pop();
while(temp!='(')
{
postfix[j]=temp;
j++;
temp=pop();
}
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
case '$':
while(prec(stack[top])>=prec(symb))
{
temp=pop();
postfix[j]=temp;
j++;
}
push(symb);
break;
default:
postfix[j]=symb;
j++;
}
}
while(top>0)
{
temp=pop();
postfix[j]=temp;
j++;
}
postfix[j]='\0';
}
void push(char item)
{
top=top+1;
stack[top]=item;
}
char pop()
{
char item;
item=stack[top];
top=top-1;
return item;
}
int prec(char symb)
{
int p;
switch(symb)
{
case '#':
p=-1;
break;
case '(':
case ')':
p=0;
break;
case '+':
case '-':
p=1;
break;
case '*':
case '/':
case '%':
p=2;
break;
case '^':return p;
case '$':
p=3;
break;
}
return p;
}
OUTPUT:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
int i, top = -1;
int op1, op2, res, s[20];
char postfix[90], symb;
int pop() {
int item;
item = s[top];
top = top - 1;
return item;
}
void main() {
printf("\n Enter a valid postfix expression:\n");
scanf("%s", postfix);
for (i = 0; postfix[i] != '\0'; i++) { // Removed the misplaced LAB PROGRAM 05
symb = postfix[i];
if (isdigit(symb)) {
push(symb - '0');
} else {
op2 = pop();
op1 = pop();
switch (symb) {
case '+':
push(op1 + op2);
break;
case '-':
push(op1 - op2);
break;
case '*':
push(op1 * op2);
break;
case '/':
push(op1 / op2);
break;
case '%':
push(op1 % op2);
break;
default:
push(0); // You might want to handle this case differently
}
}
}
res = pop();
printf("Result = %d\n", res);
}
OUTPUT:
(b)
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void tower(int n,int source,int temp,int destination)
{
if(n==0)
return;
tower(n-1,source,destination,temp);
printf("\nMove disc %d from %c to %c",n,source,destination);
tower(n-1,temp,source,destination);
}
void main()
{
int n;
printf("\nEnter the number of discs : \n");
scanf("%d",&n);
tower(n,'A','B','C');
printf("\n\nTotal Number of moves are : %d",(int)pow(2,n)-1);
}
OUTPUT:
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>
#include<stdio_ext.h>
#define MAX 3
char cq[MAX];
int front = -1, rear = -1;
while (1) {
printf("\n\n~~Menu~~:");
printf("\n==>1. Insertion and overflow demo");
printf("\n==>2. Deletion and underflow demo");
printf("\n==>3. Display");
printf("\n==>4. Exit");
printf("\nEnter Your choice : ");
scanf("%d", &ch);
__fpurge(stdin);
switch (ch) {
case 1:
printf("\n\nEnter an element to be inserted : ");
scanf(" %c", &item);
insert(item);
break;
case 2:
delete_element();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n\nPlease enter a valid choice");
break;
}
}
}
void delete_element() {
char item;
if (front == -1) {
printf("\n\n~~Circular Queue Underflow~~");
} else {
item = cq[front];
printf("\n\nDeleted element from the queue is : %c", item);
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % MAX;
}
}
}
void display() {
int i;
if (front == -1) {
printf("\n\nCircular Queue is Empty");
} else {
printf("\nCircular Queue contents are : \n");
printf("Front[%d]-> ", front);
for (i = front; i != rear; i = (i + 1) % MAX) {
printf("%c ", cq[i]);
}
printf("%c", cq[i]);
printf(" <-[%d]Rear", rear);
}
}
OUTPUT:
~~Menu~~:
==>1.Insertion and overflow demo
==>2.Deletion and underflow demo
==>3.Display
==>4.Exit
Enter Your choice :1
Enter an element to be inserted :3
~~Menu~~:
==>1.Insertion and overflow demo
==>2.Deletion and underflow demo
==>3.Display
==>4.Exit
Enter Your choice :1
Enter an element to be inserted : 4
~~Menu~~:
==>1.Insertion and overflow demo
==>2.Deletion and underflow demo
==>3.Display
==>4.Exit
Enter Your choice :3
Circular queue contents are:
Front[0]->0 0 0 0 <-Rear[1]
0304
~~Menu~~:
==>1.Insertion and overflow demo
==>2.Deletion and underflow demo
==>3.Display
==>4.Exit
Enter Your choice :2
Deleted element from the queue is : 0 0
03
~~Menu~~:
==>1.Insertion and overflow demo
==>2.Deletion and underflow demo
==>3.Display
==>4.Exit
Enter Your choice :4
_
Develop a menu driven program in c for the following operations on singly linked list
(SSL) of student data with the field : USN, Name ,program, Sem, Ph.No.
(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 of end of SLL
(d)Perform insertion/deletion at front of SLL(Demonstration of stack)
(e)Exit
#include<stdio.h>
#include<stdlib.h>
struct node
{
char usn[25], name[25], branch[25];
int sem;
int phone;
struct node * link;
};
typedef struct node * NODE;
NODE create()
{
NODE snode;
snode = (NODE) malloc(sizeof(struct node));
if (snode == NULL)
{
printf("\nMemory is not available");
exit(1);
}
printf("\nEnter the usn,Name,Branch, sem,PhoneNo of the student:");
scanf("%s %s %s %d %ld", snode -> usn, snode -> name, snode -> branch, & snode ->
sem, & snode -> phone);
snode -> link = NULL;
count++;
return snode;
}
NODE insertfront()
{
NODE temp;
temp = create();
if (start == NULL)
{
return temp;
}
NODE deletefront()
{
NODE temp;
if (start == NULL)
{
printf("\nLinked list is empty");
return NULL;
}
NODE insertend()
{
NODE cur, temp;
temp = create();
if (start == NULL)
{
return temp;
}
cur = start;
while (cur -> link != NULL)
{
cur = cur -> link;
}
cur -> link = temp;
return start;
}
NODE deleteend()
{
NODE cur, prev;
if (start == NULL)
{
printf("\nLinked List is empty");
return NULL;
}
prev = NULL;
cur = start;
while (cur -> link != NULL)
{
prev = cur;
cur = cur -> link;
}
printf("\nThe student node with the usn:%s is deleted", cur -> usn);
free(cur);
prev -> link = NULL;
count--;
return start;
}
void display()
{
NODE cur;
int num = 1;
if (start == NULL)
{
void stackdemo()
{
int ch;
int main()
{
int ch, i, n;
printf("\n--------Menu--------");
printf("\nEnter your choice for SLL operation \n");
printf("\n1:Create SLL of Student Nodes");
printf("\n2:DisplayStatus");
printf("\n3:InsertAtEnd");
printf("\n4:DeleteAtEnd");
printf("\n5:Stack Demo using SLL(Insertion and Deletion at Front)");
printf("\n6:Exit \n");
while (1)
{
printf("\nEnter your choice:");
scanf("%d", & ch);
switch (ch)
{
case 1:
printf("\nEnter the no of students: ");
scanf("%d", & n);
for (i = 1; i <= n; i++)
start = insertfront();
break;
case 2:
display();
break;
case 3:
start = insertend();
break;
case 4:
start = deleteend();
break;
case 5:
stackdemo();
break;
case 6:
exit(0);
default:
printf("\n Please enter the valid choice");
}
}
}
OUTPUT:
SLL OPERATIONS
==========
1.Insert Front
2. Insert Rear
3. Delete Front
4. Delete Rear
5. Display
6.Exit
Enter your choice
1
Enter USN:1ew22cs011
Enter name: anoop
Enter the program name: cse
Enter semester:3
Enter the phone no:847846553
SLL OPERATIONS
==========
1.Insert Front
2. Insert Rear
3. Delete Front
4. Delete Rear
5. Display
6.Exit
Enter your choice
2
Enter USN:1ew22cs012
Enter name: akash
Enter the program name: cse
Enter semester:3
Enter the phone no:65845489
SLL OPERATIONS
==========
1.Insert Front
2. Insert Rear
3. Delete Front
4. Delete Rear
5. Display
6.Exit
Enter your choice
5
The contents of SLL are:
USN
1ew22cs011 anoop cse 3 847846553
1ew22cs010 anu cse 3 847654658
1ew22cs012 akash cse 3 65845489
SLL has 3 nodes
SLL OPERATIONS
==========
1.Insert Front
2. Insert Rear
3. Delete Front
4. Delete Rear
5. Display
6.Exit
Enter your choice
3
Node deleted is anoop
Develop a menu driven program in c for the following operations on doubly linked
list (DSL) of employee data with the field : SSN, Name ,dept, designation,
sal ,Ph.No.
(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/deletion of end of DLL
(d)Perform insertion/deletion at front of DLL
(e)Demonstration how this DLL can be used as doubly ended queue
(f)Exit
#include <stdio.h>
#include <stdlib.h>
struct node {
char ssn[25], name[25], dept[10], designation[25];
int sal;
long int phone;
struct node *llink;
struct node *rlink;
};
NODE create() {
NODE enode = (NODE)malloc(sizeof(struct node));
if (enode == NULL) {
printf("\nRunning out of memory");
exit(0);
}
printf("\nEnter the ssn, Name, Department, Designation, Salary, PhoneNo of the
employee: \n");
scanf("%s %s %s %s %d %ld", enode->ssn, enode->name, enode->dept,
enode->designation, &enode->sal, &enode->phone);
enode->llink = NULL;
enode->rlink = NULL;
count++;
return enode;
}
NODE insertfront() {
NODE temp = create();
if (first == NULL) {
return temp;
}
temp->rlink = first;
first->llink = temp;
return temp;
}
void display() {
NODE cur = first;
int nodeno = 1;
if (cur == NULL) {
printf("\nNo Contents to display in DLL");
return;
}
printf("\nENode:%d||SSN:%s|Name:%s|Department:%s|Designation:%s|Salary:%d|Phone
no:%ld",
nodeno, cur->ssn, cur->name, cur->dept, cur->designation, cur->sal, cur->phone);
cur = cur->rlink;
nodeno++;
}
printf("\nNo of employee nodes is %d", count);
}
NODE deletefront() {
if (first == NULL) {
printf("\nDoubly Linked List is empty");
return NULL;
}
first = first->rlink;
if (first != NULL) {
first->llink = NULL;
}
free(temp);
count--;
return first;
}
NODE insertend() {
NODE cur, temp = create();
if (first == NULL) {
return temp;
}
cur = first;
while (cur->rlink != NULL) {
cur = cur->rlink;
}
cur->rlink = temp;
temp->llink = cur;
return first;
}
NODE deleteend() {
if (first == NULL) {
printf("\nDoubly Linked List is empty");
return NULL;
}
free(cur);
count--;
return first;
}
void deqdemo() {
int ch;
printf("\nDemo Double Ended Queue Operation");
printf("\n1:InsertQueueFront\n2:DeleteQueueFront\n3:InsertQueueRear\n4:DeleteQueueRe
ar\n5:DisplayStatus\n6:Exit\n");
while (1) {
OUTPUT:
Develop a program in C for the following operations singly circular linked list (SSL)
with header nodes
(a)Represent and evaluate a polynomial P(x,y,z)=6x^2y^2z-4yz^5+3x^3yz+2xy^5z-
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.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct node
{
int coef;
int xexp, yexp, zexp;
struct node * link;
};
typedef struct node * NODE;
NODE getnode()
{
NODE x;
x = (NODE) malloc(sizeof(struct node));
if (x == NULL)
{
printf("Running out of memory \n");
return NULL;
}
return x;
}
NODE attach(int coef, int xexp, int yexp, int zexp, NODE head)
{
NODE temp, cur;
temp = getnode();
temp -> coef = coef;
temp -> xexp = xexp;
temp -> yexp = yexp;
temp -> zexp = zexp;
cur = head -> link;
while (cur -> link != head)
{
cur = cur -> link;
}
cur -> link = temp;
temp -> link = head;
return head;
}
case 0:
if (a -> yexp > b -> yexp)
{
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
break;
}
else if (a -> yexp < b -> yexp)
{
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
break;
}
else if (a -> zexp > b -> zexp)
{
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
break;
}
else if (a -> zexp < b -> zexp)
{
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
break;
}
case 1:
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
break;
}
break;
}
if (a -> yexp != 0 || b -> yexp != 0)
{
switch (COMPARE(a -> yexp, b -> yexp))
{
case -1:
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
break;
case 0:
if (a -> zexp > b -> zexp)
{
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
break;
}
else if (a -> zexp < b -> zexp)
{
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
break;
}
case 1:
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
break;
}
break;
}
if (a -> zexp != 0 || b -> zexp != 0)
{
switch (COMPARE(a -> zexp, b -> zexp))
{
case -1:
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
break;
case 1:
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
break;
}
break;
}
}
}
while (a != head1)
{
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
}
while (b != head2)
{
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
}
return head3;
}
void main()
{
NODE head, head1, head2, head3;
int res, ch;
head = getnode();
head1 = getnode();
head2 = getnode();
head3 = getnode();
case 2:
printf("\nEnter the POLY1(x,y,z): \n");
head1 = read_poly(head1);
printf("\nPolynomial 1 is: \n");
display(head1);
--------Menu--------
1.Represent and Evaluate a Polynomial P(x,y,z)
2.Find the sum of two polynomials POLY1(x,y,z)
Enter your choice:1
--------Menu--------
1.Represent and Evaluate a Polynomial P(x,y,z)
2.Find the sum of two polynomials POLY1(x,y,z)
Enter your choice:2
Polynomial 1 is:
1x^2y^3z^4 + 2x^1y^1z^1
Enter the POLY2(x,y,z):
Polynomial 2 is:
2x^2y^3z^1 + 1x^1y^1z^1
Polynomial addition result:
1x^2y^3z^4 + 2x^2y^3z^1 + 3x^1y^1z^1
--------Menu--------
1.Represent and Evaluate a Polynomial P(x,y,z)
2.Find the sum of two polynomials POLY1(x,y,z)
Enter your choice:
=== Session Ended. Please Run the code again ===
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,15,2
(b)Traverse the BST in inorder, preorder, postorder
(c)Search the BST for a given element (KEY) and report the appropriate message
(d)Exit
#include<stdio.h>
#include<stdlib.h>
struct BST{
int data;
struct BST * lchild;
struct BST * rchild;
};
typedef struct BST* NODE;
NODE create()
{
NODE temp;
temp=(NODE)malloc(sizeof(struct BST));
printf("\nEnter the value:");
scanf("%d",&temp->data);
temp->lchild=NULL;
temp->rchild=NULL;
return temp;
}
void main()
{
int ch,key,val,i,n;
NODE root=NULL,newnode;
while(1)
{
printf("\n~~~BST MENU~~~~~");
printf("\n1.Creat a BST:");
printf("\n2.BST Traversal:");
printf("\n3.search:");
printf("\n4.Exit:");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter the number of elements:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
newnode=create();
if(root==NULL)
root=newnode;
else
insert(root,newnode);
}
break;
case 2:if(root==NULL)
printf("\nTree is not created:");
else
{
printf("\nThe preorder display:");
preorder(root);
printf("\nThe inorder display:");
inorder(root);
printf("\nThe postorder display:");
postorder(root);
}
break;
case 3:search(root);
break;
case 4:exit(0);
}
}
}
OUTPUT:
~~~BST MENU~~~~~
1.Creat a BST:
2.Search:
3.BST Transversal:
4.Exit:
Enter your choice:1
Enter the number of elements:3
Enter the value:2
Enter the value:1
Enter the value:3
~~~BST MENU~~~~~
1.Creat a BST:
2.Search:
3.BST Transversal:
4.Exit:
Enter your choice:3
The preorder display : 2 1 3
The inorder display : 1 2 3
The postorder display : 1 3 2
~~~BST MENU~~~~~
1.Creat a BST:
2.Search:
3.BST Transversal:
4.Exit:
Enter your choice:2
Enter element to be searched :1
Key element is present in BST
LAB PROGRAM 11(running)
#include <stdio.h>
#include <stdlib.h>
void create() {
printf("\nEnter the number of vertices of the digraph: ");
scanf("%d", &n);
printf("\nEnter the adjacency matrix of the graph:\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;
printf("\nEnter the source vertex to find other nodes reachable or not: ");
scanf("%d", &source);
q[++rear] = source;
visited[source] = 1;
printf("\nThe reachable vertices are: \n%d", source);
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("\n%d", i);
}
}
}
for (i = 1; i <= n; i++) {
if (visited[i] == 0)
printf("\nThe vertex that is not reachable %d", i);
}
}
void main() {
int ch;
while (1) {
printf("\n1. Create Graph\n2. BFS\n3. DFS\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1: create(); break;
case 2: bfs(); break;
case 3:
printf("\nEnter the source vertex to find the connectivity: ");
scanf("%d", &source);
printf("Reachable nodes\n");
dfs(source);
break;
default: exit(0);
}
}
}
OUTPUT:
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 an 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>
#include<stdlib.h>
int key[20],n,m;
int*ht,indx;
int count=0;
void insert(int key)
{
indx=key%m;
while(ht[indx]!=-1)
{
indx=(indx+1)%m;
}
ht[indx]=key;
count++;
}
void display()
{
int i;
if(count==0)
{
printf("\n hash table is empty");
return;
}
printf("\n hash table contents are:\n");
for(i=0;i<m;i++)
printf("\nT[%d]->%d",i,ht[i]);
}
void main()
{
int i;
printf("\nenter the number of employee records[N]:");
scanf("%d",&n);
printf("\n enter two digit memory location (m) for hash table:");
scanf("%d",&m);
ht=(int*)malloc(m*sizeof(int));
for(i=0;i<m;i++)
ht[i]=-1;
printf("\n enter four digit key values (k) for N employee records:\n");
for(i=0;i<n;i++)
scanf("%d",&key[i]);
for(i=0;i<n;i++)
{
if(count==m)
{
printf("\n~~~~~hash table is full.cannot insert the record %d key~~~~",i+1);
break;
}
insert(key[i]);
}
display();
}
OUTPUT: