Anda di halaman 1dari 91

TABLE OF INDEX

Exp. Page
Date Experiment Title Mark Signature
No No
1(a) Simple Class Program
Program to add, subtract,
1(b) multiply and divide two
numbers
1(c) Swapping of two numbers
1(d) Static class member
1(e) Friend function
Addition of two complex
1(f)
numbers
1(g) Function Overloading
1(h) Multiple Inheritance
1(i) Hybrid Inheritance
Array Implementation of List
2
ADT
Linked list Implementation
3
of List ADT
4 Cursor implementation of
List ADT
Array Implementation of
5.a
Stack ADT
Linked list Implementation
5.b
of Stack ADT
Application of stack-
6.a
Balancing Paranthesis
6.b Evaluation of expressions
Array Implementation of
7
Queue ADT
Linked list Implementation
8
of Queue ADT
9 Binary Search Tree
10 Quick Sort
Ex.No:1(a) SIMPLE CLASS PROGRAM
Date:

AIM:
To write a C++ program to display the name and age of the person using Class.

ALGORITHM
Step1: Include the header files
Step2: Define a class person
Step3: Define two member functions under the class
Step4: Get the input by using getdata() member function
Step5: Display the content using display() member function
Step6: Terminate the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class person
{
char name[30];
int age;
public:
void getdata(void);
void display(void);
};
void person::getdata(void)
{
cout<<"Enter the name:";
cin>>name;
cout<<"Enter Age:";
cin>>age;
}
void person::display(void)
{
cout<<"\n Name ->"<<name;
cout<<"\n Age->"<<age;
}
void main()
{
person p;
clrscr();
p.getdata();
p.display();
getch();
}
RESULT:
Thus the C++ program was executed and the output was obtained successfully.

Ex.No:1.(b) TO ADD, SUB, MULTIPLYAND DIVIDE TWO NUMBERS


Date:

AIM:
To write a C++ Program for Adding, Subtracting, Multiplying and Dividing two numbers.
ALGORITHM :
1. Read the two numbers n1, n2
2. If the option is Add, call the member function add with the parameters n1 and n2 and
return the Addition of the two numbers.
3. If the option is Subtract, call the member function sub with the parameters n1 and n2 and
return the difference of the two numbers.
4. If the option is Multiply, call the member function mul with the parameters n1 and n2
and return the product of the two numbers.
5. If the option is Divide, call the member function div with the parameters n1 and n2 and
return the division of n1 by n2.
6. Display the Result.
7. If the option is Exit, quit the program.
PROGRAM:
// Program to maintain a calculator
#include <conio.h>
#include <iostream.h>
class calci
{
private :
float num1;
float num2;
float result;
public :
calci() ;
void getdata();
void add () ;
void sub () ;
void mul () ;
void div () ;
void display(char op);
};
// initializes data member
calci::calci()
{
num1 = 0;
num2 = 0;
}
void calci::getdata()
{
cout << "\n\nEnter the number 1 : ";
cin >> num1;
cout << "\n\nEnter the number 2 : ";
cin >> num2;
}
// adds two numbers
void calci::add ()
{
result = num1 + num2;
display('+');
}
// subtracts two numbers
void calci::sub ()
{
result = num1 - num2;
display('-');
}
// Multiplies two numbers
void calci::mul ()
{
result = num1 * num2;
display('*');
}
// divides two numbers
void calci::div ()
{
if(num2 == 0)
{
cout << "\n\nDivide by zero error !";
return;
}

result = num1 / num2;


display('/');
}
void calci::display (char op)
{
cout << "\n\n" << num1 << " " << op << " " << num2 << " = " << result;
}
void main( )
{
calci c ;
int ch;
do
{
clrscr();
cout << "\n\tCalculator";
cout << "\n\t----------";
cout << "\n\n1. Add";
cout << "\n\n2. Subtract";
cout << "\n\n3. Multiply";
cout << "\n\n4. Divide";
cout << "\n\n5. Exit\n";
cout << "\nEnter your choice : ";
cin >> ch;
switch(ch)
{
case 1:
c.getdata();
c.add();
break;
case 2:
c.getdata();
c.sub();
break;
case 3:
c.getdata();
c.mul();
break;
case 4:
c.getdata();
c.div();
break;
default:
break;
}
getch();
}while(ch<5);
}

RESULT:

Thus the C++ program for addition, subtraction, multiplication and division of two
numbers was compiled and executed successfully
Ex no:1.(c) SWAPPING OF TWO NUMBERS
Date:

AIM:
To write a C++ program for swapping of two numbers.

ALGORITHM:
1.Include the header files.
2.Declare a function named swap.
3.Read two numbers a and b.
4. Call that function in the main program
5. For Call by value, pass the value when we call that function
6.For Call by reference, assign the address to the pointer
7.Print the swapped values
8.Compile and run the program

PROGRAM
//call by value
#include <iostream.h>
#include<conio.h>
void swap (int x,int y);//function prototype
void swap(int x,int y)//function definition
{
int temp;
temp=x;
x=y;
y=temp;
cout<<"value of x: "<<x<<endl;
cout<<"value of y: "<< y<<endl;
return;
}
int main()
{
clrscr();
int a=100;
int b=200;
int x;
cout<<"Before swap,value of a:"<<a<<endl;
cout<<"Before swap,value of b:"<<b<<endl;
swap(a,b); //function call
cout<<"After swap,value of a:"<<a<<endl;
cout<<"After swap,value of b:"<<b<<endl;
getch();
return 0;
}
//Call by reference

#include<iostream.h>
#include<conio.h>
void swap(int&x,int&y); //function prototype
int main()
{
clrscr();
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); //function call-a,b:actual arguments
cout<<"After swap,value of a:"<<a<<endl;
cout<<"After swap,value of b:"<<b<<endl;
getch();
return 0;
}
void swap(int &x,int &y) //function definition
// x,y:formal parameters
{
int temp;
temp=x;
x=y;
y=temp;
return 0;
}

RESULT:
Thus the C++ program for swapping of two numbers was executed and the output was
obtained successfully.
Ex.No:1.(d ) STATIC CLASS MEMBER
Date:

AIM:
To write a C++ program to demonstrate static class member.

ALGORITHM:
Step 1: Start the program.
Step 2: Declare the static data members in the class.
Step 3: Declare the static member function.
Step 4: Create the object for the class containing static members.
Step 5: Pass the static data value when we call the static member function in the main
program.
Step 6: Compile and execute the program.
Step 7: Terminate the program

PROGRAM:
#include<iostream.h>
#include<conio.h>
class test
{
int code;
static int count;
public:
void setcode(void)
{
code=++count;
}
void showcode(void)
{
cout<<object number:<<code<<endl;
}
static void showcount(void)
{
cout<<"count:"<<count<<endl;
}
};
int test::count;
int main()
{
clrscr();
test t1,t2;
t1.setcode();
t2.setcode();
test::showcount();
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
getch();
return 0;
}

RESULT:
Thus the C++ program for static class member was executed and the output was
obtained successfully.
Ex.No:1.(e) FRIEND FUNCTION
Date:

AIM:
To write a C++ program to obtain the value using friend function.

ALGORITHM:
Step 1: Start the program.
Step 2: Define a class sample.
Step 3: Define the member functions setvalue() and mean() .
Step 4: Create the object for the class sample.
Step 5: Pass the value for the data members.
Step 6: Calculate the mean and display the result.
Step 6: Compile and execute the program.
Step 7: Terminate the program

PROGRAM:
#include<iostream.h>
#include<conio.h>
class sample
{
int a;
int b;
public:
void setvalue()
{
a=25;
b=40;
}
friend float mean(sample s);
};
float mean(sample s)
{
return float(s.a+s.b)/2.0;
}
int main()
{
clrscr();
sample x;
x.setvalue();
cout<<"mean value="<<mean(x)<<endl;
getch();
return 0;
}

RESULT:
Thus the C++ program was executed and the output was obtained successfully.
EX NO 1.(f.) ADDITION OF TWO COMPLEX NUMBERS
Date:

AIM:
To write a C++ program for addition of complex numbers using operator overloading.

ALGORITHM :
Step1: Start the program
Step2: Create a class complex.
Step3: Using constructors get the input for complex number.
Step4: Define a member function display().
Step5: Define operator member function for + operator to perform complex number addition.
Step6: Perform addition between two complex numbers.
Step7: Display the result of addition.
Step8: Terminate the program.

