Вы находитесь на странице: 1из 85

DEFAULT ARGUMENTS

Aim:
To implement function with default arguments.
Algorithm:
1.
2.
3.
4.

Declare the Default function in the outside of the main function.


Get the values.
Define the default function
Print the values

Program:
#include<iostream.h>
void printLine(char =_,int =70);
void main()
{
printLine();
printLine(/);
printLine(*,40);
printLine(R,55);
}
void printLine(char ch, int Repeatcount)
{
int i;
cout<<endl;
for(i=0;i<Repeatcount;i++)
cout<<ch;
}
Output:
-------- --------------------------///////////////////////////////////////
****************************
RRRRRRRRRRRRRRRRRRRRRRR

Result:
Thus the program for default arguments has been executed and verified
successfully.
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

CLASS WITH STATIC DATA MEMBER


Aim:
To implement static data member in class.
Algorithm:
1.
2.
3.
4.

Create class ITEM with static data member as count.


Create a member function to increment the count.
Declare the static datamember using scope resolution operator.
Display the count value.

Program:
#include<iostream.h>
class item
{
static int count;
int num;
public:
void getdata(int a)
{
num=a;
count++;
cout<<Number<<num;
}
void showcount()
{
cout<<count;
cout<<count<<\n;
}
};
int item::count;
int main()
{
item a,b,c;
a.showcount();
b.showcount();
c.showcount();
a.getdata(20);
b.getdata(30);
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

c.getdata(40);
a.showcount();
b.showcount();
c.showcount();
}

Output:
count
count
count
Number
Number
Number
count
count
count

0
0
0
20
30
40
3
3
3

Result:
Thus the program for static data member has been executed and verified
successfully

CLASS WITH STATIC MEMBER FUNCTION


Aim:
To implement static member function in class.

Algorithm:
1.
2.
3.
4.

Create class ITEM with static data member as count.


Create a member function to increment the count.
Declare the static data member using scope resolution operator.
Display the count value.

Program:
#include<iostream.h>
class test
{
int code;
static int count;
public:
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

void setcode(void)
{
code= ++count;
}
void showcode(void)
{
cout<<Object Number:<<code<<\n;
}
static void showcount(void)
{
cout<<Count = <<count<<\n;
}
};
int test::count;
int main()
{
test t1,t2;
t1.setcount();
t2.setcount();
test::showcount();
test t3;
t1.showcode();
t2.showcode();
t3.showcode();
return(0);
}
Output:
count
count
Object Number
Object Number
Object Number

2
3
1
2
3

Result:
Thus the program for static member function has been executed and
verified successfully.

EC6312 OOPS and Data Structures Lab


Ms.M.Abinaya, Lect/IT

ADDITION OF TWO COMPLEX NUMBERS


Aim:
To write a program for object as argument and returning an object
using complex number addition.

Algorithm:
1.
2.
3.
4.
5.

The class complex contains two member variables real and imaginary.
Assign the value for real and imaginary part.
Declare a temporary variable temp.
Add real part with real of other object and store it in temps real.
Add imaginary part with imaginary of other object and store it in temps
imaginary.
6. Display temp.

Program:
#include<iostream.h>
template<class T>
class complex
{
private:
T real;
T imag;
Public:
complex()
{
real=imag=0;
}
void getdata()
{
cout<<real part?;
cin>>real;
cout<<imag part?;
cin>>imag;
}
complex operator +(complex c2);
void outdata(char *msg)
{
cout<<msg<<(<<real;
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

cout<<,<<img<<)<<endl;
}
};
Template<class T>
complex<T> complex<T>::operator +(complex<T>c2)
{
Complex<T>temp;
temp.real=real+c2.real;
temp.img=imag+c2.imag;
return(temp);
}
void main()
{
complex<int>c1,c2,c3;
cout<<Addition of integercomplexobjects.<<endl;
cout<<Enter complex number c1..<<endl;
c1.getdata();
cout<<Enterthecomplexnumberc2<<endl;
c2.getdata();
c3=c1+c2;
c3.outdata(c3=c1+c2:);
complex<float>c4,c5,c6;
cout<<Additionof float complexobjects.<<endl;
cout<<Enter complex number c4..<<endl;
c4.getdata();
cout<<Enterthecomplexnumberc5<<endl;
c5.getdata();
c6=c4+c5;
c6.outdata(c6=c4+c5:);
}

Output:
Addition of integer complexobjects
Enter complex number c1..
Real part?1
Imag part?2
Enter complex number c2..
Real part?3
Imag part?4
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

C3=c1+c2:(4,6)
Addition of float complexobjects
Enter complex number c4..
Real part?1.5
Imag part?2.5
Enter complex number c5..
Real part?2.4
Imag part?3.7
C6=c4+c5:(3.9,6.2)
Result:
Thus the program for addition of two complex numbers has been
executed and verified successfully.

FRIEND FUNCTION
Aim:
To write a c++ program for friend function.

Algorithm:
1. Create the class and declare the data member as private.
2. Declare the friend function using the keyword friend.
3. Perform the operation of adding two private variables in the friend
function.
4. Display the result.

Program:
#include <iostream.h>
using namespace std;
class myclass {
int a, b;
public:
friend int sum(myclass x);
void set_ab(int i, int j);
};
void myclass::set_ab(int i, int j)
{
a = i;
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

b = j;
}
// Note: sum() is not a member function of any class.
int sum(myclass x)
{
/* Because sum() is a friend of myclass, it can
directly access a and b. */
return x.a + x.b;
}
int main()
{
myclass n;
n.set_ab(3, 4);
cout << sum(n);
return 0;
}
Output:
7

Result:
Thus the program for friend function has been executed and verified
successfully.
CLASS AND OBJECTS
Aim:
To write a c++ program for employee wages calculation using class and
objects.

Algorithm:
1. Employee class contains name and wage variable and member function a
putname(), putwage(),getwage() and getname().
2. Putname: Assign the valve for the character array name.
3. Getname: Retrieves the value of Variable name.
4. Putwage: Assign the valve for wage variable.
5. Getwage: Retrieves the value of wage variable.
6. Im main() Put and display both name and wages.

Program:
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

#include <iostream.h>
class employee
{
char name[80]; // private by default
public:
void putname(char *n); // these are public
void getname(char *n);
private:
double wage; // now, private again
public:
void putwage(double w); // back to public
double getwage();
};
void employee::putname(char *n)
{
strcpy(name, n);
}
void employee::getname(char *n)
{
strcpy(n, name);
}
void employee::putwage(double w)
{
wage = w;
}
double employee::getwage()
{
return wage;
}
int main()
{
employee ted;
char name[80];
ted.putname("Ted Jones");
ted.putwage(75000);
ted.getname(name);
cout << name << " makes $";
cout << ted.getwage() << " per year.";
return 0;
}
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

Output:
Ted Jones makes $75000 per year.

Result:
Thus the program for classes and objects has been executed and verified
successfully.

INHERITANCE
Aim:
To Write a C++ program for implementing the inheritance.

Algorithm:
1.
2.
3.
4.
5.
6.

Define the base class with variables and functions.


Define the derived class with variables and functions.
Define main function
Declare the variables.
Inherits base class.
Access member of derived class.

Program:
#include<iostream.h>
class base
{
public:
int x;
void set_x(int n)
{x=n;
}
void show_x()
{
cout<<\n\t Base class.;
cout<<\n\tx=<<x;
}
};
class derived:public base
{
int y;
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

10

public:
void set_y(int n)
{
y=n;
}
void show_xy()
{
cout<<\n\n\t derived class;
cout<<\n\tx=<<x;
cout<<\n\ty=<<y;
}
};
void main()
{
derived obj;
int x,y;
cout<<\n enter the value of x;
cin>>x;
cout<<\n enter the value of y;
cin>>y;
obj.set_x(x);//inherits base class
obj.set_y(y);//acess member of derived class
obj.show_x();//inherits base class
obj.show_xy();//acess member of derived class
}

