sub code/name object oriented programming · pdf filewrite the program for function...

48
Department of Electronics and Communication Engineering Sub Code/Name: BCS 4L3- OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES LAB (R) Name : …………………………………… Reg No : …………………………………… Branch : …………………………………… Year & Semester : ……………………………………

Upload: dinhkhanh

Post on 29-Mar-2018

221 views

Category:

Documents


1 download

TRANSCRIPT

Department of Electronics and Communication Engineering

Sub Code/Name: BCS 4L3- OBJECT ORIENTED PROGRAMMING AND

DATA STRUCTURES LAB (R)

Name : ……………………………………

Reg No : ……………………………………

Branch : ……………………………………

Year & Semester : ……………………………………

LIST OF EXPERIMENTS

Sl No

Experiments Page No

Programs for C++ Concepts

1 Constructors and destructors

2 Static data member

3 Function overloading

4 Operator overloading

5 Inheritance

Data Structures

1 List-Polynomial operations

2 Stack- Arrayimplementation

3 Stack-Linked list implementation

4 Queue - Arrayimplementation

5 Queue --inked list implementatio

6 BinarySearch tree

INDEX

Expt. Date Name of the Experiment Marks Staff SIGN

Ex No:1

Date:

Ex.No:1. CONSTRUCTOR AND DESTRUCTOR

Aim:

Write the program for constructor and destructor using C++ concept.

Program: #include<iostream.h> #include<conio.h> class stu { private: char name[20],add[20]; int roll,zip; public: stu( ); ~stu( ); void read( ); void disp( ); }; stu::stu( ) { cout<<"This is Student Details"<<endl; } void stu::read( ) { cout<<"Enter the student Name"; cin>>name; cout<<"Enter the student roll no"; cin>>roll; cout<<"Enter the student address"; cin>>add; cout<<"Enter the Zipcode"; cin>>zip; } void stu::disp( ) { cout<<"Student Name :"<<name<<endl; cout<<"Roll no is :"<<roll<<endl; cout<<"Address is :"<<add<<endl; cout<<"Zipcode is :"<<zip; }

stu::~stu( ) { cout<<"Student Detail is Closed"; } void main( ) { stu s; clrscr( ); s.read ( ); s.disp ( ); getch( ); }

OUTPUT: Enter the student Name: arun Enter the student roll no: 1010 Enter the student address: chennai Enter the Zipcode: 101 Student Name :arun Roll no is :1010 Address is :chennai Zipcode is :101 RESULT: The Program to implement the Constructor and Destructor has been successfully verified and executed using C++

Ex No:2

Date:

STATIC DATA MEMBER FUNCTION

Aim:

Write the program for Static data member using C++ concept.

Program

#include<iostream.h>

#include<conio.h>

void main() { int x,y,z,mul(int,int); clrscr(); cout<<"\n Enter the value of x,y : "; cin>>x>>y; z=mul(x,y); cout<<"\n Multiplied value is : "<<z; getch(); } int mul(int p,int q) { int s,a,add(int,int); s=p*q; a=add(p,q); cout<<"\n Addition is : "<<a; return(s); } int add(int l, int m) { int b,c,sub(int,int); b=l+m; c=sub(l,m); cout<<"\n Subtraction is : "<<c; return(b); } int sub(int e, int f) { int d; d=e-f; return(d); }

5 a) A program to demonstrate the use of static variables within a function

#include<iostream>

using namespace std;

void count ()

{

static int c=0;

c++;

cout<<”Function is called "<<c<<" times\n";

} ;

int main ()

{

for (int i=1;i<=5;i++)

count() ;

return 0;

}

The output of the program is

Function is called 1 times

Function is called 2 times

Function is called 3 times

Function is called 4 times

Function is called 5 times

RESULT: The Program to implement the static data member has been successfully verified and executed using C++

Ex No:3

Date:

Ex.No:3. FUNCTION OVERLOADING

Aim:

Write the program for function overloading using C++ concept.

Program #include<iostream.h> #include<stdlib.h> #include<conio.h> #define pi 3.14 class fn { public: void area(int); //circle void area(int,int); //rectangle void area(float ,int,int); //triangle }; void fn::area(int a) { cout<<"Area of Circle:"<<pi*a*a; } void fn::area(int a,int b) { cout<<"Area of rectangle:"<<a*b; } void fn::area(float t,int a,int b) { cout<<"Area of triangle:"<<t*a*b; } void main() { int ch; int a,b,r; clrscr(); fn obj; cout<<"\n\t\tFunction Overloading";

cout<<"\n1.Area of Circle\n2.Area of Rectangle\n3.Area of Triangle\n4.Exit\n:”; cout<<”Enter your Choice:"; cin>>ch; switch(ch) { case 1: cout<<"Enter Radius of the Circle:"; cin>>r;

obj.area(r); break; case 2: cout<<"Enter Sides of the Rectangle:"; cin>>a>>b; obj.area(a,b); break; case 3: cout<<"Enter Sides of the Triangle:"; cin>>a>>b; obj.area(0.5,a,b); break; case 4: exit(0); } getch(); } OUTPUT: Function Overloading 1.Area of Circle 2.Area of Rectangle 3.Area of Triangle 4.Exit Enter your Choice: 1 Enter Radius of the Circle: 4 Area of Circle: 50.24 RESULT: The Program to implement the Function Overloading has been successfully verified and executed using C++.

Ex No:4

Date:

Ex.No:4. OPERATOR OVERLOADING

Aim:

Write the program for operator overloading using C++ concept

Program: #include<iostream.h> #include<conio.h> class complex { int a,b; public: void getvalue() { cout<<"Enter the value of Complex Numbers a,b:"; cin>>a>>b; } complex operator+(complex ob) { complex t; t.a=a+ob.a; t.b=b+ob.b; return(t); } complex operator-(complex ob) { complex t; t.a=a-ob.a; t.b=b-ob.b; return(t); } void display() { cout<<a<<"+"<<b<<"i"<<"\n"; } };

void main() { clrscr(); complex obj1,obj2,result,result1; obj1.getvalue(); obj2.getvalue(); result = obj1+obj2; result1=obj1-obj2; cout<<"Input Values:\n"; obj1.display(); obj2.display(); cout<<"Result:"; result.display(); result1.display(); getch(); } OUTPUT: Enter the value of Complex Numbers a,b: 4 3 Enter the value of Complex Numbers a,b: 3 2 Input Values: 4+3i 3+2i Result: 7+5i 1+1i RESULT: The Program to implement the Binary Operator overloading has been successfully verified and executed using C++.

Ex No:5

Date:

Ex.No:5. SINGLE INHERITANCE

Aim:

Write the program for operator overloading using C++ concept

Program #include<iostream.h> #include<conio.h> class emp { public: int eno; char name[20],des[20]; void get() { cout<<"Enter the employee number:"; cin>>eno; cout<<"Enter the employee name:"; cin>>name; cout<<"Enter the designation:"; cin>>des; } }; class salary:public emp { float bp,hra,da,pf,np; public: void get1() { cout<<"Enter the basic pay:"; cin>>bp; cout<<"Enter the Human Resource Allowance:"; cin>>hra; cout<<"Enter the Dearness Allowance :"; cin>>da; cout<<"Enter the Provident Fund:"; cin>>pf; }

void calculate() { np=bp+hra+da-pf; } void display() {

cout<<eno<<"\t"<<name<<"\t"<<des<<"\t"<<bp<<"\t"<<hra<<"\t"<<da<<"\t"<<pf<<"\t"<<np<<"\n"; } }; void main() { int i,n; char ch; salary s[10]; clrscr(); cout<<"Enter the number of employee:"; cin>>n; for(i=0;i<n;i++) { s[i].get(); s[i].get1(); s[i].calculate(); } cout<<"\t e_no \t e_name \t des \t bp \t hra \t da \t pf \t np \n"; for(i=0;i<n;i++) { s[i].display(); } getch(); } OUTPUT: Enter the number of employee: 1 Enter the employee number: 1010 Enter the employee name: arun Enter the designation: manager Enter the basic pay: 5000 Enter the Human Resource Allowance:1000 Enter the Dearness Allowance :500

Enter the Provident Fund:500 e_no e_name des bp hra da pf np 1010 arun manager 5000 1000 500 500 6000

RESULT: The Program to implement the Single Inheritance has been successfully verified and executed using C++.

Ex.No:5(b). MULTIPLE INHERITANCE

#include<iostream.h> #include<conio.h> class student { protected: int rno,m1,m2; public: void get() { cout<<"Enter the Roll no :"; cin>>rno; cout<<"Enter the two marks :"; cin>>m1>>m2; } }; class sports { protected: int sm; public: void getsm() { cout<<"\nEnter the sports mark :"; cin>>sm; } }; class statement:public student,public sports { int tot,avg; public: void display() { tot=(m1+m2+sm); avg=tot/3; cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal : "<<tot; cout<<"\n\tAverage : "<<avg; } }; void main() {

clrscr(); statement obj; obj.get(); obj.getsm(); obj.display(); getch(); }

OUTPUT: Enter the Roll no : 1010 Enter the two marks : 60 70 Enter the sports mark : 50 Roll No : 1010 Total : 180 Average : 60 RESULT: The Program to implement the Multiple Inheritance has been successfully verified and executed using C++.

Ex.No:5(c). MULTILEVEL INHERITANCE #include<iostream.h> #include<conio.h> class student { protected: int rollno; char *name; public: void getdata(int b,char *n) { rollno = b; name = n; } void putdata(void) { cout<< " The Name Of Student \t: "<< name<< endl; cout<< " The Roll No. Is \t: "<< rollno<< endl; } }; class test:public student { protected: float m1,m2; public: void gettest(float b,float c) { m1 = b; m2 = c; } void puttest(void) { cout<< " Marks In CP Is \t: "<< m1<< endl; cout<< " Marks In Drawing Is \t: "<<m2<<endl; } }; class result:public test { protected:

float total; public: void displayresult(void) { total = m1 + m2; putdata(); puttest(); cout<< " Total Of The Two \t: "<<total<<endl; } }; void main() { clrscr(); int x; float y,z; char n[20]; cout<< "Enter Your Name:"; cin>>n; cout<< "Enter The Roll Number:"; cin>>x; result r1; r1.getdata(x,n); cout<< "ENTER COMPUTER PROGRAMMING MARKS:"; cin>>y; cout<< "ENTER DRAWING MARKS:"; cin>>z; r1.gettest(y,z); cout<<endl<<endl<< "************ RESULT **************"<<endl; r1.displayresult(); cout<< "**********************************"<<endl; getch(); } OUTPUT: Enter Your Name: arun Enter The Roll Number: 1010 ENTER COMPUTER PROGRAMMING MARKS: 70 ENTER DRAWING MARKS: 80 ************ RESULT **************

The Name Of Student : arun The Roll No. Is : 1010 Marks In CP Is : 70 Marks In Drawing Is : 80 Total Of The Two : 150 **********************************

RESULT: The Program to implement the Multilevel Inheritance has been successfully verified and executed using C++.

Data Structures

Ex No:1 Date:

POLYNOMIAL OPERATIONS

Aim:

Write the program for Polynomial operations

Program:

//Addition of Two Polynomial

#include <iostream>

//#include <iomanip.h>

#include <conio.h>

using namespace std;

struct poly{

int coeff;

int pow;

poly *next;

};

class add2poly

{

poly *poly1, *poly2, *poly3;

public:

add2poly(){poly1=poly2=poly3=NULL;}

void addpoly();

void display();

};

void add2poly :: addpoly(){

int i,p;

poly *newl=NULL,*end=NULL;

cout<<"Enter highest power for x\n";

cin>>p;

//Read first poly

cout<<"\nFirst Polynomial\n";

for(i=p;i>=0;i--)

{

newl=new poly;

newl->pow=i;

cout<<"Enter Co-efficient for degree"<<i<<":: ";

cin>>newl->coeff;

newl->next=NULL;

if(poly1==NULL)

poly1=newl;

else

end->next=newl;

end=newl;

}

//Read Second poly

cout<<"\n\nSecond Polynomial\n";

end=NULL;

for(i=p;i>=0;i--)

{

newl=new poly;

newl->pow=i;

cout<<"Enter Co-efficient for degree"<<i<<":: ";

cin>>newl->coeff;

newl->next=NULL;

if(poly2==NULL)

poly2=newl;

else

end->next=newl;

end=newl;

}

//Addition Logic

poly *p1=poly1,*p2=poly2;

end=NULL;

while(p1 !=NULL && p2!=NULL)

{

if(p1->pow == p2->pow)

{

newl=new poly;

newl->pow=p1->pow;

newl->coeff=p1->coeff + p2->coeff;

newl->next=NULL;

if(poly3==NULL)

poly3=newl;

else

end->next=newl;

end=newl;

}

p1=p1->next;

p2=p2->next;

}

}

void add2poly :: display(){

poly *t=poly3;

cout<<"\n\nAnswer after addition is : ";

while(t!=NULL)

{

//cout.setf(ios::showpos);

cout<<t->coeff;

//cout.unsetf(ios::showpos);

cout<<"X"<<t->pow;

if(t->next!=NULL)

cout<<"+";

t=t->next;

}

}

int main(){

//clrscr();

add2poly obj;

obj.addpoly();

obj.display();

getch();

}Result: Thus the program for Polynomial operations was implemented.

Ex No: 2 Date:

Array implementation Aim: Write the program for Array implementation using data structure. Program: #include<iostream> #include<conio.h> #include<stdlib.h> using namespace std; class stack { int stk[5]; int top; public: stack() { top=-1; } void push(int x) { if(top > 4) { cout <<"stack over flow"; return; } stk[++top]=x; cout <<"inserted=>" <<x; } void pop() { if(top <0) { cout <<"stack under flow"; return; } cout <<"deleted=>" <<stk[top--]; } void display() { if(top<0) {

cout <<" stack empty"; return; } for(int i=top;i>=0;i--) cout <<stk[i] <<" "; } }; main() { int ch; stack st; while(1) { cout <<"\n1.push 2.pop 3.display 4.exit\nEnter ur choice: "; cin >> ch; switch(ch) { case 1: cout <<"Enter the element: "; cin >> ch; st.push(ch); break; case 2: st.pop(); break; case 3: st.display();break; case 4: exit(0); } } return (0); }

Ex No: 3 Date:

LINKED LIST IMPLEMENTATION Aim: Write the program for Linked list implementation using data structure. Program: #include<iostream> using namespace std; // Creating a NODE Structure struct node { int data; struct node *next; }; // Creating a class STACK class stack { struct node *top; public: stack() // constructure { top=NULL; } void push(); // to insert an element void pop(); // to delete an element void show(); // to show the stack }; // PUSH Operation void stack::push() { int value; struct node *ptr; cout<<"PUSH Operation"<<endl; cout<<"Enter a number to insert: "; cin>>value; ptr=new node; ptr->data=value; ptr->next=NULL; if(top!=NULL) ptr->next=top;

top=ptr; cout<<endl<<"New item is inserted to the stack!!!"<<endl; } // POP Operation void stack::pop() { struct node *temp; if(top==NULL) { cout<<endl<<"The stack is empty!!!"<<endl; } temp=top; top=top->next; cout<<endl<<"POP Operation........Poped value is "<<endl<<temp->data; delete temp; } // Show stack void stack::show() { struct node *ptr1=top; cout<<endl<<"The stack is "; while(ptr1!=NULL) { cout<<ptr1->data<<" ->"; ptr1=ptr1->next; } cout<<endl<<"NULL"; } // Main function int main() { stack s; int choice; while(1) { cout<<"-----------------------------------------------------------"<<endl; cout<<"STACK USING LINKED LIST"<<endl; cout<<"1:PUSH"<<endl<<"2:POP"<<endl<<"3:DISPLAY STACK"<<endl<<"4:EXIT"; cout<<endl<<"Enter your choice(1-4): ";

cin>>choice; switch(choice) { case 1: s.push(); break; case 2: s.pop(); break; case 3: s.show(); break; case 4: return 0; break; default: cout<<endl<<"Please enter correct choice(1-4)!!"; break; } } return 0; } Result: Thus the program for Linked list implementation using data structure was implemented

Ex No:4 Date:

QUEUE – ARRAY IMPLEMENTATION Aim: Write the program for Queue – Array implementation using data structure. Program #include<iostream> #include<cstdlib> using namespace std; struct node{ int info; struct node *next; }; class Queue{ private: node *rear; node *front; public: Queue(); void enqueue(); void dequeue(); void display(); }; Queue::Queue(){ rear = NULL;

front = NULL; } void Queue::enqueue(){ int data; node *temp = new node; cout<<"Enter the data to enqueue: "; cin>>data; temp->info = data; temp->next = NULL; if(front == NULL){ front = temp; }else{ rear->next = temp; } rear = temp; } void Queue::dequeue(){ node *temp = new node; if(front == NULL){ cout<<"\nQueue is Emtpty\n"; }else{ temp = front;

front = front->next; cout<<"The data Dequeued is "<<temp->info; delete temp; } } void Queue::display(){ node *p = new node; p = front; if(front == NULL){ cout<<"\nNothing to Display\n"; }else{ while(p!=NULL){ cout<<endl<<p->info; p = p->next; } } } int main(){ Queue queue; int choice; while(true){ cout<<"\n1.Enqueue\n2. Dequeue\n3. Display\n 4.Quit";

cout<<"\nEnter your choice: "; cin>>choice; switch(choice){ case 1: queue.enqueue(); break; case 2: queue.dequeue(); break; case 3: queue.display(); break; case 4: exit(0); break; default: cout<<"\nInvalid Input. Try again! \n"; break; } } return 0; } Result: Thus the Queue array implementation program was implemented using data structure.