PROGRAM
#include<iostream.h>
#include<conio.h>
class complex
{
float x;
float y;
public:
complex()
{
}
complex(float real,float image)
{
x=real;
y=image;
}
complex operator + (complex);
void display(void);
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void complex::display(void)
{
cout<<x<<"+j"<<y<<endl;
}
int main()
{
clrscr();
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c3=";c3.display();
getch();
return 0;
}

RESULT:
Thus the C++ program for addition of two complex numbers was compiled and executed
successfully
Ex.No:1.(g) FUNCTION OVERLOADING
Date:

AIM:
To write a C++ program for function overloading.

ALGORITHM:
Step 1: Start the program
Step 2: Create the functions with same name
Step 3: The function will be differed in return type and number of arguments
Step 4: Implement the function, which has the same name
Step 5: Compile and execute the program.
Step 6: Terminate the program.

PROGRAM:
#include<iostream.h>
#include<conio.h>
int volume(int);
double volume(double,int);
long volume(long,int,int);
int main()
{
clrscr();
cout<<"\n volume of cube"<<volume(10)<<"\n";
cout<<"\n volume of cylinder"<<volume(3)<<"\n";
cout<<"\n volume of rectangle"<<volume(100,75,15)<<"\n";
getch();
return 0;
}
int volume(int s)
{
return(s*s*s);
}
double volume(double r,int h)
{
return(3.14*r*r*h);
}
long volume(long l,int b,int h)
{
return(l*b*h);
}

RESULT:
Thus the program was executed and the output was obtained successfully.
Ex.No:1.(h) MULTIPLE INHERITANCE
Date:

AIM:
To write a C++ program to illustrate the Multiple Inheritance concept.

ALGORITHM:
Step1: Include the header files.
Step2: Define a class student.
Step3: Define two member functions getno() and putno() under the class student .
Step4: Define a class test which is inherited from the class student.
Step5: Define two member functions getmarks() and putmarks() under class test.
Step6: Define another one class result which is inherited from test.
Step7: Display the mark details of a student.
Step8: Terminate the program

PROGRAM:
#include<iostream.h>
#include<conio.h>
class student
{
private:
int rollno;
public:
void getno(int);
void putno(void);
};
void student::getno(int a)
{
rollno=a;
}
void student::putno()
{
cout<<"Rollno:"<<rollno<<"\n";
}
class test:public student
{
protected:
float sub1;
float sub2;
public:
void getmarks(float,float);
void putmarks(void);
};
void test::getmarks(float x,float y)
{
sub1=x;
sub2=y;
}
void test::putmarks()
{
cout<<"Marks in sub1="<<sub1<<"\n";
cout<<"Marks in sub2="<<sub2<<"\n";
}
class result : public test
{
float total;
public:
void display(void);
};
void result::display(void)
{
total=sub1+sub2;
putno();
putmarks();
cout<<"total="<<total<<"\n";
}
void main()
{
result s1;
clrscr();
s1.getno(111);
s1.getmarks(75.0,59.5);
s1.display();
getch();
}

RESULT:
Thus the C++ program was executed and the output was obtained successfully.
Ex.No:1(i) HYBRID INHERITANCE
Date:

AIM:
To write a C++ program to illustrate the Hybrid inheritance concept.

ALGORITHM:
Step1: Include the header files.
Step2: Define a class student.
Step3: Define two member functions get_number() and put_number() under the class
student.
Step4: Define a class test which is inherited from the class student.
Step5: Define two member functions getmarks() and putmarks() under class test.
Step6: Define a class sports.
Step7: Define two member functions get_score() and put_score() under class sports.
Step8: Define another one class result which is inherited from test and sports.
Step9: Calculate the total marks secured.
Step 10: Display the mark details of a student.
Step11: Terminate the program.

PROGRAM:
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number=a;
}
void put_number(void)
{
cout<<"roll no:"<<roll_number<<"\n";
}
};
class test:public student
{
protected:
float part1,part2;
public:
void get_marks(float x,float y)
{
part1=x;
part2=y;
}
void put_marks(void)
{
cout<<"marks obtained:"<<"\n";
cout<<"part1="<<part1<<"\n";
cout<<"part2="<<part2<<"\n";
}
};
class sports
{
protected:
float score;
public:
void get_score(float s)
{
score=s;
}
void put_score(void)
{
cout<<"sports wt:"<<score<<"\n";
}
};
class result:public test,public sports
{
float total;
public:
void display(void);
};
void result::display(void)
{
total=part1+part2+score;
put_number();
put_marks();
put_score();
cout<<"total score:"<<total<<"\n";
}
int main()
{
clrscr();
result student_1;
student_1.get_number(1234);
student_1.get_marks(27.5,33.0);
student_1.get_score(6.0);
student_1.display();
getch();
return 0;
}

RESULT:
Thus the program was executed and the output was obtained successfully.
Ex.No:2 ARRAY IMPLEMENTATION OF LIST ADT
Date:

AIM:
To write a C++ program for array implementation of list ADT
.
ALGORITHM:
1. Declare an array ahead of time.
2. Get the input values of n elements and store them in the array.
3. To insert an element at a position p, increase the array size by one, shift all the elements
after p one step forward and then assign the new element at the required position.
4. To delete an element at a position p, shift all the elements after that position by one and
then decrease the size of the array by 1.
5. To find the position of an element inside an array, search the array elements one by one
from the start to the size of the array until the required element is found and return the
position of the element. If the element is not found return 0.

PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<process.h>
void create();
void insert();
void deletion();
void search();
void display();
int a,b[20],n,d,e,f,i;
void main()
{
int c;
char g='y';
clrscr();
do
{
cout<<"\n Main Menu";
cout<<"\n 1.Create \n 2Insert \n 3.Search \n 4.Delete \n 5.Display \n 6.Exit";
cout<<"\n enter your choice\n";
cin>>c;
switch(c)
{
case 1: create(); break;
case 2: insertion(); break;
case 3: search(); break;
case 4: delete(); break;
case 5: display(); break;
case 6: exit(0); break;
default:
cout<<"The given number is not between 1-5\n";
}
cout<<"\nDo u want to continue \n";
cin>>g;
clrscr();
}
while(g=='y'|| g=='Y');
getch();
}
void create()
{
cout<<"\n Enter the limit of elements you want to create";
cin>>n;
cout<<"\n Enter the numbers\n";
for(i=0;i<n;i++)
{
cin>>b[i];
}
}
void deletion()
{
cout<<"Enter the value u want to delete \n";
cin>>d;
for(i=0;i<n;i++)
{
if(b[i]==d)
{
b[i]=0;
}}}
void search()
{
cout<<"Enter the search element\n";
cin>>e;
for(i=0;i<n;i++)
{
if(b[i]==e)
{
cout<<"Value found in the position\n"<<i;
}}}
void insert()
{
cout<<"Enter how many number u want to insert \n";
cin>>f;
for(i=0;i<f;i++)
{
cin>>b[n++];
}}
void display()
{
cout<<"\n\n\n";
for(i=0;i<n;i++)
{
cout<<"\n\n\n"<<b[i];
}}

RESULT:
Thus, the array implementation of list ADT program was executed and the output was
verified successfully.
Ex.No:3 LINKED LIST IMPLEMENTATION OF LIST ADT
Date:

AIM:
To write a C++ Program for linked list implementation of list ADT.

