CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

155
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. SREE SAKTHI ENGINEERING COLLEGE Karamadai, Coimbatore – 641 104. 2014-2015 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 1

description

PROGRAMMING & DATA STRUCTURE II LABORATORY

Transcript of CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

Page 1: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

SREE SAKTHI ENGINEERING

COLLEGE

Karamadai, Coimbatore – 641 104.

2014-2015

DEPARTMENT OF

COMPUTER SCIENCE AND ENGINEERING

CS6311

PROGRAMMING AND DATA STRUCTURE LABORATORY II

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 1

Page 2: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

EX: NO: 1(A) CONSTRUCTOR AND DESTRUCTORDATE :

AIM:To write a simple c++ program for constructor and destructor

ALGORITHM:

STEP 1 : Start the program.

STEP 2 : Create the class and object.

STEP 3: Create a constructor, that constructor name should be same as class name.

STEP 4: Create a destructor using tilde symbol, destructor name should be same as

constructor name.

STEP 5: Execute the program.

STEP 6: Stop the program.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 2

Page 3: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

PROGRAM:

#include<iostream.h>#include<conio.h>class fover{public:fover(){cout<<" constructor without any argument is created:";}fover(int a){cout<<" constructor with argument is created:";cout<<"the value of A is passed using implicit call is\t"<<a;}~fover(){cout<<"\n the object is deleted";}};void main(){clrscr();fover f(5);getch();}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 3

Page 4: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

Constructor with argument is createdThe value of A is passed using implicit call is 5

RESULT:

Thus a simple c++ program for constructor and destructor was executed and output was verified.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 4

Page 5: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

EX:NO:1(B) COPY CONSTRUCTORDATE :

AIM:To write a simple c++ program for copy constructor.

ALGORITHM:

STEP1: start the program

STEP2:Create the class and object

STEP3: Create a constructor and copy constructor ,that constructors name should be same

as class name.

STEP4: Create a destructor using tilde symbol, destructor name should be same as

constructor name

STEP 5: Execute the program

STEP 6: Stop the program

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 5

Page 6: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

PROGRAM:

#include <iostream>#include<conio.h>class test{public:int a;test(int pa){a = pa;cout<<”t.a is \n “<<a;}test(test &t) //copy constructor{a=t.a;cout<<"\n t.a1 is "<< t.a;}};void main(){Clrscr();test t(5);test t1(t);getchar();}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 6

Page 7: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

RESULT:

Thus a simple c++ program for copy constructor was executed and output was verified

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 7

Page 8: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

EX:NO:2 FRIEND FUNCTION AND FRIEND CLASSDATE :

AIM:To find the mean value of a given number using friend function and friend class.

ALGORITHM:

STEP 1:  Start the program.

STEP 2:  Declare the class name as Base with data members and member functions.

STEP 3:  The function get() is used to read the 2 inputs from the user.

STEP 4:  Declare the friend function mean(base ob) inside the class.

STEP 5:  Outside the class to define the friend function and do the following.

STEP 6:  Return the mean value (ob.val1+ob.val2)/2 as a float.

STEP 7:  Stop the program.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 8

Page 9: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

PROGRAM:

#include<iostream.h>#include<conio.h>class  base{int val1,val2;public:void get(){cout<<"Enter two values:";cin>>val1>>val2;}friend float mean(base ob); //FRIEND FUNCTIONS & CLASS declaration};float mean(base ob){return float(ob.val1+ob.val2)/2; //function definition}void main(){clrscr();base obj;obj.get();cout<<"\n Mean value is : "<<mean(obj);getch();}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 9

Page 10: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

RESULT:

Thus the mean value of a given number using friend function and friend class was executed and output was verified.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 10

Page 11: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

EX:NO:3(A) INHERITANCEDATE :

AIM:To write a simple c++ program using single , multilevel, multiple and hybrid inheritance

ALGORITHM:

STEP 1: Start the program

STEP 2: Create a base class, and derived class

STEP 3: Declare the variables and function

STEP 4: Create an object for class name

STEP 5: Calling a function through an object

STEP 6: Execute the program

STEP 7: Stop the program

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 11

Page 12: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

PROGRAM:

1) SINGLE INHERITANCE:

#include<iostream.h>

#include<conio.h>

class emp

{

public:

int eno;

void get()

{

cout<<"Enter the employee number:";

cin>>eno;

}

};

class salary:public emp

{

public:

void display()

{

cout<<"employee no from base class is derived";

cout<<eno;

}

};

void main()

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 12

Page 13: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

{

clrscr();

salary s1;

s1.get();

s1.display();

getch();

}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 13

Page 14: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 14

Page 15: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

2)MULTILEVEL INHERITANCE:

#include<iostream.h>

#include<conio.h>

class people

{

public:

int employeeno;

};

class employee : public people

{

public:

int salary;

};

class teamleader : public employee

{

public:

void input()

{

cout<<"enter employee no";

cin>>employeeno;

cout<<"enter employee salary";

cin>>salary;

}

void display()

{

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 15

Page 16: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

cout<<"\n employee no is \t "<<endl;

cout<<employeeno;

cout<<"\n employee salary is \t "<<endl;

cout<<salary;

}

};

void main()

{

clrscr();

teamleader shawn;

shawn.input();

shawn.display();

getch();

}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 16

Page 17: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 17

Page 18: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

3) 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 mark1:";

cin>>m1;

cout<<"Enter the mark2:";

cin>>m2;

}

};

class sports

{

protected:

int sm;

public:

void getsm()

{

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 18

Page 19: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

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<<"roll no ="<<rno<<endl;

cout<<"total ="<<tot<<endl;

cout<<"average ="<<avg<<endl;

}};

void main()

{

clrscr();

statement obj;

obj.get();

obj.getsm();

obj.display();

getch();

}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 19

Page 20: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 20

Page 21: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

4)HYBRID INHERITANCE:

#include <iostream.h>

#include<conio.h>

class mm

{

protected:

int rollno;

public:

void get_num(int a)

{

rollno = a;

}

void put_num()

{ cout << "Roll Number Is:"<< rollno << "\n"; }

};

class marks : public mm

{

protected:

int sub1;

int sub2;

public:

void get_marks(int x,int y)

{

sub1 = x;

sub2 = y;

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 21

Page 22: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

}

void put_marks(void)

{

cout << "Subject 1:" << sub1 << "\n";

cout << "Subject 2:" << sub2 << "\n";

}

};

class extra

{

protected:

float e;

public:

void get_extra(float s)

{

e=s;

}

void put_extra(void)

{

cout << "Extra Score::" << e << "\n";

}

};

class res : public marks, public extra

{

protected:

float tot;

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 22

Page 23: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

public:

void disp(void)

{

tot = sub1+sub2+e;

put_num();

put_marks();

put_extra();

cout << "Total:"<< tot;

}

};

int main()

{

clrscr();

res std1;

std1.get_num(10);

std1.get_marks(10,20);

std1.get_extra(33.12);

std1.disp();

getch();

return 0;

}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 23

Page 24: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

RESULT:

Thus a simple c++ program using single , multilevel, multiple and hybrid inheritance.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 24

Page 25: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

EX:NO:4(A) POLYMORPHISMDATE :

AIM:To write a Program to implement in polymorphism using C++.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Declare the base class base.

STEP 3: Declare and define the virtual function show().

STEP 4: Declare and define the function display().

STEP 5: Create the derived class from the base class.

STEP 6: Declare and define the functions display() and show().

STEP 7: Create the base class object

STEP 8: Call the functions display() and show() using the base class object .

STEP 9: Create the derived class object and call the functions display() and show() using

the derived class object

STEP 10: Stop the program.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 25

Page 26: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

PROGRAM:

#include<iostream.h>#include<conio.h>class base{public:void show(){cout<<"\n Base class show:";}void display(){cout<<"\n Base class display:" ;}};class drive:public base{public:void display(){cout<<"\n Drive class display:";}void show(){cout<<"\n Drive class show:";}};void main(){clrscr();base ob1;ob1.show();ob1.display();drive ob;ob.show();ob.display();getch();}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 26

Page 27: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

Base class showBase class displayDrive class show

Drive class display

RESULT :

Thus a Program to implement virtual function using C++ was executed and output is verified

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 27

Page 28: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

EX NO:4(B) FUNCTION OVERLOADING WITH DEFAULT ARGUMENTSDATE :

AIM:

i) To write a c++ program to illustrate the concept of function overloading.ii) To write a c++ program to illustrate the concept of function overloading with default

argument.

ALGORITHM:

STEP 1: Declare necessary variables.

STEP2: Create a class with add(int,int) ,add(float,float) as member functions and

necessary variable.

STEP3: add(int,int) is used to add two integer values.

STEP4: add(float,float) is used to add two floatvalues.

STEP5: Using object call the required function with corresponding input.

STEP6: Display the output.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 28

Page 29: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

PROGRAM:

1) FUNCTION OVERLOADING PROGRAM:

#include<iostream.h>#include<conio.h>class fover{public:int a,b,c;float x,y,z;void add(int a,int b){c=a+b;cout<<"\n the integer value is :" <<c;}void add(float x,float y){z=x+y;cout<<"\n the added float values"<<z;}};void main(){int a,b;float x,y;clrscr();fover f;cout<<"\n enter the integer values:";cin>>a>>b;f.add(a,b);cout<<"\n enter the float values:";cin >>x>>y;f.add(x,y);getch();}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 29

Page 30: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

Enter the float value: 4 4The integer value is 8Enter the float value:5 5The added float value is 10

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 30

Page 31: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

2) FUNCTION OVERLOADING WITH DEFAULT ARGUMENT:

#include<iostream.h>#include<conio.h>class fover{public:int a,b,c;float x,y,z;void add(int a=2,int b=3){c=a+b;cout<<"\n the integer value is :" <<c;}void add(float x,float y){z=x+y;cout<<"\n the added float values"<<z;}};void main(){float x,y;clrscr();fover f;f.add();cout<<"\n enter the float values:";cin >>x>>y;f.add(x,y);getch();}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 31

Page 32: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

The integer value is 8Enter the float value:5 5The added float value is 10

RESULT:

Thus the C++ program using function overloading with default argument was executed and output verified.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 32

Page 33: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

EX NO:5 VIRTUAL FUNCTIONDATE :

AIM:To write a c++ program to illustrate the concept of virtual function.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Declare the base class base.

STEP 3: Declare and define the virtual function show().

STEP 4: Declare and define the function display().

STEP 5: Create the derived class from the base class.

STEP 6: Declare and define the functions display() and show().

STEP 7: Create the base class object and pointer variable.

STEP 8: Call the functions display() and show() using the base class object and pointer.

STEP 9: Create the derived class object and call the functions display() and show() using

the derived class object and pointer.

STEP 10: Stop the program.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 33

Page 34: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

PROGRAM:

#include<iostream.h>

#include<conio.h>

class base

{

public:

virtual void show()

{

cout<<"\n  Base class show:";

}

void display()

{

cout<<"\n  Base class display:" ;

}

};

class drive:public base

{

public:

void display()

{

cout<<"\n  Drive class display:";

}

void show()

{

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 34

Page 35: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

cout<<"\n  Drive class show:";

}

};

void main()

{

clrscr();

base obj1;

base *p;

cout<<"\n\t P points to base:\n"  ;

p=&obj1;

p->display();

p->show();

cout<<"\n\n\t P points to drive:\n";

drive obj2;

p=&obj2;

p->display();

p->show();

getch();

}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 35

Page 36: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

P points to Base

Base class displayBase class show

P points to Drive

Base class Display

Drive class Show

RESULT:

Thus the C++ program using virtual function was executed and output verified.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 36

Page 37: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

EX.NO:6 (A) UNARY OPERATORS OVERLOADING

DATE :

AIM:Program to implement operator overloading using unary operators with member

functions in C++.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Create a class that defines the data type that is to be used in the overloading

operation.

STEP 3: Declare the operator function operator counter() in the public part of the

class.

STEP 4: It may be either a member function or a friend function.

STEP 5: Define the operator function to implement the required operations.

STEP 6: Create objects for each function.

STEP 7: Using that objects invoke the functions

STEP 8: Finally display the results using the function named show()

STEP 9: End the program.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 37

Page 38: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

PROGRAM:

#include <iostream.h>

#include <conio.h>class counter

{

int count;

public:

counter()

{count=0;}

counter(int a)

{count=a;

}

Voidoperator ++() //for prefix

{count++;}

counter operator ++(int) //for eg:- c1 = c2++

{ //and postfix expression

count++;

counter temp;

temp.count=count;

return temp;

}

void getdata(void)

{

cout<<"\n\nEnter Value for Count :-";

cin>>count;

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 38

Page 39: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

}

void display(void)

{

cout<<"\nValue of count is "<<count<<endl;

}

};

void main()

{

clrscr();

counter o1(9),o2,o3;

o1++;

o1.display();

o2.getdata();

o2++;

++o2;

o2.display();

3=o2++;

o3.display();

getch();

}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 39

Page 40: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

Value of count is 10Enter value for count :- 5Value of count is 7

Value of count is 8

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 40

Page 41: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

PROGRAM:

#include<iostream.h>#include<conio.h>class UnaryOp{public:int x,y,z;UnaryOp(){x=0;y=0;z=0;}UnaryOp(int a,int b,int c){x=a;y=b;z=c;}void display(){cout<<"\n\n\t"<<x<<"  "<<y<<"    "<<z;}// Overloaded minus (-) operatorUnaryOp operator- (){x= -x;y= -y;z= -z;}};int main(){clrscr();UnaryOp u1(10,-40,70);cout<<"\n\nNumbers are :::\n";u1.display();-u1;           // call unary minus operator function

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 41

Page 42: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

cout<<"\n\nNumbers are after applying overloaded minus (-) operator :::\n";u1.display();  // display u1getch();}OUTPUT:

Number are::

10 -40 70

Number are after applying overloaded minus (-) operator ::

-10 40 -70

RESULT:

Thus the c++ program for unary operator overloading with member function was executed successfully.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 42

Page 43: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

EX.NO:6 (B) BINARY OPERATORS OVERLOADING

DATE :

AIM:Program to implement operator overloading using binary operators with member

functions in C++.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Create a class that defines the data type that is to be used in the overloading

operation.

STEP 3: Declare the operator function operator oper() in the public part of the class.

STEP 4: It may be either a member function or a friend function.

STEP 5: Define the operator function to implement the required operations.

STEP 6: Create objects for each function.

STEP 7: Using that objects invoke the functions

STEP 8: Finally display the results using the function named show()

STEP 9: End the program.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 43

Page 44: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

PROGRAM:

#include <iostream.h>