Ex No: 5 Date:

QUEUE – LINKED LIST IMPLEMENTATION Aim: Write the program for Queue – linked list implementation using data structure. Program #include<iostream> #include<cstdlib> using namespace std; struct node{ int info; struct node *next; }; class Queue{ private: node *rear; node *front; public: Queue(); void enqueue(); void dequeue(); void display(); }; Queue::Queue(){ rear = NULL;

front = NULL; } void Queue::enqueue(){ int data; node *temp = new node; cout<<"Enter the data to enqueue: "; cin>>data; temp->info = data; temp->next = NULL; if(front == NULL){ front = temp; }else{ rear->next = temp; } rear = temp; } void Queue::dequeue(){ node *temp = new node; if(front == NULL){ cout<<"\nQueue is Emtpty\n"; }else{ temp = front;

front = front->next; cout<<"The data Dequeued is "<<temp->info; delete temp; } } void Queue::display(){ node *p = new node; p = front; if(front == NULL){ cout<<"\nNothing to Display\n"; }else{ while(p!=NULL){ cout<<endl<<p->info; p = p->next; } } } int main(){ Queue queue; int choice; while(true){ cout<<"\n1.Enqueue\n2. Dequeue\n3. Display\n 4.Quit";

cout<<"\nEnter your choice: "; cin>>choice; switch(choice){ case 1: queue.enqueue(); break; case 2: queue.dequeue(); break; case 3: queue.display(); break; case 4: exit(0); break; default: cout<<"\nInvalid Input. Try again! \n"; break; } } return 0; } Result:Thus the program for Queue – linked list implementation using data structure was implemented

Ex No: 6 Date:

BINARY SEARCH TREE Aim: Write the program for Binary Search tree using data structure. Program #include <iostream> #include <cstdlib> using namespace std; class BinarySearchTree { private: struct tree_node { tree_node* left; tree_node* right; int data; }; tree_node* root; public: BinarySearchTree() { root = NULL; } bool isEmpty() const { return root==NULL; } void print_inorder(); void inorder(tree_node*); void print_preorder(); void preorder(tree_node*); void print_postorder(); void postorder(tree_node*); void insert(int); void remove(int); }; // Smaller elements go left // larger elements go right void BinarySearchTree::insert(int d) { tree_node* t = new tree_node; tree_node* parent;

t->data = d; t->left = NULL; t->right = NULL; parent = NULL; // is this a new tree? if(isEmpty()) { cout<<"Empty"<<endl; root = t; } else { cout<<"Not Empty"<<endl; //Note: ALL insertions are as leaf nodes tree_node* curr; curr = root; // Find the Node's parent while(curr) { parent = curr; if(t->data > curr->data) curr = curr->right; else curr = curr->left; } if(t->data < parent->data) parent->left = t; else parent->right = t; } } void BinarySearchTree::remove(int d) { //Locate the element bool found = false; if(isEmpty()) { cout<<" This Tree is empty! "<<endl; return; } tree_node* curr; tree_node* parent; curr = root; while(curr != NULL)

{ if(curr->data == d) { found = true; break; } else { parent = curr; if(d>curr->data) curr = curr->right; else curr = curr->left; } } if(!found) { cout<<" Data not found! "<<endl; return; } // 3 cases : // 1. We're removing a leaf node // 2. We're removing a node with a single child // 3. we're removing a node with 2 children // Node with single child if((curr->left == NULL && curr->right != NULL)|| (curr->left != NULL && curr->right == NULL)) { if(curr->left == NULL && curr->right != NULL) { if(parent->left == curr) { parent->left = curr->right; delete curr; } else { parent->right = curr->right; delete curr; } } else // left child present, no right child {

if(parent->left == curr) { parent->left = curr->left; delete curr; } else { parent->right = curr->left; delete curr; } } return; } //We're looking at a leaf node if( curr->left == NULL && curr->right == NULL) { if(parent->left == curr) parent->left = NULL; else parent->right = NULL; delete curr; return; } //Node with 2 children // replace node with smallest value in right subtree if (curr->left != NULL && curr->right != NULL) { tree_node* chkr; chkr = curr->right; if((chkr->left == NULL) && (chkr->right == NULL)) { curr = chkr; delete chkr; curr->right = NULL; } else // right child has children { //if the node's right child has a left child // Move all the way down left to locate smallest element if((curr->right)->left != NULL) { tree_node* lcurr;