Output:
enter the value of x 10
enter the value of y 20
base class.
x=10
derived class..
x=10
y=20

Result:
Thus the program for inheritance has been executed and verified
successfully.
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

11

FUNCTION OVERLOADING
Aim:
To Write a C++ program for implementing the function overloading.

Algorithm:
1.
2.
3.
4.
5.

Define class.
Define the functions with different arguments.
Define main function
Declare the variables.
Call the Different task of function.
6. Print the output.

Program:
#include<iostream.h>
class test
{
public:
int sum(int,int);
float sum(float,float);
double sum(double,double);
};
int test::sum(int a.int b)
{
return(a+b);
}
float test::sum(float a ,float b)
{
return(a+b);
}
double test::sum(double a,double b)
{
return(a+b);
}
void main()
{
test obj;
int choice;
int a,b;
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

12

float x,y;
double m,n;
double result=0;
cout<<\n\t\t main menu;
cout<<\n\t1. Addition of two integer numbers;
cout<<\n\t2. Addition of two float numbers;
cout<<\n\t3. Addition of two double numbers<<endl;
cout<<\n enter your choice:;
cin>>choice;
switch(choice)
{
case 1: cout<<\n enter 2 numbers;
cin>>a>>b;
result=obj.sum(a,b);
break;
case 2:
cout<<\n enter 2 number;
cin>>x>>y;
result=obj.sum(x,y);
break;
case 3:
cout<<\n enter 2 number;
cin>>m>>n;
result=obj.sum(m,n);
break;
default:
cout<<wrong choice;
break;
}
cout<<\n\n result<<result<<endl;
getch();
}

Output:
Main menu
1.Addition of two integer numbers
2.Addition of two float numbers
3.Addition of two double numbers
enter your choice 2
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

13

enter 2 number 1.13 5.6


result 6.73

Result:
Thus the program for function overloading has been executed and
verified successfully.
CALL BY RREFERENCE
Aim:
To write a C++ program for Call by Reference

Algorithm:
1.
2.
3.
4.
5.

Declare the swap function.


The function has the values with reference
Call the swap function in function main
Define the swap function with the reference value.
Display the values.

