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
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
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
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
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
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
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
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
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
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
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
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