tree_node* lcurrp; lcurrp = curr->right; lcurr = (curr->right)->left; while(lcurr->left != NULL) { lcurrp = lcurr; lcurr = lcurr->left; } curr->data = lcurr->data; delete lcurr; lcurrp->left = NULL; } else { tree_node* tmp; tmp = curr->right; curr->data = tmp->data; curr->right = tmp->right; delete tmp; } } return; } } void BinarySearchTree::print_inorder() { inorder(root); } void BinarySearchTree::inorder(tree_node* p) { if(p != NULL) { if(p->left) inorder(p->left); cout<<" "<<p->data<<" "; if(p->right) inorder(p->right); } else return; } void BinarySearchTree::print_preorder()

{ preorder(root); } void BinarySearchTree::preorder(tree_node* p) { if(p != NULL) { cout<<" "<<p->data<<" "; if(p->left) preorder(p->left); if(p->right) preorder(p->right); } else return; } void BinarySearchTree::print_postorder() { postorder(root); } void BinarySearchTree::postorder(tree_node* p) { if(p != NULL) { if(p->left) postorder(p->left); if(p->right) postorder(p->right); cout<<" "<<p->data<<" "; } else return; } int main() { BinarySearchTree b; int ch,tmp,tmp1; while(1) { cout<<endl<<endl; cout<<" Binary Search Tree Operations "<<endl; cout<<" ----------------------------- "<<endl; cout<<" 1. Insertion/Creation "<<endl; cout<<" 2. In-Order Traversal "<<endl; cout<<" 3. Pre-Order Traversal "<<endl; cout<<" 4. Post-Order Traversal "<<endl;

cout<<" 5. Removal "<<endl; cout<<" 6. Exit "<<endl; cout<<" Enter your choice : "; cin>>ch; switch(ch) { case 1 : cout<<" Enter Number to be inserted : "; cin>>tmp; b.insert(tmp); break; case 2 : cout<<endl; cout<<" In-Order Traversal "<<endl; cout<<" -------------------"<<endl; b.print_inorder(); break; case 3 : cout<<endl; cout<<" Pre-Order Traversal "<<endl; cout<<" -------------------"<<endl; b.print_preorder(); break; case 4 : cout<<endl; cout<<" Post-Order Traversal "<<endl; cout<<" --------------------"<<endl; b.print_postorder(); break; case 5 : cout<<" Enter data to be deleted : "; cin>>tmp1; b.remove(tmp1); break; case 6 : system("pause"); return 0; break; } } } Result: Thus the Binary search Tree Program was implemented.

Ex No: 7 Date:

BFS Aim: Write the program for BFS using data structure. Program #include<iostream> #include<conio.h> #include<stdlib.h> using namespace std; int cost[10][10],i,j,k,n,queue[10],front,rear,v,visit[10],visited[10]; int main() { int m; cout <<"enter no of vertices"; cin >> n; cout <<"enter no of edges"; cin >> m; cout <<"\nEDGES \n"; for(k=1;k<=m;k++) { cin >>i>>j; cost[i][j]=1; } cout <<"enter initial vertex"; cin >>v; cout <<"Visitied vertices\n"; cout << v; visited[v]=1; k=1; while(k<n) { for(j=1;j<=n;j++) if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1) { visit[j]=1; queue[rear++]=j; } v=queue[front++]; cout<<v << " "; k++; visit[v]=0;

visited[v]=1; } getch(); } Result: BFS program was implemented using data structure

Ex Bo:8 Date:

DFS Aim: Write the Program for DFS using Data structure. Program: #include<iostream> #include<conio.h> #include<stdlib.h> using namespace std; int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10]; main() { int m; cout <<"enterno of vertices"; cin >> n; cout <<"ente no of edges"; cin >> m; cout <<"\nEDGES \n"; for(k=1;k<=m;k++) { cin >>i>>j; cost[i][j]=1; } cout <<"enter initial vertex"; cin >>v; cout <<"ORDER OF VISITED VERTICES"; cout << v <<" "; visited[v]=1; k=1; while(k<n) { //cout<<"Top=" <<top<<endl; for(j=n;j>=1;j--) if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1) { //cout<<"Inside condition ==>"; visit[j]=1; stk[top]=j; top++;

} //cout<<"Top=>"<<top; v=stk[--top]; cout<<v << " "; k++; visit[v]=0; visited[v]=1; } getch(); } Result: DFS program was implemented using data structure