#include<conio.h>

class oper

{

int a,b;

public:

oper()

{

}

oper(int x,int y)

{

a=x;

b=y;

}

void show()

{

cout<<a<<" ";

cout<<b<<"\n";

}

oper operator+(oper op2);

};

// Overload + for oper.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 44

Page 45: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

oper oper::operator+(oper op2)

{

oper temp;

temp.a=op2.a+a;

temp.b=op2.b+b;

return temp;

}

void main()

{

clrscr();

oper ob1(10,20),ob2(15,30);

ob1.show(); // displays 10 20

ob2.show(); // displays 15 30

ob1 = ob1 + ob2;

ob1.show(); // displays 25 50

getch();

}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 45

Page 46: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

10 20

15 30

25 50

RESULT:

Thus the c++ program for binary operator overloading with member function was

executed successfully.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 46

Page 47: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

EX.NO:7(a) IMPLEMENTATION OF TEMPLATE FOR LINKED LIST CLASSDATE : WITH NECESSARY METHODS

AIM:To write a C++ program to implement the template of linked list class.

ALGORITHM:

STEP 1: Create a node with one data part and one address part.

STEP 2: In the insert function check whether the head is null or not. If it is null make it

a new value as head node.

STEP 3: Otherwise place the new value in appropriate place in the list.

STEP 4: In the delete function find the availability of the node using search method and

if the node is available delete it. Otherwise display “ ”.

STEP 5: In the count function count the number of nodes in the list and display.

STEP 6: In the delete function get the position to be deleted and delete the node.

STEP 7: Exit the program

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 47

Page 48: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

PROGRAM:

#include<iostream.h>

#include<conio.h>

#include<process.h>

template < class T >

class node

{

private :

T data;

class node<T>* link;

friend class list<T>;

}; template < class T >

class list

{

private:

class node<T>*head;

T c;

public:

list();

void create();

void insert();

int count(void);

void del();

void disp(void);

~list();

};

template < class T >

list<T> :: list()

{

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 48

Page 49: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

head=NULL;

}

template<class T>

void list<T>::create()

{

T x;

if(head==NULL)

{

cout<<"Enter the element: ";

cin>>x;

class node<T>* t = new node<T>;

t->data=x;

t->link=NULL;

head=t;

}

else

cout<<"\nLinked List already created";

}

template < class T >

void list<T> :: disp()

{

class node<T>* t = head;

while(t!=NULL)

{

cout<<t->data<<"->";

t = t->link;

}

cout<<"NULL\n";

}

template < class T >

void list<T> :: insert()

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 49

Page 50: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

{

T x,pos,pos1;

cout<<"\nEnter the element and position: ";

cin>>x>>pos;

class node<T>* t = new node<T>;

class node<T> *t1,*t2;

t->data = x;

t->link = NULL;

t1=head;

pos1=1;

while((pos!=pos1)&&(t1!=NULL))

{t2=t1;

t1=t1->link;

pos1++;

}

if(pos>1)

{

t2->link=t;

t->link=t1;

}

else

{

t->link=t1;

head=t;

}

}

template < class T >

int list<T> :: count()

{

int count = 0;

class node<T>* t= head;

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 50

Page 51: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

while(t!=NULL)

{

count++ ;

t = t->link;}

return count;

}

template < class T >

void list<T> :: del()

{

class node<T> *t,*t1;

T c=0,pos,pos1;

c=count();

cout<<"No of elements= "<<c<<endl;

cout<<"Enter the position to delete: ";

cin>>pos;

t=head;

pos1=1;

while((pos!=pos1)&&(t!=NULL))

{

t1=t;

t=t->link;

pos1++;

}

if (pos==1)

{

t=t->link;

head=t;

}

else

if((pos>1)&&(pos<=c))

{

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 51

Page 52: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

if(pos>1)

{

t1->link=t->link;

}

else

cout<<"\n Invalid position";

}

} template < class T >

list<T> :: ~list()

{

class node<T>* t;

while(head)

{

t = head;

head =head -> link;

delete t;

}

}

void main()

{

list<int> a;

clrscr();

int opt;

cout<<"\t\tLinked List using C++\n\n"<<endl;

do{

cout<<"\n(1).create (2).Insert (3).Count

(4).Display (5).Delete (6) Exit "<<endl;

cout<<"Enter your choice: ";

cin>>opt;

switch(opt)

{

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 52

Page 53: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

case 1:

a.create();

break;

case 2:

a.insert();

break;

case 3:

cout<<"No of elements: "<<a.count();

break;

case 4:

a.disp();

break;

case 5:

a.del();

break;

case 6: exit(0);

}

}while(opt != 6);

}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 53

Page 54: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

(1).create (2).Insert (3).Count (4).Display

(5).Delete (6) Exit

Enter your choice: 1

Enter the element: 10

(1).create (2).Insert (3).Count (4).Display

(5).Delete (6) Exit

Enter your choice: 2

Enter the element and position: 20 2

(1).create (2).Insert (3).Count (4).Display

(5).Delete (6) Exit

Enter your choice: 2

Enter the element and position: 30 3

(1).create (2).Insert (3).Count (4).Display

(5).Delete (6) Exit

Enter your choice: 3

No of elements: 3

(1).create (2).Insert (3).Count (4).Display

(5).Delete (6) Exit

Enter your choice: 4

10->20->30->NULL

(1).create (2).Insert (3).Count (4).Display

(5).Delete (6) Exit

Enter your choice: 5

No of elements= 3

Enter the position to delete: 3

(1).create (2).Insert (3).Count (4).Display

(5).Delete (6) Exit

RESULT:

Thus the program to implement the template of linked list class is implemented.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 54

Page 55: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

EX:NO:7(B) FUNCTION TEMPLATESDATE :

AIM:To write a c++ program for swapping two values using function templates.

ALGORITHM:

STEP1: Declare a template <classT>.

STEP 2: Start the main program.

STEP 3: Declare the integer variables.

STEP 4: Print the values.

STEP 5: Stop the program.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 55

Page 56: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

PROGRAM:

#include <iostream.h>

#include<conio.h>

template <class X> void swapargs(X &a, X &b)

{

X temp;

temp = a;

a = b;

b = temp;

}

int main()

{

int i=10, j=20;

float x=10.1, y=23.3;

clrscr();

cout << "Original i, j: " << i << ' ' << j << endl;

cout << "Original x, y: " << x << ' ' << y << endl;

swapargs(i, j); // swap integers

swapargs(x, y); // swap floats

cout << "Swapped i, j: " << i << ' ' << j << endl;

cout << "Swapped x, y: " << x << ' ' << y << endl;

getch();

return 0;

}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 56

Page 57: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

Original i, j: 10 20Original x, y: 10.1 23.299999Swapped i, j: 20 10Swapped x, y: 23.2999999 10.1

RESULT:

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 57

Page 58: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

Thus the c++ program for swapping two values using function templates was executedand output is verified.

EX:NO: 8 EXCEPTION HANDLING

DATE :

AIM:To perform exception handling with Try, catch.and throw.

ALGORITHM:

STEP1: Start the program.

STEP 2: Declare and define the function test().

STEP3: Within the try block check whether the value is greater than zero or not.

a.    if  the value greater than zero throw the value and catch the corresponding

exception.

b.   Otherwise throw the character and catch the corresponding exception.

STEP 4: Read the integer and character values for the function test().

STEP5: Stop the program.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 58

Page 59: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