Program:
#include <iostream.h>
void swap(int &x, int &y);
int main ()
{
int a = 100;
int b = 200;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
swap(a, b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
return 0;
}
void swap(int &x, int &y)
{
int temp;
temp = x;
x = y;
y = temp;
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

14

return;
}
Output:
Before swap, value of a:100
Before swap, value of b:200
After swap, value of a:200
After swap, value of b:100

Result:
Thus the program for Call by Reference has been executed and verified
successfully.
CALL BY VALUE
Aim:
To write a C++ program for call by value.

Algorithm:
1.
2.
3.
4.
5.

Declare the swap function.


The function has the values with value
Call the swap function in function main
Define the swap function with the value.
Display the values.

Program:
#include <iostream.h>
void swap(int x, int y);
int main ()
{
int a = 100;
int b = 200;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
swap(a, b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
return 0;
}
void swap(int x, int y)
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

15

{
int temp;
temp = x;
x = y;
y = temp;
return;
}

Output:
Before swap, value of a:100
Before swap, value of b:200
After swap, value of a:200
After swap, value of b:100

Result:
Thus the program for Call by value has been executed and verified
successfully.
MATRIX MULTIPLICATION USING STATIC, FRIEND AND
DEFAULT FUNCTIONS
Aim:
To write a C++ program to perform matrix manipulation using static
variable, default argument and friend function.
Algorithm:
1. Declare the class as Matrix.
2. Declare the data member as r, c and **x.
3. Declare the member function as Member function with default argument
is used to initialize the value of the matrix.
4. get() function is used to get the values of two matrices.
5. add() and mul() function are used to perform addition and multiplication
of the
matrices.
6. In the main, create objects A and B for the Matrix class.
7. Call the get() method to get the value of matrix A and B.
8. Call the add() and mul() method to perform the particular operation and
finally
display the result.
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

16

Program:
#include<stdio.h>
#include<conio.h>
#include<iomanip.h>
class matrix
{
static int r,c;
int**x;
public:
matrix(int r1=2,int c1=2);
void get();
void put();
friend matrix add(matrix,matrix);
friend matrix mul(matrix,matrix);
};
matrix::matrix(int r1,int c1)
{
r=r1;c=c1;
x=new int*[r];
for(int i=0;i<r;i++)
x[i]=new int[c];
for(i=0;i<r;i++)
for(int j=0;j<c;j++)
{
x[i][j]=0;
}
}
void matrix::get()
{
cout<<"\n enter the matrix of size"<<r<<"x"<<c<<endl;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
cin>>x[i][j];
}
void matrix::put()
{
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

17

for(int i=0;i<r;i++,cout<<endl)
for(int j=0;j<c;j++)
cout<<setw(4)<<x[i][j];
}
int matrix::r;
int matrix::c;
matrix add(matrix a,matrix b)
{
matrix c;
for(int i=0;i<a.r;i++)
for(int j=0;j<a.c;j++)
c.x[i][j]=a.x[i][j]+(b.x[i][j]);
return c;
}
matrix mul(matrix a,matrix b)
{
matrix c;
for(int i=0;i<a.r;i++)
for(int j=0;j<b.c;j++)
for(int k=0;k<a.c;k++)
{
c.x[i][j]=c.x[i][j]+a.x[i][k]*(b.x[k][j]);
}
return c;
}
void main()
{
clrscr();
matrix a,b,c1,d1;
a.get();
b.get();
cout<<"The matrix A:"<<endl;
a.put();
cout<<"The matrix B:"<<endl;
b.put();
c1=add(a,b);
cout<<"The resultant matrix (A+B):"<<endl;
c1.put();
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

18

cout<<"\n\n The resultant matrix(A*B):"<<endl;


d1=mul(a,b);
d1.put();
getch();
}

Output:
Enter the matrix of size2x2
23
23
Enter the matrix of size2x2
45
45
The matrix A:
2 3
2 3
The matrix B:
4 5
4 5
The resultant matrix(A+B):
6 8
6 8
The resultant matrix(A*B):
20 25
20 25

Result:
Thus the program for matrix multiplication using static variable , default
argument, friend function has been executed and verified successfully.

EC6312 OOPS and Data Structures Lab


Ms.M.Abinaya, Lect/IT

19

IMPLEMENTATION OF VARIOUS LIST OPERATIONS USING


ARRAYS
Aim:
To Implement a program for various List operations using arrays

Algorithm:
1.
2.
3.
4.
5.
6.

Create a structure called Node


Create an array of size 10.
Create a constructor for initializing the variables
Create a variables head.
Create functions for list & create , insert & delete.
In create function get the values and keep it as a head, and increment the
index value and add the values after head.
7. In display function display all the values as a list format.
8. In delete function delete any node and add next node to the previous
node link.
9. In insert function, insert the new node, break the link and add the new
inserted node.
10.In search function, search the new node whether it is present or not.
11.Write the function definition
12.Run the program.

Program:
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
class arr_list
{
private:
struct node
{
int data;
int next;
}
a[10];
public:
int head;
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

20

arr_list();
int create();
void display(int);
void insert();
void del();
void search();
};
arr_list::arr_list()
{
for(int i=0;i<10;i++)
{
a[i].data=-1;
}
}
int arr_list::create()
{
int head,i;
cout<<"\nenter the index for first node";
cin>>i;
head=i;
while(i!=-1)
{
cout<<"\nenter the data and index of the first element";
cin>>a[i].data;
cout<<"";
cin>>a[i].next;
i=a[i].next;
}
return head;
}
void arr_list::display(int i)
{
while(i!=-1)
{
if(a[i].data==-1)
cout<<"";
else
{
cout<<a[i].data<<"->";
}
i=a[i].next;
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

21

}
cout<<"NULL";
}
void arr_list::insert()
{
int i,new_data,temp;
cout<<"\nenter the new data which is to be inserted";
cin>>new_data;
cout<<"\nenter the data after whice you want to insert";
cin>>temp;
for(i=0;i<10;i++)
{
if(a[i].data==temp)
{
if(a[i+1].data++-1)
{
a[i+1].next=a[i].next;
a[i].next=i+1;
a[i+1].data=new_data;
}
}
}
}
void arr_list::del()
{
int i,temp,current,new_next;
cout<<"\nenter the node to be delete";
cin>>temp;
for(i=0;i<10;i++)
{
if(a[i].data==temp)
{
if(a[i].next==-1)
{
a[i].data=-1;
}
current=i;
new_next=a[i].next;
}
}
for(i=0;i<10;i++)
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

22

{
if(a[i].next==current)
{
a[i].next=new_next;
a[current].data=-1;
}
}
}
void arr_list::search()
{
int i,temp,flag=0;
cout<<"\nenter the node to be searched";
cin>>temp;
for(i=0;i<10;i++)
{
if(a[i].data==temp)
{
flag=1;
break;
}
}
if(flag==1)
cout<<"\nthe"<<temp<<"node is present is the list";
else
cout<<"\nthe node is not present";
}
void main()
{
char ans;
int choice;
arr_list obj;
clrscr();
cout<<"\t\tprogram for implementing list using array";
cout<<"\nmain menu";
cout<<"\n1.creation";
cout<<"\n2.display";
cout<<"\n3.insertion of element in the list";
cout<<"\n4.deletion of element form the list";
cout<<"\n5.searching of element from the list";
cout<<"\n6.exit";
do
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

23

{
cout<<"\nenter your choice";
cin>>choice;
switch(choice)
{
case 1:
obj.head=obj.create();
break;
case 2:
obj.display(obj.head);
break;
case 3:
obj.insert();
obj.display(obj.head);
break;
case 4:
obj.del();
obj.display(obj.head);
break;
case 5:
obj.search();
break;
case 6:
exit(0);
}
cout<<"\ndo you wish to go to main menu?";
ans=getch();
}
while(ans=='Y.'||ans=='y');
cout<<"Thank You";
getch();
}

EC6312 OOPS and Data Structures Lab


Ms.M.Abinaya, Lect/IT

24

Output:
program for implementing list using array
main menu
1.creation
2.display
3.insertion of element in the list
4.deletion of element form the list
5.searching of element from the list
6.exit
enter your choice1
enter the index for first node3
enter the data and index of the first element2 5
enter the data and index of the first element5 4
enter the data and index of the first element4 -1
do you wish to go to main menu?
enter your choice2
2->5->4->NULL
do you wish to go to main menu?
enter your choice3
enter the new data which is to be inserted6
enter the data after whice you want to insert5
2->5->6->4->NULL
do you wish to go to main menu?
enter your choice4
enter the node to be delete5
2->6->4->NULL
do you wish to go to main menu?
enter your choice5
enter the node to be searched6
the6node is present is the list
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

25

do you wish to go to main menu?


enter your choice5
enter the node to be searched1
the node is not present
do you wish to go to main menu?
enter your choice6

Result:
Thus the Array implantation of List ADT
successfully.

program was executed

LINKED LIST IMPLEMENTATION USING LISTADT


Aim:
To implement a linked list and do all operations on it.

Algorithm:
1.
2.
3.
4.

Start the process.


Initialize and declare variables.
Enter the choice.
If choice is INSERT then
a. Enter the element to be inserted.
b. Get a new node and set DATA[NEWNODE] = ITEM.
c. Find the node after which the new node is to be inserted.
d. Adjust the link fields.
e. Print the linked list after insertion.
5. If choice is DELETE then
a. Enter the element to be deleted.
b. Find the node containing the element (LOC) and its preceding node
(PAR).
c. Set ITEM = DATA[LOC] and delete the node LOC.
d. Adjust the link fields so that PAR points to the next element. Ie
LINK[PAR] = LINK [ LOC].
e. Print the linked list after deletion.
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

26

6. Stop the process.


Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
class sll
{
private:
struct node
{
int data;
struct node*next;
}
*head;
public:
sll();
void create();
void display();
void search(int key);
void insert_head();
void insert_after();
void insert_last();
void dele();
~sll();
};
sll::sll()
{
head=NULL;
}
sll::~sll()
{
node*temp,*temp1;
temp=head->next;
delete head;
while(temp!=NULL)
{
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

27

temp1=temp->next;
delete temp;
temp=temp1;
}
}
void sll::create()
{
node*temp,*New;
int val,flag;
char ans;
flag=TRUE;
do
{
cout<<"\nenter the data";
cin>>val;
New=new node;
if(New==NULL)
cout<<"unable to allocate memory\n";
New->data=val;
New->next=NULL;
if(flag==TRUE)
{
head=New;
temp=head;
flag=FALSE;
}
else
{
temp->next=New;
temp=New;
}
cout<<"\ndo you want to enter more elements?(Y,n)";
ans=getch();
}while(ans=='y'||ans=='Y');
cout<<"\nthe singly linked list is created\n";
getch();
}
void sll::display()
{
node*temp;
temp=head;
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

28

if(temp==NULL)
{
cout<<"\nthe list is empty\n";
getch();
clrscr();
return;
}
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
getch();
}
void sll::search(int key)
{
node*temp;
int found;
temp=head;
if(temp==NULL)
{
cout<<"linked list is empty\n";
getch();
clrscr();
}
found=FALSE;
while(temp!=NULL&&found==FALSE)
{
if(temp->data!=key)
temp=temp->next;
else
found=TRUE;
}
if(found==TRUE)
{
cout<<"\n the element is present in the list\n";
getch();
}
}
void sll::dele()
{
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

29

node*temp,*prev;
int key;
temp=head;
cout<<"\nenter the data of the node you want to delete:";
cin>>key;
while(temp!=NULL)
{
if(temp->data==key)
break;
prev=temp;
temp=temp->next;
}
if(temp==NULL)
cout<<"\nnode not found";
else
{
if(temp==head)
head=temp->next;
else
prev->next=temp->next;
delete temp;
cout<<"\nthe element is deleted\n";
}
getch();
}
void sll::insert_last()
{
node*New,*temp;
cout<<"\nenter the element which you want to insert";
cin>>New->data;
if(head==NULL)
head=New;
else
{
temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=New;
New->next=NULL;
}
}
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

30

void sll::insert_after()
{
int key;
node*temp,*New;
New=new node;
cout<<"\nenter the element whice you want to insert";
cin>>New->data;
if(head==NULL)
{
head=New;
}
else
{
cout<<"\nenter the element after whice you want to insert the node";
cin>>key;
temp=head;
do
{
if(temp->data==key)
{
New->next=temp->next;
temp->next=New;
break;
}
else
temp=temp->next;
}
while(temp!=NULL);
}
}
void sll::insert_head()
{
node*New,*temp;
New=new node;
cout<<"\nenter the element which you want to insert";
cin>>New->data;
if(head==NULL)
head=New;
else
{
temp=head;
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

31

New->next=temp;
head=New;
}
}
void main()
{
sll s;
int choice,val,ch1;
clrscr();
cout<<"\nprogram to perform various operations on linked list";
cout<<"\n1.create";
cout<<"\n2.display";
cout<<"\n3.search";
cout<<"\n4.insert an element in a list";
cout<<"\n5.delete an element from list";
cout<<"\n6.quit";
do
{
cout<<"\nenter your choice(1-6)";
cin>>choice;
switch(choice)
{
case 1:
s.create();
break;
case 2:
s.display();
break;
case 3:
cout<<"\nenter the element you want to search";
cin>>val;
s.search(val);
break;
case 4:
cout<<"\nthe list is:\n";
s.display();
cout<<"\nmenu";
cout<<"\n1.insert at beginning\n2.insert after";
cout<<"\n3.insert at end";
cout<<"\nenter your choice";
cin>>ch1;
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

32

switch(ch1)
{
case 1:
s.insert_head();
break;
case 2:
s.insert_after();
break;
case 3:
s.insert_last();
break;
default:
cout<<"\ninvalid choice";
}
s.display();
break;
case 5:
s.dele();
s.display();
break;
case 6:
exit(0);
default:
cout<<"\n invalid choice";
}
}while(1);
getch();
}

Output:
program to perform various operations on linked list
1.create
2.display
3.search
4.insert an element in a list
5.delete an element from list
6.quit
enter your choice(1-6)1
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

33

enter the data2


do you want to enter more elements?(Y,n)
enter the data3
do you want to enter more elements?(Y,n)
enter the data4
do you want to enter more elements?(Y,n)
the singly linked list is created
enter your choice(1-6)2
234
enter your choice(1-6)3
enter the element you want to search2
the element is present in the list
enter your choice(1-6)3
enter the element you want to search1
enter your choice(1-6)
4
the list is:
234
menu
1.insert at beginning
2.insert after
3.insert at end
enter your choice1
enter the element which you want to insert1
1234
enter your choice(1-6)4
the list is:
1234
menu
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

34

1.insert at beginning
2.insert after
3.insert at end
enter your choice2
enter the element whice you want to insert5
enter the element after whice you want to insert the node3
12354
enter your choice(1-6)4
the list is:
12354
menu
1.insert at beginning
2.insert after
3.insert at end
enter your choice3
enter the element which you want to insert6
123546
enter your choice(1-6)5
enter the data of the node you want to delete:2
the element is deleted
13546
enter your choice(1-6)6

Result:
Thus the given program Linked List implementation of the list ADT was
executed successfully.

EC6312 OOPS and Data Structures Lab


Ms.M.Abinaya, Lect/IT

35

CURSOR IMPLEMENTATION LIST ADT


Aim:
To write a C++ program for cursor implementation of list ADT.

Algorithm:
1. Start the program.
2. Create a node with two fields data and link field.
o Allocate space for the node dynamically.
o Create link between the created nodes and let the last node be with
NULL Link
o Insert the input data in the data field and press 1 to stop the
same.
3. Get the choice of operations either insertion or deletion.
4. For insertion get the position in which insertion is to be done and the
element to be inserted. Check for the start, middle or end position of
insertion. Insert the node and change its link accordingly.
5. For deletion get the position in which deletion is to be done. Delete the
node and then link it to the next node. Before deletion check whether
there is data in the list to be deleted.
6. Using display option list the elements of the list.
7. Stop the program.

Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 20
class LIST
{
private:
int list[MAX];
public:
int create();
void display(int);
void reverse(int);
int search(int);
void delet(int);
};
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

36

int LIST::create()
{
int n,i;
clrscr();
cout<<"\nhow many elements you want in the list:";
cin>>n;
if(n>MAX)
cout<<"\nerror:Number of elements exceeds the limit";
for(i=0;i<n;i++)
{
cout<<"\nenter the element number"<<i+1<<":";
cin>>list[i];
}
cout<<"\nthe List is successfully\n";
getch();
return(n);
}
void LIST::display(int n)
{
int i;
clrscr();
cout<<"\nthe List is...\n";
for(i=0;i<n;i++)
cout<<"\n"<<list[i];
cout<<"\npress any key to continue...\n";
getch();
}
void LIST::reverse(int n)
{
int i;
clrscr();
cout<<"\nthe reversed list is...\n\n";
for(i=n-1;i>=0;i--)
cout<<"\n"<<list[i];
cout<<"\npress any key to continue..\n";
getch();
}
int LIST::search(int n)
{
int i,key;
clrscr();
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

37

cout<<"\nentre thenumber you want to search?";


cin>>key;
for(i=0;i<n;i++)
{
if(list[i]==key)
{
cout<<"\nthe given numbre is at position:"<<i<<"\n";
getch();
return i;
}
}
cout<<"\nthe given number is not in the list\n";
getch();
return -1;
}
void LIST::delet(int n)
{
int i;
i=search(n);
list[i]=-1;
cout<<"\nthe elements is now deleted";
cout<<"\n we put -1 to indicate empty location";
getch();
}
void main()
{
LIST obj;
int choice,len,position;
char ans;
do
{
clrscr();
cout<<"\n\tprogram to perform operations on ordered list";
cout<<"\n1.create";
cout<<"\n2.display";
cout<<"\n3.search for a number";
cout<<"\n4.reverse";
cout<<"\n5.delete";
cout<<"\n6.quit";
cout<<"\nenter your choice(1-6)";
cin>>choice;
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

38

switch(choice)
{
case 1:
len=obj.create();
break;
case 2:
obj.display(len);
break;
case 3:
position=obj.search(len);
break;
case 4:
obj.reverse(len);
break;
case 5:
obj.delet(len);
break;
case 6:
cout<<"\ndo you want to exit(y/n)?";
ans=getch();
if(ans=='y'||ans=='y')
exit(0);
else
break;
default:
clrscr();
cout<<"\n invalid choice,tryagain";
getch();
}
}
while(choice!=6);
}

Output:
program to perform operations on ordered list
1.create
2.display
3.search for a number
4.reverse
5.delete
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

39

6.quit
enter your choice(1-6)1
how many elements you want in the list:4
enter the element number1:1
enter the element number2:2
enter the element number3:3
enter the element number4:4
the List is successfully
program to perform operations on ordered list
1.create
2.display
3.search for a number
4.reverse
5.delete
6.quit
enter your choice(1-6)2
the List is...
1
2
3
4
press any key to continue...
program to perform operations on ordered list
1.create
2.display
3.search for a number
4.reverse
5.delete
6.quit
enter your choice(1-6)3
enter the number you want to search?2
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

40

the given number is at position:1


program to perform operations on ordered list
1.create
2.display
3.search for a number
4.reverse
5.delete
6.quit
enter your choice(1-6)4
the reversed list is...
4
3
2
1
press any key to continue..
program to perform operations on ordered list
1.create
2.display
3.search for a number
4.reverse
5.delete
6.quit
enter your choice(1-6)5
enter thenumber you want to search?3
the given numbre is at position:2
the elements is now deleted
we put -1 to indicate empty location
the List is...
1
2
-1
4
press any key to continue...
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

41

Result:
Thus the Given Program Cursor implementation of list was executed
successfully.
STACK ADT USING ARRAY IMPLEMENTATION
Aim:
To write a program for stack using array implementation.

Algorithm:
1. Define a array which stores stack elements..
2. The operations on the stack are
a. PUSH data into the stack
b. POP data out of stack
3. PUSH DATA INTO STACK
a. Enter the data to be inserted into stack.
b. If TOP is NULL
a. the input data is the first node in stack.
b. the link of the node is NULL.
c. TOP points to that node.
c. If TOP is NOT NULL
a. the link of TOP points to the new node.
b. TOP points to that node.
4. POP DATA FROM STACK
a. If TOP is NULL the stack is empty
b. If TOP is NOT NULL
i. the link of TOP is the current TOP.
ii. the pervious TOP is popped from stack.
5. The stack represented by linked list is traversed to display its content.

Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class stack
{
int stk[5];
int top;
public:
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

42

stack()
{
top=-1;
}
void push(int x)
{
if(top>4)
{
cout<<"stack over flow";
return;
}
stk[++top]=x;
cout<<"inserted"<<x;
}
void pop()
{
if(top<0)
{
cout<<"stack under flow";
return;
}
cout<<"deleted"<<stk[top--];
}
void display()
{
if(top<0)
{
cout<<"stack empty";
return;
}
for(int i=top;i>=0;i--)
cout<<stk[i]<<" ";
}
};
int main()
{
int ch;
stack st;
clrscr();
cout<<"\n1.push\n2.pop\n3.display\n4.exit";
while(1)
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

43

{
cout<<"\nenter a choice";
cin>>ch;
switch(ch)
{
case 1:
cout<<"entre the element";
cin>>ch;
st.push(ch);
break;
case 2:
st.pop();
break;
case 3:
st.display();
break;
case 4:
exit(0);
}
}
return 0;
}

Output:
1.push
2.pop
3.display
4.exit
enter a choice1
entre the element3
inserted3
enter a choice1
entre the element2
inserted2
enter a choice1
entre the element4
inserted4
enter a choice1
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

44

entre the element5


inserted5
enter a choice3
5423
enter a choice2
deleted5
enter a choice3
423
enter a choice 4

Result:
Thus the program for implementation of stack ADT using array has been
executed and verified successfully.
STACK ADT USING LINKED LIST IMPLEMENTATION
Aim:
To write a program for stack ADT using linked list implementation.

Algorithm:
1. Define a struct for each node in the stack.
a. Each node in the stack contains data and link to the next node.
TOP pointer points to last node inserted in the stack.
2. The operations on the stack are
a. PUSH data into the stack
b. POP data out of stack
3. PUSH DATA INTO STACK
a. Enter the data to be inserted into stack.
b. If TOP is NULL
i. the input data is the first node in stack.
ii. the link of the node is NULL.
iii. TOP points to that node.
c. If TOP is NOT NULL
i. the link of TOP points to the new node.
ii. TOP points to that node.
4. POP DATA FROM STACK
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

45

a. If TOP is NULL the stack is empty


b. If TOP is NOT NULL
i. the link of TOP is the current TOP.
ii. the pervious TOP is popped from stack.
5. The stack represented by linked list is traversed to display its content.

Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class Linked_list_Stack
{
private:
struct node
{
int data;
node *next;
};
node *top;
node *entry;
node *print;
node *bottom;
node *last_entry;
node *second_last_entry;
public:
Linked_list_Stack( );
void pop( );
void push( );
void print_list( );
void show_working( );
};
Linked_list_Stack::Linked_list_Stack ( )
{
top=NULL;
bottom=NULL;
}
void Linked_list_Stack::push( )
{
int num;
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

46

cout<<"\n\t Enter value to push onto Stack : ";


cin>>num;
entry=new node;
if(bottom==NULL)
{
entry->data=num;
entry->next=NULL;
bottom=entry;
top=entry;
}
else
{
entry->data=num;
entry->next=NULL;
top->next=entry;
top=entry;
}
cout<<"\n\t *** "<<num<<" is pushed onto the Stack."<<endl;
}
void Linked_list_Stack::pop( )
{
if(bottom==NULL)
cout<<"\n\t *** Error : Stack is empty. \n"<<endl;
else
{
for(last_entry=bottom;last_entry->next!=NULL;
last_entry=last_entry->next)
second_last_entry=last_entry;
if(top==bottom)
bottom=NULL;
int poped_element=top->data;
delete top;
top=second_last_entry;
top->next=NULL;
cout<<"\n\t *** "<<poped_element<<" is poped from the Stack."<<endl;
}
}
void Linked_list_Stack::print_list( )
{
print=bottom;
if(print!=NULL)
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

47

cout<<"\n\t Values pushed onto Stack are : \n"<<endl;


else
cout<<"\n\t *** Nothing to show. "<<endl;
while(print!=NULL)
{
cout<<"\t "<<print->data<<endl;
print=print->next;
}
}
void Linked_list_Stack::show_working( )
{
int choice;
clrscr( );
cout<<"\n\n********** Implementation of Linked List as a Stack
**********"<<endl;
cout<<"\n1.Push elements to stack"<<endl;
cout<<"2.Pop elements to stack"<<endl;
cout<<"3.Print the elements of stack"<<endl;
cout<<"4.Exit"<<endl;
do
{
cout<<"\nEnter your Choice : ";
cin>>choice;
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: print_list( );
break;
case 4: exit(0);
break;
default:
cout<<"enter the valid choice";
}
}while(1);
}
int main( )
{
Linked_list_Stack obj;
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

48

obj.show_working( );
return 0;
}

Output:
********** Implementation of Linked List as a Stack**********
1.Push elements to stack
2.Pop elements to stack
3.Print the elements of stack
4.Exit
Enter your Choice : 1
Enter value to push onto Stack : 2
*** 2 is pushed onto the Stack.
Enter your Choice : 1
Enter value to push onto Stack : 3
*** 3 is pushed onto the Stack.
Enter your Choice : 1
Enter value to push onto Stack : 4
*** 4 is pushed onto the Stack.
Enter your Choice : 3
Values pushed onto Stack are :
2
3
4
Enter your Choice : 2
*** 4 is poped from the Stack.
Enter your Choice : 3
Values pushed onto Stack are :
2
3
Enter your Choice : 4
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

49

Result:
Thus the Given Program for stack ADT using linked list implementation
was executed successfully.

SOURCE FILES FOR STACK APPLICATION1 ARRAY


IMPLEMENTATIN
Aim:
To write a C++ program for the application of stack for checking well
formed parenthesis of the expression using array implementation.

Algorithm:
1.
2.
3.
4.
5.
6.
7.

Crate a header file named stack.h .


In this file declare the class and all the stack operations.
In push operation increment the top value and get the value
In pop operation delete the value and decrement the top.
In full and empty operation check the stack whether it have value or not.
Now create a stack application program.
Chosen an application as checking well formed of parenthesis for this
application use stack operations.
8. These operations are used from the external file stack.h.
9. Hence will include this file in the include file section.

Program:
stack.h
#define size 10
class stk_class
{
private:
struct stack{
char s[size];
int top;
}st;
public:
stk_class();
void push(char item);
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

50

int stempty();
char pop();
int stfull();
};
stk_class::stk_class()
{
st.top=-1;
}
void stk_class::push(char item)
{
st.top++;
st.s[st.top]=item;
}
int stk_class::stempty()
{
if(st.top==-1)
return 1;
else
return 0;
}
int stk_class::stfull()
{
if(st.top==size)
return 1;
else
return 0;
}
char stk_class::pop()
{
char item;
item=st.s[st.top];
st.top--;
return(item);
}
program.cpp
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include "d:\stack.h"
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

51

#define size 10
void main(void)
{
char item;
char ans,bracket[10];
stk_class obj;
int i;
clrscr();
cout<<"\n\t\t program for stack application using separate header file";
cout<<"\n enter the expression and put $at end ";
cin>>bracket;
i=0;
if(bracket[i]==')')
cout<<"\n the expression is invalid";
else
{
do
{
while(bracket[i]=='(')
{
obj.push(bracket[i]);
i++;
}
while(bracket[i]==')')
{
item=obj.pop();
i++;
}
}
while(bracket[i]!='$');
if(!obj.stempty())
cout<<"\n the expression is invalid";
else
cout<<"\n the expression has well formed parenthesis";
}
getch();
}
Output:
program for stack application using separate header file
enter the expression and put $at end (())()$
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

52

the expression has well formed parenthesis


program for stack application using separate header file
enter the expression and put $at end ((())$
the expression is invalid
Result:
Thus the program for application of stack checking well formed of
parenthesis implemented using arrays was executed successfully.

SOURCE FILES FOR STACK APPLICATION1 LINKED LIST


IMPLEMENTATIN
Aim:
To write a C++ program for the application of stack for checking well
formed parenthesis of the expression using linked list implementation.

Algorithm:
1.
2.
3.
4.
5.
6.
7.

Crate a header file named stack.h .


In this file declare the class and all the stack operations.
In push operation increment the top value and get the value
In pop operation delete the value and decrement the top.
In full and empty operation check the stack whether it have value or not.
Now create a stack application program.
Chosen an application as checking well formed of parenthesis for this
application use stack operations.
8. These operations are used from the external file stack.h.
9. Hence will include this file in the include file section.

Program:
stack.h
class stk_class
{
private:
typedef struct stack
{
char data;
struct stack * next;
}node;
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

53

node *top;
public:
stk_class();
void push(char item);
int sempty();
void pop();
};
stk_class ::stk_class()
{
top=NULL;
}
void stk_class::push(char item)
{
node *New;
New=new node;
if(New==NULL)
cout<<"\n memory cannot be allocated \n";
else
{
New->data=item;
New->next=top;
top=New;
}
}
int stk_class::sempty()
{
if(top==NULL)
return 1;
else
return 0;
}
void stk_class::pop()
{
node *temp;
temp=top;
top=top->next;
delete temp;
}

EC6312 OOPS and Data Structures Lab


Ms.M.Abinaya, Lect/IT

54

program.cpp
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#include"d:\stack1.h"
void main(void)
{
char ans,bracket[10];
char data,item;
stk_class obj;
int choice;
int i;
clrscr();
cout<<"\n\t\t enter the expression and put $ at the end ";
cin>>bracket;
i=0;
if(bracket[i]==')')
cout<<"\n the expression is invalid";
else
{
do
{
if(bracket[i]=='(')
{
obj.push(bracket[i]);
}
else if(bracket[i]==')')
{
if(obj.sempty())
{
cout<<"\n the expression is invalid";
getch();
exit(0);
}
obj.pop();
}
i++;
}
while(bracket[i]!='$');
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

55

if(obj.sempty())
cout<<"\n the expression has well formed parenthesis";
else
cout<<"\n the expression is invalid";
}
getch();
}
Output:
enter the expression and put $ at the end ((()))()$
the expression has well formed parenthesis
enter the expression and put $ at the end ((())$
the expression is invalid

Result:
Thus the program for application of stack checking well formed of
parenthesis implemented using linked list was executed successfully.
SOURCE FILES FOR STACK APPLICATION2 ARRAY
IMPLEMENTATIN
Aim:
To write a C++ program for the application of stack for evaluation of
postfix expression using array implementation.

Algorithm:
1.
2.
3.
4.
5.
6.
7.

Crate a header file named stack.h .


In this file declare the class and all the stack operations.
In push operation increment the top value and get the value
In pop operation delete the value and decrement the top.
In full and empty operation check the stack whether it have value or not.
Now create a stack application program.
Chosen an application as evaluating the postfix expression. for this
application use stack operations.
8. Check the expression. If it is operand then push the value or the operator
pop the value.
9. Then do the corresponding operation like +, -, *, /.
10. These operations are used from the external file stack.h.
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

56

11.Hence will include this file in the include file section.


Program
Stack2.h

#define MAX 10
class stk_class
{
struct stack
{
double s[MAX];
int top;
}st;
public:
stk_class();
void push(double val);
double pop();
};
stk_class::stk_class()
{
st.top=0;
}
void stk_class::push(double val)
{
if(st.top+1>=MAX)
cout<<"\n stack is full";
st.top++;
st.s[st.top]=val;
}
double stk_class::pop()
{
double val;
if(st.top==-1)
cout<<"\n stack is empty\n";
st.top--;
return(val);
}
program.cpp
#include<iostream.h>
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

57

#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include "d:\stack2.h"
#define size 80
void main()
{
char exp[size];
int len;
double result;
double post(char exp[]);
clrscr();
cout<<"enter the postfix expression\n";
cin>>exp;
len=strlen(exp);
exp[len]='$';
result=post(exp);
cout<<"the value of the expression is"<<result;
getch();
exit(0);
}
double post(char exp[])
{
stk_class obj;
char ch,*type;
double result ,val,op1,op2;
int i;
i=0;
ch=exp[i];
while(ch!='$')
{
if(ch>='0'&&ch<='9')
type="operand";
else if(ch=='+'||ch=='-'||ch=='^'||ch=='*'||ch=='/')
type="operator";
if(strcmp(type,"operand")==0)
{
val=ch-48;
obj.push(val);
}
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

58

else
if(strcmp(type,"operator")==0)
{
op2=obj.pop();
op1=obj.pop();
switch(ch)
{
case '+':result=op1+op2;
break;
case '-':result=op1-op2;
break;
case '*':result=op1*op2;
break;
case '/': result=op1/op2;
break;
case '^':result=pow(op1,op2);
break;
}
obj.push(result);
}
i++;
ch=exp[i];
}
result=obj.pop();
return(result);
}

Output:
enter the postfix expression
19*3+5/
the value of the expression is 2

Result:
Thus the program for application of stack evaluating postfix expression
using array implementation has been executed and verified successfully.

EC6312 OOPS and Data Structures Lab


Ms.M.Abinaya, Lect/IT

59

SOURCE FILES FOR STACK APPLICATION2 LINKED


LIST IMPLEMENTATIN
Aim:
To write a C++ program for the application of stack for evaluation of
postfix expression using array implementation.

Algorithm:
1.
2.
3.
4.
5.
6.
7.

Crate a header file named stack.h .


In this file declare the class and all the stack operations.
In push operation increment the top value and get the value
In pop operation delete the value and decrement the top.
In full and empty operation check the stack whether it have value or not.
Now create a stack application program.
Chosen an application as evaluating the postfix expression. for this
application use stack operations.
8. Check the expression. If it is operand then push the value or the operator
pop the value.
9. Then do the corresponding operation like +, -, *, /.
10. These operations are used from the external file stack.h.
11.Hence will include this file in the include file section.

Program
stack3.h
class stk_class
{
typedef struct stack
{
char data;
struct stack *next;
}node;
public:
node *top;
stk_class();
void push(char item);
char pop();
};
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

60

stk_class::stk_class()
{
top=NULL;
}
void stk_class::push(char item)
{
node *New;
New=new node;
New->data =item;
New->next=top;
top=New;
}
char stk_class::pop()
{
char item;
node *temp;
item=top->data;
temp=top;
top=top->next;
delete temp;
return item;
}

program.cpp
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>
#include"d:\stack3.h"
#define size 80
void main()
{
char exp[size];
int len;
double result;
double post(char exp[]);
clrscr();
cout<<"enter the postfix expression\n";
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

61

cin>>exp;
len=strlen(exp);
exp[len]='$';
result=post(exp);
cout<<"the value of the expression is"<<result;
getch();
exit(0);
}
double post(char exp[])
{
char ch,*type;
double result ,val,op1,op2;
int i;
stk_class obj;
i=0;
ch=exp[i];
while(ch!='$')
{
if(ch>='0'&&ch<='9')
type="operand";
else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='^')
type="operator";
if(strcmp(type,"operand")==0) /* if the character is operand*/
{
val=ch-48;
obj.push(val);
}
else
if(strcmp(type,"operator")==0)/*if it is operator*/
{
op2=obj.pop();
op1=obj.pop();
switch(ch)
{
case '+':result=op1+op2;
break;
case '-':result=op1-op2;
break;
case '*':result=op1*op2;
break;
case '/': result=op1/op2;
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

62

break;
case '^':result=pow(op1,op2);
break;
}
obj.push(result);
}
i++;
ch=exp[i];
}
result=obj.pop();/*pop the result*/
return(result);
}
Output:
enter the postfix expression
19*3+5/
the value of the expression is 2

Result:
Thus the program for application of stack evaluating postfix expression
using linked list implementation has been executed and verified successfully.
QUEUE ADT USING ARRAY IMPLEMENTATION
Aim:
To write a program for Queue using array implementation.

Algorithm:
1. Define a array which stores queue elements.
2. The operations on the queue are
a. INSERT data into the queue
b. DELETE data out of queue
3. INSERT DATA INTO queue
a. Enter the data to be inserted into queue.
b. If TOP is NULL
i. the input data is the first node in queue.
ii. the link of the node is NULL.
iii. OP points to that node.
c. If TOP is NOT NULL
i. the link of TOP points to the new node.
ii. TOP points to that node.
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

63

4. DELETE DATA FROM queue


a. If TOP is NULL, the queue is empty
b. If TOP is NOT NULL
i. the link of TOP is the current TOP.
ii. the pervious TOP is popped from queue.
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;
};
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;
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

64

if(rear>=4)
{
cout<<"queue over flow";
return;
}
rear++;
while(temp->next!=NULL)
temp=temp->next;
temp1=new node;
temp->next=temp1;
temp1->next=NULL;
temp1->data=x;
}
}
void display()
{
node *temp;
temp=head;
if(rear<0)
{
cout<<" queue under flow";
return;
}
while(temp!=NULL)
{
cout<<temp->data<< " ";
temp=temp->next;
}
}
void dequeue()
{
node *temp;
temp=head;
if(rear<0)
{
cout<<"queue under flow";
return;
}
if(front==rear)
{
front=rear=-1;
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

65

head=NULL;
return;
}
front++;
head=head->next;
}
};
main()
{
queue s1;
int ch;
clrscr();
cout<<"\n\n\tQUEUE USING LINKED LIST";
cout <<"\n1.enqueue\n2.dequeue\n3.DISPLAY\n4.EXIT";
while(1)
{
cout<<"\n enter your choice:";
cin >> ch;
switch(ch)
{
case 1:
cout <<"\n enter a element";
cin >> ch;
s1.enqueue(ch);
break;
case 2: s1.dequeue();
break;
case 3: s1.display();
break;
case 4: exit(0);
}
}
return 0;
}