ALGORITHM:
1. Create a node with a data field and pointer next pointing to the node data type
2. If the option is Create, then Read the number of elements, n and call the member
function append
begin
i=0
Repeat until i < n
begin
Read num
Call append(num)
i=i+1
end
end
3. If the option is Insert at beginning, Read the the element, num and pass the num as
parameter to the member function addatbeg
begin
temp = create a new node
temp -> data = num
temp -> link = p
p = temp
end
4. If the option is Insert at a position, Read the the element, num and the position, loc and
pass the loc, num as parameters to the member function addafter
begin
temp = p
i=0
Repeat until i < loc-1
begin
temp = temp -> link
if ( temp == NULL )
begin
Display there is lesser elements than the location, loc
Return
end
end
r = create a new node
r -> data = num
r -> link = temp -> link
temp -> link = r
end
5. If the option is Insert at end, Read the the element, num and pass the num as parameter
to the member function append
p is the head node
begin
if ( p == NULL )
begin
p = Create a new node
p -> data = num
p -> link = NULL
end
else
begin
temp = p
Repeat until ( temp -> link != NULL )
temp = temp -> link
r = create a new node
r -> data = num
r -> link = NULL
temp -> link = r
end
end
6. If the option is Delete then Read the element to be deleted, num and pass it as parameter
to the member function del
begin
temp = p
repeat until ( temp != NULL )
begin
if ( temp -> data == num )
begin
if ( temp == p )
p = temp -> link
else
old -> link = temp -> link
delete temp
return
end
else
begin
old = temp
temp = temp -> link
end
end
Display Element not found
end
7. If the option is find then Read the element to be found, num and pass it as parameter to
the member function find and it returns the position if the number is found else -1
begin
node *q = p
pos = 1
Repeat until q != NULL and q->data != num
begin
q = q->link
pos = pos + 1
end
if(q != NULL) then
Display the number is found at position, pos
else
Display the number is not found in the list
end
8. If the option is count then
begin
node *temp = p
c=0
repeat until ( temp != null )
begin
temp = temp -> link
c =c+1
end
return c
end
9. If the option is display then
begin
node *temp = p
Repeat until ( temp != NULL )
begin
Display temp->data
temp = temp -> link
end
return c
10. end
11. If the option is Exit, quit the program.

PROGRAM:
#include <conio.h>
#include <iostream.h>
class linklist
{
private :
struct node
{
int data ;
node * next ;
}*p ;
public :
linklist() ;
void create();
void append (int num) ;
void addatbeg (int num) ;
void addafter (int loc, int num) ;
void display() ;
void find(int num);
int count() ;
void del (int num) ;
~linklist() ;
};

// initializes data member


linklist::linklist()
{
p = NULL ;
}
// adds a node at the end of a linked list
void linklist::append (int num)
{
node *temp, *r ;
// if the list is empty, create first node
if ( p == NULL )
{
p = new node ;
p -> data = num ;
p -> next = NULL ;
}
else
{
// go to last node
temp = p ;
while ( temp -> next != NULL )
temp = temp -> next ;

// add node at the end


r = new node ;
r -> data = num ;
r -> next = NULL ;
temp -> next = r ;
}
}
// adds a new node at the beginning of the linked list
void linklist::addatbeg(int num)
{
node *temp ;

// add new node


temp = new node ;

temp -> data = num ;


temp -> next = p ;
p = temp ;
}
// adds a new node after the specified number of nodes
void linklist::addafter(int loc, int num)
{
node *temp, *r ;
temp = p ;

// skip to desired portion


for ( int i = 0 ; i < loc-1 ; i++ )
{
temp = temp -> next ;
// if end of linked list is encountered
if ( temp == NULL )
{
cout << "\nThere are less than " << loc << " elements in list\n";
return ;
}
}
// insert new node
r = new node ;
r -> data = num ;
r -> next = temp -> next ;
temp -> next = r ;
}
// counts the number of nodes present in the linked list
int linklist :: count( )
{
node *temp = p ;
int c = 0 ;
// traverse the entire linked list
while ( temp != NULL )
{
temp = temp -> next ;
c++ ;
}

return c ;
}
// displays the contents of the linked list
void linklist::display()
{
node *temp = p ;
cout << endl ;
// traverse the entire linked list
while ( temp != NULL )
{
cout << temp -> data << " " ;
temp = temp -> next ;
}
}
// deletes the specified node from the linked list
void linklist::del(int num)
{
node *old, *temp ;
temp = p ;
while ( temp != NULL )
{
if ( temp -> data == num )
{
// if node to be deleted is the
// first node in the linked list
if ( temp == p )
p = temp -> next ;
// deletes the intermediate nodes in the linked list
else
old -> next = temp -> next ;
// free the memory occupied by the node
delete temp ;
return ;
}
// traverse the linked list till the last node is reached
else
{
// old points to the previous node
old = temp ;
// go to the next node
temp = temp -> next ;
}
}
cout << "\n\nElement " << num << " not found" ;
}
// Searches for the element
void linklist::find(int num)
{
node *q = p ;
// traverse the linked list
int pos = 1;
while ( q != NULL && q->data != num)
{
q = q->next ;
pos++;
}
if(q != NULL)
cout << "\n\nThe number " << num << " is found at position " << pos;
else
cout << "\n\nThe number " << num << " is not found in the list";
}
// deallocates memory
linklist :: ~linklist( )
{
node *q ;

while ( p != NULL )
{
q = p -> next ;
delete p ;
p=q ;
}
}
void main( )
{
linklist l ;
int ch, ch1, loc, num, numtot, ctr;

do
{
clrscr();
cout << "\n\tList ADT using Linked List";
cout << "\n\t--------------------------";
cout << "\n\n1. Create";
cout << "\n\n2. Insert";
cout << "\n\n3. Delete";
cout << "\n\n4. Find";
cout << "\n\n5. Count";
cout << "\n\n6. Display";
cout << "\n\n7. Exit\n";
cout << "\nEnter your choice : ";
cin >> ch;
switch(ch)
{
case 1:
cout << "\n\nEnter the number of elements to be added : ";
cin >> numtot;
ctr = 0;
cout << "\n\nEnter the numbers...\n";
while (ctr < numtot)
{
cin >> num;
l.append(num);
ctr++;
}
l.display();
break;
case 2:
l.display();
cout << "\n\n1. Insert at Beginning";
cout << "\n\n2. Insert after a position";
cout << "\n\n3. Insert at End";
cout << "\n\n4. Exit\n";
cout << "\nEnter your choice : ";
cin >> ch1;
switch(ch1)
{
case 1:
cout << "\n\nEnter the number : ";
cin >> num;
l.addatbeg(num);
break;
case 2:
cout << "\n\nEnter the number : ";
cin >> num;
cout << "\n\nEnter the position : ";
cin >> loc;
l.addafter(loc, num);
break;
case 3:
cout << "\n\nEnter the number : ";
cin >> num;
l.append(num);
break;
default:
break;
}
l.display();
break;
case 3:
l.display();
cout << "\n\nEnter the number to be deleted : ";
cin >> num;
l.del(num);
l.display();
break;
case 4:
l.display();
cout << "\n\nEnter the number to be found : ";
cin >> num;
l.find(num);
break;
case 5:
l.display();
numtot = l.count();
cout << "\n\nThe number of elements is " << numtot;
break;
case 6:
l.display();
break;
default:
break;
}
getch();
}while(ch<7);
}

RESULT:
Thus, the array implementation of List ADT program was executed and the output was
verified successfully.

Ex no 4. CURSOR LIST IMPLEMENTATION OF LIST ADT

AIM:
To write a C++ Program for processing a List ADT using Linked Lists.

ALGORITHM :
1. Create a node with a data field and next field pointing to the node data type
2. If the option is Create, then Read the number of elements, n and call the member
function insert
begin
i=0
Repeat until i < n
begin
Read num
Call insert(num)
i=i+1
end
end
3. If the option is Insert, Read the the element, num and pass the num as parameter to the
member function Insert
begin
if(p == NULL) then
begin
p = alloc();
cursorspace[p].next = 0;
cursorspace[p].data = num;
end
else
begin
int pos = p;
int tmp = alloc();
Repeat while(cursorspace[pos].next != 0)
pos = cursorspace[pos].next;
cursorspace[tmp].data = num;
cursorspace[tmp].next = cursorspace[pos].next;
cursorspace[pos].next = tmp;
end
end
4. If the option is Delete then Read the element to be deleted, num and pass it as parameter
to the member function del
begin
if(cnt == 0) then
begin
Display The List is empty..."
return;
end
int num;
Display Enter the number to be deleted
Read num;
if(cnt == 1 && cursorspace[p].data == num) then
begin
initialize();
return;
end
int temp = findprevious(num);
if(temp == 0) then // The Head node - so no previous node
begin
int t = cursorspace[p].next;
free(p);
p = t;
return;
end
pos = temp;
if(cursorspace[pos].next != 0)then
begin
t = cursorspace[pos].next;
cursorspace[pos].next = cursorspace[t].next;
free(t);
end
else
Display Element ", num, not found" ;
end
5. If the option is find then Read the element to be found, num and pass it as parameter to
the member function find and it returns the position if the number is found else -1
begin
temp = p
c =1
// traverse the entire linked list
Repeat while ( temp != 0 && cursorspace[temp].data != num)
begin
temp = cursorspace[temp].next ;
c=c+1
if(temp != NULL) then
Display the number is found at position, c
else
Display the number is not found in the list
end
6. If the option is count then
begin
if(p==NULL) then
return 0;
c = 1;
pos = p;
Repeat while(cursorspace[pos].next != 0)
begin
c++;
// traverse the entire linked list
pos = cursorspace[pos].next;
end
return c
end
7. If the option is display then
begin
int pos = p;

Repeat while(pos != 0)
begin
Display cursorspace[pos].data;
// traverse the entire linked list
pos = cursorspace[pos].next;
end
end
8. If the option is Exit, quit the program.

PROGRAM :
// Program to maintain a List ADT using Cursors
#include <conio.h>
#include <iostream.h>
const int MAX = 20;
class cursorlist
{
private :
struct node
{
int data ;
int next ;
}cursorspace[MAX];
int p;
public :
cursorlist() ;
int alloc();
void free(int t);
void create();
void initialize();
void insert(int num) ;
void display() ;
void find(int num);
int findprevious(int num);
int count() ;
void del () ;
~cursorlist() ;
};
// initializes data member
cursorlist::cursorlist()
{
initialize();
}
void cursorlist::initialize()
{
for(int i = 0; i<MAX; i++)
{
cursorspace[i].data = NULL;
cursorspace[i].next = i+1;
}
cursorspace[MAX-1].next = NULL;
p = NULL;
}
int cursorlist::alloc()
{
int temp = cursorspace[0].next;
cursorspace[0].next = cursorspace[temp].next;
return temp;
}

void cursorlist::free(int t)
{
cursorspace[t].next = cursorspace[0].next;
cursorspace[0].next = t;
cursorspace[t].data = NULL;
}
// adds a new node at the beginning of the linked list
void cursorlist::insert(int num)
{
if(p == NULL)
{
p = alloc();
cursorspace[p].next = 0;
cursorspace[p].data = num;
}
else
{
int pos = p;
int tmp = alloc();
while(cursorspace[pos].next != 0)
pos = cursorspace[pos].next;
cursorspace[tmp].data = num;
cursorspace[tmp].next = cursorspace[pos].next;
cursorspace[pos].next = tmp;
}
}
// counts the number of nodes present in the linked list
int cursorlist :: count( )
{
if(p==NULL)
return 0;

int c = 1;
int pos = p;

while(cursorspace[pos].next != 0)
{
c++;
// traverse the entire linked list
pos = cursorspace[pos].next;
}
return c ;
}
// displays the contents of the linked list
void cursorlist::display()
{
int pos = p;
while(pos != 0)
{
cout << cursorspace[pos].data << " " ;
// traverse the entire linked list
pos = cursorspace[pos].next;
}
}
// deletes the specified node from the linked list
void cursorlist::del()
{
int cnt = count();
if(cnt == 0)
{
cout << "\n\nThe List is empty...";
return;
}
int num;
cout << "\n\nEnter the number to be deleted : ";
cin >> num;
if(cnt == 1 && cursorspace[p].data == num)
{
initialize();
return;
}
int temp = findprevious(num);
if(temp == 0) // The Head node - so no previous node
{
int t = cursorspace[p].next;
free(p);
p = t;
return;
}
int pos = temp;
if(cursorspace[pos].next != 0)
{
int t = cursorspace[pos].next;
cursorspace[pos].next = cursorspace[t].next;
free(t);
}
else
cout << "\n\nElement " << num << " not found" ;
}
// Searches for the element
int cursorlist::findprevious(int num)
{
int temp = p;
// if(temp == 0 && cursorspace[p].data == num) //head node
// return 0;

while ( temp != 0)
{
int t = cursorspace[temp].next;
if(cursorspace[t].data != num)
temp = cursorspace[temp].next ;
else
break;
}
return temp;
}
// Searches for the element
void cursorlist::find(int num)
{
int temp = p;
int c = 1;

// traverse the entire linked list


while ( temp != 0 && cursorspace[temp].data != num)
{
temp = cursorspace[temp].next ;
c++;
}
if(temp != NULL)
cout << "\n\nThe number " << num << " is found at position " << c;
else
cout << "\n\nThe number " << num << " is not found in the list";
}
// deallocates memory
cursorlist :: ~cursorlist( )
{
for(int i=0; i<MAX; i++)
{
cursorspace[i].data = NULL;
cursorspace[i].next = i+1;
}
free(p);
}
void main( )
{
cursorlist l ;
int ch, ch1, loc, num, numtot, ctr;
do
{ clrscr();
cout << "\n\tList ADT using Linked List";
cout << "\n\t--------------------------";
cout << "\n\n1. Create";
cout << "\n\n2. Insert";
cout << "\n\n3. Delete";
cout << "\n\n4. Find";
cout << "\n\n5. Count";
cout << "\n\n6. Display";
cout << "\n\n7. Exit\n";
cout << "\nEnter your choice : ";
cin >> ch;
switch(ch)
{
case 1:
cout << "\n\nEnter the number of elements to be added : ";
cin >> numtot;
ctr = 0;
cout << "\n\nEnter the numbers...\n";
while (ctr < numtot)
{
cin >> num;
l.insert(num);
ctr++;
}
l.display();
break;
case 2:
l.display();
cout << "\n\nEnter the number : ";
cin >> num;
l.insert(num);
l.display();
break;
case 3:
l.display();
l.del();
l.display();
break;
case 4:
l.display();
cout << "\n\nEnter the number to be found : ";
cin >> num;
l.find(num);
break;
case 5:
l.display();
numtot = l.count();
cout << "\n\nThe number of elements is " << numtot;
break;
case 6:
l.display();
break;
default:
break;
}
getch();
}while(ch<7);
}
RESULT:
Thus the C++ Program for the List ADT using Cursors was Compiled and Executed
Successfully
Ex.No:5(a) ARRAY IMPLEMENTATION OF STACK ADT
Date:

AIM:
To write a C++ program for the array implementation of stack using ADT

ALGORITHM:

Step 1:Initialize MAX = 10, maximum number of elements in the array


Step 2:If the option is Create, then Read the number of elements, n and pass n as a parameter
to the member function create
begin
i=0
Repeat until i < n
begin
Read num
Call push(num)
i=i+1
end
end
Step 3:If the option is Push, Read the element, num and pass the num as parameter to the
member function push
begin
if ( top == MAX - 1 ) then
begin
Display Stack is full
return
end
top = top + 1
arr[top] = item
end
Step 4:If the option is pop then return the data popped
begin
if ( top == -1 ) then
begin
Display Stack is empty
return NULL
end
data = arr[top]
top = top - 1
return data
end
Step 5:If the option is display then
begin
if(top != -1)
begin
i=top
Repeat until i >= 0
begin
Display arr[i]
i = i -1
end
end
else
Display Stack is empty
end
Step 6:If the option is Exit, quit the program.

PROGRAM:
// Program implements array as a stack
#include <conio.h>
#include <iostream.h>
const int MAX = 10 ;
class stack
{
private :
int arr[MAX] ;
int top ;
public :
stack( ) ;
void push ( int item ) ;
int pop( ) ;
void display();
};
// initializes data member
stack :: stack( )
{
top = -1 ;
}
// adds an element to the stack
void stack :: push ( int item )
{
if ( top == MAX - 1 )
{
cout << endl << "Stack is full" ;
return ;
}
top++ ;
arr[top] = item ;
}
// removes an element from the stack
int stack :: pop( )
{
if ( top == -1 )
{
cout << endl << "Stack is empty" ;
return NULL ;
}
int data = arr[top] ;
top-- ;
return data ;
}
void stack :: display()
{
if(top != -1)
{
cout << "\n\nStack Elements...\n\n";
for(int i=top; i >= 0; i--)
cout << " " << arr[i] << "\n";
}
else
cout << endl << "\nStack is empty\n" ;
}
void main( )
{
stack s ;
int ch, num, numtot, ctr;
do
{
clrscr();
cout << "\n\tStack ADT using Array";
cout << "\n\t---------------------";
cout << "\n\n1. Create";
cout << "\n\n2. Push";
cout << "\n\n3. Pop";
cout << "\n\n4. Display";
cout << "\n\n5. Exit\n";
cout << "\nEnter your choice : ";
cin >> ch;
switch(ch)
{
case 1:
cout << "\n\nEnter the number of elements to be added : ";
cin >> numtot;
ctr = 0;
cout << "\n\nEnter the numbers...\n";
while (ctr < numtot)
{
cin >> num;
s.push(num);
ctr++;
}
s.display();
break;
case 2:
s.display();
cout << "\n\nEnter the number : ";
cin >> num;
s.push(num);
s.display();
break;
case 3:
s.display();
num = s.pop();
cout << "Item Popped - " << num << "\n";
s.display();
break;
case 4:
s.display();
break;
default:
break;
}
getch();
}while(ch < 5);
}
RESULT:
Thus the C++ program for array implementation of stack ADT was executed and output
was verified successfully.
Ex. No: 5(b) LINKED LIST IMPLEMENTATION OF STACK ADT
Date:

