0% found this document useful (0 votes)
3 views

DSA 1_10

The document contains two C++ programs related to student management. The first program allows creating, displaying, sorting, and searching student records using various algorithms, while the second program implements a linked list to manage student information with functionalities for insertion, deletion, counting, and displaying data. Both programs include user interaction through a menu-driven interface for executing different operations.

Uploaded by

Mr. Omkar
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)
3 views

DSA 1_10

The document contains two C++ programs related to student management. The first program allows creating, displaying, sorting, and searching student records using various algorithms, while the second program implements a linked list to manage student information with functionalities for insertion, deletion, counting, and displaying data. Both programs include user interaction through a menu-driven interface for executing different operations.

Uploaded by

Mr. Omkar
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
You are on page 1/ 42

EXPERIMENT NO.

1
PROGRAM CODE:-
#include<iostream>
#include<string.h>
using namespace std;
typedef struct student
{
int roll_num;
char name [20];
float marks;
}stud;
void create(stud s[20],int n);
void display(stud s[20],int n);
void bubble_sort(stud s[20],int n);
void insertionSort(stud s[20],int n);
void quick_sort(stud s[20],int,int);
int partition(stud s[20],int,int);
void search(stud s[20],int n,int key);
int bsearch(stud s[20], char x[20],int low,int high);
int main()
{
stud s[20];
int ch,n,key,result;
char x[20];
do
{
cout<<"\n 1) Create Student Database ";
cout<<"\n 2) Display Student Records ";
cout<<"\n 3) Bubble Sort ";
cout<<"\n 4) Insertion Sort ";
cout<<"\n 5) Quick Sort ";
cout<<"\n 6) Linear search ";
cout<<"\n 7) Binary search ";
cout<<"\n 8) Exit ";
cout<<"\n Enetr Your Choice:=";
cin>>ch;
switch(ch)
{case 1:
cout<<"\n Enter The Number Of Records:=";
cin>>n;
create(s,n);
break;
case 2:
display(s,n);
break;
case 3:
bubble_sort(s,n);
break;
case 4:
insertionSort(s,n);
break;
case 5:
quick_sort(s,0,n-1);
cout<<"\n"<< "\t"<< "Roll No"<< "\t"<<" Name" <<"\t"<< "Marks";
for(int i=n-1; i>=n-10; i--)
{ cout<<"\n";
cout<<"\t "<< s[i].roll_num<<"\t "<<s[i].name<<"\t "<<s[i].marks;}
break;
case 6:
cout<<"\n Enter the marks which u want to search:=";
cin>>key;
search(s,n,key);
break;
case 7:
cout<<"\n Enter the name of student which u want to search:=";
cin>>x;
insertionSort(s,n);
result=bsearch(s,x,0,(n-1));
if(result==-1)
{cout<<" \n Student name you want to search for is not present ! \n";}
else {cout<<" \n The student is present :\t" << s[result].name;}
break;
case 8:return 0;
default:cout<<"\n Invalid choice !! Please enter your choice again."<<endl;}
}while(ch!=8);}
void create(stud s[20],int n)
{int i;
for(i=0;i<n;i++)
{cout<<"\n Enter the roll number:=";
cin>>s[i].roll_num;
cout<<"\n Enter the Name:=";
cin>>s[i].name;
cout<<"\n Enter the marks:=";
cin>>s[i].marks;
}}
void display(stud s[20],int n)
{int i;
cout<<"\n"<< "\t"<< "Roll No"<< "\t"<<" Name" <<"\t"<< "Marks";
for(i=0;i<n;i++)
{cout<<"\n";
cout<<"\t "<< s[i].roll_num<<"\t "<<s[i].name<<"\t "<<s[i].marks;
}}
//bubble sort to sort in ascending order on roll number
void bubble_sort(stud s[20],int n)
{
int i,j;
stud temp;
for(i=1;i<n;i++)
{for(j=0;j<n-i;j++)
{if(s[j].roll_num>s[j+1].roll_num)
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}}}}
// insertion sort to sort on names in ascending order
void insertionSort(stud s[20], int n)
{ int i, j;
stud key;
for (i = 1; i < n; i++){

key= s[i]; j = i - 1;
/* Move elements of arr[0..i-1], that are

greater than key, to one position ahead


of their current position */
while (j >= 0 && strcmp(s[j].name, key.name) >0)
{
s[j + 1]= s[j];
j = j - 1;
}
s[j + 1]= key;
}
}
//Quick sort to sort on marks
void quick_sort(stud s[20], int l,int u)
{
int j;
if(l<u)
{
j=partition(s,l,u);
quick_sort(s,l,j-1);
quick_sort(s,j+1,u);
}
}
int partition(stud s[20], int l,int u)
{
int i,j;
stud temp, v;
v=s[l]; i=l; j=u+1;
do
{
do
i++;
while(s[i].marks<v.marks&&i<=u);
do
j--;
while(v.marks<s[j].marks);
if(i<j)
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}while(i<j);
s[l]=s[j];
s[j]=v;
return(j);
}
// linear search for marks if more than one student having same marks print all of them
void search(stud s[20],int n,int key)
{
int i;
cout<<"\n"<< "\t"<< "Roll No"<< "\t"<<" Name" <<"\t"<< "Marks";
for(i=0;i<n;i++)
{
if(key==s[i].marks)

{
cout<<"\n\t "<< s[i].roll_num<<"\t "<<s[i].name<<"\t "<<s[i].marks;
}}}
int bsearch(stud s[20], char x[20],int low,int high)
{
int mid;
while(low<=high)
{
mid=(low+high)/2;
if(strcmp(x,s[mid].name)==0)
{
return mid;
}
else if(strcmp(x,s[mid].name)<0)
{
high=mid-1;
}
else
{
low=mid+1;
}}
return -1;}
OUTPUT:
1) Create Student Database
2) Display Student Records
3) Bubble Sort
4) Insertion Sort
5) Quick Sort
6) Linear search
7) Binary search
8) Exit
Enetr Your Choice:=1
Enter The Number Of Records:=3
Enter the roll number:=4
Enter the Name:=sanju
Enter the marks:=78
Enter the roll number:=1
Enter the Name:=shantanu
Enter the marks:=90
Enter the roll number:=10
Enter the Name:=omkar
Enter the marks:=99

1) Create Student Database


2) Display Student Records
3) Bubble Sort
4) Insertion Sort
5) Quick Sort
6) Linear search
7) Binary search
8) Exit
Enter Your Choice:=2
Roll No Name Marks
4 sanju 78
1 shanky 90
10 omkar 99

1) Create Student Database


2) Display Student Records
3) Bubble Sort
4) Insertion Sort
5) Quick Sort
6) Linear search
7) Binary search
8) Exit
Enter Your Choice:=3

1) Create Student Database


2) Display Student Records
3) Bubble Sort
4) Insertion Sort
5) Quick Sort
6) Linear search
7) Binary search
8) Exit
Enter Your Choice:=2
Roll No Name Marks
1 shanky 90
4 sanju 78
10 omkar 99
1) Create Student Database
2) Display Student Records
3) Bubble Sort
4) Insertion Sort
5) Quick Sort
6) Linear search
7) Binary search
8) Exit
Enter Your Choice:=4
1) Create Student Database
2) Display Student Records
3) Bubble Sort
4) Insertion Sort
5) Quick Sort
6) Linear search
7) Binary search
8) Exit
Enter Your Choice:=2
Roll No Name Marks
1 shanky 90
4 sanju 78
10 omkar 99

1) Create Student Database


2) Display Student Records
3) Bubble Sort
4) Insertion Sort
5) Quick Sort

6) Linear search
7) Binary search
8) Exit
Enter Your Choice:=2

Roll No Name Marks


1 shanky 90
4 sanju 78
10 omkar 99
1) Create Student Database
2) Display Student Records
3) Bubble Sort
4) Insertion Sort
5) Quick Sort
6) Linear search
7) Binary search
8) Exit
Enter Your Choice:=8
Experiment no. 2
Program code:-
#include<iostream>
#include<string.h>
using namespace std;
struct node
{ int prn,rollno;
char name[50];
struct node *next;
};
class info
{
node
*s=NULL,*head1=NULL,*temp1=NULL,*head2=NULL,*temp2=NULL,*head=NULL,*temp
=NULL;
int b,c,i,j,ct;
char a[20];
public:
node *create()
void insertp();
void insertm()
void delm();
void delp();
void dels();
void display();
void count();
void reverse();
void rev(node *p);
void concat();
};
node *info::create()
{ node *p=new(struct node);
cout<<"enter name of student ";
cin>>a;
strcpy(p->name,a);
cout<<"\n enter prn no. of student \n";
cin>>b;
p->prn=b;
cout<<"enter student rollno";
cin>>c;
p->rollno=c;
p->next=NULL;
return p;
}
void info::insertm()
{
node *p=create();
if(head==NULL)
{ head=p;
}
else
{ temp=head;
while(temp->next!=NULL)
{ temp=temp->next; }
temp->next=p;
} }
void info::insertp()
{
node *p=create();
if(head==NULL)
{ head=p;
}
else
{ temp=head;
head=p;
head->next=temp->next;
}
}
void info::display()
{ if(head==NULL)
{ cout<<"linklist is empty";
}
else
{
temp=head;
cout<<" prn rolln0 NAME \n";
while(temp->next!=NULL)
{ cout<<" \n"<<temp->prn<<" "<<temp->rollno<<" "<<temp->name;
temp=temp->next;
}
cout<<" "<<temp->prn<<" "<<temp->rollno<<" "<<temp->name;
}
}
void info::delm()
{ int m,f=0;
cout<<"\n enter the prn no. of student whose data you want to delete";
cin>>m;
temp=head;
while(temp->next!=NULL)
{
if(temp->prn==m)
{ s->next=temp->next;
delete(temp); f=1;
}
s=temp;
temp=temp->next;
} if(f==0)
{ cout<<"\n sorry memeber not deleted "; }
}
void info::delp()
{ temp=head;
head=head->next;
delete(temp);
}
void info::dels()
{
temp=head;
while(temp->next!=NULL)
{ s=temp;
temp=temp->next;

} s->next=NULL;
delete(temp);
}
void info::count()
{ temp=head; ct=0;
while(temp->next!=NULL)
{ temp=temp->next; ct++; }
ct++;
cout<<" Count of members is:"<<ct;
}
void info::reverse()
{ rev(head); }
void info::rev(node *temp)
{ if(temp==NULL)
{ return; }
else
{ rev(temp->next); }
cout<<" "<<temp->prn<<" "<<temp->rollno<<" "<<temp->name;
}
void info::concat()
{ int k,j;
cout<<"enter no. of members in list1";
cin>>k;
head=NULL;
for(i=0;i<k;i++)
{ insertm();
head1=head;
} head=NULL;
cout<<"enter no. of members in list2";
cin>>j;
for(i=0;i<j;i++)
{ insertm();
head2=head;
} head=NULL;
temp1=head1;
while(temp1->next!=NULL)
{ temp1=temp1->next; }
temp1->next=head2;
temp2=head1;
cout<<" prn rolln0 NAME \n";
while(temp2->next!=NULL)
{
cout<<"\n "<<temp2->prn<<"
temp2=temp2->next;
"<<temp2->rollno<<" "<<temp2->name<<"\n";;
}
cout<<"\n "<<temp2->prn<<" "<<temp2->rollno<<" "<<temp2->name<<"\n";
}
int main()
{ info s;
int i;
char ch;
do{
cout<<"\n choice the options";
cout<<"\n 1. To insert president ";
cout<<"\n 2. To insert member ";
cout<<"\n 3. To insert secretary ";
cout<<"\n 4. To delete president ";
cout<<"\n 5. To delete member ";
cout<<"\n 6. To delete secretary ";
cout<<"\n 7. To display data ";
cout<<"\n 8. Count of members";
cout<<"\n 9. To display reverse of string ";
cout<<"\n 10.To concatenate two strings ";
cin>>i;
switch(i)
{ case 1: s.insertp();
break;
case 2: s.insertm();
break;
case 3: s.insertm();
break;
case 4: s.delp();
break;
case 5: s.delm();
break;
case 6: s.dels();
break;
case 7: s.display();
break;
case 8: s.count();
break;
case 9: s.reverse();
break;
case 10: s.concat();
break;
default: cout<<"\n unknown choice";
}
cout<<"\n do you want to continue enter y/Y \n";
cin>>ch;
}while(ch=='y'||ch=='Y');
return 0;
}
OUTPUT:-
choice the options
1. To insert president
2. To insert member
3. To insert secretary
4. To delete president
5. To delete member
6. To delete secretary
7. To display data
8. Count of members
9. To display reverse of string
10. To concatenate two strings 9

do you want to continue enter y/Y


y

choice the options


1. To insert president
2. To insert member
3. To insert secretary
4. To delete president
5. To delete member
6. To delete secretary
7. To display data
8. Count of members
9. To display reverse of string
10. To concatenate two strings 1
enter name of student asha

enter prn no. of student


1234
enter student rollno43
do you want to continue enter y/Y
y

choice the options


1. To insert president
2. To insert member
3. To insert secretary
4. To delete president
5. To delete member
6. To delete secretary
7. To display data
8. Count of members
9. To display reverse of string
10. To concatenate two strings 2
enter name of student hina
enter prn no. of student
234
enter student rollno8
do you want to continue enter y/Y
y

choice the options


1. To insert president

2. To insert member
3. To insert secretary
4. To delete president
5. To delete member
6. To delete secretary
7. To display data
8. Count of members
9. To display reverse of string
10. To concatenate two strings 3
enter name of student hari
enter prn no. of student
4444
enter student rollno23
do you want to continue enter y/Y
y

choice the options


1. To insert president
2. To insert member
3. To insert secretary
4. To delete president
5. To delete member
6. To delete secretary
7. To display data
8. Count of members
9. To display reverse of string
10. To concatenate two strings 7
prn rolln0 NAME
1234 43 asha
234 8 hina 4444 23 hari
do you want to continue enter y/Y
y
choice the options
1. To insert president
2. To insert member
3. To insert secretary
4. To delete president
5. To delete member
6. To delete secretary
7. To display data
8. Count of members
9. To display reverse of string
10. To concatenate two strings 8
Count of members is:3
do you want to continue enter y/Y
y

choice the options


1. To insert president
2. To insert member
3. To insert secretary
4. To delete president
5. To delete member
6. To delete secretary
7. To display data

8. Count of members
9. To display reverse of string
10. To concatenate two strings 10
enter no. of members in list11
enter name of student wama
enter prn no. of student
23456
enter student rollno22
enter no. of members in list21
enter name of student qqq
enter prn no. of student

2223
enter student rollno34
prn rolln0 NAME

23456 22 wama
2223 34 qqq

do you want to continue enter y/Y


y
choice the options
1. To insert president
2. To insert member
3. To insert secretary
4. To delete president
5. To delete member
6. To delete secretary
7. To display data
8. Count of members
9. To display reverse of string
10. To concatenate two strings 9

do you want to continue enter y/Y


y
choice the options
1. To insert president
2. To insert member
3. To insert secretary
4. To delete president
5. To delete member
6. To delete secretary
7. To display data
8. Count of members
9. To display reverse of string
10. To concatenate two strings 7
linklist is empty
do you want to continue enter y/Y
y
EXPERIMENT NO. 3
PROGRAM:-
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include<ctype.h>
struct node {
int data;
struct node *next;
};
struct node *top = NULL;
/* create a new node with the given data */
struct node* createNode(int data) {
struct node *ptr = (struct node *) malloc(sizeof (struct node));
ptr->data = data;
ptr->next = NULL;
}
/* push the input data into the stack */
void push (int data) {
struct node *ptr = createNode(data);
if (top == NULL) {
top = ptr;
return;
}
ptr->next = top;
top = ptr;
}
/* pop the top element from the stack */
int pop () {
int data;
struct node *temp;
if (top == NULL)
return -1;
data = top->data;
temp = top;
top = top->next;
free(temp);
return (data);
}
int main() {
char str[100];
50nti , data = -1, operand1, operand2, result;
/* i/p postfix expr from the user */
cout<<“Enter ur postfix expression:”;
fgets(str, 100, stdin);
for (i = 0; i < strlen(str); i++) {
if (isdigit(str[i])) {
/*
* if the i/p char is digit, parse
* character by character to get
* complete operand
*/
data = (data == -1) ? 0 : data;
Prepared By.. Prof. Khatal K.B.
data = (data * 10) + (str[i] – 48);

continue;
}
/* push the operator into the stack */
if (data != -1) {
push(data);
}
if (str[i] == ‘+’ || str[i] == ‘-‘
|| str[i] == ‘*’ || str[i] == ‘/’) {
/*
* if the i/p character is an operator,
* then pop two elements from the stack,
* apply operator and push the result into
* the stack
*/
operand2 = pop();
operand1 = pop();
if (operand1 == -1 || operand2 == -1)
break;
switch (str[i]) {
case ‘+’:
result = operand1 + operand2;
/* pushing result into the stack */
push(result);
break;
case ‘-‘:
result = operand1 – operand2;
push(result);
break;
case ‘*’:
result = operand1 * operand2;
push(result);
break;
case ‘/’:
result = operand1 / operand2;
push(result);

break;
}
}
data = -1;
}
if (top != NULL && top->next == NULL)
cout<<“Output:%d\n”, top->data;
else
cout<<“u ve entered wrong expression\n”;
return 0;
}

OUTPUT:-

Enter ur postfix expression:10 20 * 30 40 10 / - +


Output: 226
EXPERIMENT NO. 4
PROGRAM:-
#include <iostream>
#define MAX 10
using namespace std;
struct queue
{ int data[MAX];
int front,rear;
};
class Queue
{ struct queue q;
public:
Queue(){q.front=q.rear=-1;}
int isempty();
int isfull();
void enqueue(int);
int delqueue();
void display();
};
int Queue::isempty()
{
return(q.front==q.rear)?1:0;
}
int Queue::isfull()
{ return(q.rear==MAX-1)?1:0;}
void Queue::enqueue(int x)
{q.data[++q.rear]=x;}
int Queue::delqueue()
{return q.data[++q.front];}
void Queue::display()
{ int i;
cout<<"\n";
for(i=q.front+1;i<=q.rear;i++)
cout<<q.data[i]<<" ";
}
int main()
{ Queue obj;
int ch,x;
do{ cout<<"\n 1. insert job\n 2.delete job\n 3.display\n 4.Exit\n Enter your choice:";
cin>>ch;
switch(ch)
{ case 1: if (!obj.isfull())
{ cout<<"\n Enter data:";
cin>>x;
obj.enqueue(x);
}
else
cout<< "Queue is overflow";
break;
case 2: if(!obj.isempty())
cout<<"\n Deleted Element="<<obj.delqueue();
else
{ cout<<"\n Queue is underflow"; }
cout<<"\nremaining jobs :";

obj.display();

break;
case 3: if (!obj.isempty())
{ cout<<"\n Queue contains:";
obj.display();
}
else
break;
cout<<"\n Queue is empty";
case 4: cout<<"\n Exit";
}
}while(ch!=4);
return 0;
}

OUTPUT :-
1. insert job
2. delete job
3.display
4.Exit
Enter your choice:1
Enter data:34

1. insert job
2. delete job
3.display
4.Exit
Enter your choice:1
Enter data:64
1. insert job
2. delete job
3.display
4.Exit
Enter your choice:1
Enter data:84

1. insert job
2. delete job
3.display
4.Exit
Enter your choice:1
Enter data:93

1. insert job
2. delete job
3.display
4.Exit
Enter your choice:3
Queue contains:

34 64 84 93

1. insert job
2. delete job
3.display
4.Exit

Enter your choice:2


Deleted Element=34
remaining jobs :
64 84 93

1. insert job
2. delete job
3.display
4.Exit
Enter your choice:3
Queue contains:
64 84 93

1. insert job
2. delete job
3.display
4.Exit
Enter your choice:4
Exit
EXPERIMENT NO. 5
PROGRAM:-
#include<iostream>
//#include
//#include
using namespace std;
#define SIZE 5
class dequeue
{
int a[10],front,rear,count;
public:
dequeue();
void add_at_beg(int);
void add_at_end(int);
void delete_fr_front();
void delete_fr_rear();
void display();
};
dequeue::dequeue()
{
front=-1;
rear=-1;
count=0;
}
void dequeue::add_at_beg(int item)
{
int i;
if(front==-1)
{
front++;
rear++;
a[rear]=item;
count++;
}
else if(rear>=SIZE-1)
{
}
else
{
cout<<"\nInsertion is not possible,overflow!!!!";
for(i=count;i>=0;i--)
{
a[i]=a[i-1];
}
a[i]=item;
count++;
rear++;
}
}
void dequeue::add_at_end(int item)
{
if(front==-1)
{
front++;
rear++;

a[rear]=item;
count++;

}
else if(rear>=SIZE-1)
{
}
else
{
}}
cout<<"\nInsertion is not possible,overflow!!!";
return;
a[++rear]=item;
void dequeue::display()
{
for(int i=front;i<=rear;i++)
{
cout<<a[i]<<" "; }
}
void dequeue::delete_fr_front()
{
if(front==-1)
{
}
else
{
cout<<"Deletion is not possible:: Dequeue is empty";
return;
if(front==rear)
{
front=rear=-1;
return;
}
cout<<"The deleted element is "<<a[front];
front=front+1;
}}
void dequeue::delete_fr_rear()
{
if(front==-1)
{
}
else
{
cout<<"Deletion is not possible:Dequeue is empty";
return;
if(front==rear)
{
front=rear=-1;
}
cout<<"The deleted element is "<< a[rear];
rear=rear-1;
}}
int main()
{
int c,item;
dequeue d1;

do
{
cout<<"\n\n****DEQUEUE OPERATION****\n";
cout<<"\n1-Insert at beginning";
cout<<"\n2-Insert at end";
cout<<"\n3_Display";
cout<<"\n4_Deletion from front";
cout<<"\n5-Deletion from rear";
cout<<"\n6_Exit";
cout<<"\nEnter your choice<1-4>:";
cin>>c;
switch(c)
{
case 1:
cout<<"Enter the element to be inserted:";
cin>>item;
d1.add_at_beg(item);
break;

case 2:
cout<<"Enter the element to be inserted:";
cin>>item;
d1.add_at_end(item);
break;

case 3:
d1.display();
break;

case 4:
d1.delete_fr_front();
break;

case 5:
d1.delete_fr_rear();
break;
case 6:
exit(1);
break;

default:
cout<<"Invalid choice";

break;
}
}while(c!=7);
return 0;}

OUTPUT :-

****DEQUEUE OPERATION****

1- Insert at beginning
2- Insert at end
3_Display
4_Deletion from front
5-Deletion from rear
6_Exit
Enter your choice<1-4>:1
Enter the element to be inserted:45

****DEQUEUE OPERATION****

1- Insert at beginning
2- Insert at end
3_Display
4_Deletion from front
5-Deletion from rear
6_Exit
Enter your choice<1-4>:2
Enter the element to be inserted:46

****DEQUEUE OPERATION****

1- Insert at beginning
2- Insert at end
3_Display
4_Deletion from front
5-Deletion from rear
6_Exit
Enter your choice<1-4>:3
45 46

****DEQUEUE OPERATION****
1- Insert at beginning
2- Insert at end
3_Display
4_Deletion from front
5-Deletion from rear
6_Exit
Enter your choice<1-4>:4
The deleted element is 45

****DEQUEUE OPERATION****

1- Insert at beginning
2- Insert at end
3_Display
4_Deletion from front
5-Deletion from rear
6_Exit
Enter your choice<1-4>:3
46

****DEQUEUE OPERATION****

1- Insert at beginning
2- Insert at end
3_Display
4_Deletion from front
5-Deletion from rear
6_Exit
Enter your choice<1-4>:5
The deleted element is 0

****DEQUEUE OPERATION****

1- Insert at beginning
2- Insert at end
3_Display
4_Deletion from front
5-Deletion from rear
6_Exit
Enter your choice<1-4>:3

****DEQUEUE OPERATION****

1- Insert at beginning
2- Insert at end
3_Display
4_Deletion from front
5-Deletion from rear
6_Exit
Enter your choice<1-4>:
EXPERIMENT NO. 6
PROGRAM:-
#include <iostream>
# include <cstdlib>
# include <string.h>
using namespace std;
/*
* Node Declaration
*/
struct node
{
char label[10];
int ch_count;
struct node *child[10];
}*root;
/*
* Class Declaration
*/
class BST
{
public:
void create_tree();
void display(node * r1);
BST()
{
root = NULL;
}
};
void BST::create_tree()
{
int tbooks,tchapters,i,j,k;
root = new node();
cout<<"Enter name of book";
cin>>root->label;
cout<<"Enter no. of chapters in book";
cin>>tchapters;
root->ch_count = tchapters;
for(i=0;i<tchapters;i++)
{
root->child[i] = new node;
cout<<"Enter Chapter name\n";
cin>>root->child[i]->label;
cout<<"Enter no. of sections in Chapter: "<<root->child[i]->label;
cin>>root->child[i]->ch_count;
for(j=0;j<root->child[i]->ch_count;j++)
{
root->child[i]->child[j] = new node;
cout<<"Enter Section "<<j+1<<"name\n";
cin>>root->child[i]->child[j]->label;
//cout<<"Enter no. of subsections in "<<r1->child[i]->child[j]->label;
//cin>>r1->child[i]->ch_count;
}
}
}
void BST::display(node * r1)
{
int i,j,k,tchapters;
if(r1 != NULL)
{
cout<<"\n-----Book Hierarchy---";

cout<<"\n Book title : "<<r1->label;


tchapters = r1->ch_count;
for(i=0;i<tchapters;i++)
{

cout<<"\n Chapter "<<i+1;


cout<<" "<<r1->child[i]->label;
cout<<"\n Sections";
for(j=0;j<r1->child[i]->ch_count;j++)
{
//cin>>r1->child[i]->child[j]->label;
cout<<"\n "<<r1->child[i]->child[j]->label;
}
}
}
}
/*
* Main Contains Menu
*/
int main()
{
int choice;
BST bst;
while (1)
{
cout<<" ---------------- "<<endl;
cout<<"Book Tree Creation"<<endl;
cout<<" ---------------- "<<endl;
cout<<"1.Create"<<endl;
cout<<"2.Display"<<endl;
cout<<"3.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
bst.create_tree();
case 2:
bst.display(root);
break;
case 3:
exit(1);
default:

cout<<"Wrong choice"<<endl;
}
}
}
OUTPUT :-