EC6312 OOPS and Data Structures Lab


Ms.M.Abinaya, Lect/IT

66

Output:
QUEUE USING LINKED LIST
1.enqueue
2.dequeue
3.DISPLAY
4.EXIT
enter your choice:1
enter a element2
enter your choice:1
enter a element3
enter your choice:1
enter a element4
enter your choice:3
234
enter your choice:2
enter your choice:3
34
enter your choice:4

Result:
Thus the Program for Queue using array implementation has been
executed and verified successfully.
QUEUE ADT OPERATIONS USING LINKED LIST
Aim:
To write a program for Queue using Linked implementation.

Algorithm:
1. Define a struct for each node in the queue.
2. Each node in the queue contains data and link to the next node.
3. Front and rear pointer points to first and last node inserted in the queue.
4. The operations on the queue are
a. INSERT data into the queue
b. DELETE data out of queue
5. INSERT DATA INTO queue
a. Enter the data to be inserted into queue.
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

67

b. 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.
c. If TOP is NOT NULL
i. the link of TOP points to the new node.
ii. TOP points to that node.
6. DELETE DATA FROM queue
a. If TOP is NULL
i. the queue is empty
b. If TOP is NOT NULL
i. the link of TOP is the current TOP.
ii. the pervious TOP is popped from queue.
6. The queue represented by linked list is traversed to display its content.
Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class queue
{
int queue1[5];
int rear,front;
public:
queue()
{
rear=-1;
front=-1;
}
void insert(int x)
{
if(rear>4)
{
cout<<"queue over flow";
front=rear=-1;
return;
}
queue1[++rear]=x;
cout<<"\ninserted "<<x;
}
void delet()
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

68

{
if(front==rear)
{
cout<<"queue under flow";
return;
}
cout<<"\ndeleted "<<queue1[++front];
}
void display()
{
if(rear==front)
{
cout<<" queue empty";
return;
}
for(int i=front+1;i<=rear;i++)
cout<<queue1[i]<<" ";
}
};
main()
{
int ch;
clrscr();
queue qu;
cout<<"\n1.insert \n2.delete \n3.display \n4.exit";
while(1)
{
cout<<"\nEnter ur choice";
cin>>ch;
switch(ch)
{
case 1:cout<<"\nenter the element";
cin>>ch;
qu.insert(ch);
break;
case 2:qu.delet();
break;
case 3:qu.display();
break;
case 4:exit(0);
}
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

69

}
return(0);
}
Output:
1.insert
2.delete
3.display
4.exit
Enter ur choice1
enter the element5
inserted 5
Enter ur choice1
enter the element6
inserted 6
Enter ur choice1
enter the element9
inserted 9
Enter ur choice3
569
Enter ur choice2
deleted 5
Enter ur choice3
69
Enter ur choice4

