EC2209 Manual
EC2209 Manual
Date:
Aim:
To write a C++ program to implement the basic concepts of Class and Objects.
Algorithm:
1) Start the program.
2) Define a class stud with the needed data members.
3) Define the function input () o read the value such as rno, name, and 3marks.
4) Define the function print () to calculate the total marks, and average.
5) Display the calculated values with the rno and name.
6) In main (), create an object for the class stud.
7) Call the function input () with the object.
8) Call the function print () with the object.
9) Stop the program.
avg=tot/3;
cout<<"\n TOTAL = "<<tot<<endl;
cout<<"\n AVERAGE = "<<avg<<endl;
}
else
{
cout<<" FAIL "<<endl;
}
}
};
void main()
{
clrscr();
stud s;
s.input();
s.print();
getch();
}
90
98
78
RESULT = PASS
TOTAL = 266
AVERAGE = 88
10
60
90
NAME: RAVI.S
REGISTER NUMBER: 4321
THREE SUBJECT MARKS:
RESULT = FAIL
10
60
90
Result:
Thus the C++ program for implementing the concept of Classes and Objects has
been executed successfully and the output was verified.
Ex No: 1(B)
Date:
Aim:
To write a C++ program to implement the basic concepts of Constructor and
Destructor.
Algorithm:
1) Start the program.
2) Define a class prime.
3) Define the Constructor under public section, in that read the input.
4) Depend on the input check the given input is prime or not.
5) Define the Destructor to de-allocate the memory for the data members.
6) In main (), create an object for the class prime.
7) Once the object is created, the constructor invoked automatically.
8) Destructor gets invoked at the termination of the program.
9) Stop the program.
};
void main()
{
clrscr();
prime p;
getch();
}
ENTER A NUMBER: 8
ENTER A NUMBER: 5
Result:
Thus the C++ program for implementing the concepts of Constructor and
Destructor has been executed successfully and the output was verified.
Ex No: 2
Date:
Array Implementation
Aim:
To write a C++ program to implement the basic concepts of an Array.
Algorithm:
1) Start the program.
2) Define a class array and declare the needed variables.
3) Define the function arrayin (), read the value for n and read the elements for the
array.
4) Define the function print (), to display the array elements.
5) Define the function insert (), insert the element in the array at the given location.
6) Define the function del (), to delete the element in the given location.
7) In main (), create an object for the class.
8) Call the function arrayin () with the object.
9) Display the menus such as insert, delete and display option inside the switch case.
10) Read the choice, depend on the choice, the case gets execute and
the respective function is invoked.
11) Stop the program.
void main()
{
array k;
int ch;
clrscr();
k.arrayin();
cout<<"\n\n\t\t INDEX \n"<<endl;
cout<<"\n 1.ARRAY ELEMENT INSERTION \n"<<endl;
cout<<"\n 2.ARRAY ELEMENT DELETION \n"<<endl;
cout<<"\n 3.exit \n"<<endl;
cout<<"\n ENTER YOUR CHOICE : ";
cin>>ch;
switch (ch)
{
case 1:
k.insert();
break;
case 2:
k.del();
break;
case 3:
exit(0);
default: cout<<"\n INVALID CHOICE "<<endl;
}
getch();
}
INDEX
1. ARRAY ELEMENT INSERTION
2. ARRAY ELEMENT DELETION
3. EXIT
ENTER YOUR CHOICE: 1
ENTER THE POSITION TO INSERT: 2
ENTER THE ELEMENT TO INSERT: 25
THE NEW ARRAY ELEMENTS AFTER INSERTION:
10
25
20
30
40
50
ARRAY IMPLEMENTATION
ENTER THE ARRAY SIZE: 5
ENTER THE ARRAY ELEMENTS ONE BY ONE:
10
20
30
40
50
INDEX
1. ARRAY ELEMENT INSERTION
2. ARRAY ELEMENT DELETION
3. EXIT
ENTER YOUR CHOICE: 2
ENTER THE POSITION TO DELETE: 2
THE NEW ARRAY ELEMENTS AFTER DELETION: 10
30
40
50
Result:
Thus the C++ program for implementing the concept of Array has been executed
successfully and the output was verified.
Ex No: 3
Date:
Aim:
To write a C++ program to implement all the operations of Linked List.
Algorithm:
1) Start the program.
2) Define the list using structure.
3) In main (), display the menus such as Insert, delete, and display.
4) Read the choice, depend on the choice, the case gets execute and it invokes the
respective function.
5) To insert an element, we can have the following:
6) Define the function insempty (), to insert an element in the empty list.
7) Define the function insbegin (), to insert an element at the beginning of the list.
8) Define the function insmiddle (), to insert an element at the middle of the list.
9) Define function insend (), to insert an element at the end of the list.
10) To delete an element, we can have the following:
11) Define the function delsingle (), to delete the single node element.
12) Define the function delbegin (), to delete the element at the
beginning of the list.
13) Define the function delmiddle (), to delete the element at the
middle of the list.
14) Define the function delend (), to delete the element at the end of
the list.
15) Define the function display (), to display the elements in the list.
16) Stop the program.
getch();
}
void insempty()
{
if(start!=NULL)
cout<<"\n LIST IS NOT EMPTY ";
else
{
struct list*node;
node=new list;
cout<<"\n ENTER THE VALUE : ";
cin>>node->data;
node->next=NULL;
start=node;
}
}
void insbegin()
{
if(start==NULL)
cout<<"\n LIST IS EMPTY SO PLEASE INSERT AT EMPTY ";
else
{
struct list*node;
node=new list;
cout<<"\n ENTER THE VALUE : ";
cin>>node->data;
node->next=start;
start=node;
}
}
void insmiddle()
{
int item;
if(start->next==NULL)
cout<<"\n IT IS NOT POSSIBLE TO INSERT AN ELEMENT AT MIDDLE ";
else
{
struct list *node,*pre,*ptr,*temp;
node=new list;
cout<<"\n ENTER THE VALUE : ";
cin>>node->data;
cout<<"\n ENTER THE ELEMENT BEFORE INSERTION : ";
cin>>item;
pre=start;
while(pre->data!=item)
{
temp=pre;
pre=pre->next;
}
temp->next=node;
node->next=pre;
}
}
void insend()
{
if (start==NULL)
cout<<"\n LIST IS EMPTY ";
else
{
INSERT OPERATIONS
1. INSERT AT EMPTY
2. INSERT AT BEGIN
3. INSERT AT MIDDLE
4. INSERT AT END
ENTER YOUR CHOICE: 1
ENTER THE VALUE: 10
INSERT OPERATIONS
1. INSERT AT EMPTY
2. INSERT AT BEGIN
3. INSERT AT MIDDLE
4. INSERT AT END
ENTER YOUR CHOICE: 2
ENTER THE VALUE: 20
20
10
INSERT OPERATIONS
1. INSERT AT EMPTY
2. INSERT AT BEGIN
3. INSERT AT MIDDLE
4. INSERT AT END
ENTER YOUR CHOICE: 3
ENTER THE VALUE: 30
ENTER ELEMENT BEFORE INSERTION: 10
4. EXIT
ENTER YOUR CHOICE: 3
LIST HAVING THE FOLLOWING ELEMENTS: 20
30
10
INSERT OPERATIONS
1. INSERT AT EMPTY
2. INSERT AT BEGIN
3. INSERT AT MIDDLE
4. INSERT AT END
ENTER YOUR CHOICE: 4
ENTER THE VALUE: 40
20
30
10
40
DELETE OPERATIONS
1. SINGLE NODE DELETION
2. DELETE AT BEGIN
3. DELETE AT MIDDLE
4. DELETE AT END
ENTER YOUR CHOICE: 1
LIST HAVING MORE THAN ONE NODE
DELETE OPERATIONS
1. SINGLE NODE DELETION
2. DELETE AT BEGIN
3. DELETE AT MIDDLE
4. DELETE AT END
30
10
40
DELETE OPERATIONS
1. SINGLE NODE DELETION
2. DELETE AT BEGIN
3. DELETE AT MIDDLE
4. DELETE AT END
ENTER YOUR CHOICE: 3
ENTER THE ELEMENT TO DELETE: 10
1. INSERT AN ELEMENT
2. DELETE AN ELEMENT
3. DISPLAY AN ELEMENT
4. EXIT
ENTER YOUR CHOICE: 3
NOW, LIST HAVING THE FOLLOWING ELEMENTS:
30
40
DELETE OPERATIONS
1. SINGLE NODE DELETION
2. DELETE AT BEGIN
3. DELETE AT MIDDLE
4. DELETE AT END
ENTER YOUR CHOICE: 4
DELETED ELEMENT IS: 30
DELETE OPERATIONS
1. SINGLE NODE DELETION
2. DELETE AT BEGIN
3. DELETE AT MIDDLE
4. DELETE AT END
ENTER YOUR CHOICE: 4
IT IS NOT POSSIBLE TO DELETE AT END
1. INSERT AN ELEMENT
2. DELETE AN ELEMENT
3. DISPLAY AN ELEMENT
4. EXIT
ENTER YOUR CHOICE: 2
DELETE OPERATIONS
LIST OPERATIONS
1. INSERT AN ELEMENT
2. DELETE AN ELEMENT
3. DISPLAY AN ELEMENT
4. EXIT
ENTER YOUR CHOICE: 3
LIST IS EMPTY
1. INSERT AN ELEMENT
2. DELETE AN ELEMENT
3. DISPLAY AN ELEMENT
4. EXIT
ENTER YOUR CHOICE: 2
DELETE OPERATIONS
LIST OPERATIONS
1. INSERT AN ELEMENT
2. DELETE AN ELEMENT
3. DISPLAY AN ELEMENT
4. EXIT
ENTER YOUR CHOICE: 4
Result:
Thus the C++ program for implementing the Linked List Operations has been
executed successfully and the output was verified.
Ex No: 4
Date:
Aim:
To write a C++ program to implement the Cursor concepts for LIST ADT.
Algorithm:
1) Start the program.
2) Display the option such as Insert, Delete, Display, Search and Exit.
3) Depend on the choice, case gets execute and the respective function is called.
4) CASE 1: read the total number of elements and read the elements
In the array.
5) CASE 2: insert an element at the desired location.
6) CASE 3: delete an element from the specified location.
7) CASE 4: display the elements present in the array.
8) Stop the program.
case 2:
cout<<"\n ENTER THE INSERT POSITION : ";
cin>>i;
for(j=n;j>i-1;j--)
a[j]=a[j-1];
cout<<"\n ENTER THE INSERT VALUE : ";
cin>>a[i];
n++;
break;
case 3:
if(n<0)
cout<<"\n THE LIST IS EMPTY ";
else
{
cout<<"\n ENTER THE DELETING POSITION : ";
cin>>j;
cout<<"\n THE DELETED ELEMENT IS : "<<a[j];
for(i=j-1;i<n;i++)
a[i]=a[i+1];
n--;
}
getch();
break;
case 4:
for(i=1;i<n;i++)
cout<<a[i]<<"\t";
getch();
break;
case 5:
if(n<0)
4. DISPLAY
5. SEARCH
6. EXIT
ENTER YOUR CHOICE: 4
10
20
30
40
50
4. DISPLAY
5. SEARCH
6. EXIT
ENTER YOUR CHOICE: 4
5
10
20
30
40
50
3. DELETION
4. DISPLAY
5. SEARCH
6. EXIT
ENTER YOUR CHOICE: 4
10
20
30
40
50
2. INSERTION
3. DELETION
4. DISPLAY
5. SEARCH
6. EXIT
ENTER YOUR CHOICE: 5
ENTER THE SEARCHING ELEMENT: 20
THE GIVEN ELEMENT FOUND IN THE POSITION 1
1. CREATION
2. INSERTION
3. DELETION
4. DISPLAY
5. SEARCH
6. EXIT
ENTER YOUR CHOICE: 6
Result:
Thus the C++ program for implementing the Cursor concepts for LIST ADT Operations
has been executed successfully and the output was verified.
Ex No: 5
Date:
Aim:
To write a C++ program to implement the STACK ADT using array implementations.
Algorithm:
1) Start the program.
2) In main (), display the options such as Push, Pop, and Display.
3) Read the choice, depend on the choice the case gets execute and the respective
function gets invoked.
4) Define the function push (), to insert a new element into the stack.
5) Define the function pop (), to delete an element from the stack.
6) Define the function display (), to display the elements in the stack.
7) Stop the program.
break;
case 4:
exit(0);
}
}
while(ch<4);
}
void push()
{
int no;
if(top==size-1)
cout<<"\n STACK IS FULL "<<endl;
else
{
cout<<"\n ENTER THE NUMBER : ";
cin>>no;
top++;
s[top]=no;
}
}
void pop()
{
int no;
if(top==-1)
cout<<"\n STACK IS EMPTY "<<endl;
else
{
no=s[top];
cout<<"\n ELEMENT "<<no<<" IS POPPED OUT FROM THE STACK "<<endl;
top--;
}
}
void display()
{
int i;
if(top==-1)
{
cout<<"\n STACK IS EMPTY "<<endl;
return;
}
cout<<"\n THE ELEMENTS IN STACK ARE : "<<endl;
for(i=top;i>=0;i--)
{
cout<<s[i]<<endl;
}
}
2. POP
3. DISPLAY
4. EXIT
ENTER UR CHOICE: 1
ENTER THE NUMBER: 30
2. POP
3. DISPLAY
4. EXIT
ENTER UR CHOICE: 3
THE ELEMENTS IN STACK ARE:
10
Result:
Thus the C++ program for implementing the STACK ADT operations using Array
has been executed successfully and the output was verified.
Ex No: 6
Date:
Aim:
To write a C++ program to implement the STACK ADT using linked list.
Algorithm:
1) Start the program.
2) In main (), display the options such as Push, Pop, and Display.
3) Read the choice, depend on the choice the case gets execute and the respective
function gets invoked.
4) Define the function sadd (), to insert a new element into the stack.
5) Define the function sdelete (), to delete an element from the stack.
6) Define the function sprint (), to display the elements in the stack.
7) Stop the program.
sadd();
break;
case 2:
sdelete();
break;
case 3:
sprint();
break;
case 4:
exit(0);
}
if(ch<=3)
goto W;
getch();
}
void sadd()
{
if(top==n)
{
cout<<"\n STACK IS FULL \n";
}
else
{
if(start==NULL)
{
struct list * node;
node=new list;
cout<<"\n ENTER THE VALUE TO PUSH : ";
cin>>node->data;
node->next=NULL;
start=node;
top++;
}
else
{
struct list * node;
node=new list;
cout<<"\n ENTER THE VALUE TO PUSH : ";
cin>>node->data;
node->next=start;
start=node;
top++;
}
}
}
void sprint()
{
struct list*ptr;
if(start==NULL)
cout<<"\n STACK IS EMPTY ";
else
{
cout<<"\n STACK HAVING THE FOLLOWING ELEMENTS : ";
for(ptr=start;(ptr);ptr=ptr->next)
cout<<ptr->data<<"\n";
getch();
}
}
void sdelete()
{
if(top<=0)
{
cout<<"\n STACK IS EMPTY ";
}
else
{
if(start->next!=NULL)
{
cout<<"\n DELETED ELEMENT IS : "<<start->data;
start=start->next;
}
else
{
cout<<"\n DELETED ELEMENT IS : "<<start->data;
start=NULL;
}
}
top--;
}
STACK OPERATIONS
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 1
ENTER THE VALUE TO PUSH: 10
STACK OPERATIONS
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 1
ENTER THE VALUE TO PUSH: 20
STACK OPERATIONS
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 1
STACK OPERATIONS
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 1
ENTER THE VALUE TO PUSH: 40
STACK OPERATIONS
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 1
ENTER THE VALUE TO PUSH: 50
STACK OPERATIONS
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 1
STACK IS FULL
STACK OPERATIONS
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 3
STACK HAVING THE FOLLOWING ELEMENTS:
50
40
30
20
10
STACK OPERATIONS
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 2
DELETED ELEMENT IS: 50
STACK OPERATIONS
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 3
STACK HAVING THE FOLLOWING ELEMENTS:
40
30
20
10
STACK OPERATIONS
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 2
DELETED ELEMENT IS: 40
STACK OPERATIONS
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 3
STACK HAVING THE FOLLOWING ELEMENTS:
30
20
10
STACK OPERATIONS
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 2
STACK OPERATIONS
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 3
STACK HAVING THE FOLLOWING ELEMENTS:
20
10
STACK OPERATIONS
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 2
DELETED ELEMENT IS: 20
STACK OPERATIONS
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 3
STACK HAVING THE FOLLOWING ELEMENTS:
STACK OPERATIONS
10
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 2
DELETED ELEMENT IS: 10
STACK OPERATIONS
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 3
STACK IS EMPTY
STACK OPERATIONS
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 2
STACK IS EMPTY
STACK OPERATIONS
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 4
Result:
Thus the C++ program for implementing the STACK ADT operations using Linked
List has been executed successfully and the output was verified.
Ex No: 7
Date:
Aim:
To write a C++ program to convert Infix to postfix expression using STACK Application.
Algorithm:
1) Start the program.
2) Define the class stack and declare the needed variables.
3) Define the constructor to initialize the data members.
4) Define the function get () to read the equation.
5) Define the function post () and prcd () to convert the Infix to Postfix conversion.
6) In main (), declare an object for the class.
7) Call the function get () and post () and prcd () with the object.
8) Stop the program.
while(st[x]!='\0')
{
if((st[x]>='A'&&st[x]<'Z')||(st[x]>='a'&&st[x]<='z'))
ch[y++]=st[x];
else if(st[x]==')')
{
while(eq[z]!=')')
ch[y++]=eq[z--];
top=eq[--z];
}
else
{
while(z>=0&&prcd(top,st[x]==1))
{
ch[y++]=top;
top=eq[--z];
}
eq[++z]=st[x];
top=st[x];
}
x++;
}
while(z>=0)
{
if(eq[z]=='(')
z--;
else
ch[y++]=eq[z--];
}
ch[y]='\0';
Result:
Thus the C++ program to convert Infix to postfix expression using STACK
Application has been executed successfully and the output was verified.
Ex No: 8
Date:
Aim:
To write a C++ program to implement all QUEUE operations using Arrays.
Algorithm:
1) Start the program.
2) In main () display the option such as Add, Delete, and Print.
3) Read the choice, depend on the choice the case gets execute and the respective
function gets invoked.
4) Define the function qadd (), to insert an element in the queue.
5) Define the function qdel (), to delete an element from the queue.
6) Define the function qprint (), to display he elements of queue.
7) Stop the program.
break;
case 3:
qprint();
break;
case 4:
exit(0);
}
if(ch<=3)
goto W;
}
void qadd()
{
if(rear>=n)
{
cout<<"\n QUEUE IS FULL \n";
}
else
{
cout<<"\n ENTER THE ELEMENT : ";
cin>>queue[rear];
rear++;
}
}
void qprint()
{
int i;
clrscr();
if(rear==front)
{
cout<<"\n QUEUE IS EMPTY \n";
}
else
{
cout<<"\n QUEUE HAVING THE FOLLOWING ELEMENTS : \n";
for(i=front;i<rear;i++)
cout<<"\n ELEMENT IN THE POSITION "<<i<<" IS "<<queue[i]<<endl;
}
getch();
}
void qdelete()
{
if(rear==front)
{
cout<<"\n QUEUE IS EMEPTY \n";
getch();
}
else
{
cout<<"\n DELETED ELEMENT IN THE QUEUE IS : "<<queue[front];
front++;
}
}
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 1
ENTER THE ELEMENT: 10
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 1
ENTER THE ELEMENT: 20
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 1
ENTER THE ELEMENT: 30
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 1
ENTER THE ELEMENT: 40
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 1
ENTER THE ELEMENT: 50
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 1
QUEUE IS FULL
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 3
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 2
DELETED ELEMENT IN THE QUEUE IS: 10
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 3
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 3
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 2
DELETED ELEMENT IN THE QUEUE IS: 30
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 3
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 2
DELETED ELEMENT IN THE QUEUE IS: 40
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 3
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 2
DELETED ELEMENT IN THE QUEUE IS: 50
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 3
QUEUE IS EMPTY
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 2
QUEUE IS EMEPTY
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 4
Result:
Thus the C++ program for implementing the QUEUE operations using array has
been executed successfully and the output was verified.
Ex No: 9
Date:
Aim:
To write a C++ program to implement all QUEUE operations using Linked List.
Algorithm:
1) Start the program.
2) Define the list using structure.
3) In main (), display the option such as Add, Delete, and Print.
4) Read the choice, depend on the choice the case gets execute and
The respective function gets invoked.
5) Define the function qadd (), to insert an element in the queue.
6) Define the function qdel(), to delete an element from the queue.
7) Define the function qprint (), to display the elements of queue.
8) Stop the program.
qadd();
break;
case 2:
qdelete();
break;
case 3:
print();
break;
case 4:
exit(0);
}
if(ch<=3)
goto W;
}
void qadd()
{
if(rear==n)
{
cout<<"\n QUEUE IS FULL\n";
}
else
{
if(start==NULL)
{
struct list*node;
node=new list;
cout<<"\n ENTER THE VALUE TO INSERT : ";
cin>>node->data;
node->next=NULL;
start=node;
rear=rear+1;
}
else
{
struct list*node,*pre,*ptr;
node=new list;
cout<<"\n ENTER THE VALUE TO INSERT : ";
cin>>node->data;
for(ptr=start;(ptr);ptr=ptr->next)
{
pre=ptr;
}
pre->next=node;
node->next=NULL;
rear=rear+1;
}
}
}
void print()
{
int i;
clrscr();
if(rear==front)
{
cout<<"\n QUEUE IS EMPTY ";
}
else
{
struct list*ptr;
cout<<"\n QUEUE HAVING THE FOLLOWING ELEMENTS : ";
for(ptr=start;(ptr);ptr=ptr->next)
cout<<ptr->data<<"\n";
}
}
void qdelete()
{
if(rear==front)
{
cout<<"\n QUEUE IS EMPTY ";
getch();
}
else
{
if(start->next==NULL)
{
cout<<"\n DELETED ELEMENT IS : "<<start->data;
start=NULL;
front=front+1;
}
else
{
cout<<"\n DELETED ELEMENT IS : "<<start->data;
start=start->next;
front=front+1;
}
}
}
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 1
ENTER THE VALUE TO INSERT: 10
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 1
ENTER THE VALUE TO INSERT: 20
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 1
ENTER THE VALUE TO INSERT: 30
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 1
ENTER THE VALUE TO INSERT: 40
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 1
ENTER THE VALUE TO INSERT: 50
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 1
QUEUE IS FULL
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 3
QUEUE HAVING THE FOLLOWING ELEMENTS:
10
20
30
40
50
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 3
QUEUE HAVING THE FOLLOWING ELEMENTS:
20
30
40
50
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 2
DELETED ELEMENT IS: 20
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 3
QUEUE HAVING THE FOLLOWING ELEMENTS:
30
40
50
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 2
DELETED ELEMENT IS: 30
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 3
QUEUE HAVING THE FOLLOWING ELEMENTS:
40
50
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 2
DELETED ELEMENT IS: 40
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 3
QUEUE HAVING THE FOLLOWING ELEMENTS: 50
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 2
DELETED ELEMENT IS: 50
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 3
QUEUE IS EMPTY
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 1
QUEUE IS FULL
QUEUE OPERATIONS
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE: 4
Result:
Thus the C++ program for implementing the QUEUE operations using Linked List
has been executed successfully and the output was verified.
Ex No: 10
Date:
Aim:
To write a C++ program to implement a Binary Search Tree.
Algorithm:
1) Start the program.
2) Define the node using structure.
3) Display the choice such as Insert, Pre-order, Post-order and In-order.
4) Depend on the choice, the case gets execute and respective function gets invoked.
5) Define the function insert (), to insert an element in the binary tree.
6) Define the function preorder (), to perform preorder traversal.
7) Define the function inorder (), to perform inorder traversal.
8) Define the function postorder (), to perform postorder traversal.
9) Stop the program.
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
count++;
}
else
if(count%2==0)
tree->left=insert(tree->left,ele);
else
tree->right=insert(tree->right,ele);
return(tree);
}
void preorder(node*tree)
{
if(tree!=NULL)
{
cout<<tree->data;
preorder(tree->left);
preorder(tree->right);
getch();
}
}
void inorder(node*tree)
{
if(tree!=NULL)
{
inorder(tree->left);
cout<<tree->data;
inorder(tree->right);
getch();
}
}
void postorder(node*tree)
{
if(tree!=NULL)
{
postorder(tree->left);
postorder(tree->right);
cout<<tree->data;
getch();
}
}
10
20
30
20
10
30
20
30
10
Result:
Thus the C++ program for implementing the Binary Search Tree has been
executed successfully and the output was verified.
Ex No: 11
Date:
Heap Sort
Aim:
To write a C++ program to sort a given numbers using Heap Sort.
Algorithm:
1) Start the program.
2) In main (), read the elements in the array.
3) Call the function fnSortHeap(), to sort the given numbers in an ascending order.
4) Display the elements in the sorted order.
5) Stop the program.
{
int i,o;
int lchild,rchild,mchild,root,temp;
//FIND THE ROOT ELEMENT OF THE CURRENT ELEMENT
root=(arr_ubound-1)/2;
//CREATING THE HEAP
for(o=root;o>=0;o--)
{
for(i=root;i>=0;i--)
{
lchild=(2*i)+1;
rchild=(2*i)+2;
if((lchild<=arr_ubound)&&(rchild<=arr_ubound))
{
if(arr[rchild]>=arr[lchild])
mchild=rchild;
else
mchild=lchild;
}
else
{
if(rchild>arr_ubound)
mchild=lchild;
else
mchild=rchild;
}
if(arr[i]<arr[mchild]) // SWAP ELEMENTS
{
temp=arr[i];
arr[i]=arr[mchild];
arr[mchild]=temp;
}
}
}
temp=arr[0]; //MOVE THE MAXIMUM ELEMENT TO THE END OF THE
ARRAY
arr[0]=arr[arr_ubound];
arr[arr_ubound]=temp;
return;
}
HEAP SORT
Result:
Thus the C++ program for sorting a given numbers using Heap Sort has been
executed successfully and the output was verified.
Ex No: 12
Date:
Quick Sort
Aim:
To write a C++ program to sort a given numbers using Quick Sort.
Algorithm:
1) Start the program.
2) In main (), read the elements in the array.
3) Display the elements in the unsorted manner.
4) Define the function partition () and Quick Sort (), to sort the given numbers in an
ascending order.
5) Display the elements in the sorted order.
6) Stop the program.
int piv_index,i;
if(low<high)
{
piv_index=partition(low,high,arr);
quick_sort(low,piv_index-1,arr);
quick_sort(piv_index+1,high,arr);
}
}
QUICK SORT
2
45
1
897
6
4
45
897
45
897
Result:
Thus the C++ program for sorting a given numbers using Quick Sort has been
executed successfully and the output was verified.