AIM:
To write a C++ program for stack ADT using linked list implementation.

ALGORITHM:
1. Create a node with a data field and pointer next pointing to the node data type
2. If the option is Create, then Read the number of elements, n and pass n as a parameter
to the member function create
begin
i=0
Repeat until i < n
begin
Read num
Call push(num)
i=i+1
end
end
3. If the option is Push, Read the element, num and pass the num as parameter to the
member function push
begin
if ( temp == NULL ) then
begin
Display Stack is full
return
end
temp -> data = item
temp -> link = top
top = temp
end
4. If the option is pop then return the data popped
begin
if ( top == -1 ) then
begin
Display Stack is empty
return NULL
end
node *temp

temp = top
item = temp -> data
top = top -> link

delete temp
return item
end
5. If the option is display then
begin
if(top != -1)
begin
node *temp, *p
p = top
Repeat while ( p != NULL )
begin
Display p->data
p = p -> link

end
end
else
Display Stack is empty
end
6. If the option is Exit, quit the program.

PROGRAM:
// Program implements linked list as a stack
#include <conio.h>
#include <iostream.h>
class stack
{
private :
// structure containing data part and linkpart
struct node
{
int data ;
node *link ;
} *top ;
public :

stack( ) ;
void push ( int item ) ;
int pop( ) ;
void display();
~stack( ) ;
};
// initializes data member
stack :: stack( )
{
top = NULL ;
}
// adds a new node to the stack as linked list
void stack :: push ( int item )
{
node *temp ;
temp = new node ;

if ( temp == NULL )
cout << endl << "Stack is full" ;

temp -> data = item ;


temp -> link = top ;
top = temp ;
}
// pops an element from the stack
int stack :: pop( )
{
if ( top == NULL )
{
cout << endl << "Stack is empty" ;
return NULL ;
}
node *temp ;
int item ;
temp = top ;
item = temp -> data ;
top = top -> link ;
delete temp ;
return item ;
}
void stack :: display()
{
if ( top == NULL )
return ;

cout << "\n\nStack Elements...\n\n";


node *temp, *p;
p = top;
while ( p != NULL )
{
cout << p->data << "\n";
p = p -> link ;
}
}
// deallocates memory
stack :: ~stack( )
{
if ( top == NULL )
return ;
node *temp ;
while ( top != NULL )
{
temp = top ;
top = top -> link ;
delete temp ;
}
}
void main( )
{
stack s ;
int ch, num, numtot, ctr;
do
{
clrscr();
cout << "\n\tStack ADT using Linked List";
cout << "\n\t---------------------------";
cout << "\n\n1. Create";
cout << "\n\n2. Push";
cout << "\n\n3. Pop";
cout << "\n\n4. Display";
cout << "\n\n5. Exit\n";
cout << "\nEnter your choice : ";
cin >> ch;
switch(ch)
{
case 1:
cout << "\n\nEnter the number of elements to be added : ";
cin >> numtot;
ctr = 0;
cout << "\n\nEnter the numbers...\n";
while (ctr < numtot)
{
cin >> num;
s.push(num);
ctr++;
}
s.display();
break;
case 2:
s.display();
cout << "\n\nEnter the number : ";
cin >> num;
s.push(num);
s.display();
break;
case 3:
s.display();
num = s.pop();
cout << "Item Popped - " << num << "\n";
s.display();
break;
case 4:
s.display();
break;
default:
break;
}
getch();
}while(ch < 5);
}

RESULT:
Thus the C++ program for stack ADT using linked list was executed and output was
verified successfully.

Ex.No:6.a STACK APPLICATION


Date: BALANCING PARANTHESIS

AIM:
To write a C++ Program for Balancing Paranthesis in an expression using Stack ADT.
ALGORITHM :
1. Read the expression into an array
2. Make an empty stack
3. Read characters until end of file
4. If the character is an opening symbol, push it onto the stack.
5. If it is a closing symbol, then if the stack is empty report an error.
6. Otherwise pop the stack.
7. If the symbol popped is not the corresponding opening symbol, then report an error.
8. At the end of file, if the stack is not empty, report an error.
9. Otherwise, report that the expression is well balanced.

PROGRAM :
STACKBAL.H
// Program implements linked list as a stack
#include <conio.h>
#include <iostream.h>
const int MAX = 50;
class stack
{
private :
// structure containing data part and linkpart
struct node
{
char data ;
node *link ;
} *top ;
public :
stack( ) ;
void push ( char item ) ;
char pop( ) ;
char peek( ) ;
int Isempty( ) ;
~stack( ) ;
};
// initializes data member
stack :: stack( )
{
top = NULL ;
}
// adds a new node to the stack as linked list
void stack :: push ( char item )
{
node *temp ;
temp = new node ;
if ( temp == NULL )
cout << endl << "Stack is full" ;

temp -> data = item ;


temp -> link = top ;
top = temp ;
}
// pops an element from the stack
char stack :: pop( )
{
if ( top == NULL )
return NULL ;
node *temp ;
char item ;
temp = top ;
item = temp -> data ;
top = top -> link ;
delete temp ;
return item ;
}
// pops an element from the stack
char stack :: peek( )
{
if ( top == NULL )
return NULL ;
char item ;
item = top -> data ;
return item ;
}
// pops an element from the stack
int stack :: Isempty( )
{
if ( top == NULL )
return 1 ;
else
return 0;
}
// deallocates memory
stack :: ~stack( )
{
if ( top == NULL )
return ;
node *temp ;
while ( top != NULL )
{
temp = top ;
top = top -> link ;
delete temp ;
}
}

BALPARAN.CPP
#include<iostream.h>
#include<conio.h>
#include<malloc.h>
#include<string.h>
#include"stackbal.h"
// Creating a NODE Structure
class balance:public stack
{
public:
int bal(char str[]);
};
// Balance parenthesis
int balance::bal(char str[])
{
int i = 0;
int count = 0;
int len = strlen(str);
while (i < len)
{
if (str[i] == ' ')
{
i++;
continue;
}

if ( str[i] == '(' || str[i] == '{' || str[i] == '[' )


{
push(str[i]);
count++;
}

if ( str[i] == ')' || str[i] == '}' || str[i] == ']' )


{
if ( Isempty() == 1 )
{
cout << " parenthesis " << str[i] << " at position "
<< i << " is unbalanced " << endl;
return -1;
}
else
{
if ( (str[i] == ')' && peek() == '(') ||
(str[i] == '}' && peek() == '{') ||
(str[i] == ']' && peek() == '['))
{
pop();
count--;
}
else
{
cout << " \n\nParenthesis mismatch";
return -1;
}
}
}
i++;
}
if(count == 0)
return 1;
else
return -1;
}
// Main function
void main()
{
balance b;
char sExpr[MAX];
cout << "\n\nEnter the expression to be balanced : ";
cin >> sExpr;
int result = b.bal(sExpr);
if(result == 1)
cout << "\n\nExpression " << sExpr << " is balanced..";
else
cout << "\n\nExpression " << sExpr << " is not balanced...";
getch();
}

RESULT:

Thus the C++ Program for Balancing Paranthesis in an expression using Stack ADT was
Compiled and Executed Successfully.

Ex no 6(b) STACK APPLICATION


Date: EVALUVATION OF EXPRESSIONS

AIM:
To write a C++ Program for Evaluvating an expression using Stack ADT.

ALGORITHM :
1. Read an expression in postfix form into an array
2. If white spaces encountered skip
3. If digit is encountered push it onto the stack
4. Otherwise it is an operator, hence pop the operands and perform the operationusing the
operator
5. Push the result onto the stack
6. The final result is in the stack, hence after processing the expression, pop the stack and
print the contents popped.

PROGRAM :
STCKEXPR.H
// The Stack Class
const int MAX = 50;
class stack
{
public :
char stack[MAX] ;
int top ;
public :
stack( ) ;
void push ( int item ) ;
int pop( ) ;
void display();
};
// initializes data member
stack :: stack( )
{
top = -1 ;
// strcpy ( stack, "" ) ;
}
// adds an element to the stack
void stack :: push ( int item )
{
top++ ;
stack[top] = item;
}
// removes an element from the stack
int stack :: pop( )
{
int data = stack[top] ;
top-- ;
return data ;
}
EXPREVAL.CPP
// Program to evaluate an epression entered in postfix form
#include <conio.h>
#include <iostream.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include "stckexpr.h"
class postfix:public stack
{
private :
int stack[MAX] ;
int top, nn ;
char *s ;

public :
postfix( ) ;
void setexpr ( char *str ) ;
void calculate( ) ;
void show( ) ;
};
// initializes data members
postfix :: postfix( )
{
top = -1 ;
}
// sets s to point to the given expr.
void postfix :: setexpr ( char *str )
{
s = str ;
}
// evaluates the postfix expression
void postfix :: calculate( )
{
int n1, n2, n3 ;
while ( *s )
{
/* skip whitespace, if any */
if ( *s == ' ' || *s == '\t' )
{
s++ ;
continue ;
}
/* if digit is encountered */
if ( isdigit ( *s ) )
{
nn = *s - '0' ;
push ( nn ) ;
}
else
{
/* if operator is encountered */
n1 = pop( ) ;
n2 = pop( ) ;
switch ( *s )
{
case '+' :
n3 = n2 + n1 ;
break ;
case '-' :
n3 = n2 - n1 ;
break ;
case '/' :
n3 = n2 / n1 ;
break ;
case '*' :
n3 = n2 * n1 ;
break ;
case '%' :
n3 = n2 % n1 ;
break ;
case '$' :
n3 = pow ( n2 , n1 ) ;
break ;
default :
cout << "Unknown operator" ;
exit ( 1 ) ;
}
push ( n3 ) ;
}
s++ ;
}
}
// displays the result
void postfix :: show( )
{
nn = pop ( ) ;
cout << "Result is: " << nn ;
}
void main( )
{
char expr[MAX] ;
clrscr();
cout << "\nEnter postfix expression to be evaluated : " ;
cin.getline ( expr, MAX ) ;
postfix q ;
q.setexpr ( expr ) ;
q.calculate( ) ;
q.show( ) ;
getch();
}

RESULT:

Thus the C++ Program for Evaluvating an expression using Stack ADT was Compiled and
executed Successfully.
Ex. No: 7 ARRAY IMPLEMENTATION OF QUEUE ADT
Date:

AIM:
To write a C++ Program for processing a Queue ADT using Arrays.

ALGORITHM :
1. Initialize MAX = 10, maximum number of elements in the array
2. If the option is Create, then Read the number of elements, n and pass n as a parameter
to the member function create
begin
i=0
Repeat until i < n
begin
Read num
Call addq(num)
i=i+1
end
end
3. If the option is Add, Read the element, num and pass the num as parameter to the
member function addq
begin
if(rear == MAX - 1)
begin
Display Queue is full
return ;
end
rear = rear + 1 ;
arr[rear] = item ;
if ( front == -1 )
front = 0 ;
end
4. If the option is delete then Read the element, num and pass the num as parameter to the
member function delq
begin
if ( front == -1 )
begin
Display Queue is Empty
return NULL ;
end
data = arr[front] ;
arr[front] = 0 ;
if ( front == rear )
front = rear = -1 ;
else
front = front + 1

return data ;
end
5. If the option is display then
begin
i=0
Repeat until i < MAX and arr[i] != 0
begin
Display arr[i]
i = i -1
end
end
end
6. If the option is find then Read the element, num and pass the num as parameter to the
member function find
begin
i=0
Repeat until i < MAX
begin
if arr[i] = num then
return i
end
return -1
end
7. If the option is Exit, quit the program.

PROGRAM:
// Program that implements queue as an array
#include <conio.h>
#include <iostream.h>
const int MAX = 10 ;

class queue
{
private :
int arr[MAX] ;
int front, rear ;
public :

queue( ) ;
void create(int numtot);
void addq (int item) ;
int delq( ) ;
int find(int item);
void display();
int count();
};
// initialises data members
queue::queue()
{
front = -1 ;
rear = -1 ;
for(int i=0; i < MAX; i++)
arr[i] = 0;
}
// Creates a queue of length numtot
void queue::create(int numtot)
{
int num;
for(int i = 0; i < numtot; i++)
{
cin >> num;
addq(num);
}
}
// adds an element to the queue
void queue:: addq (int item)
{
if(rear == MAX - 1)
{
cout << "\nQueue is full" ;
return ;
}
rear++ ;
arr[rear] = item ;
if ( front == -1 )
front = 0 ;
}
// removes an element from the queue
int queue::delq()
{
int data ;
if ( front == -1 )
{
cout << "\nQueue is Empty" ;
return NULL ;
}
data = arr[front] ;
arr[front] = 0 ;
if ( front == rear )
front = rear = -1 ;
else
front++ ;
return data ;
}
// searches queue for a given element num
int queue::find(int num)
{
// Traverse the array
for ( int i = 0 ; i < MAX ; i++ )
{
if ( arr[i] == num )
return i;
}

return -1;
}
// Counts the number of elements inserted into the queue
int queue::count()
{
return front - rear + 1;
}
// displays the contents of a queue
void queue::display()
{
cout<< endl ;
// traverse the entire array
for ( int i = 0 ; i < MAX; i++ )
{
if( arr[i] != 0)
cout << " " << arr[i] ;
}
}
void main()
{
queue q;
int ch, loc, num, ntot;
do
{
clrscr();
cout << "\n\tQueue ADT using Arrays";
cout << "\n\t----------------------";
cout << "\n\n1. Create";
cout << "\n\n2. Insert";
cout << "\n\n3. Delete";
cout << "\n\n4. Find";
cout << "\n\n5. Count";
cout << "\n\n6. Display";
cout << "\n\n7. Exit\n";
cout << "\nEnter your choice : ";
cin >> ch;
switch(ch)
{
case 1:
cout << "\n\nEnter the number of elements to be added : ";
cin >> ntot;
cout << "\n\nEnter the numbers...\n";
q.create(ntot);
q.display();
break;
case 2:
q.display();
cout << "\n\nEnter the number : ";
cin >> num;
q.addq(num);
q.display();
break;
case 3:
q.display();
num = q.delq();
if(num != NULL)
cout << "\n\nThe element " << num << " is deleted"
;
q.display();
break;
case 4:
q.display();
cout << "\n\nEnter the number to be found : ";
cin >> num;
loc = q.find(num);
if(loc == -1)
cout << "\n\nThe element " << num
<< " is not present in the queue" ;
else
cout << "\n\nThe element " << num
<< " is present at position "<< ( loc + 1 );
break;
case 5:
q.display();
ntot = q.count();
cout << "\n\nThe number of elements is " << ntot;
break;
case 6:
q.display();
break;
default:
break;
}
getch();
}while(ch<7);
}

RESULT:

Thus the C++ Program for the Queue ADT using Arrays was Compiled and Executed
Successfully.

Ex. No: 8 LINKED LIST IMPLEMENTATION OF QUEUE ADT


Date:

AIM:
To write a C++ Program for processing a Queue ADT using Linked Lists.

ALGORITHM :
1. Create a node with a data field and pointer next pointing to the node data type
2. If the option is Create, then Read the number of elements, n and pass n as a parameter
to the member function create
begin
i=0
Repeat until i < n
begin
Read num
Call addq(num)
i=i+1
end
end
3. If the option is Add, Read the element, num and pass the num as parameter to the
member function addq
begin
temp = create a new node ;
if ( temp == NULL )
cout << "\nQueue is full" ;
temp -> data = item ;
temp -> link = NULL ;

if ( front == NULL )
begin
rear = front = temp ;
return ;
end

rear -> link = temp ;


rear = rear -> link ;
end
4. If the option is delete then call the member function delq
begin
if ( front == NULL )
begin
cout << "\nQueue is empty" ;
return NULL ;
end

item = front -> data ;


temp = front ;
front = front -> link ;
delete temp ;
return item ;
end
5. If the option is display then call the member function display
begin
temp = front ;

Repeat while ( temp != NULL )