Result :
Thus the program for Queue ADT operations using Linked List has
been executed and verified successfully.

EC6312 OOPS and Data Structures Lab


Ms.M.Abinaya, Lect/IT

70

BINARY SEARCH TREE

Aim:
To write a program for binary search tree.

Algorithm:
1. Declare function create(),search(),delete(),Display().
2. Create a structure for a tree contains left pointer and right pointer.
3. Insert an element is by checking the top node and the leaf node and the
operation will be performed.
4. Deleting an element contains searching the tree and deleting the item.
5. display the Tree elements.

Program:
#include<iostream.h>
#include<conio.h>
class bintree
{
typedef struct bst
{
int data;
struct bst *left,*right;
}
node;
node *root,*New,*temp,*parent;
public:
bintree()
{
root=NULL;
}
void create();
void display();
void delet();
void find();
void insert(node *,node*);
void inorder(node *);
void search(node**,int,node **);
void del(node *,int);
};
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

71

void bintree::create()
{
New=new node;
New->left=NULL;
New->right=NULL;
cout<<"\n enter the element";
cin>>New->data;
if(root==NULL)
root=New;
else
insert(root,New);
}
void bintree::insert(node *root,node *New)
{
if(New->data<root->data)
{
if(root->left==NULL)
root->left=New;
else
insert(root->left,New);
}
if(New->data>root->data)
{
if(root->right==NULL)root->right=New;
else
insert(root->right,New);
}
}
void bintree::display()
{
if(root==NULL)
cout<<"tree is not created";
else
{
cout<<"\n the tree is: ";
inorder(root);
}
}
void bintree::inorder(node *temp)
{
if(temp!=NULL)
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

72

{
inorder(temp->left);
cout<<" "<<temp->data;
inorder(temp->right);
}
}
void bintree::find()
{
int key;
cout <<"\n enter the element which you want to search";
cin>>key;
temp=root;
search(&temp,key,&parent);
if(temp==NULL)
cout<<"\n element is not present";
else
cout<<"\n parent of node "<<temp->data<<" is"<<parent->data;
}
void bintree::search(node **temp,int key,node ** parent)
{
if(*temp==NULL)
cout<<endl<<" tree is not created"<<endl;
else
{
while(*temp!=NULL)
{
if((*temp)->data==key)
{
cout<<"\n the "<<(*temp)->data<<" element is present";
break;
}
*parent=*temp;//stores the parent value
if((*temp)->data>key)
*temp=(*temp)->left;
else
*temp=(*temp)->right;
}
}
return;
}
void bintree::delet()
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