PROGRAM:

#include<iostream.h>

#include<conio.h>

void test(int x)

{

try

{

if(x>0)

throw x;

else

throw 'x';

}

catch(int x)

{

cout<<"Catch a integer and that integer is:"<<x;

}

catch(char x)

{

cout<<"Catch a character and that character is:"<<x;

}

}

void main()

{

clrscr();

cout<<"Testing multiple catches\n:";

test(10);

test(0);

getch();

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 59

Page 60: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

}

OUTPUT:

Testing multiple catches

Catch a integer and that integer is: 10

Catch a character and that character is: x

RESULT:

Thus the simple program for exception handling using C++ was executed and output is verified.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 60

Page 61: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

EX.NO: 9 GENERATING TEMPLATES FOR STANDARD SORTING

DATE : ALGORITHMS

AIM:

To develop a template of standard sorting Algorithm such as bubble sort, insertion sort,

merges sort and quick sort

ALGORITHM:

STEP 1: Start the program

STEP 2: Declare and define the template class

STEP 3: Declare the member function , data member .

STEP 4: Define the template function outside the class

STEP 5: Get the input elements

STEP 6: Display the options such as bubble sort, insertion sort , quick sort , merge sort

and heap sort

STEP 7: Get the choice from the user

STEP 8: By using switch simultaneously call the corresponding sorting functions

STEP 9: Display the sorted values

STEP 10: End the program

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 61

Page 62: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

PROGRAM:

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

template<class T>

class sort

{

private:

T a[20],size;

int i,j;

public:

void input(int);

void bubble();

void insertion();

void merge(int,int,int,int);

void merge_split(int,int);

void quick(int,int);

void swap(T a[],int,int);

void percolatedown(int);

void heap();

void view();

void menu();

};

template<class T>

void sort<T>::menu()

{

cout<<"\t1.BUBBLE SORT\n\t2.INSERTION

SORT\n\t3.QUICK

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 62

Page 63: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

SORT\n\t4.MERGE SORT\n\t5.HEAP

SORT\n\t6.EXIT\n";

}

template<class T>

void sort<T>::input(int no)

{

size=no;

cout<<"Enter the element:";

for(i=1;i<=size;i++)

cin>>a[i];

}

template<class T>

void sort<T>::bubble()

{

int t;

for(i=1;i<=size;i++)

{

for(j=i+1;j<=size;j++)

if ( a[i]>a[j])

{

t=a[i];

a[i]=a[j];

a[j]=t;

}

}

}

template<class T>

void sort<T>::insertion()

{

for(i=2;i<=size;i++)

{

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 63

Page 64: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

int t=a[i];

for(j=i;j>1&&t<a[j-1];j--)

a[j]=a[j-1];

a[j]=t;

} }

template<class T>

void sort<T>::swap(T a[],int m,int n)

{

int t;

t=a[m]=a[n];

a[n]=t;

}

template<class T>

void sort<T>::quick(int first,int last)

{

int pivot;

if(first<last)

{

i=first;j=last;pivot=a[first];

while(i<j)

{

while(pivot>=a[i]&&i<last)

i++;

while(pivot<=a[j]&&j>first)

j--;

if(i<j)

swap(a,i,j);

}

swap(a,first,j);

quick(first,j-1);

quick(j+1,last);

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 64

Page 65: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

}

}

template<class T>

void sort<T>::merge_split(int first,int last){

int mid;

if(first<last)

{

mid=(first+last)/2;

merge_split(first,mid);

merge_split(mid+1,last);

merge(first,mid,mid+1,last);

}

}

template<class T>

void sort<T>::merge(int f1,int l1,int f2,int l2)

{

int b[20],k=1;

i=f1;

j=f2;

while(i<=l1&&j<=l2)

{

if(a[i]<=a[j])

b[k++]=a[i++];

else

b[k++]=a[j++];

}

while(i<=l1)

b[k++]=a[j++];

while(j<=l2)

b[k++]=a[j++];

i=f1;

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 65

Page 66: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

j=1;

while(j<k)

a[i++]=b[j++];

}template<class T>void sort<T>::heap()

{

int maxitem[10];

for (int i=size/2;i>0;i--)

percolatedown(i);

cout<<"The sorted order is:";

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

{

maxitem[i]=a[1];

cout<<maxitem[i]<<" ";

a[1]=a[size--];

percolatedown(1);

}

}

template<class T>

void sort<T>::percolatedown(int hole)

{

int child;

int temp=a[hole];

for(;hole*2<=size;hole=child)

{

child=hole*2;

if(child!=size&&a[child+1]<a[child])

child++;

if(a[child]<temp)

a[hole]=a[child];

else

break;

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 66

Page 67: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

}

a[hole]=temp;

}

template<class T>void sort<T>::view()

{

cout<<"The sorted element:";

for(i=1;i<=size;i++)

cout<<a[i]<<" ";

}

void main()

{

clrscr();

int size,ch;

sort<int>s;

s.menu();

cout<<"Enter the number of elements:";

cin>>size;

s.input(size);

while(1)

{

cout<<"\n\nEnter the choice:\n";

cin>>ch;

switch(ch)

{

case 1:

cout<<"BUBBLE SORT\n";

s.bubble();s.view();

break;

case 2:

cout<<"INSERION SORT\n";

s.insertion();

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 67

Page 68: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

s.view();

break;

case 3:

cout<<"QUICK SORT\n";

s.quick(1,size);

s.view();

break;

case 4:

cout<<"MERGE SORT\n";

s.merge_split(1,size);

s.view();

break;

case 5:cout<<"HEAP SORT\n";

s.heap();

break;

default:exit(0);}

}

}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 68

Page 69: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

1.BUBBLE SORT2.INSERTION SORT

3.QUICK SORT

4.MERGE SORT

5.HEAP SORT

6.EXIT

Enter the number of elements:5

Enter the element:5 1 4 2 3

Enter the choice: 1

BUBBLE SORT

The sorted element:1 2 3 4 5

Enter the choice: 2

INSERION SORT

The sorted element:1 2 3 4 5

Enter the choice: 3

QUICK SORT

The sorted element:1 2 3 4 5

Enter the choice: 4

MERGE SORT

The sorted element:1 2 3 4 5

Enter the choice: 5

HEAP SORT The sorted order is:1 2 3 4 5

RESULT:

Thus the c++ program for the develop a template of standard sorting algorithm such as

bubble sort , insertion sort , merge sort and quick sort is verified successfully.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 69

Page 70: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

EX.NO :10 FILE OPERATIONS WITH RANDOMLY GENERATED COMPLEX DATE : NUMBER

AIM:To Write a C++ program that randomly generates complex numbers and writes them in a

file along with an operator. The numbers are written to file in the format (a + ib). Write another

program to read one line at a time from this file, perform the corresponding operation on the two

complex numbers read, and write the result to another file (one per line).

ALGORITHM:

STEP 1: Start the program.

STEP 2: Create a class called COMPLEX and create its members: real and imaginary.

STEP 3: Generate the random numbers by using rand() function.

STEP 4: Write the randomly generated numbers in a file called “complex1.txt”.

STEP 5: Add the two complex numbers.

STEP 6: Store the Resultant complex number in another file called “result.txt”

STEP 7: Display the resultant value.

STEP 8: Stop the program.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 70

Page 71: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

PROGRAM:

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

#include<stdlib.h>

#include<time.h>

class complex

