SlideShare une entreprise Scribd logo
1  sur  132
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
SREE SAKTHI ENGINEERING 
COLLEGE 
Karamadai, Coimbatore – 641 104. 
2014-2015 
DEPARTMENT OF 
COMPUTER SCIENCE AND ENGINEERING 
CS6311 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 1
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
PROGRAMMING AND DATA STRUCTURE 
LABORATORY II 
EX: NO: 1(A) CONSTRUCTOR AND DESTRUCTOR 
DATE : 
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
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 ist"<<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
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
OUTPUT: 
Constructor with argument is created 
The value of A is passed using implicit call is 5 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 4
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
RESULT: 
Thus a simple c++ program for constructor and destructor was executed and 
output was verified. 
EX:NO:1(B) COPY CONSTRUCTOR 
DATE : 
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
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); 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 6
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
getchar(); 
} 
OUTPUT: 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 7
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
RESULT: 
Thus a simple c++ program for copy constructor was executed and output was 
verified 
EX:NO:2 FRIEND FUNCTION AND FRIEND CLASS 
DATE : 
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
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 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 9
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
} 
void main() 
{ 
clrscr(); 
base obj; 
obj.get(); 
cout<<"n Mean value is : "<<mean(obj); 
getch(); 
} 
OUTPUT: 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 10
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
RESULT: 
Thus the mean value of a given number using friend function and friend class was 
executed and output was verified. 
EX:NO:3(A) INHERITANCE 
DATE : 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 11
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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 12
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"; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 13
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
cout<<eno; 
} 
}; 
void main() 
{ 
clrscr(); 
salary s1; 
s1.get(); 
s1.display(); 
getch(); 
} 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 14
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
OUTPUT: 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 15
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() 
{ 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 16
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
cout<<"enter employee no"; 
cin>>employeeno; 
cout<<"enter employee salary"; 
cin>>salary; 
} 
void display() 
{ 
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 17
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
OUTPUT: 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 18
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:"; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 19
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
cin>>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<<"roll no ="<<rno<<endl; 
cout<<"total ="<<tot<<endl; 
cout<<"average ="<<avg<<endl; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 20
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
}}; 
void main() 
{ 
clrscr(); 
statement obj; 
obj.get(); 
obj.getsm(); 
obj.display(); 
getch(); 
} 
OUTPUT: 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 21
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; 
} 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 22
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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; 
} 
void put_marks(void) 
{ 
cout << "Subject 1:" << sub1 << "n"; 
cout << "Subject 2:" << sub2 << "n"; 
} 
}; 
class extra 
{ 
protected: 
float e; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 23
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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; 
public: 
void disp(void) 
{ 
tot = sub1+sub2+e; 
put_num(); 
put_marks(); 
put_extra(); 
cout << "Total:"<< tot; 
} 
}; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 24
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
int main() 
{ 
clrscr(); 
res std1; 
std1.get_num(10); 
std1.get_marks(10,20); 
std1.get_extra(33.12); 
std1.disp(); 
getch(); 
return 0; 
} 
OUTPUT: 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 25
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
RESULT: 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 26
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
Thus a simple c++ program using single , multilevel, multiple and hybrid 
inheritance. 
EX:NO:4(A) POLYMORPHISM 
DATE : 
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 27
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(); 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 28
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
ob1.display(); 
drive ob; 
ob.show(); 
ob.display(); 
getch(); 
} 
OUTPUT: 
Base class show 
Base class display 
Drive class show 
Drive class display 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 29
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
RESULT : 
Thus a Program to implement virtual function using C++ was executed and output is 
verified 
EX NO:4(B) FUNCTION OVERLOADING WITH DEFAULT ARGUMENTS 
DATE : 
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 30
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; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 31
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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(); 
} 
OUTPUT: 
Enter the float value: 4 4 
The integer value is 8 
Enter the float value:5 5 
The added float value is 10 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 32
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; 
} 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 33
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
}; 
void main() 
{ 
float x,y; 
clrscr(); 
fover f; 
f.add(); 
cout<<"n enter the float values:"; 
cin >>x>>y; 
f.add(x,y); 
getch(); 
} 
OUTPUT: 
The integer value is 8 
Enter the float value:5 5 
The added float value is 10 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 34
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
RESULT: 
Thus the C++ program using function overloading with default argument was 
executed and output verified. 
EX NO:5 VIRTUAL FUNCTION 
DATE : 
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. 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 35
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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. 
PROGRAM: 
#include<iostream.h> 
#include<conio.h> 
class base 
{ 
public: 
virtual void show() 
{ 
cout<<"n Base class show:"; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 36
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
} 
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 obj1; 
base *p; 
cout<<"nt P points to base:n" ; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 37
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
p=&obj1; 
p->display(); 
p->show(); 
cout<<"nnt P points to drive:n"; 
drive obj2; 
p=&obj2; 
p->display(); 
p->show(); 
getch(); 
} 
OUTPUT: 
P points to Base 
Base class display 
Base class show 
P points to Drive 
Base class Display 
Drive class Show 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 38
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
RESULT: 
Thus the C++ program using virtual function was executed and output verified. 
EX.NO:6 (A) UNARY OPERATORS OVERLOADING 
DATE : 
AIM: 
Program to implement operator overloading using unary operators with member 
functions in C++. 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 39
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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. 
PROGRAM: 
#include <iostream.h> 
#include <conio.h> 
class counter 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 40
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
{ 
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<<"nnEnter Value for Count :-"; 
cin>>count; 
} 
void display(void) 
{ 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 41
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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(); 
} 
OUTPUT: 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 42
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
Value of count is 10 
Enter value for count :- 5 
Value of count is 7 
Value of count is 8 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 43
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<<"nnt"<<x<<" "<<y<<" "<<z; 
} 
// Overloaded minus (-) operator 
UnaryOp operator- () 
{ 
x= -x; 
y= -y; 
z= -z; 
}}; 
int main() 
{ 
clrscr(); 
UnaryOp u1(10,-40,70); 
cout<<"nnNumbers are :::n"; 
u1.display(); 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 44
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
-u1; // call unary minus operator function 
cout<<"nnNumbers are after applying overloaded minus (-) operator :::n"; 
u1.display(); // display u1 
getch();} 
OUTPUT: 
Number are:: 
10 -40 70 
Number are after applying overloaded minus (-) operator :: 
-10 40 -70 
RESULT: 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 45
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
Thus the c++ program for unary operator overloading with member function was 
executed successfully. 
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 46
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"; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 47
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
} 
oper operator+(oper op2); 
}; 
// Overload + for oper. 
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 48
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
OUTPUT: 
10 20 
15 30 
25 50 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 49
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
RESULT: 
Thus the c++ program for binary operator overloading with member function 
was 
executed successfully. 
EX.NO:7(a) IMPLEMENTATION OF TEMPLATE FOR LINKED LIST 
CLASS 
DATE : 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 50
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; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 51
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
T c; 
public: 
list(); 
void create(); 
void insert(); 
int count(void); 
void del(); 
void disp(void); 
~list(); 
}; 
template < class T > 
list<T> :: list() 
{ 
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 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 52
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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<<"NULLn"; 
} 
template < class T > 
void list<T> :: insert() 
{ 
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++; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 53
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
} 
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; 
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(); 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 54
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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)) 
{ 
if(pos>1) 
{ 
t1->link=t->link; 
} 
else 
cout<<"n Invalid position"; 
} 
} template < class T > 
list<T> :: ~list() 
{ 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 55
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
class node<T>* t; 
while(head) 
{ 
t = head; 
head =head -> link; 
delete t; 
} 
} 
void main() 
{ 
list<int> a; 
clrscr(); 
int opt; 
cout<<"ttLinked List using C++nn"<<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) 
{ 
case 1: 
a.create(); 
break; 
case 2: 
a.insert(); 
break; 
case 3: 
cout<<"No of elements: "<<a.count(); 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 56
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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 57
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: 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 58
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
Thus the program to implement the template of linked list class is implemented. 
EX:NO:7(B) FUNCTION TEMPLATES 
DATE : 
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 59
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 60
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
OUTPUT: 
Original i, j: 10 20 
Original x, y: 10.1 23.299999 
Swapped i, j: 20 10 
Swapped x, y: 23.2999999 10.1 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 61
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
RESULT: 
Thus the c++ program for swapping two values using function templates was 
executed 
and 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 62
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) 
{ 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 63
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
cout<<"Catch a character and that character is:"<<x; 
} 
} 
void main() 
{ 
clrscr(); 
cout<<"Testing multiple catchesn:"; 
test(10); 
test(0); 
getch(); 
} 
OUTPUT: 
Testing multiple catches 
Catch a integer and that integer is: 10 
Catch a character and that character is: x 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 64
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
RESULT: 
Thus the simple program for exception handling using C++ was executed and output 
is verified. 
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 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 65
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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 
PROGRAM: 
#include<iostream.h> 
#include<conio.h> 
#include<stdlib.h> 
template<class T> 
class sort 
{ 
private: 
T a[20],size; 
int i,j; 
public: 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 66
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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 SORTnt2.INSERTION 
SORTnt3.QUICK 
SORTnt4.MERGE SORTnt5.HEAP 
SORTnt6.EXITn"; 
} 
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> 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 67
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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++) 
{ 
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]; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 68
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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); 
} 
} 
template<class T> 
void sort<T>::merge_split(int first,int last){ 
int mid; 
if(first<last) 
{ 
mid=(first+last)/2; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 69
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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; 
j=1; 
while(j<k) 
a[i++]=b[j++]; 
}template<class T>void sort<T>::heap() 
{ 
int maxitem[10]; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 70
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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; 
} 
a[hole]=temp; 
} 
template<class T>void sort<T>::view() 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 71
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
{ 
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<<"nnEnter the choice:n"; 
cin>>ch; 
switch(ch) 
{ 
case 1: 
cout<<"BUBBLE SORTn"; 
s.bubble();s.view(); 
break; 
case 2: 
cout<<"INSERION SORTn"; 
s.insertion(); 
s.view(); 
break; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 72
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
case 3: 
cout<<"QUICK SORTn"; 
s.quick(1,size); 
s.view(); 
break; 
case 4: 
cout<<"MERGE SORTn"; 
s.merge_split(1,size); 
s.view(); 
break; 
case 5:cout<<"HEAP SORTn"; 
s.heap(); 
break; 
default:exit(0);} 
} 
} 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 73
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
OUTPUT: 
1.BUBBLE SORT 
2.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 74
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 75
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() 
{ 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 76
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
clrscr(); 
ofstream ocom("complex1.txt"); 
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; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 77
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
icom>>imag; 
imag=(ch=='+')?imag:-imag; 
icom>>no; 
icom>>op; 
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:"; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 78
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
a.display(); 
cout<<"n complex 2:"; 
b.display(); 
cout<<"n Resultant complex:"; 
c.display(); 
ofstream out("result.txt"); 
out<<"("<<c.real<<((c.imag<0)?"-i":"+i")<<c.imag<<")"; 
out.close();} 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 79
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
OUTPUT: 
complex1.txt: 
(0+i72)+(39+i71) 
result.txt 
(39+i143) 82 
RESULT: 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 80
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
Thus the program to implement the randomly generates complex numbers and file 
operation is executed successfully 
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 81
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( ) 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 82
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
{ top = -1 ; } 
void postfix :: setexpr ( char *str ) 
{ s = str ; } 
void postfix :: push ( int item ) 
{ 
if ( top == MAX - 1 ) 
cout << endl << "Stack is full" ; 
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' ) 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 83
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
{ 
s++ ; 
continue ; 
} 
if ( isdigit ( *s ) ) 
{ 
nn = *s - '0' ; 
push ( nn ) ; 
} 
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 ; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 84
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
break ; 
case '$' : 
n3 = pow ( n2 , n1 ) ; 
break ; 
default : 
cout << "Unknown operator" ; 
exit ( 1 ) ; 
} 
push ( n3 ) ; 
} 
s++ ; 
} 
} 
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 85
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
OUTPUT: 
Enter postfix expression to be evaluated: 243-+8* 
Result is: 24 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 86
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
RESULT: 
Thus the C++ program for stack to evaluate postfix expression was executed and 
output was verified successfully. 
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 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 87
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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. 
PROGRAM: 
#include<iostream.h> 
#include<conio.h> 
#include<stdlib.h> 
class node 
{ 
public: 
class node *next; 
int data; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 88
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
}; 
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 
{ 
node *temp,*temp1; 
temp=head; 
if(rear >= 4) 
{ 
cout <<"Queue over flow"; 
return; } 
rear++; 
while(temp->next != NULL) 
temp=temp->next; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 89
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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() 
{ 
node *temp; 
temp=head; 
if( rear < 0) 
{ 
cout <<"Queue under flow"; 
return; } 
if(front == rear) 
{ 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 90
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
front = rear =-1; 
head=NULL; 
return;} 
front++; 
head=head->next; 
}}; 
main() 
{ 
queue s1; 
int ch; 
clrscr(); 
cout<<"nntQUEUE USING LINKED LIST"; 
cout <<"n1.Enqueuen2.Dequeuen3.Displayn4.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; 
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 91
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
OUTPUT: 
QUEUE USING LINKED LIST 
1. Enqueue 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 92
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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. 
EX.NO:12 BINARY SEARCH TREE 
DATE : 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 93
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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 94
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); 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 95
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
void findmax(btreenode *t); 
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; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 96
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
if(*t==NULL) 
{ 
cout<<"n Tree is empty"; 
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) 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 97
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
{ 
if(parent->leftchild==x) 
parent->leftchild=x->rightchild; 
else 
parent->rightchild=x->rightchild; 
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) 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 98
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
q=q->leftchild; 
else 
q=q->rightchild; 
} } 
void btree::inorder(btreenode *t) 
{ 
if(t!=NULL) 
{ 
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; } 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 99
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
else 
return; } 
btree::~btree() 
{ del(root); } 
void btree::del(btreenode *t) 
{ 
if(t!=NULL) 
{ 
del(t->leftchild); 
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<<"nn"; 
} 
void btree::findmin(btreenode *t) 
{ 
if(t!=NULL) 
while(t->leftchild!=NULL) 
t=t->leftchild; 
cout<<"n The minimum element is "<<t->data<<"nn"; 
} 
void btree::find(int x,btreenode *t) 
{ 
if(t==NULL) 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 100
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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; 
int n,num,a[20],ch,opt; 
clrscr(); 
do 
{ 
cout<<"ntt BINARY SEARCH TREEnn"; 
cout<<"nt1. Creationnt2. Insertion.nt3. Deletionnt4. Displaynt"; 
cout<<"5. FindMinnt6. FindMaxnt7. Findnt8. Exitnn"; 
cout<<"nt 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++) 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 101
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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<<"ntt Displayn"; 
cout<<"n1.Inordern2.Preordern3.Postordern4.Back to main menun"; 
do { 
cout<<"nnEnter 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); 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 102
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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); } } 
while(ch!=8); 
getch(); } 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 103
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 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 104
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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 
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 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 105
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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. 
EX.NO:13 TREE TRAVERSALS 
DATE : 
AIM: 
To write a C++ program for constructingpreorder, inorder and postorder using tree 
traversals. 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 106
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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. 
PROGRAM: 
#include <iostream.h> 
#include<conio.h> 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 107
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
// 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: 
void addNode(int key, Node* leaf); 
void freeNode(Node* leaf); 
}; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 108
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
// 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 { 
cout << "add other node ... " << key << endl; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 109
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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()); 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 110
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
cout << n->Key() << " "; 
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); 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 111
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
tree->addNode(50); 
cout << "In order traversal" << endl; 
tree->inOrder(tree->Root()); 
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 112
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 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 113
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
RESULT: 
Thus the C++ program for tree traversals was created, executed and output was 
verified successfully. 
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 114
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 Costn"; 
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++) 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 115
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
if(cost[i][j]==0) 
cost[i][j]=31999; 
cout <<"ORDER OF VISITED VERTICES"; 
k=1; 
while(k<n) 
{ 
m=31999; 
if(k==1) 
{ 
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; 
} 
} 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 116
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
cost[v][u]=31999; 
v=u; 
cout<<v << " "; 
k++; 
visit[v]=0; visited[v]=1; 
} 
} 
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 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 117
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
RESULT: 
Thus the C++ program for prim’s algorithm was created, executed and output was 
verified successfully. 
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 118
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); 
}; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 119
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
void kruskal::read() 
{ 
cout<<"enter the no. of vertixn"; 
cin>>n; 
cout<<"enter the adjacency matrixn"; 
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); 
} 
void kruskal::kruskals(int cost[][10],int n) 
{ 
cout<<"the minimum cost edges aren"; 
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]) 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 120
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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() 
{ 
clrscr(); 
kruskal k; k.read();getch();} 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 121
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 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 122
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
edge(4->2)=1 
minimum cost=3 
RESULT: 
Thus the C++ program for kruskal’s algorithm was created, executed and output 
was verified successfully. 
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. 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 123
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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 
PROGRAM: 
#include<iostream.h> 
#include<stdio.h> 
#include<conio.h> 
#define INFINITY 999 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 124
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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; 
} 
else if (temp >= b[j]) 
{ 
b[j / 2] = b[j]; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 125
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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]) 
{ 
int i, j, k, c = 0, temp; 
a[0].cost = 0; 
a[0].from = 0; 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 126
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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++) 
{ 
if (min == a[i].cost && a[i].value == 0) 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 127
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
{ 
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 128
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 edgen"; 
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 129
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
OUTPUT: 
Enter the source, destination and cost of edge 
013 
Enter the source, destination and cost of edge 
026 
Enter the source, destination and cost of edge 
122 
Enter the source, destination and cost of edge 
134 
Enter the source, destination and cost of edge 
231 
Enter the source, destination and cost of edge 
244 
Enter the source, destination and cost of edge 
252 
Enter the source, destination and cost of edge 
342 
Enter the source, destination and cost of edge 
364 
Enter the source, destination and cost of edge 
461 
Enter the source, destination and cost of edge 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 130
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
452 
Enter the source, destination and cost of edge 
561 
Cost Source Node 
0 0 
3 0 
5 1 
6 2 
8 3 
7 2 
8 5 
RESULT: 
PROGRAMMING & DATA STRUCTURE II (REG-2013) 
RADHA MANI.M (AP/CSE) Page 131
SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 
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 132

Contenu connexe

Tendances

Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in CSaket Pathak
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rathSANTOSH RATH
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Saket Pathak
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings imtiazalijoono
 
c-programming-using-pointers
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointersSushil Mishra
 
Object Oriented Programming Using C++ Practical File
Object Oriented Programming Using C++ Practical FileObject Oriented Programming Using C++ Practical File
Object Oriented Programming Using C++ Practical FileHarjinder Singh
 
Understanding storage class using nm
Understanding storage class using nmUnderstanding storage class using nm
Understanding storage class using nmmohamed sikander
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions imtiazalijoono
 
Simple c program
Simple c programSimple c program
Simple c programRavi Singh
 
Programming with c language practical manual
Programming with c language practical manualProgramming with c language practical manual
Programming with c language practical manualAnil Bishnoi
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solutionAzhar Javed
 

Tendances (20)

Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
 
Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
 
C programs
C programsC programs
C programs
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
c-programming-using-pointers
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointers
 
Object Oriented Programming Using C++ Practical File
Object Oriented Programming Using C++ Practical FileObject Oriented Programming Using C++ Practical File
Object Oriented Programming Using C++ Practical File
 
Understanding storage class using nm
Understanding storage class using nmUnderstanding storage class using nm
Understanding storage class using nm
 
Function basics
Function basicsFunction basics
Function basics
 
Cquestions
Cquestions Cquestions
Cquestions
 
C Prog - Array
C Prog - ArrayC Prog - Array
C Prog - Array
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
C questions
C questionsC questions
C questions
 
Simple c program
Simple c programSimple c program
Simple c program
 
Pnno
PnnoPnno
Pnno
 
Programming with c language practical manual
Programming with c language practical manualProgramming with c language practical manual
Programming with c language practical manual
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
Revision1schema C programming
Revision1schema C programmingRevision1schema C programming
Revision1schema C programming
 

En vedette

Programming & Data Structure Lecture Notes
Programming & Data Structure Lecture NotesProgramming & Data Structure Lecture Notes
Programming & Data Structure Lecture NotesFellowBuddy.com
 
ΟΔΟΣ ΔΗΜΟΣΘΕΝΗ ΜΙΤΣΗ ΛΕΜΕΣΟΣ
ΟΔΟΣ ΔΗΜΟΣΘΕΝΗ ΜΙΤΣΗ ΛΕΜΕΣΟΣΟΔΟΣ ΔΗΜΟΣΘΕΝΗ ΜΙΤΣΗ ΛΕΜΕΣΟΣ
ΟΔΟΣ ΔΗΜΟΣΘΕΝΗ ΜΙΤΣΗ ΛΕΜΕΣΟΣnicolaidoumarina
 
Conventions of short films
Conventions of short filmsConventions of short films
Conventions of short filmspelboy123
 
Οι διδυμοι πυργοι της Λεμεσου
Οι διδυμοι πυργοι της ΛεμεσουΟι διδυμοι πυργοι της Λεμεσου
Οι διδυμοι πυργοι της Λεμεσουnicolaidoumarina
 
Καβάφης Κωνσταντίνος
Καβάφης ΚωνσταντίνοςΚαβάφης Κωνσταντίνος
Καβάφης Κωνσταντίνοςnicolaidoumarina
 
Καβάφης Κωνσταντίνος
Καβάφης ΚωνσταντίνοςΚαβάφης Κωνσταντίνος
Καβάφης Κωνσταντίνοςnicolaidoumarina
 
ΟΙ ΖΟΥΛΟΥ Παναγιώτα
ΟΙ ΖΟΥΛΟΥ  ΠαναγιώταΟΙ ΖΟΥΛΟΥ  Παναγιώτα
ΟΙ ΖΟΥΛΟΥ Παναγιώταnicolaidoumarina
 
Κωνσταντίνος Καβάφης
Κωνσταντίνος  Καβάφης  Κωνσταντίνος  Καβάφης
Κωνσταντίνος Καβάφης nicolaidoumarina
 
Big data on AWS
Big data on AWSBig data on AWS
Big data on AWSStylight
 
Ινδιάνοι - Δημήτρης και Κωνσταντίνος
Ινδιάνοι - Δημήτρης και ΚωνσταντίνοςΙνδιάνοι - Δημήτρης και Κωνσταντίνος
Ινδιάνοι - Δημήτρης και Κωνσταντίνοςnicolaidoumarina
 
Κωνσταντίνος Καβάφης
Κωνσταντίνος ΚαβάφηςΚωνσταντίνος Καβάφης
Κωνσταντίνος Καβάφηςnicolaidoumarina
 
ΟΔΟΣ ΔΗΜΟΣΘΕΝΗ ΜΙΤΣΗ
ΟΔΟΣ ΔΗΜΟΣΘΕΝΗ ΜΙΤΣΗΟΔΟΣ ΔΗΜΟΣΘΕΝΗ ΜΙΤΣΗ
ΟΔΟΣ ΔΗΜΟΣΘΕΝΗ ΜΙΤΣΗnicolaidoumarina
 
Sample ppt new niche interior by mulavira interior systems
Sample ppt new niche interior   by mulavira interior systemsSample ppt new niche interior   by mulavira interior systems
Sample ppt new niche interior by mulavira interior systemsMulavira Interior Systems
 
Conventions of short films
Conventions of short filmsConventions of short films
Conventions of short filmspelboy123
 
Οδός στρατηγού μακρυγιάννη
Οδός στρατηγού μακρυγιάννηΟδός στρατηγού μακρυγιάννη
Οδός στρατηγού μακρυγιάννηnicolaidoumarina
 
ΟΔΟΣ ΒΟΡΕΙΟΥ ΗΠΕΙΡΟΥ ΛΕΜΕΣΟΣ
ΟΔΟΣ ΒΟΡΕΙΟΥ ΗΠΕΙΡΟΥ ΛΕΜΕΣΟΣΟΔΟΣ ΒΟΡΕΙΟΥ ΗΠΕΙΡΟΥ ΛΕΜΕΣΟΣ
ΟΔΟΣ ΒΟΡΕΙΟΥ ΗΠΕΙΡΟΥ ΛΕΜΕΣΟΣnicolaidoumarina
 
Οδός Χρυσανθου Επισκόπου Πάφου
Οδός Χρυσανθου Επισκόπου ΠάφουΟδός Χρυσανθου Επισκόπου Πάφου
Οδός Χρυσανθου Επισκόπου Πάφουnicolaidoumarina
 

En vedette (20)

Programming & Data Structure Lecture Notes
Programming & Data Structure Lecture NotesProgramming & Data Structure Lecture Notes
Programming & Data Structure Lecture Notes
 
ΟΔΟΣ ΔΗΜΟΣΘΕΝΗ ΜΙΤΣΗ ΛΕΜΕΣΟΣ
ΟΔΟΣ ΔΗΜΟΣΘΕΝΗ ΜΙΤΣΗ ΛΕΜΕΣΟΣΟΔΟΣ ΔΗΜΟΣΘΕΝΗ ΜΙΤΣΗ ΛΕΜΕΣΟΣ
ΟΔΟΣ ΔΗΜΟΣΘΕΝΗ ΜΙΤΣΗ ΛΕΜΕΣΟΣ
 
Conventions of short films
Conventions of short filmsConventions of short films
Conventions of short films
 
Οι διδυμοι πυργοι της Λεμεσου
Οι διδυμοι πυργοι της ΛεμεσουΟι διδυμοι πυργοι της Λεμεσου
Οι διδυμοι πυργοι της Λεμεσου
 
Καβάφης Κωνσταντίνος
Καβάφης ΚωνσταντίνοςΚαβάφης Κωνσταντίνος
Καβάφης Κωνσταντίνος
 
Καβάφης Κωνσταντίνος
Καβάφης ΚωνσταντίνοςΚαβάφης Κωνσταντίνος
Καβάφης Κωνσταντίνος
 
بحث د عهد حوري
بحث د عهد حوريبحث د عهد حوري
بحث د عهد حوري
 
Καβάφης
ΚαβάφηςΚαβάφης
Καβάφης
 
ΟΙ ΖΟΥΛΟΥ Παναγιώτα
ΟΙ ΖΟΥΛΟΥ  ΠαναγιώταΟΙ ΖΟΥΛΟΥ  Παναγιώτα
ΟΙ ΖΟΥΛΟΥ Παναγιώτα
 
Κωνσταντίνος Καβάφης
Κωνσταντίνος  Καβάφης  Κωνσταντίνος  Καβάφης
Κωνσταντίνος Καβάφης
 
Big data on AWS
Big data on AWSBig data on AWS
Big data on AWS
 
Ινδιάνοι - Δημήτρης και Κωνσταντίνος
Ινδιάνοι - Δημήτρης και ΚωνσταντίνοςΙνδιάνοι - Δημήτρης και Κωνσταντίνος
Ινδιάνοι - Δημήτρης και Κωνσταντίνος
 
Κωνσταντίνος Καβάφης
Κωνσταντίνος ΚαβάφηςΚωνσταντίνος Καβάφης
Κωνσταντίνος Καβάφης
 
ΟΔΟΣ ΔΗΜΟΣΘΕΝΗ ΜΙΤΣΗ
ΟΔΟΣ ΔΗΜΟΣΘΕΝΗ ΜΙΤΣΗΟΔΟΣ ΔΗΜΟΣΘΕΝΗ ΜΙΤΣΗ
ΟΔΟΣ ΔΗΜΟΣΘΕΝΗ ΜΙΤΣΗ
 
Sample ppt new niche interior by mulavira interior systems
Sample ppt new niche interior   by mulavira interior systemsSample ppt new niche interior   by mulavira interior systems
Sample ppt new niche interior by mulavira interior systems
 
Conventions of short films
Conventions of short filmsConventions of short films
Conventions of short films
 
Οδός στρατηγού μακρυγιάννη
Οδός στρατηγού μακρυγιάννηΟδός στρατηγού μακρυγιάννη
Οδός στρατηγού μακρυγιάννη
 
ΟΔΟΣ ΒΟΡΕΙΟΥ ΗΠΕΙΡΟΥ ΛΕΜΕΣΟΣ
ΟΔΟΣ ΒΟΡΕΙΟΥ ΗΠΕΙΡΟΥ ΛΕΜΕΣΟΣΟΔΟΣ ΒΟΡΕΙΟΥ ΗΠΕΙΡΟΥ ΛΕΜΕΣΟΣ
ΟΔΟΣ ΒΟΡΕΙΟΥ ΗΠΕΙΡΟΥ ΛΕΜΕΣΟΣ
 
Article
ArticleArticle
Article
 
Οδός Χρυσανθου Επισκόπου Πάφου
Οδός Χρυσανθου Επισκόπου ΠάφουΟδός Χρυσανθου Επισκόπου Πάφου
Οδός Χρυσανθου Επισκόπου Πάφου
 

Similaire à CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarSivakumar R D .
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaonyash production
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALPrabhu D
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarSivakumar R D .
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignmentSaket Pathak
 
C# Tutorial MSM_Murach chapter-15-slides
C# Tutorial MSM_Murach chapter-15-slidesC# Tutorial MSM_Murach chapter-15-slides
C# Tutorial MSM_Murach chapter-15-slidesSami Mut
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiMuhammed Thanveer M
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual finalAhalyaR
 
22316-2019-Summer-model-answer-paper.pdf
22316-2019-Summer-model-answer-paper.pdf22316-2019-Summer-model-answer-paper.pdf
22316-2019-Summer-model-answer-paper.pdfPradipShinde53
 
cbse 12 computer science IP
cbse 12 computer science IPcbse 12 computer science IP
cbse 12 computer science IPD. j Vicky
 
Example for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfExample for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfrajaratna4
 
cbse 12 computer science investigatory project
cbse 12 computer science investigatory project  cbse 12 computer science investigatory project
cbse 12 computer science investigatory project D. j Vicky
 
cbse 12 computer science investigatory project
cbse 12 computer science investigatory project  cbse 12 computer science investigatory project
cbse 12 computer science investigatory project D. j Vicky
 
JavaScript Modules Past, Present and Future
JavaScript Modules Past, Present and FutureJavaScript Modules Past, Present and Future
JavaScript Modules Past, Present and FutureIgalia
 

Similaire à CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY (20)

Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUAL
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignment
 
Opps manual final copy
Opps manual final   copyOpps manual final   copy
Opps manual final copy
 
OOPs manual final copy
OOPs manual final   copyOOPs manual final   copy
OOPs manual final copy
 
C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
C# Tutorial MSM_Murach chapter-15-slides
C# Tutorial MSM_Murach chapter-15-slidesC# Tutorial MSM_Murach chapter-15-slides
C# Tutorial MSM_Murach chapter-15-slides
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer Melayi
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
22316-2019-Summer-model-answer-paper.pdf
22316-2019-Summer-model-answer-paper.pdf22316-2019-Summer-model-answer-paper.pdf
22316-2019-Summer-model-answer-paper.pdf
 
Static and const members
Static and const membersStatic and const members
Static and const members
 
Ocs752 unit 4
Ocs752   unit 4Ocs752   unit 4
Ocs752 unit 4
 
cbse 12 computer science IP
cbse 12 computer science IPcbse 12 computer science IP
cbse 12 computer science IP
 
Example for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfExample for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdf
 
cbse 12 computer science investigatory project
cbse 12 computer science investigatory project  cbse 12 computer science investigatory project
cbse 12 computer science investigatory project
 
cbse 12 computer science investigatory project
cbse 12 computer science investigatory project  cbse 12 computer science investigatory project
cbse 12 computer science investigatory project
 
JavaScript Modules Past, Present and Future
JavaScript Modules Past, Present and FutureJavaScript Modules Past, Present and Future
JavaScript Modules Past, Present and Future
 

Dernier

UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesPrabhanshu Chaturvedi
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 

Dernier (20)

UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 

CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY

  • 1. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. SREE SAKTHI ENGINEERING COLLEGE Karamadai, Coimbatore – 641 104. 2014-2015 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING CS6311 PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 1
  • 2. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. PROGRAMMING AND DATA STRUCTURE LABORATORY II EX: NO: 1(A) CONSTRUCTOR AND DESTRUCTOR DATE : 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
  • 3. 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 ist"<<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
  • 4. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. OUTPUT: Constructor with argument is created The value of A is passed using implicit call is 5 PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 4
  • 5. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. RESULT: Thus a simple c++ program for constructor and destructor was executed and output was verified. EX:NO:1(B) COPY CONSTRUCTOR DATE : 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
  • 6. 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); PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 6
  • 7. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. getchar(); } OUTPUT: PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 7
  • 8. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. RESULT: Thus a simple c++ program for copy constructor was executed and output was verified EX:NO:2 FRIEND FUNCTION AND FRIEND CLASS DATE : 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
  • 9. 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 PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 9
  • 10. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. } void main() { clrscr(); base obj; obj.get(); cout<<"n Mean value is : "<<mean(obj); getch(); } OUTPUT: PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 10
  • 11. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. RESULT: Thus the mean value of a given number using friend function and friend class was executed and output was verified. EX:NO:3(A) INHERITANCE DATE : PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 11
  • 12. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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 12
  • 13. 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"; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 13
  • 14. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. cout<<eno; } }; void main() { clrscr(); salary s1; s1.get(); s1.display(); getch(); } PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 14
  • 15. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. OUTPUT: PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 15
  • 16. 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() { PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 16
  • 17. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. cout<<"enter employee no"; cin>>employeeno; cout<<"enter employee salary"; cin>>salary; } void display() { 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 17
  • 18. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. OUTPUT: PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 18
  • 19. 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:"; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 19
  • 20. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. cin>>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<<"roll no ="<<rno<<endl; cout<<"total ="<<tot<<endl; cout<<"average ="<<avg<<endl; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 20
  • 21. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. }}; void main() { clrscr(); statement obj; obj.get(); obj.getsm(); obj.display(); getch(); } OUTPUT: PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 21
  • 22. 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; } PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 22
  • 23. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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; } void put_marks(void) { cout << "Subject 1:" << sub1 << "n"; cout << "Subject 2:" << sub2 << "n"; } }; class extra { protected: float e; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 23
  • 24. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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; public: void disp(void) { tot = sub1+sub2+e; put_num(); put_marks(); put_extra(); cout << "Total:"<< tot; } }; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 24
  • 25. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. int main() { clrscr(); res std1; std1.get_num(10); std1.get_marks(10,20); std1.get_extra(33.12); std1.disp(); getch(); return 0; } OUTPUT: PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 25
  • 26. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. RESULT: PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 26
  • 27. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. Thus a simple c++ program using single , multilevel, multiple and hybrid inheritance. EX:NO:4(A) POLYMORPHISM DATE : 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 27
  • 28. 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(); PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 28
  • 29. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. ob1.display(); drive ob; ob.show(); ob.display(); getch(); } OUTPUT: Base class show Base class display Drive class show Drive class display PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 29
  • 30. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. RESULT : Thus a Program to implement virtual function using C++ was executed and output is verified EX NO:4(B) FUNCTION OVERLOADING WITH DEFAULT ARGUMENTS DATE : 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 30
  • 31. 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; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 31
  • 32. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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(); } OUTPUT: Enter the float value: 4 4 The integer value is 8 Enter the float value:5 5 The added float value is 10 PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 32
  • 33. 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; } PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 33
  • 34. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. }; void main() { float x,y; clrscr(); fover f; f.add(); cout<<"n enter the float values:"; cin >>x>>y; f.add(x,y); getch(); } OUTPUT: The integer value is 8 Enter the float value:5 5 The added float value is 10 PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 34
  • 35. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. RESULT: Thus the C++ program using function overloading with default argument was executed and output verified. EX NO:5 VIRTUAL FUNCTION DATE : 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. PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 35
  • 36. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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. PROGRAM: #include<iostream.h> #include<conio.h> class base { public: virtual void show() { cout<<"n Base class show:"; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 36
  • 37. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. } 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 obj1; base *p; cout<<"nt P points to base:n" ; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 37
  • 38. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. p=&obj1; p->display(); p->show(); cout<<"nnt P points to drive:n"; drive obj2; p=&obj2; p->display(); p->show(); getch(); } OUTPUT: P points to Base Base class display Base class show P points to Drive Base class Display Drive class Show PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 38
  • 39. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. RESULT: Thus the C++ program using virtual function was executed and output verified. EX.NO:6 (A) UNARY OPERATORS OVERLOADING DATE : AIM: Program to implement operator overloading using unary operators with member functions in C++. PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 39
  • 40. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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. PROGRAM: #include <iostream.h> #include <conio.h> class counter PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 40
  • 41. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. { 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<<"nnEnter Value for Count :-"; cin>>count; } void display(void) { PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 41
  • 42. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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(); } OUTPUT: PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 42
  • 43. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. Value of count is 10 Enter value for count :- 5 Value of count is 7 Value of count is 8 PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 43
  • 44. 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<<"nnt"<<x<<" "<<y<<" "<<z; } // Overloaded minus (-) operator UnaryOp operator- () { x= -x; y= -y; z= -z; }}; int main() { clrscr(); UnaryOp u1(10,-40,70); cout<<"nnNumbers are :::n"; u1.display(); PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 44
  • 45. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. -u1; // call unary minus operator function cout<<"nnNumbers are after applying overloaded minus (-) operator :::n"; u1.display(); // display u1 getch();} OUTPUT: Number are:: 10 -40 70 Number are after applying overloaded minus (-) operator :: -10 40 -70 RESULT: PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 45
  • 46. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. Thus the c++ program for unary operator overloading with member function was executed successfully. 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 46
  • 47. 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"; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 47
  • 48. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. } oper operator+(oper op2); }; // Overload + for oper. 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 48
  • 49. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. OUTPUT: 10 20 15 30 25 50 PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 49
  • 50. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. RESULT: Thus the c++ program for binary operator overloading with member function was executed successfully. EX.NO:7(a) IMPLEMENTATION OF TEMPLATE FOR LINKED LIST CLASS DATE : 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 50
  • 51. 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; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 51
  • 52. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. T c; public: list(); void create(); void insert(); int count(void); void del(); void disp(void); ~list(); }; template < class T > list<T> :: list() { 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 PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 52
  • 53. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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<<"NULLn"; } template < class T > void list<T> :: insert() { 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++; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 53
  • 54. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. } 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; 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(); PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 54
  • 55. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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)) { if(pos>1) { t1->link=t->link; } else cout<<"n Invalid position"; } } template < class T > list<T> :: ~list() { PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 55
  • 56. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. class node<T>* t; while(head) { t = head; head =head -> link; delete t; } } void main() { list<int> a; clrscr(); int opt; cout<<"ttLinked List using C++nn"<<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) { case 1: a.create(); break; case 2: a.insert(); break; case 3: cout<<"No of elements: "<<a.count(); PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 56
  • 57. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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 57
  • 58. 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: PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 58
  • 59. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. Thus the program to implement the template of linked list class is implemented. EX:NO:7(B) FUNCTION TEMPLATES DATE : 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 59
  • 60. 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 60
  • 61. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. OUTPUT: Original i, j: 10 20 Original x, y: 10.1 23.299999 Swapped i, j: 20 10 Swapped x, y: 23.2999999 10.1 PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 61
  • 62. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. RESULT: Thus the c++ program for swapping two values using function templates was executed and 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 62
  • 63. 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) { PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 63
  • 64. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. cout<<"Catch a character and that character is:"<<x; } } void main() { clrscr(); cout<<"Testing multiple catchesn:"; test(10); test(0); getch(); } OUTPUT: Testing multiple catches Catch a integer and that integer is: 10 Catch a character and that character is: x PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 64
  • 65. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. RESULT: Thus the simple program for exception handling using C++ was executed and output is verified. 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 PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 65
  • 66. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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 PROGRAM: #include<iostream.h> #include<conio.h> #include<stdlib.h> template<class T> class sort { private: T a[20],size; int i,j; public: PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 66
  • 67. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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 SORTnt2.INSERTION SORTnt3.QUICK SORTnt4.MERGE SORTnt5.HEAP SORTnt6.EXITn"; } 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> PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 67
  • 68. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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++) { 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]; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 68
  • 69. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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); } } template<class T> void sort<T>::merge_split(int first,int last){ int mid; if(first<last) { mid=(first+last)/2; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 69
  • 70. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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; j=1; while(j<k) a[i++]=b[j++]; }template<class T>void sort<T>::heap() { int maxitem[10]; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 70
  • 71. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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; } a[hole]=temp; } template<class T>void sort<T>::view() PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 71
  • 72. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. { 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<<"nnEnter the choice:n"; cin>>ch; switch(ch) { case 1: cout<<"BUBBLE SORTn"; s.bubble();s.view(); break; case 2: cout<<"INSERION SORTn"; s.insertion(); s.view(); break; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 72
  • 73. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. case 3: cout<<"QUICK SORTn"; s.quick(1,size); s.view(); break; case 4: cout<<"MERGE SORTn"; s.merge_split(1,size); s.view(); break; case 5:cout<<"HEAP SORTn"; s.heap(); break; default:exit(0);} } } PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 73
  • 74. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. OUTPUT: 1.BUBBLE SORT 2.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 74
  • 75. 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 75
  • 76. 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() { PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 76
  • 77. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. clrscr(); ofstream ocom("complex1.txt"); 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; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 77
  • 78. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. icom>>imag; imag=(ch=='+')?imag:-imag; icom>>no; icom>>op; 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:"; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 78
  • 79. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. a.display(); cout<<"n complex 2:"; b.display(); cout<<"n Resultant complex:"; c.display(); ofstream out("result.txt"); out<<"("<<c.real<<((c.imag<0)?"-i":"+i")<<c.imag<<")"; out.close();} PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 79
  • 80. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. OUTPUT: complex1.txt: (0+i72)+(39+i71) result.txt (39+i143) 82 RESULT: PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 80
  • 81. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. Thus the program to implement the randomly generates complex numbers and file operation is executed successfully 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 81
  • 82. 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( ) PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 82
  • 83. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. { top = -1 ; } void postfix :: setexpr ( char *str ) { s = str ; } void postfix :: push ( int item ) { if ( top == MAX - 1 ) cout << endl << "Stack is full" ; 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' ) PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 83
  • 84. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. { s++ ; continue ; } if ( isdigit ( *s ) ) { nn = *s - '0' ; push ( nn ) ; } 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 ; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 84
  • 85. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. break ; case '$' : n3 = pow ( n2 , n1 ) ; break ; default : cout << "Unknown operator" ; exit ( 1 ) ; } push ( n3 ) ; } s++ ; } } 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 85
  • 86. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. OUTPUT: Enter postfix expression to be evaluated: 243-+8* Result is: 24 PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 86
  • 87. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. RESULT: Thus the C++ program for stack to evaluate postfix expression was executed and output was verified successfully. 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 PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 87
  • 88. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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. PROGRAM: #include<iostream.h> #include<conio.h> #include<stdlib.h> class node { public: class node *next; int data; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 88
  • 89. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. }; 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 { node *temp,*temp1; temp=head; if(rear >= 4) { cout <<"Queue over flow"; return; } rear++; while(temp->next != NULL) temp=temp->next; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 89
  • 90. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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() { node *temp; temp=head; if( rear < 0) { cout <<"Queue under flow"; return; } if(front == rear) { PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 90
  • 91. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. front = rear =-1; head=NULL; return;} front++; head=head->next; }}; main() { queue s1; int ch; clrscr(); cout<<"nntQUEUE USING LINKED LIST"; cout <<"n1.Enqueuen2.Dequeuen3.Displayn4.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; 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 91
  • 92. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. OUTPUT: QUEUE USING LINKED LIST 1. Enqueue PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 92
  • 93. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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. EX.NO:12 BINARY SEARCH TREE DATE : PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 93
  • 94. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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 94
  • 95. 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); PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 95
  • 96. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. void findmax(btreenode *t); 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; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 96
  • 97. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. if(*t==NULL) { cout<<"n Tree is empty"; 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) PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 97
  • 98. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. { if(parent->leftchild==x) parent->leftchild=x->rightchild; else parent->rightchild=x->rightchild; 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) PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 98
  • 99. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. q=q->leftchild; else q=q->rightchild; } } void btree::inorder(btreenode *t) { if(t!=NULL) { 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; } PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 99
  • 100. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. else return; } btree::~btree() { del(root); } void btree::del(btreenode *t) { if(t!=NULL) { del(t->leftchild); 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<<"nn"; } void btree::findmin(btreenode *t) { if(t!=NULL) while(t->leftchild!=NULL) t=t->leftchild; cout<<"n The minimum element is "<<t->data<<"nn"; } void btree::find(int x,btreenode *t) { if(t==NULL) PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 100
  • 101. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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; int n,num,a[20],ch,opt; clrscr(); do { cout<<"ntt BINARY SEARCH TREEnn"; cout<<"nt1. Creationnt2. Insertion.nt3. Deletionnt4. Displaynt"; cout<<"5. FindMinnt6. FindMaxnt7. Findnt8. Exitnn"; cout<<"nt 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++) PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 101
  • 102. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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<<"ntt Displayn"; cout<<"n1.Inordern2.Preordern3.Postordern4.Back to main menun"; do { cout<<"nnEnter 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); PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 102
  • 103. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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); } } while(ch!=8); getch(); } PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 103
  • 104. 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 PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 104
  • 105. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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 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 PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 105
  • 106. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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. EX.NO:13 TREE TRAVERSALS DATE : AIM: To write a C++ program for constructingpreorder, inorder and postorder using tree traversals. PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 106
  • 107. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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. PROGRAM: #include <iostream.h> #include<conio.h> PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 107
  • 108. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. // 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: void addNode(int key, Node* leaf); void freeNode(Node* leaf); }; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 108
  • 109. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. // 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 { cout << "add other node ... " << key << endl; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 109
  • 110. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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()); PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 110
  • 111. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. cout << n->Key() << " "; 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); PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 111
  • 112. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. tree->addNode(50); cout << "In order traversal" << endl; tree->inOrder(tree->Root()); 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 112
  • 113. 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 PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 113
  • 114. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. RESULT: Thus the C++ program for tree traversals was created, executed and output was verified successfully. 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 114
  • 115. 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 Costn"; 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++) PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 115
  • 116. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. if(cost[i][j]==0) cost[i][j]=31999; cout <<"ORDER OF VISITED VERTICES"; k=1; while(k<n) { m=31999; if(k==1) { 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; } } PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 116
  • 117. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. cost[v][u]=31999; v=u; cout<<v << " "; k++; visit[v]=0; visited[v]=1; } } 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 PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 117
  • 118. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. RESULT: Thus the C++ program for prim’s algorithm was created, executed and output was verified successfully. 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 118
  • 119. 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); }; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 119
  • 120. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. void kruskal::read() { cout<<"enter the no. of vertixn"; cin>>n; cout<<"enter the adjacency matrixn"; 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); } void kruskal::kruskals(int cost[][10],int n) { cout<<"the minimum cost edges aren"; 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]) PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 120
  • 121. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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() { clrscr(); kruskal k; k.read();getch();} PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 121
  • 122. 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 PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 122
  • 123. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. edge(4->2)=1 minimum cost=3 RESULT: Thus the C++ program for kruskal’s algorithm was created, executed and output was verified successfully. 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. PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 123
  • 124. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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 PROGRAM: #include<iostream.h> #include<stdio.h> #include<conio.h> #define INFINITY 999 PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 124
  • 125. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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; } else if (temp >= b[j]) { b[j / 2] = b[j]; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 125
  • 126. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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]) { int i, j, k, c = 0, temp; a[0].cost = 0; a[0].from = 0; PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 126
  • 127. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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++) { if (min == a[i].cost && a[i].value == 0) PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 127
  • 128. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. { 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 128
  • 129. 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 edgen"; 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 129
  • 130. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. OUTPUT: Enter the source, destination and cost of edge 013 Enter the source, destination and cost of edge 026 Enter the source, destination and cost of edge 122 Enter the source, destination and cost of edge 134 Enter the source, destination and cost of edge 231 Enter the source, destination and cost of edge 244 Enter the source, destination and cost of edge 252 Enter the source, destination and cost of edge 342 Enter the source, destination and cost of edge 364 Enter the source, destination and cost of edge 461 Enter the source, destination and cost of edge PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 130
  • 131. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 452 Enter the source, destination and cost of edge 561 Cost Source Node 0 0 3 0 5 1 6 2 8 3 7 2 8 5 RESULT: PROGRAMMING & DATA STRUCTURE II (REG-2013) RADHA MANI.M (AP/CSE) Page 131
  • 132. SREE SAKTHI ENGINEERING COLLEGE – KARAMADAI. 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 132