begin
cout << temp -> data << " " ;
temp = temp -> link ;
end
end
6. If the option is find then Read the element, num and pass the num as parameter to the
member function find
begin
q = front ;
pos = 1;
Repeat while ( q != NULL and q->data != num)
begin
q = q->link ;
pos++;
end
if(q != NULL) then
Display The number is found at position, pos;
else
Display The number is not found in the list;
end
7. If the option is Exit, quit the program.

PROGRAM:
// Program that implements queue as a linked list
#include <conio.h>
#include <iostream.h>

class queue
{
private :
struct node
{
int data ;
node *link ;
} *front, *rear ;
public :
queue( ) ;
void create(int numtot);
void addq ( int item ) ;
int delq( ) ;
void find(int item);
void display();
int count();
~queue( ) ;
};
// initialises data member
queue :: queue( )
{
front = rear = NULL ;
}
// Creates a queue of length numtot
void queue::create(int numtot)
{
int num;
for(int i = 0; i < numtot; i++)
{
cin >> num;
addq(num);
}
}
// adds an element to the queue
void queue :: addq ( int item )
{
node *temp ;
temp = new node ;
if ( temp == NULL )
cout << "\nQueue is full" ;
temp -> data = item ;
temp -> link = NULL ;
if ( front == NULL )
{
rear = front = temp ;
return ;
}
rear -> link = temp ;
rear = rear -> link ;
}
// removes an element from the queue
int queue :: delq( )
{
if ( front == NULL )
{
cout << "\nQueue is empty" ;
return NULL ;
}
node *temp ;
int item ;
item = front -> data ;
temp = front ;
front = front -> link ;
delete temp ;
return item ;
}
// Searches for the element
void queue::find(int num)
{
node *q = front ;
// traverse the linked list
int pos = 1;
while ( q != NULL && q->data != num)
{
q = q->link ;
pos++;
}
if(q != NULL)
cout << "\n\nThe number " << num << " is found at position " << pos;
else
cout << "\n\nThe number " << num << " is not found in the list";
}
// displays the contents of the linked list
void queue::display()
{
node *temp = front ;
cout << endl ;
// traverse the entire linked list
while ( temp != NULL )
{
cout << temp -> data << " " ;
temp = temp -> link ;
}
}
// counts the number of nodes present in the linked list
int queue::count( )
{
node *temp = front ;
int c = 0 ;
// traverse the entire linked list
while ( temp != NULL )
{
temp = temp -> link ;
c++ ;
}
return c ;
}
// deallocates memory
queue :: ~queue( )
{
if ( front == NULL )
return ;
node *temp ;
while ( front != NULL )
{
temp = front ;
front = front -> link ;
delete temp ;
}
}
void main( )
{
queue q ;
int ch, loc, num, ntot;
do
{
clrscr();
cout << "\n\tQueue ADT using Linked List";
cout << "\n\t---------------------------";
cout << "\n\n1. Create";
cout << "\n\n2. Insert";
cout << "\n\n3. Delete";
cout << "\n\n4. Find";
cout << "\n\n5. Count";
cout << "\n\n6. Display";
cout << "\n\n7. Exit\n";
cout << "\nEnter your choice : ";
cin >> ch;
switch(ch)
{
case 1:
cout << "\n\nEnter the number of elements to be added : ";
cin >> ntot;
cout << "\n\nEnter the numbers...\n";
q.create(ntot);
q.display();
break;
case 2:
q.display();
cout << "\n\nEnter the number : ";
cin >> num;
q.addq(num);
q.display();
break;
case 3:
q.display();
num = q.delq();
if(num != NULL)
cout << "\n\nThe element " << num << " is deleted"
;
q.display();
break;
case 4:
q.display();
cout << "\n\nEnter the number to be found : ";
cin >> num;
q.find(num);
break;
case 5:
q.display();
ntot = q.count();
cout << "\n\nThe number of elements is " << ntot;
break;
case 6:
q.display();
break;
default:
break;
}
getch();
}while(ch<7);
}

RESULT:

Thus the C++ Program for the Queue ADT using Linked List was Compiled and Executed
Successfully.

Ex no 9 BINARY SEARCH TREE


Date:

AIM:

To write a C++ program to perform binary search tree.


ALGORITHM :