{

public:

int real;

int imag;

complex(int r, int i)

{

real = r;

imag = i;

}

complex()

{

real = imag = 0;

}

void display(void);

};

void complex::display(void)

{

cout<<real<<((imag<0)?"-

i":"+i")<<imag<<"\n";

}

void main()

{

clrscr();

ofstream ocom("complex1.txt");

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 71

Page 72: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

float real,imag;

time_t ti;

srand((unsigned) time(&ti));

real = rand()%100;

imag = rand()%100;

ocom<<"("<<real<<((imag<0)?"-

i":"+i")<<imag<<")"<<"+";

real = rand()%100;

imag = rand()%100;

ocom<<"("<<real<<((imag<0)?"-

i":"+i")<<imag<<")"<<"\n";

ocom.close();

ifstream icom("complex1.txt");

char no,t,ch,op;

icom>>no;

icom>>real;

icom>>ch;

icom>>no;

icom>>imag;

imag=(ch=='+')?imag:-imag;

icom>>no;

icom>>op;

complex a(real,imag);

icom>>no;

icom>>real;

icom>>ch;

icom>>no;

icom>>imag;

imag=(ch=='+')?imag:-imag;

icom>>no;

icom>>op;

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 72

Page 73: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

complex b(real,imag);

complex c;

switch(op)

{

case '+':

c.real = a.real+b.real;

c.imag = a.imag+b.imag;

break;

case '-':

c.real = a.real-b.real;

c.imag = a.imag-b.imag;

break;

case '*':

c.real = (a.real*b.real)-(a.imag*b.imag);

c.imag = (a.real*b.imag)+(a.imag*b.real);

break;

case '/':

float qt;

qt = b.real*b.real+b.imag*b.imag;

c.real = (a.real*b.real+a.imag*b.imag)/qt;

c.imag = (a.imag*b.real-a.real*b.imag)/qt;

break;

default:

cout<<"\n Invalid";}

cout<<"\n complex 1:";

a.display();

cout<<"\n complex 2:";

b.display();

cout<<"\n Resultant complex:";

c.display();

ofstream out("result.txt");

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 73

Page 74: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

out<<"("<<c.real<<((c.imag<0)?"-i":"+i")<<c.imag<<")";

out.close();}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 74

Page 75: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

complex1.txt:

(0+i72)+(39+i71)

result.txt

(39+i143) 82

RESULT:

Thus the program to implement the randomly generates complex numbers and file

operation is executed successfully

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 75

Page 76: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

EX.NO:11(A) PROGRAM SOURCE FILES FOR STACK APPLICATION

DATE : EVALUATION OF POSTFIX EXPRESSION

AIM:To write a C++ program for evaluating the postfix expression.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Scan the Postfix string from left to right.

STEP 3: Initialise an empty stack.

a. If the scannned character is an operand, add it to the stack. If the scanned

character is an operator, there will be atleast two operands in the stack.

b. If the scanned character is an Operator, then we store the top most element of

the stack(topStack) in a variable temp. Pop the stack. Now evaluate

topStack(Operator)temp. Let the result of this operation be retVal. Pop the

stack and Push retVal into the stack.

STEP 4: Repeat this step till all the characters are scanned.

STEP 5: After all characters are scanned, we will have only one element in the stack.

Return topStack.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 76

Page 77: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

PROGRAM:

#include <iostream.h>

#include <stdlib.h>

#include <math.h>

#include <ctype.h>

#include<conio.h>

const int MAX = 50 ;

class postfix

{

private :

int stack[MAX] ;

int top, nn ;

char *s ;

public :

postfix( ) ;

void setexpr ( char *str ) ;

void push ( int item ) ;

int pop( ) ;

void calculate( );

void show( ) ;

} ;

postfix :: postfix( )

{ top = -1 ; }

void postfix :: setexpr ( char *str )

{ s = str ; }

void postfix :: push ( int item )

{

if ( top == MAX - 1 )

cout << endl << "Stack is full" ;

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 77

Page 78: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

else

{

top++ ;

stack[top] = item;

} }

int postfix :: pop( )

{

if ( top == -1 )

{

cout << endl << "Stack is empty" ;

return NULL ;

}

int data = stack[top] ;

top-- ;

return data ;

}

void postfix :: calculate( )

{

int n1, n2, n3 ;

while ( *s )

{

if ( *s == ' ' || *s == '\t' )

{

s++ ;

continue ;

}

if ( isdigit ( *s ) )

{

nn = *s - '0' ;

push ( nn ) ;

}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 78

Page 79: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

else

{

n1 = pop( ) ;

n2 = pop( ) ;

switch ( *s )

{

case '+' :

n3 = n2 + n1 ;

break ;

case '-' :

n3 = n2 - n1 ;

break ;

case '/' :

n3 = n2 / n1 ;

break ;

case '*' :

n3 = n2 * n1 ;

break ;

case '%' :

n3 = n2 % n1 ;

break ;

case '$' :

n3 = pow ( n2 , n1 ) ;

break ;

default :

cout << "Unknown operator" ;

exit ( 1 ) ;

}

push ( n3 ) ;

}

s++ ;

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 79

Page 80: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

}

}

void postfix :: show( )

{

nn = pop ( ) ;

cout << "Result is: "<< nn ;

getch();

}

void main( )

{

char expr[MAX] ;

cout << "\nEnter postfix expression to be evaluated : " ;

cin.getline ( expr, MAX ) ;

postfix q ;

q.setexpr ( expr ) ;

q.calculate( ) ;

q.show( ) ;

}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 80

Page 81: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

Enter postfix expression to be evaluated: 243-+8*

Result is: 24

RESULT:

Thus the C++ program for stack to evaluate postfix expression was executed and output

was verified successfully.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 81

Page 82: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

EX.NO:11(B) QUEUE ADT OPERATIONS USING LINKED LIST

DATE :

AIM:To write a C++ program for implementing Queue using linked list.

ALGORITHM:

STEP 1: Define a struct for each node in the queue. Each node in the queue contains data

and link to thenext node. Front and rear pointer points to first and last node

inserted in the queue.

STEP 2: The operations on the queue are

1. INSERT data into the queue

2. DELETE data out of queue

STEP 3: INSERT DATA INTO queue

1. Enter the data to be inserted into queue.

2. If TOP is NULL

(i) The input data is the first node in queue.

(ii) The link of the node is NULL.

(iii) TOP points to that node.

3. If TOP is NOT NULL

(i) The link of TOP points to the new node.

(ii) TOP points to that node.

STEP 4: DELETE DATA FROM queue

1. If TOP is NULL

(i) The queue is empty

2. If TOP is NOT NULL

(i) The link of TOP is the current TOP.

(ii) The pervious TOP is popped from queue.

STEP 5: The queue represented by linked list is traversed to display its content.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 82

Page 83: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

PROGRAM:

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

class node

{

public:

class node *next;

int data;

};

class queue : public node

{

node *head;

int front,rear;

public:

queue()

{

front=-1; rear=-1;

}

void enqueue(int x)

{

if (rear < 0 )

{

head =new node;

head->next=NULL;

head->data=x;

rear ++; }

else

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 83

Page 84: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

{

node *temp,*temp1;

temp=head;

if(rear >= 4)

{

cout <<"Queue over flow";

return; }

rear++;

while(temp->next != NULL)

temp=temp->next;

temp1=new node;

temp->next=temp1;

temp1->next=NULL;

temp1->data=x;

}

}

void display()

{

node *temp;

temp=head;

if (rear < 0)

{

cout <<" Queue under flow";

return; }

while(temp != NULL)

{

cout <<temp->data<< " ";

temp=temp->next;

} }

void dequeue()

{

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 84

Page 85: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

node *temp;

temp=head;

if( rear < 0)

{

cout <<"Queue under flow";

return; }

if(front == rear)

{

front = rear =-1;

head=NULL;

return;}

front++;

head=head->next;

}};

main()

{

queue s1;

int ch;

clrscr();

cout<<"\n\n\tQUEUE USING LINKED LIST";

cout <<"\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit";

while(1)

{

cout<<"\n Enter your choice:";

cin >> ch;

switch(ch){

case 1:

cout <<"\n Enter the elements:";

cin >> ch;

s1.enqueue(ch);break;

case 2: s1.dequeue();break;

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 85

Page 86: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

case 3:

cout<<"The elements in the list are:\n";

s1.display();break;

case 4: exit(0);}}return (0); }

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 86

Page 87: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

QUEUE USING LINKED LIST

1. Enqueue

2. Dequeue

3. Display

4. Exit

Enter your choice: 1

Enter the elements: 10

Enter your choice: 1

Enter the elements: 20

Enter your choice: 3

The elements in the list are:

10 20

Enter your choice: 2

Enter your choice: 3

The elements in the list are:

20

Enter your choice: 2

Enter your choice: 3

The elements in the list are:

Enter your choice: 4

RESULT:

Thus the C++ program for queue ADT using linked list implementation was created,

executed and output was verified successfully.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 87

Page 88: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

EX.NO:12 BINARY SEARCH TREE

DATE :

AIM:To write a C++ program for constructing binary search tree.

ALGORITHM:

STEP 1: Declare function create(),search(),delete(),Display().

STEP 2: Create a structure for a tree contains left pointer and right pointer.

STEP 3: Insert an element is by checking the top node and the leaf node and the

operation will be performed.

STEP 4: Deleting an element contains searching the tree and deleting the item.

STEP 5: Display the Tree elements.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 88

Page 89: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

PROGRAM:

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

#define TRUE 1

#define FALSE 0

struct btreenode

{

btreenode *leftchild;

btreenode *rightchild;

int data;

}*root;

class btree

{

public:

btree();

void buildtree(int num);

static void insert(btreenode **t,int num);

static void search(btreenode **t,int num,

btreenode **par,btreenode **x,int *found);

void remove(int num);

static void rem(btreenode **t,int num);

void display();

static void inorder(btreenode *t);

static void preorder(btreenode *t);

static void postorder(btreenode *t);

~btree();

static void del(btreenode *t);

void findmax(btreenode *t);

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 89

Page 90: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

void findmin(btreenode *t);

void find(int x,btreenode *t);};

btree::btree()

{ root=NULL; }

void btree::buildtree(int num)

{

insert(&root,num);

}

void btree::insert(btreenode **t,int num)

{

if(*t==NULL) {

*t=new btreenode;

(*t)->leftchild=NULL;

(*t)->data=num;

(*t)->rightchild=NULL; }

else

{

if(num<(*t)->data)

insert(&((*t)->leftchild),num);

else

insert(&((*t)->rightchild),num);

} }

void btree::remove(int num)

{ rem(&root,num); }

void btree::rem(btreenode **t,int num)

{

int found;

btreenode *parent,*x,*xsucc;

if(*t==NULL)

{

cout<<"\n Tree is empty";

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 90

Page 91: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

return; }

parent=x=NULL;

search(t,num,&parent,&x,&found);

if(found==FALSE)

{

cout<<"\n data to be deleted, not found";

return; }

if(x->leftchild!=NULL && x->rightchild !=NULL)

{

parent=x;

xsucc=x->rightchild;

while(xsucc->leftchild!=NULL)

{

parent=xsucc;

xsucc=xsucc->leftchild; }

x->data=xsucc->data;

x=xsucc; }

if(x->leftchild==NULL && x->rightchild==NULL)

{

if(parent->rightchild==x)

parent->rightchild=NULL;

else

parent->leftchild=NULL;

delete x;

return; }

if(x->leftchild==NULL && x->rightchild!=NULL)

{

if(parent->leftchild==x)

parent->leftchild=x->rightchild;

else

parent->rightchild=x->rightchild;

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 91

Page 92: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

delete x;

return; }

if(x->leftchild!=NULL && x->rightchild==NULL)

{

if(parent->leftchild==x)

parent->leftchild=x->leftchild;

else

parent->rightchild=x->leftchild;

delete x;

return; } }

void btree::search(btreenode **t, int num, btreenode **par, btreenode **x, int *found)

{

btreenode *q;

q=*t;

*found=FALSE;

*par=FALSE;

while(q!=NULL)

{

if(q->data==num)

{

*found=TRUE; *x=q;

return; }

*par=q;

if(q->data>num)

q=q->leftchild;

else

q=q->rightchild;

} }

void btree::inorder(btreenode *t)

{

if(t!=NULL)

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 92

Page 93: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

{

inorder(t->leftchild);

cout<<t->data<<"\t";

inorder(t->rightchild);

} }

void btree::preorder(btreenode *t)

{

if(t!=NULL)

{

cout<<"\t"<<t->data;

preorder(t->leftchild);

preorder(t->rightchild);

}

else

return; }

void btree::postorder(btreenode *t)

{

if(t!=NULL)

{

postorder(t->leftchild);

postorder(t->rightchild);

cout<<"\t"<<t->data; }

else

return; }

btree::~btree()

{ del(root); }

void btree::del(btreenode *t)

{

if(t!=NULL)

{

del(t->leftchild);

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 93

Page 94: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

del(t->rightchild);

}

delete t; }

void btree::findmax(btreenode *t)

{

if(t!=NULL)

while(t->rightchild!=NULL)

t=t->rightchild;

cout<<"\n The maximum element is "<<t->data<<"\n\n";

}

void btree::findmin(btreenode *t)

{

if(t!=NULL)

while(t->leftchild!=NULL)

t=t->leftchild;

cout<<"\n The minimum element is "<<t->data<<"\n\n";

}

void btree::find(int x,btreenode *t)

{

if(t==NULL)

cout<<"\n Element is not found";

else

if(x<t->data)

find(x,t->leftchild);

else if(x>t->data)

find(x,t->rightchild);

else

cout<<"\n Element "<<t->data<<" is found"; }

void main()

{

btree b;

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 94

Page 95: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

int n,num,a[20],ch,opt;

clrscr();

do

{

cout<<"\n\t\t BINARY SEARCH TREE\n\n";

cout<<"\n\t1. Creation\n\t2. Insertion.\n\t3. Deletion\n\t4. Display\n\t";

cout<<"5. FindMin\n\t6. FindMax\n\t7. Find\n\t8. Exit\n\n";

cout<<"\n\t Enter your choice : ";

cin>>ch;

switch(ch)

{

case 1:

cout<<"\n Enter the number of elements to build BST:";

cin>>n;

cout<<"\n Enter the elements of the BST :";

for(int i=0;i<n;i++)

cin>>a[i];

for(i=0;i<n;i++)

b.buildtree(a[i]);

break;

case 2:

cout<<"\n Enter the elements to insert : ";

cin>>num;

b.insert(&root,num);break;

case 3:

cout<<"\n Enter the elements to delete : ";

cin>>num;

b.remove(num); break;

case 4:

cout<<"\n\t\t Display\n";

cout<<"\n1.Inorder\n2.Preorder\n3.Postorder\n4.Back to main menu\n";

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 95

Page 96: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

do {

cout<<"\n\nEnter your choice of display:\t ";

cin>>opt;

switch(opt)

{

case 1:

cout<<"\n The data in inorder form is \n";

b.inorder(root);

cout<<endl; break;

case 2:

cout<<"\n The data in preorder form is \n";

b.preorder(root);

cout<<endl;break;

case 3:

cout<<"\n The data in postorder form is:\n ";

b.postorder(root);

cout<<endl;break;

case 4:

cout<<"\n Back to main menu \n";

break; } }

while(opt!=4);break;

case 5:

b.findmin(root); break;

case 6:

b.findmax(root); break;

case 7:

cout<<"\n Enter the element to find : ";

cin>>num;

b.find(num,root); break;

case 8:

exit(1); } }

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 96

Page 97: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

while(ch!=8);

getch(); }

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 97

Page 98: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

BINARY SEARCH TREE

1. Creation

2. Insertion

3. Deletion

4. Display

5. Find min

6. Find max

7. Find

8. Exit

Enter your choice: 1

Enter the number of elements to build BST: 7

Enter the elements of BST:

3 8 1 5 4 10 2

Enter your choice: 2

Enter the elements to insert: 6

Enter your choice: 5

The minimum element is 1

Enter your choice: 6

The maximum element is 10

Enter your choice: 3

Enter the elements to delete: 2

Enter your choice: 3

Enter the elements to delete: 8

Enter your choice: 4

DISPLAY

1. Inorder

2. Preorder

3. Postorder

4. Back to main menu

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 98

Page 99: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

Enter your choice to display: 1

The data in inorder form is

1 3 4 5 6 10

Enter your choice to display: 2

The data in preorder form is

3 1 10 5 4 6

Enter your choice to display: 3

The data in Postorder form is

1 4 6 5 10 3

Enter your choice to display: 4

Back to main menu

Enter your choice: 7

Enter the element to find: 6

Element 6 is found

Enter your choice: 8

RESULT:

Thus the C++ program for binary search tree was created, executed and output was

verified successfully.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 99

Page 100: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

EX.NO:13 TREE TRAVERSALS

DATE :

AIM:

To write a C++ program for constructingpreorder, inorder and postorder using tree

traversals.

ALGORITHM:

STEP 1: Declare class as node() and tree().

STEP 2: To traverse a non-empty binary search tree in pre-order, perform the following

operations recursively at each node, starting with the root node:

1. Visit the root.

2. Traverse the left sub-tree.

3. Traverse the right sub-tree.

STEP 3: To traverse a non-empty binary search tree in in-order (symmetric), perform the

Following operations recursively at each node:

1. Traverse the left sub-tree.

2. Visit the root.

3. Traverse the right sub-tree.

STEP 4: To traverse a non-empty binary search tree in post-order, perform the following

operations recursively at each node:

1. Traverse the left sub-tree.

2. Traverse the right sub-tree.

3. Visit the root.

STEP 5: Display the tree order traversals.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 100

Page 101: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

PROGRAM:

#include <iostream.h>

#include<conio.h>

// Node class

class Node {

int key;

Node* left;

Node* right;

public:

Node() { key=-1; left=NULL; right=NULL; };

void setKey(int aKey) { key = aKey; };

void setLeft(Node* aLeft) { left = aLeft; };

void setRight(Node* aRight) { right = aRight; };

int Key() { return key; };

Node* Left() { return left; };

Node* Right() { return right; };

};

// Tree class

class Tree {

Node* root;

public:

Tree();

~Tree();

Node* Root() { return root; };

void addNode(int key);

void inOrder(Node* n);

void preOrder(Node* n);

void postOrder(Node* n);

private:

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 101

Page 102: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

void addNode(int key, Node* leaf);

void freeNode(Node* leaf);

};

// Constructor

Tree::Tree() {

root = NULL;

}

// Destructor

Tree::~Tree() {

freeNode(root);

}

// Free the node

void Tree::freeNode(Node* leaf)

{

if ( leaf != NULL )

{

freeNode(leaf->Left());

freeNode(leaf->Right());

delete leaf;

}

}

// Add a node

void Tree::addNode(int key) {

// No elements. Add the root

if ( root == NULL ) {

cout << "add root node ... " << key << endl;

Node* n = new Node();

n->setKey(key);

root = n;

}

else {

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 102

Page 103: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

cout << "add other node ... " << key << endl;

addNode(key, root);

}

}

// Add a node (private)

void Tree::addNode(int key, Node* leaf) {

if ( key <= leaf->Key() ) {

if ( leaf->Left() != NULL )

addNode(key, leaf->Left());

else {

Node* n = new Node();

n->setKey(key);

leaf->setLeft(n);

}

}

else {

if ( leaf->Right() != NULL )

addNode(key, leaf->Right());

else {

Node* n = new Node();

n->setKey(key);

leaf->setRight(n);

}

}

}

// Print the tree in-order

// Traverse the left sub-tree, root, right sub-tree

void Tree::inOrder(Node* n) {

if ( n ) {

inOrder(n->Left());

cout << n->Key() << " ";

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 103

Page 104: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

inOrder(n->Right());

}

}

// Print the tree pre-order

// Traverse the root, left sub-tree, right sub-tree

void Tree::preOrder(Node* n) {

if ( n ) {

cout << n->Key() << " ";

preOrder(n->Left());

preOrder(n->Right());

}

}

// Print the tree post-order

// Traverse left sub-tree, right sub-tree, root

void Tree::postOrder(Node* n) {

if ( n ) {

postOrder(n->Left());

postOrder(n->Right());

cout << n->Key() << " ";

}

}

// Test main program

int main() {

Tree* tree = new Tree();

tree->addNode(30);

tree->addNode(10);

tree->addNode(20);

tree->addNode(40);

tree->addNode(50);

cout << "In order traversal" << endl;

tree->inOrder(tree->Root());

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 104

Page 105: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

cout << endl;

cout << "Pre order traversal" << endl;

tree->preOrder(tree->Root());

cout << endl;

cout << "Post order traversal" << endl;

tree->postOrder(tree->Root());

cout << endl; delete tree; return 0;

}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 105

Page 106: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:-

add root node ... 30

add other node ... 10

add other node ... 20

add other node ... 40

add other node ... 50

In order traversal

10 20 30 40 50

Pre order traversal

30 10 20 40 50

Post order traversal

20 10 50 40 30

RESULT:

Thus the C++ program for tree traversals was created, executed and output was verified

successfully.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 106

Page 107: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

EX.NO:14(a) MINIMUM SPANNING TREE

DATE : PRIM’S ALGORITHM

AIM:

To write a C++ program for constructingprim’s algorithm.

ALGORITHM:

STEP 1: Declare function main().

STEP 2: enter the number of vertices and edges for constructing prim’s algorithm.

STEP 3: Insert an edges cost for each vertices and edges which you had created.

STEP 4: After inserting edges cost, its show the order of visited vertices .

STEP 5: Display the visited vertices.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 107

Page 108: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

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],u;

main()

{

int m,c;

cout <<"enterno of vertices";

cin >> n;

cout <<"enter no of edges";

cin >> m;

cout <<"\nEDGES Cost\n";

for(k=1;k<=m;k++)

{

cin >>i>>j>>c;

cost[i][j]=c;

}

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

if(cost[i][j]==0)

cost[i][j]=31999;

cout <<"ORDER OF VISITED VERTICES";

k=1;

while(k<n)

{

m=31999;

if(k==1)

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 108

Page 109: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

{

for(i=1;i<=n;i++)

for(j=1;j<=m;j++)

if(cost[i][j]<m)

{

m=cost[i][j];

u=i;

}

}

else

{

for(j=n;j>=1;j--)

if(cost[v][j]<m && visited[j]!=1 && visit[j]!=1)

{

visit[j]=1;

stk[top]=j;

top++;

m=cost[v][j];

u=j;

}

}

cost[v][u]=31999;

v=u;

cout<<v << " ";

k++;

visit[v]=0; visited[v]=1;

}

}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 109

Page 110: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

Enter no of vertices3

enter no of edges4

EDGES Cost

1 2 3

4 8 9

6 8 7

5 7 6

ORDER OF VISITED VERTICES1 2

RESULT:

Thus the C++ program for prim’s algorithm was created, executed and output was