73

{
int key;
cout<<"\n enter the element you want to delete";
cin>>key;
if(key==root->data)
{
bintree();// assigning a value NULL to root
}
else
del(root,key);}
void bintree::del(node *root,int key)
{
node *temp_succ;
if(root==NULL)
cout<<" tree is not created";
else
{
temp=root;
search(&temp,key,&parent);
if(temp->left!=NULL&&temp->right!=NULL)
{
parent=temp;
temp_succ=temp_succ->left;
while(temp_succ->left!=NULL)
{
parent=temp_succ;
temp_succ=temp_succ->left;
}
temp->data=temp_succ->data;
temp->right=NULL;
cout<<" now deleted it!";
return;
}
if(temp->left!=NULL&&temp->right==NULL)
{
if(parent->left==temp)
parent->left=temp->left;
else
parent->right=temp->left;
temp=NULL;
delete temp;
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

74

cout<<" now deleted it!";


return;
}
if(temp->left==NULL&&temp->right!=NULL)
{
if(parent->left==temp)
parent->left=temp->right;
else
parent->right=temp->right;
temp=NULL;
delete temp;
cout<<" now deleted it!";
return;
}
if(temp->left==NULL&&temp->right==NULL)
{
if(parent->left==temp)
parent->left=NULL;
else
parent->right=NULL;
cout<<" now deleted it!";
return;
}
}
}
void main()
{
int choice;
char ans='N';
bintree tr;
clrscr();
cout<<"\n\t program for binary search tree";
cout<<"\n1.create\n2.search\n3.delete\n4.display";
do
{
cout<<"\n\n enter your choice:";
cin>>choice;
switch(choice)
{
case 1:do
{
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

75

tr.create();
cout<<"do you want to enter more elements: (y/n)"<<endl;
ans=getche();
}
while(ans=='y');
break;
case 2:tr.find();
break;
case 3:
tr.delet();
break;
case 4:
tr.display();
break;
}}
while(choice!=5);
}