1.Create
2.Display
3.Quit
Enter your choice : 1
Enter name of bookC++
Enter no. of chapters in book2
Enter Chapter name
Operators
Enter no. of sections in Chapter: Operators1
Enter Section 1name
Arithmetic
Enter Chapter name
Functions
Enter no. of sections in Chapter: Functions1
Enter Section 1name
FunctionDefine

-----Book Hierarchy---
Book title : C++
Chapter 1 Operators
Sections
Arithmetic
Chapter 2 Functions
Sections
Function Define
………………… Book Tree Creation……………
1. Create
2. Display
3.Quit
EXPERIMENT NO. 7
PROGRAM:-
#include <iostream>
using namespace std;
#include <conio.h>
struct tree
{
tree *l, *r;
int data;
}*root = NULL, *p = NULL, *np = NULL, *q;
void create()
{
int value,c = 0;
while (c < 7)
{
if (root == NULL)
{
root = new tree;
cout<<"enter value of root node\n";
cin>>root->data;
root->r=NULL;
root->l=NULL;
}
else
{
p = root;
cout<<"enter value of node\n";
cin>>value;
while(true)
{
if (value < p->data)
{
if (p->l == NULL)
{
p->l = new tree;
p = p->l;
p->data = value;
p->l = NULL;
p->r = NULL;
cout<<"value entered in left\n";
break;
}
else if (p->l != NULL)
{
p = p->l;
}
}
else if (value > p->data)
{
if (p->r == NULL)
{
p->r = new tree;
p = p->r;
p->data = value;
p->l = NULL;
p->r = NULL;
cout<<"value entered in right\n";
break;
}
else if (p->r != NULL)
{
p = p->r;
}
}
}
}
c++;
}
}
void inorder(tree *p)
{
if (p != NULL)
{
inorder(p->l);
cout<<p->data<<endl;
inorder(p->r);
}
}
void preorder(tree *p)
{
if (p != NULL)
{
cout<<p->data<<endl;
preorder(p->l);
preorder(p->r);
}
}
void postorder(tree *p)
{
if (p != NULL)
{
postorder(p->l);
postorder(p->r);
cout<<p->data<<endl;
}
}
int main()
{
create();
cout<<"printing traversal in inorder\n";
inorder(root);
cout<<"printing traversal in preorder\n";
preorder(root);
cout<<"printing traversal in postorder\n";
postorder(root);
getch();
}
OUTPUT :-
enter value of root node
7
enter value of node
8
value entered in right
enter value of node
4
value entered in left
enter value of node
6
value entered in right
enter value of node
3
value entered in left
enter value of node
5
value entered in left
enter value of node
2
value entered in left
printing traversal in inorder
2
3
4
5
6
7
8
printing traversal in preorder
7
4
3
2
6
5
8
printing traversal in postorder
2
3
5
6
4
8
7
EXPERIMENT NO. 8
PROGRAM:-
#include<iostream>
using namespace std;
struct node
{
int data;
node *L;
node *R;
};
node *root,*temp;
int count,key;
class bst
{
public:
void create();
void insert(node*,node*);
void disin(node*);
void dispre(node*);
void dispost(node*);
void search(node*,int);
int height(node*);
void mirror(node*);
void min(node*);
bst()
{
root=NULL;
count=0;
}
};
void bst::create()
{
char ans;
do
{
temp=new node;
cout<<"Enter the data : ";
cin>>temp->data;
temp->L=NULL;
temp->R=NULL;
if(root==NULL)
{
root=temp;
}
else
insert(root,temp);
cout<<"Do you want to insert more value : "<<endl;
cin>>ans;
count++;
cout<<endl;
}while(ans=='y');
cout<<"The Total no.of nodes are:" <<count;
}
void bst::insert(node *root,node* temp)
{
if(temp->data>root->data)
{
if(root->R==NULL)
{
root->R=temp;
}
else
insert(root->R,temp);
}
else
{
if(root->L==NULL)
{
root->L=temp;
}
else
insert(root->L,temp);
}
}
void bst::disin(node *root)
{
if(root!=NULL)
{
disin(root->L);
cout<<root->data<<"\t";
disin(root->R);
count++;
}
}
void bst::dispre(node *root)
{
if(root!=NULL)
{
cout<<root->data<<"\t";
dispre(root->L);
dispre(root->R);
}
}
void bst::dispost(node *root)
{
if(root!=NULL)
{
dispost(root->L);
dispost(root->R);
cout<<root->data<<"\t";
}
}
void bst::search(node * root,int key)
{
int flag=0;
cout<<"\nEnter your key : "<<endl;
cin>>key;
temp=root;
while(temp!=NULL)
{
if(key==temp->data)
{
cout<<"KEY FOUND\n";
flag=1;
break;
} node *parent=temp; if(key>parent->data)
{
temp=temp->R;
}
else
{
temp=temp->L;
}
}
if(flag==0)
{
cout<< "KEY NOT FOUND " <<endl;
}
} int bst::height(node *root)
{ int hl,hr;
if(root==NULL)
{ return 0; }
else if(root->L==NULL && root->R==NULL)
{
return 0;
}
cout<<endl; hr=height(root->R);
hl=height(root->L);
if(hr>hl)
{
return(1+hr);
}
else
{
return(1+hl);
}
}
void bst::min(node *root)
{
temp=root;
cout<<endl; while(temp->L!=NULL)
{
temp=temp->L;
}
cout<<root->data;
}
void bst::mirror(node *root)
{
temp=root;
if(root!=NULL)
{
mirror(root->L);
mirror(root->R);
temp=root->L;
root->L=root->R;
root->R=temp;
}
}
int main()
{
bst t;
int ch;
char ans;
do
{
cout<<"\n1) Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search
6) inorder
7) preorder 8) postorder" <<endl;
cin>>ch;
switch(ch)
{
case 1:
t.create();
break;
case 2:
cout<<"\"n Number of nodes in longest path:"<<(1+(t.height(root)));
break;
case 3:
cout<<"\nThe min element is:";
t.min(root);
break;
case 4:
t.mirror(root);
cout<<"\nThe mirror of tree is: ";
t.disin(root);
break;
case 5:
t.search(root,key);
break;
case 6:
cout<<"\n***************INORDER**************" <<endl;
t.disin(root);
break;
case 7:
cout<<"\n***************PREORDER**************"<<endl;
t.dispre(root);

break;
case 8:
cout<<"\n*******************POSTORDER**************"<<endl;
t.dispost(root);
break;
}
cout<<"\nDo you want to continue :"; cin>>ans;
}while(ans=='y');
return 0;
}
OUTPUT:-

1) Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search 6) inorder
7)
preorder 8) postorder
1
Enter the data : 3
Do you want to insert more value :
y

Enter the data : 4


Do you want to insert more value :
y

Enter the data : 1


Do you want to insert more value :
y

Enter the data : 6


Do you want to insert more value :
y

Enter the data : 9


Do you want to insert more value :
y

Enter the data : 7


Do you want to insert more value :
n

The Total no.of nodes are:6


Do you want to continue :y

1) Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search 6) inorder
7)
preorder 8) postorder
2

"n Number of nodes in longest path:5


Do you want to continue :y

1) Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search 6) inorder
7)
preorder 8) postorder
3

The min element is:


3
Do you want to continue :y

1)Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search 6) inorder
7) preorder 8) postorder
4

The mirror of tree is: 9 7 6 4 3 1


Do you want to continue :y

1) Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search 6) inorder
7)
preorder 8) postorder
5

Enter your key :


1
KEY NOT FOUND

Do you want to continue :y

1) Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search 6) inorder
7)
preorder 8) postorder
6

***************INORDER**************
976431
Do you want to continue :y

1) Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search 6) inorder
7)
preorder 8) postorder
7

***************PREORDER**************
346971
Do you want to continue :y

1) Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search 6) inorder
7)
preorder 8) postorder
8
*******************POSTORDER**************
796413
Do you want to continue :y

1) Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search 6) inorder
7)
preorder 8) postorder
EXPERIMENT NO. 9
PROGRAM:-

// C++ implementation of the approach


#include <bits/stdc++.h>
using namespace std;

class Graph {

// Number of vertex
int v;

// Number of edges
int e;

// Adjacency matrix
int** adj;

public:

// To create the initial adjacency matrix


Graph(int v, int e);

// Function to insert a new edge


void addEdge(int start, int e);

// Function to display the BFS traversal


void BFS(int start);
};

// Function to fill the empty adjacency matrix


Graph::Graph(int v, int e)
{
this->v = v;
this->e = e;
adj = new int*[v];
for (int row = 0; row < v; row++) {
adj[row] = new int[v];
for (int column = 0; column < v; column++) {
adj[row][column] = 0;
}
}
}

// Function to add an edge to the graph


void Graph::addEdge(int start, int e)
{

// Considering a bidirectional edge


adj[start][e] = 1;
adj[e][start] = 1;
}

// Function to perform BFS on the graph


void Graph::BFS(int start)

{
// Visited vector to so that
// a vertex is not visited more than once
// Initializing the vector to false as no
// vertex is visited at the beginning
vector<bool> visited(v, false);
vector<int> q;
q.push_back(start);

// Set source as visited


visited[start] = true;

int vis;
while (!q.empty()) {
vis = q[0];

// Print the current node


cout << vis << " ";
q.erase(q.begin());

// For every adjacent vertex to the current vertex


for (int i = 0; i < v; i++) {
if (adj[vis][i] == 1 && (!visited[i])) {

// Push the adjacent node to the queue


q.push_back(i);

// Set
visited[i] = true;
}
}
}
}

// Driver code
int main()
{
int v = 5, e = 4;

// Create the graph


Graph G(v, e);
G.addEdge(0, 1);
G.addEdge(0, 2);
G.addEdge(1, 3);

G.BFS(0);
}

OUTPUT:-

0123
EXPERIMENT NO. 10
PROGRAM:-
#include <iostream>
#include<iomanip>
using namespace std;

const int MAX=10;


class EdgeList; //forward declaration
class Edge //USED IN KRUSKAL
{
int u,v,w;
public:
Edge(){} //Empty Constructor

Edge(int a,int b,int weight)


{
u=a;
v=b;
w=weight;
}
friend class EdgeList;
friend class PhoneGraph;
};
//---- EdgeList Class ----------
class EdgeList
{
Edge data[MAX];
int n;
public:
friend class PhoneGraph;
EdgeList()
{ n=0;}
void sort();
void print();
};
//----Bubble Sort for sorting edges in increasing weights' order --- //
void EdgeList::sort()
{
Edge temp;
for(int i=1;i<n;i++)
for(int j=0;j<n-1;j++)
if(data[j].w>data[j+1].w)
{
temp=data[j];
data[j]=data[j+1];
data[j+1]=temp;
}
}
void EdgeList::print()
{
int cost=0;
for(int i=0;i<n;i++)
{
cout<<"\n"<<i+1<<" "<<data[i].u<<"--"<<data[i].v<<" = "<<data[i].w;
cost=cost+data[i].w;
}
cout<<"\nMinimum cost of Telephone Graph = "<<cost;
}