verified successfully.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 110

Page 111: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

EX.NO:14(B) MINIMUM SPANNING TREE

DATE : KRUSKAL’S ALGORITHM

AIM:

To write a C++ program for constructingkruskal’s algorithm.

ALGORITHM:

STEP 1: Declare class as kruskal and function as read and kruskal.

STEP 2: Enter the number of vertices and adjacency matrix for constructing kruskal’s

algorithm.

STEP 3:Then it’s find the minimum cost for each edges present in kruskal’s graph .

STEP 4: Display the minimum cost edges of kruskal’s graph .

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 111

Page 112: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

PROGRAM:

#include<iostream.h>

#include<conio.h>

int parent [10];

class kruskal

{

int a,b,u,v,i,j,n,noofedges;

int visited[10],min,mincost,cost[10][10];

public:kruskal()

{

noofedges=1;

mincost=0;

}

void read();

void kruskals(int cost[][10],int n);

};

void kruskal::read()

{

cout<<"enter the no. of vertix\n";

cin>>n;

cout<<"enter the adjacency matrix\n";

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

{

cin>>cost[i][j];

if(cost[i][j]==0)

cost[i][j]=999;

}

kruskals(cost,n);

}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 112

Page 113: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

void kruskal::kruskals(int cost[][10],int n)