Output:
program for binary search tree
1.create
2.search
3.delete
4.display
enter your choice 1
enter the element 10
do you want to enter more elements(y/n)
y
enter the element 8
do you want to enter more elements(y/n)
y
enter the element 7
do you want to enter more elements(y/n)
y
enter the element 9
do you want to enter more elements(y/n)
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

76

y
enter the element 12
do you want to enter more elements(y/n)
y
enter the element 11
do you want to enter more elements(y/n)
y
enter the element 13
do you want to enter more elements(y/n)
n
1.create
2.search
3.delete
4.display
enter your choice 4
the tree is: 7 8 9 10 11 12 13
1.create
2.search
3.delete
4.display
enter your choice 2
enter the element which you want to search 13
the 13 element is present
parent of node 13 is 12
1.create
2.search
3.delete
4.display
enter your choice 3
enter the element you wish to delete 12
the 12 element is present now deleted it!
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

77

1.create
2.search
3.delete
4.display
enter your choice 4
the tree is : 7 8 9 10 11 13
1.create
2.search
3.delete
4.display
enter your choice 3
enter the element you wish to delete 11
the 11 element is present now deleted it!
1.create
2.search
3.delete
4.display
enter your choice 4
the tree is : 7 8 9 10 13
1.create
2.search
3.delete
4.display
enter your choice 3
enter the element you wish to delete 7
the 7 element is present now deleted it!
1.create
2.search
3.delete
4.display
enter your choice 4
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