// Phone Graph Class


class PhoneGraph
{
int data[MAX][MAX];
int n;

public:
PhoneGraph(int num)
{
n=num;
}
void readgraph();
void printGraph();
int mincost(int cost[],bool visited[]);
int prim();
void kruskal(EdgeList &spanlist);
int find(int belongs[], int vertexno);
void unionComp(int belongs[], int c1,int c2);
};
void PhoneGraph::readgraph()
{
cout<<"Enter Adjacency(Cost) Matrix: \n";
for(int i=0;i<n;i++)
{
for(int j=0;j<n; j++)
cin>>data[i][j];
}
}
void PhoneGraph::printGraph()
{
cout<<"\nAdjacency (COST) Matrix: \n";
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<setw(3)<<data[i][j];
}
cout<<endl;
}
}
int PhoneGraph::mincost(int cost[],bool visited[]) //finding vertex with minimum cost
{
int min=9999,min_index; //initialize min to MAX value(ANY) as temporary
for(int i=0;i<n;i++)

{
if(visited[i]==0 && cost[i]<min)
{
min=cost[i];
min_index=i;
}
}
return min_index; //return index of vertex which is not visited and having minimum cost
}
int PhoneGraph::prim()
{
bool visited[MAX];
int parents[MAX];
int cost[MAX]; //saving minimum cost

for(int i=0;i<n;i++)
{
cost[i]=9999; //set cost as infinity/MAX_VALUE
visited[i]=0; //initialize visited array to false
}
cost[0]=0; //starting vertex cost
parents[0]=-1; //make first vertex as a root
for(int i=0;i<n-1;i++)
{
int k=mincost(cost,visited);
visited[k]=1;

for(int j=0;j<n;j++)
{
if(data[k][j] && visited[j]==0 && data[k][j] < cost[j])
{
parents[j]=k;
cost[j]=data[k][j];
}
}
}
cout<<"Minimum Cost Telephone Map:\n";
for(int i=1;i<n;i++)
{
cout<<i<<" -- "<<parents[i]<<" = "<<cost[i]<<endl;
}
int mincost=0;
for (int i = 1; i < n; i++)
mincost+=cost[i]; //data[i][parents[i]];
return mincost;
}
// ------- Kruskal's Algorithm
void PhoneGraph::kruskal(EdgeList &spanlist)
{
int belongs[MAX]; //Separate Components at start (No Edges, Only vertices)
int cno1,cno2; //Component 1 & 2
EdgeList elist;

for(int i=1;i<n;i++)
for(int j=0;j<i;j++)
{
if(data[i][j]!=0)
{
elist.data[elist.n]=Edge(i,j,data[i][j]); //constructor for initializing edge
elist.n++;
}
}
elist.sort(); //sorting in increasing weight order
for(int i=0;i<n;i++)
belongs[i]=i;
for(int i=0;i<elist.n;i++)
{
cno1=find(belongs,elist.data[i].u); //find set of u
cno2=find(belongs,elist.data[i].v); ////find set of v
if(cno1!=cno2) //if u & v belongs to different sets
{
spanlist.data[spanlist.n]=elist.data[i]; //ADD Edge to spanlist
spanlist.n=spanlist.n+1;
unionComp(belongs,cno1,cno2); //ADD both components to same set
}
}
}
void PhoneGraph::unionComp(int belongs[],int c1,int c2)
{
for(int i=0;i<n;i++)
{
if(belongs[i]==c2)
belongs[i]=c1;
}
}
int PhoneGraph::find(int belongs[],int vertexno)
{
return belongs[vertexno];
}

// MAIN PROGRAM
int main() {
int vertices,choice;
EdgeList spantree;
cout<<"Enter Number of cities: ";
cin>>vertices;
PhoneGraph p1(vertices);
p1.readgraph();
do
{
cout<<"\n1.Find Minimum Total Cost(By Prim's Algorithm)"
<<"\n2.Find Minimum Total Cost(by Kruskal's Algorithms)"
<<"\n3.Re-Read Graph(INPUT)"
<<"\n4.Print Graph"
<<"\n0. Exit"

<<"\nEnter your choice: ";


cin>>choice;
switch(choice)
{
case 1:
cout<<" Minimum cost of Phone Line to cities is: "<<p1.prim();
break;
case 2:
p1.kruskal(spantree);
spantree.print();
break;
case 3:
p1.readgraph();
break;
case 4:
p1.printGraph();
break;
default:
cout<<"\nWrong Choice!!!";
}
}while(choice!=0);
return 0;
}

OUTPUT :-

Sample INPUT: vertices =7


{{0, 28, 0, 0, 0,10,0},
{28,0,16,0,0,0,14},
{0,16,0,12,0,0,0},
{0,0,12,0,22,0,18},
{0,0,0,22,0,25,24},
{10,0,0,0,25,0,0},
{0,14,0,18,24,0,0},
};
Minimum Cost: 99

You might also like