{

cout<<"the minimum cost edges are\n";

while(noofedges<n)

{

min=999;

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

if(cost[i][j]<min)

{

min=cost[i][j];

a=u=i;

b=v=j;

}

while(parent[u])

u=parent[u];

while(parent[v])

v=parent[v];

if(u!=v)

{

noofedges++;

cout<<"\nedge("<<a<<"->" <<b<<")="<<min;

mincost+=min;

parent[v]=u;

}

cost[a][b]=cost[b][a]=999;

}

cout<<"\nminimum cost="<<mincost;

}

void main()

{

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 113

Page 114: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

clrscr();

kruskal k; k.read();getch();}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 114

Page 115: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

enter the no. of vertix

4

enter the adjacency matrix

1 0 1 0 1 1

1 0 0 0 1 0

0 1 0 1 0 1

the minimum cost edges are

edge(1->3)=1

edge(2->1)=1

edge(4->2)=1

minimum cost=3

RESULT:

Thus the C++ program for kruskal’s algorithm was created, executed and output was

verified successfully.

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 115

Page 116: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

EX.NO:15 SHORTEST PATH ALGORITHMS

DATE : DIJKSTRA’S ALGORITHM

AIM:

To write a C++ program for constructing dijkstra’s algorithm.

ALGORITHM:

STEP 1: Declare structure as node.

STEP 2: Define function as min_heapify , build min_heap, addEdge and bell for

constructing dijkstra’s algorithm.

STEP 3: Then it’s find the shortest paths from one node to others using the concept of a

priority queue.

STEP 4: A priority queue is an abstract data type which is like a regular queue or stack data structure, but where additionally each element has a “priority” associated with it.

STEP5: Display the djikstra’s Algorithm of finding shortest paths from one node to

others using the concept of a priority queue

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 116

Page 117: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

PROGRAM:

#include<iostream.h>

#include<stdio.h>

#include<conio.h>

#define INFINITY 999

struct node

{

int cost;

int value;

int from;

}a[7];

void min_heapify(int *b, int i, int n)

{

int j, temp;

temp = b[i];

j = 2 * i;

while (j <= n)

{

if (j < n && b[j + 1] < b[j])

{

j = j + 1;

}

if (temp < b[j])

{

break;

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 117

Page 118: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

}

else if (temp >= b[j])

{

b[j / 2] = b[j];

j = 2 * j;

}

}

b[j / 2] = temp;

return;

}

void build_minheap(int *b, int n)

{

int i;

for(i = n / 2; i >= 1; i--)

{

min_heapify(b, i, n);

}

}

void addEdge(int am[][7], int src, int dest, int cost)

{

am[src][dest] = cost;

return;

}

void bell(int am[][7])

{

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 118

Page 119: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

int i, j, k, c = 0, temp;

a[0].cost = 0;

a[0].from = 0;

a[0].value = 0;

for (i = 1; i < 7; i++)

{

a[i].from = 0;

a[i].cost = INFINITY;

a[i].value = 0;

}

while (c < 7)

{

int min = 999;

for (i = 0; i < 7; i++)

{

if (min > a[i].cost && a[i].value == 0)

{

min = a[i].cost;

}

else

{

continue;

}

}

for (i = 0; i < 7; i++)

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 119

Page 120: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

{

if (min == a[i].cost && a[i].value == 0)

{

break;

}

else

{

continue;

}

}

temp = i;

for (k = 0; k < 7; k++)

{

if (am[temp][k] + a[temp].cost < a[k].cost)

{

a[k].cost = am[temp][k] + a[temp].cost;

a[k].from = temp;

}

else

{

continue;

}

}

a[temp].value = 1;

c++;

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 120

Page 121: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

}

cout<<"Cost"<<"\t"<<"Source Node"<<endl;

for (j = 0; j < 7; j++)

{

cout<<a[j].cost<<"\t"<<a[j].from<<endl;

}

}

int main()

{

int n, am[7][7], c = 0, i, j, cost;

for (int i = 0; i < 7; i++)

{

for (int j = 0; j < 7; j++)

{

am[i][j] = INFINITY;

}

}

while (c < 12)

{

cout<<"Enter the source, destination and cost of edge\n";

cin>>i>>j>>cost;

addEdge(am, i, j, cost);

c++;

} bell(am);getch();}

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 121

Page 122: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

OUTPUT:

Enter the source, destination and cost of edge013Enter the source, destination and cost of edge026Enter the source, destination and cost of edge122Enter the source, destination and cost of edge134Enter the source, destination and cost of edge231Enter the source, destination and cost of edge244Enter the source, destination and cost of edge252Enter the source, destination and cost of edge342Enter the source, destination and cost of edge364Enter the source, destination and cost of edge461Enter the source, destination and cost of edge45

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 122

Page 123: CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI.

2Enter the source, destination and cost of edge561Cost Source Node0 03 05 16 28 37 28 5

RESULT:

Thus the C++ program for dijsktra’s algorithm was created, executed and output was

verified successfully

PROGRAMMING & DATA STRUCTURE II (REG-2013)RADHA MANI.M (AP/CSE) Page 123