78

the tree is : 8 9 10 13
1.create
2.search
3.delete
4.display
enter your choice 5

Result:
Thus the Program for Binary Search Tree has been executed and verified
successfully.

HEAP SORT

Aim:
To write a c program to perform heap sort.

Algorithm:
1.
2.
3.
4.
5.

Get the size of the array from the user.


Get the elements to be sorted.
Sorting is performed when we call the heap sort function.
Now the array is contained with sorted elements.
Display the sorted elements.

Program:
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define MAX 10
class heap
{
private:
int arr[MAX];
int n;
public:
Heap();
void insert(int num);
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

79

void makespace();
void heapsort();
void display();
};
heap::heap()
{
n=0;
for(int i=0;i<MAX;i++)
arr[i]=0;
}
void heap::insert(int num)
{
if(n<Max)
{
arr[n]=num;
n++;
}
else
cout<<\n array is full;
}
void heap::makeheap()
{
for(int i=1;i<n;i++)
{
int val =arr[i];
int j=I;
int f=(j-1)/2;
while(j>0&&arr[f]<val)//creating a MAX heap
{
arr[j]=arr[f];
j=f;
f=(j-1)/2;
}arr[j]=val;
}}
void heap::heapsprt()
{
for(int i=n-1;i>0;i--)
{
int temp=arr[i];
arr[i]=arr[0];
int k=0;
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

80

int j;
if(i==1)
j=-1;
else
j=1;
if(i>2&&arr[2]>arr[1])
j=2;
while(j>=0&&temp<arr[j])
{
arr[k]=arr[j];
k=j;
j=2*k+1;
if(j+1<=i-1&&arr[j]<arr[j+1])
j++;
if(j>i-1)
j=-1;
}
arr[k]=temp;
}
}
void heap::display()
{
for(int i=0;i<n;i++)
cout<<\n<<arr[i];
cout<<\n;
}
void main()
{
heap obj;
obj.insert(14);
obj.insert(12);
obj.insert(9);
obj.insert(8);
obj.insert(7);
obj.insert(10);
obj.insert(18);
cout<<\n the elements are ..<<endl;
obj.display();
odj.makeheap();
cout<<\n heapified<<endl;
obj.display();
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

81

obj.heapsort();
cout<<\n elements sorted by heap sort<<endl;
obj.display();
getch();
}
Output:
the elements are.
14
12
9
8
7
10
heapified
12
14
8
7
9
10
Elements sorted by heap sort
7
8
9
10
12
14

Result:
Thus the program for Heap Sort has been executed and verified
successfully.

EC6312 OOPS and Data Structures Lab


Ms.M.Abinaya, Lect/IT

82

QUICK SORT

Aim:
To write a program to perform quick sort.

Algorithm:
1.
2.
3.
4.
5.

Get the value of how many no. to be sorted.


Get the elements from the user.
Two function quicksort() and swap(). Quick sort to perform sorting.
Swap() is just to rearrange the values.
Quick sort algorithm works by partitioning the array to be sorted, then
recursively sorting each partition.
6. Display the sorted value.

Program:
# include <iostream.h>
# include <conio.h>
void swap(long &,long &);
void quick_sort(long [],int,int);
main( )
{
clrscr( );
const int array_size=10;
long array[array_size]={0};
cout<<"
\n\n***********************
Quick
******************"<<endl;
cout<<"\n * Array size = 10"<<endl;
cout<<" * Data Type = long"<<endl;
cout<<" Enter the array : "<<endl<<endl;
for(int count_1=0;count_1<array_size;count_1++)
{
cout<<"\t Element["<<count_1<<"] = ";
cin>>array[count_1];
}
quick_sort(array,0,array_size-1);
cout<<" \n\nSorted Array : \n\n";
for(int count_2=0;count_2<array_size;count_2++)
{
cout<<"\tElement["<<count_2<<"] = "<<array[count_2]<<endl;
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

Sort

83

}
cout<<"
\n\n\n*******************************************************
***";
getch( );
return 0;
}
void swap(long &element_1,long &element_2)
{
long temp=element_1;
element_1=element_2;
element_2=temp;
}
void quick_sort(long array[],int first,int last)
{
if(first>=last)
{
}
else
{
int middle=array[last];
int count_1=first-1;
int count_2=last;
while(count_1<count_2)
{
do
{
count_1++;
}
while(array[count_1]<middle);
do
{
count_2--;
}
while(count_2>=0 && array[count_2]>middle);
if(count_1<count_2)
swap(array[count_1],array[count_2]);
}
swap(array[count_1],array[last]);
quick_sort(array,first,count_1-1);
quick_sort(array,count_1+1,last);
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

84

}
}

Output:
*********************** Quick Sort ******************
* Array size = 10
* Data Type = long
Enter the array :
Element[0] = 6
Element[1] = 4
Element[2] = 2
Element[3] = 8
Element[4] = 4
Element[5] = 1
Element[6] = 3
Element[7] = 9
Element[8] = 0
Element[9] = 7
Sorted Array :
Element[0] = 0
Element[1] = 1
Element[2] = 2
Element[3] = 3
Element[4] = 4
Element[5] = 4
Element[6] = 6
Element[7] = 7
Element[8] = 8
Element[9] = 9
**********************************************************

Result:
Thus the program for quick sort has been executed and verified
successfully.
EC6312 OOPS and Data Structures Lab
Ms.M.Abinaya, Lect/IT

85

Вам также может понравиться