1. Create a node with a data field and pointer left and right pointing to the node data type
2. If the option is Insert, then Read the number of elements, n and call the member function
Insert with the parameters btreenode and n
begin
if ( sr == NULL ) then
begin
sr = create a new btreenode
if(root == NULL)
root = sr
sr->leftchild = NULL
sr->data = num
sr->rightchild = NULL
end
else
begin
if ( num < sr->data )
sr->leftchild=insert(sr->leftchild, num) ;
else
sr->rightchild=insert(sr->rightchild, num ) ;
end
return sr;
end
3. If the option is Delete then Read the element to be deleted, num and pass it as parameter
to the member function del
begin
if ( sr == NULL )
begin
return NULL;
end
if(num < sr->data) then
sr->leftchild = rem(sr->leftchild, num;
else if(num > sr->data)
sr->rightchild = rem(sr->rightchild, num)
else if(sr->leftchild && sr->rightchild)
begin
x = FindMin(sr->rightchild)
sr->data = x->data
sr->rightchild = rem(sr->rightchild, sr->data)
end
else
begin
x = sr
if(sr->leftchild == NULL)
sr = sr->rightchild
else if(sr->rightchild == NULL)
sr = sr->leftchild
if(x==root)
root = NULL;
delete x
end
end
4. If the option is find then Read the element to be found, num and pass it as parameter to
the member function find
begin
if ( sr == NULL )
return NULL
if( num < sr->data)
return Find(sr->leftchild, num)
else if(num > sr->data)
return Find(sr->rightchild, num)
else
return sr;
end
5. If the option is Minimum/Maximum then call the member function FindMin/FindMax
Procedure FindMin
begin
if ( sr == NULL )
begin
Display Tree is empty
return NULL
end
else if(sr->leftchild == NULL)
return s
else
return FindMin(sr->leftchild)
end

Procedure FindMax
begin
if ( sr == NULL )
begin
Display Tree is empty
return NULL
end
else if(sr->rightchild == NULL)
return s
else
return FindMin(sr->rightchild)
end
6. If the option is traverse then call the member functions inorder, postorder, preorder
Procedure preorder
begin
if ( sr != NULL )
begin
Display sr -> data
preorder ( sr -> leftchild )
preorder ( sr -> rightchild )
end
else
return
end

Procedure inorder
begin
if ( sr != NULL )
begin
inorder ( sr -> leftchild )
Display sr -> data
inorder ( sr -> rightchild )
end
else
return
end

Procedure postorder
begin
if ( sr != NULL )
begin
postorder ( sr -> leftchild )
postorder ( sr -> rightchild )
Display sr -> data
end
else
return
end

7. If the option is Exit, quit the program


PROGRAM :
// Program for Binary Tree Operations
// Program with inorder, preorder and postorder functions
#include <iostream.h>
#include <conio.h>
struct btreenode
{
btreenode *leftchild ;
int data ;
btreenode *rightchild ;
}*root;
class btree
{
public :
btree( ) ;
void buildtree ( int num ) ;
btreenode* insert ( btreenode *sr, int num ) ;
void traverse( ) ;
void inorder ( btreenode *sr ) ;
void preorder ( btreenode *sr ) ;
void postorder ( btreenode *sr ) ;
void levelorder ( btreenode *sr );
void del ( btreenode *sr ) ;
btreenode* FindMin ( btreenode *sr ) ;
btreenode* FindMax ( btreenode *sr ) ;
btreenode* Find ( btreenode *sr, int num ) ;
void remove ( int num ) ;
btreenode* rem ( btreenode *sr, int num) ;
~btree( ) ;
};
// initialises data member
btree :: btree( )
{
root = NULL ;
}
// build tree by calling insert( )
void btree :: buildtree ( int num )
{
insert(root, num ) ;
}
// inserts a new node in a binary search tree
btreenode* btree :: insert ( btreenode *sr, int num )
{
if ( sr == NULL )
{
sr = new btreenode ;
if(root == NULL)
root = sr;
sr->leftchild = NULL ;
sr->data = num ;
sr->rightchild = NULL ;
}
else // search the node to which new node will be attached
{
// if new data is less, traverse to left
if ( num < sr->data )
sr->leftchild=insert(sr->leftchild, num) ;
else
// else traverse to right
sr->rightchild=insert(sr->rightchild, num ) ;
}
return sr;
}
// traverses btree
void btree :: traverse( )
{
cout << "\n\nIn-order Traversal: " ;
inorder ( root ) ;

cout << "\n\nPre-order Traversal: " ;


preorder ( root ) ;

cout << "\n\nPost-order Traversal: " ;


postorder ( root ) ;
}
// traverse a binary search tree in a LDR (Left-Data-Right) fashion
void btree :: inorder ( btreenode *sr )
{
if ( sr != NULL )
{
inorder ( sr -> leftchild ) ;
// print the data of the node whose
// leftchild is NULL or the path
// has already been traversed
cout << "\t" << sr -> data ;
inorder ( sr -> rightchild ) ;
}
else
return ;
}
// traverse a binary search tree in a DLR (Data-Left-right) fashion
void btree :: preorder ( btreenode *sr )
{
if ( sr != NULL )
{
// print the data of a node
cout << "\t" << sr -> data ;
// traverse till leftchild is not NULL
preorder ( sr -> leftchild ) ;
// traverse till rightchild is not NULL
preorder ( sr -> rightchild ) ;
}
else
return ;
}
// traverse a binary search tree in LRD (Left-Right-Data) fashion
void btree :: postorder ( btreenode *sr )
{
if ( sr != NULL )
{
postorder ( sr -> leftchild ) ;
postorder ( sr -> rightchild ) ;
cout << "\t" << sr -> data ;
}
else
return ;
}
// calls rem to delete node
void btree :: remove ( int num )
{
rem ( root, num ) ;
}
// deletes a node from the binary search tree
btreenode* btree :: rem ( btreenode *sr, int num )
{
btreenode *x;
// if tree is empty
if ( sr == NULL )
{
// cout << "\nTree is empty" ;
return NULL;
}
if(num < sr->data) // Traverse towards left
sr->leftchild = rem(sr->leftchild, num);
else if(num > sr->data) // Traverse towards right
sr->rightchild = rem(sr->rightchild, num);
// Found element to be deleted
else if(sr->leftchild && sr->rightchild)
{
// Replace with the smallest data
x = FindMin(sr->rightchild);
sr->data = x->data;
sr->rightchild = rem(sr->rightchild, sr->data);
}
else // One or Zero children
{
x = sr;
if(sr->leftchild == NULL)
sr = sr->rightchild;
else if(sr->rightchild == NULL)
sr = sr->leftchild;
if(x==root)
root = NULL;
delete x;
}
return sr;
}
// returns whether the node is found or not
btreenode* btree :: Find ( btreenode *sr, int num)
{
// if tree is empty
if ( sr == NULL )
return NULL;
if( num < sr->data)
return Find(sr->leftchild, num);
else if(num > sr->data)
return Find(sr->rightchild, num);
else
return sr;
}
// Finds minimum from the Tree
btreenode* btree :: FindMin( btreenode *sr)
{
// if tree is empty
if ( sr == NULL )
{
cout << "\nTree is empty" ;
return NULL;
}
else if(sr->leftchild == NULL)
return sr;
else
return FindMin(sr->leftchild);
}
// Finds maximum from the Tree
btreenode* btree :: FindMax( btreenode *sr)
{
// if tree is empty
if ( sr == NULL )
{
cout << "\nTree is empty" ;
return NULL;
}
else if(sr->rightchild == NULL)
return sr;
else
return FindMax(sr->rightchild);
}
// calls del to deallocate memory
btree :: ~btree( )
{
del ( root ) ;
}
// deletes nodes of a binary tree
void btree :: del ( btreenode *sr )
{
if ( sr != NULL )
{
del ( sr->leftchild ) ;
del ( sr->rightchild ) ;
}
delete sr ;
}
void main( )
{
btree bt ;
int numtot, num, ch;
btreenode *btn;
do
{
clrscr();
cout << "\n\tBINARY TREE";
cout << "\n\t-----------";
cout << "\n\n1. Insert";
cout << "\n\n2. Delete";
cout << "\n\n3. Traversal";
cout << "\n\n4. Search";
cout << "\n\n5. Find Minimum / Maximum";
cout << "\n\n6. Exit\n";
cout << "\nEnter your choice : ";
cin >> ch;
switch(ch)
{

case 1:
cout << "\n\nEnter Number of items to be inserted: " ;
cin >> numtot ;
cout << "\n\nEnter the data... " ;
for(int i=0; i< numtot; i++)
{
cin >> num ;
bt.buildtree ( num ) ;
}
break;
case 2:
bt.inorder(root);
cout << "\n\nEnter item to be deleted : ";
cin >> num ;
btn = bt.rem(root, num);
if(btn == NULL)
cout << num << " is not found in the Tree";
else
cout << num << " is found in the Tree";
bt.inorder(root);
break;
case 3:
bt.traverse();
break;
case 4:
bt.inorder(root);
cout << "\n\nEnter item to be searched : ";
cin >> num ;
btn = bt.Find(root, num);
if(btn == NULL)
cout << num << " is not found in the Tree";
else
cout << num << " is found in the Tree";
break;
case 5:
bt.inorder(root);
btn = bt.FindMin(root);
cout << "\n\nMinimum : " << btn->data;
btn = bt.FindMax(root);
cout << "\n\nMaximum : " << btn->data;
break;
default :
break;
}
getch();
}while(ch < 6);
}
RESULT:

Thus the C++ program for binary search tree was Compiled and Executed Successfully
Ex no:10 QUICK SORT
Date:

AIM:
To write a C++ Program for sorting a given set of numbers using Quick sort

ALGORITHM :
1. Read no of elements, n
2. Use getdata() member function to get the member values of the class.
i=0
Repeat until i < n
begin
Read, a[i]
i=i+1
end
3. Use the sort member function to sort the elements with the parameters, left and right
position
begin
if(left < right)
{
i=left+1;
j=right;
pivot=left;
static int ctr=1;
do
{
while(a[pivot]>=a[i])
i++;
while(a[pivot]<a[j])
j--;
if(i<j && a[pivot]<a[i] && a[pivot]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
cout << "\n" << ctr << "\t";
for(k=0;k<n;k++)
cout << a[k] << "\t";
cout << endl;
ctr++;
}
else
break;
} while(1);
temp=a[pivot];
a[pivot]=a[j];
a[j]=temp;
cout << "\n" << ctr << "\t";
for(k=0;k<n;k++)
cout << a[k] << "\t";
cout << endl;
ctr++;
sort(left, j-1);
sort(j+1, right);
}
return;
end
4. Use display() member function to display the member values of the class.
Procedure display
begin
i=0
Repeat until i < n
begin
Display a[i]
i=i+1
end
end
5. Quit the program

PROGRAM :
// Quick sort Program
#include <iostream.h>
#include <conio.h>
// Quick sort Class
class quicksort
{
public:
int a[50], b[50], i,j,t,temp, temp1[50];
int n;
void getdata();
void sort(int left, int right);
void display();
};
// Getting the data for the array
void quicksort::getdata()
{
cout<<"\nEnter the no. of elements \n";
cin>>n;
cout<<"\nEnter the elements \n";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
}
// Printing the array elements in sorted order
void quicksort::display()
{
for(i=0;i<n;i++)
cout << a[i] << " ";
}
// Sorting of the Array - Quick Sort
void quicksort::sort(int left, int right)
{
int i, j, pivot, temp, k;
if(left < right)
{
i=left+1;
j=right;
pivot=left;
static int ctr=1;
do
{
while(a[pivot]>=a[i])
i++;
while(a[pivot]<a[j])
j--;
if(i<j && a[pivot]<a[i] && a[pivot]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
cout << "\n" << ctr << "\t";
for(k=0;k<n;k++)
cout << a[k] << "\t";
cout << endl;
ctr++;
}
else
break;
} while(1);
temp=a[pivot];
a[pivot]=a[j];
a[j]=temp;
cout << "\n" << ctr << "\t";
for(k=0;k<n;k++)
cout << a[k] << "\t";
cout << endl;
ctr++;
sort(left, j-1);
sort(j+1, right);
}
return;
}
// The actual call in the main program
void main()
{
quicksort srt;
clrscr();
srt.getdata();
cout << "\n\nThe given Array is....";
srt.display();
cout << "\n\nPasses of the Quick Sort\n";
srt.sort(0, srt.n-1);

cout << "\n\nSorted array...\n\n";


srt.display();
getch();
}

RESULT:
Thus the C++ Program for sorting a given set of numbers using Quick Sort was Compiled
and Executed Successfully.

Anda mungkin juga menyukai