C++ Lab
C++ Lab
Write a c++ program to perform insertion and deletion operation using arrays:
#include<iostream.h>
int a[20],b[20],c[40];
int m,n,p,val,i,j,key,pos,temp;
//Function Prototype//
void display();
void insert();
void del();
int main()
int choice;
cin>>n;
for (i=0;i<n;i++)
cin>>a[i];
do {
cout<<"\n\n--------Menu-----------\n";
cout<<"1.Insert\n";
cout<<"2.Delete\n";
cout<<"3.Exit\n";
cout<<"-----------------------";
cin>>choice;
switch (choice)
case 1: insert();
break;
case 2: del();
break;
case 3:break;
} while (choice!=3);
return 0;
int i;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
}//end of display()
void insert()//inserting an element in to an array
cin>>pos;
cin>>val;
a[i+1]=a[i];
a[pos-1]=val;
n=n+1;
display();
}//end of insert()
cin>> pos;
val= a [pos];
a[i]=a[i+1];
n=n-1;
display();
OUTPUT:
--------Menu-----------
1.Insert
2.Delete
3.Exit
-----------------------
--------Menu-----------
1.Insert
2.Delete
3.Exit
-----------------------
542
--------Menu-----------
1.Insert
2.Delete
3.Exit
-----------------------
2.Write a C++ program to convert infix arithmetic expression to post fix expression.
#include<iostream>
#include<stack>
bool isOperator(char c)
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='^')
return true;
else
return false;
int precedence(char c)
if(c == '^')
return 3;
return 2;
else
return -1;
string postfix;
for(int i=0;i<infix.length();i++)
postfix+=infix[i];
s.push(infix[i]);
char temp=s.top();
postfix+=temp;
s.pop();
if(s.top()=='(')
s.pop();
else if(isOperator(infix[i]))
if(s.empty())
s.push(infix[i]);
else
if(precedence(infix[i])>precedence(s.top()))
s.push(infix[i]);
else if((precedence(infix[i])==precedence(s.top()))&&(infix[i]=='^'))
s.push(infix[i]);
}
else
while((!s.empty())&&( precedence(infix[i])<=precedence(s.top())))
postfix+=s.top();
s.pop();
s.push(infix[i]);
while(!s.empty())
postfix+=s.top();
s.pop();
return postfix;
int main()
{
cin>>infix_exp;
return 0;
OUTPUT:
a+b*c+d
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
}
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
top--;
}
void display() {
if(top>=0) {
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
int main() {
cout<<"4) Exit"<<endl;
do {
cin>>ch;
switch(ch) {
case 1: {
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
OUTPUT:
1) Push in stack
3) Display stack
4) Exit
Enter choice:
Enter choice:
Enter choice:
Enter choice:
Enter choice:
Enter choice:
Enter choice:
Enter choice:
Enter choice:
Enter choice:
Stack Underflow
Enter choice:
Exit
4. Write a C++ program to simulate the working of Circular Queue using an array.
#include <iostream>
#define SIZE 5
int A[SIZE];
bool isempty()
return true;
else
return false;
//queue is full
else
front = 0;
rear = (rear+1)%SIZE;
A[rear] = value;
void dequeue ( )
if( isempty() )
cout<<"Queue is empty\n";
else
else
void showfront( )
if( isempty())
cout<<"Queue is empty\n";
else
void displayQueue()
if(isempty())
cout<<"Queue is empty\n";
else
int i;
{
cout<<A[i]<<" ";
}
else
{
i=front;
{
cout<<A[i]<<" ";
i++;
}
i=0;
{
cout<<A[i]<<" ";
i++;
}
}
//Main Function
int main()
{
while( flag == 1)
cin>>choice;
switch (choice)
{
cin>>value;
enqueue(value);
break;
case 2: dequeue();
break;
case 3: showfront();
break;
case 4: displayQueue();
break;
case 5: flag = 0;
break;
}
return 0;
OUTPUT:
Enter Value:
Enter Value:
Enter Value:
567
3
element at front is:5
67
Queue is empty
Queue is empty
Queue is empty
5
5. Write a C++ Program to perform Create and Display operations using Linked List.
#include <iostream>
//Declaring node
struct node{
int data;
};
int main()
int n;
cin>>n;
head=createLinkedList(n);
dispalyLinkedList(head);
return 0;
int i;
for(i=0;i<n;i++)
{
//Creating a node
cin>>newNode->data;
newNode->next=NULL;
to head*/
if(head==NULL)
{
head=newNode;
}
else
{
ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=newNode;
}
}
return head;
if(head==NULL){
}
else{
/*If list has elements then loop through the loop and
ptr=head;
while(ptr!=NULL)
{
cout<<ptr->data;
cout<<" ";
ptr=ptr->next;
}
}
OUTPUT
#include <stdlib.h>
#define MAX 10
int i;
{
if (a[i] == x)
return (i + 1);
}
return -1;
int i, mid;
while (l <= h)
{
mid = (l + h) / 2;
if (a[mid] == x)
l = mid + 1;
else
h = mid - 1;
}
return -1;
int i;
{
cin>>a[i];
}
int main()
while (1)
{
cout<<"\n";
cin>>s;
switch (s)
{
case 1:
cin>>n;
inputArray(a, n);
cin>>x;
break;
case 2:
cin>>n;
inputArray(a, n);
cin>>x;
break;
default:
exit(0);
}
if (pos != -1)
{
cout<<" "<<pos;
}
else
}
return 0;
OUTPUT
Press 3 to exit
20
30
40
Press 3 to exit
Press 3 to exit
3
7.Write a program using templates to sort a list of elements. Give user the option to perform sorting using Insertion sort, Bubble sort or Selection
sort.
#include<iostream.h>
void accept(int Arr[], int s);
void display(int Arr[], int s);
void isort(int Arr[], int s);
void ssort(int Arr[], int s);
void bsort(int Arr[],int s);
int main()
{
int Arr[100],n,choice;
cout<<"Enter Size of Array ";
cin>>n;
do
{
cout<<"\n\nMENU";
cout<<"\n1. Accept elements of array";
cout<<"\n2. Display elements of array";
cout<<"\n3. Sort the array using insertion sort method";
cout<<"\n4. Sort the array using selection sort method";
cout<<"\n5. Sort the array using bubble sort method";
cout<<"\n6. Exit";
cout<<"\n\nEnter your choice 1-6 :";
cin>>choice;
switch(choice)
{
case 1: accept(Arr,n);
break;
case 2: display(Arr,n);
break;
case 3: isort(Arr,n);
break;
case 4: ssort(Arr,n);
break;
case 5: bsort(Arr,n);
break;
case 6: break;
default:cout<<"\nInvalid choice";
}
}while(choice!=6);
return 0;
}
void accept(int Arr[], int s)
{
for(int I=0;I<s;I++)
{
cout<<"Enter element "<<I+1<<":";
cin>>Arr[I];
}
}
void display(int Arr[], int s)
{
cout<<"The elements of the array are:\n";
for(int I=0;I<s;I++)
cout<<Arr[I]<<" ";
}
void isort(int Arr[], int s)
{
int I,J,Temp;
for(I=1;I<s;I++)
{
Temp=Arr[I];
J=I-1;
while((Temp<Arr[J]) && (J>=0))
{
Arr[J+1]=Arr[J];
J--;
}
Arr[J+1]=Temp;
}
}
void ssort(int Arr[], int s)
{
int I,J,Temp,Small;
for(I=0;I<s-1;I++)
{
Small=I;
for(J=I+1;J<s;J++) //finding the smallest element
if(Arr[J]<Arr[Small])
Small=J;
if(Small!=I)
{
Temp=Arr[I]; //Swapping
Arr[I]=Arr[Small];
Arr[Small]=Temp;
}
}
}
void bsort(int Arr[],int s)
{
int I,J,Temp;
for(I=0;I<s-1;I++)
{
for(J=0;J<(s-1-I);J++)
if(Arr[J]>Arr[J+1])
{
Temp=Arr[J]; //swapping
Arr[J]=Arr[J+1];
Arr[J+1]=Temp;
}
}
}
OUTPUT:-
Enter Size of Array 5
MENU
1. Accept elements of array
2. Display elements of array
3. Sort the array using insertion sort method
4. Sort the array using selection sort method
5. Sort the array using bubble sort method
6. Exit
MENU
1. Accept elements of array
2. Display elements of array
3. Sort the array using insertion sort method
4. Sort the array using selection sort method
5. Sort the array using bubble sort method
6. Exit
MENU
1. Accept elements of array
2. Display elements of array
3. Sort the array using insertion sort method
4. Sort the array using selection sort method
5. Sort the array using bubble sort method
6. Exit
PART-B
OUTPUT:
constructor invoked for the class Department
constructor invoked for the class Employee
destructor invoked for the class Employee
destructor invoked for the classs Department
OUTPUT:
45
45.55555556
int main()
int x,y,temp;
int *a,*b;
cin>>x>>y;
a=&x;
b=&y;
cout<<"x = "<<x<<endl;
cout<<"y = "<<y<<endl;
temp=*a;
*a=*b;
*b=temp;
cout<<"x = "<<x<<endl;
cout<<"y = "<<y<<endl;
return 0;
OUTPUT:
enter x & y : 4
before swap :
x=4
y=3
after swap :
x=3
y=4
4.Perform Queue operations using Array implementation.
#include <iostream.h>
void Insert() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cin>>val;
rear++;
queue[rear] = val;
}
void Delete() {
return ;
} else {
front++;;
}
void Display() {
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<queue[i]<<" ";
cout<<endl;
}
}
int main() {
int ch;
cout<<"4) Exit"<<endl;
do {
cin>>ch;
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
}
} while(ch!=4);
return 0;}
OUTPUT:
4) Exit
3
Queue elements are : 2 1
Exit
OUTPUT:
ENTER THE NUMBER OF ELEMENTS IN AN ARRAY
5
ENTER5ELEMENTS OF AN ARRAY TO BE SORTED50
40
30
20
10
SORTED ARRAY
10 20 30 40 50
int main()
{
struct Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
cout << "\nPreorder traversal of binary tree is \n";
printPreorder(root);
cout << "\nInorder traversal of binary tree is \n";
printInorder(root);
cout << "\nPostorder traversal of binary tree is \n";
printPostorder(root);
return 0;
}
Output: