Professional Documents
Culture Documents
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
struct node
{
char name[20];
int age;
node *link;
};
class stack
{
node *top, *ptr;
public:
stack() { top=NULL; }
void stackpush()
{
node *newptr;
newptr=new node;
newptr->link=NULL;
cout<<"\nEnter the name: ";
gets(newptr->name);
cout<<"Enter the age: ";
cin>>newptr->age;
if(top==NULL)
top=newptr;
else
{ ptr=top;
top=newptr;
newptr->link=ptr;
}
cout<<"\nNow the stack is:\n";
display(top);
}
void stackpop()
{
if(top==NULL)
cout<<"\nUnderflow!";
else
{ ptr=top;
top=top->link;
delete ptr;
}
cout<<"\nNow the stack is:\n";
display(top);
}
void display(node *np);
}obj;
void stack::display(node *np)
{
while(np!=NULL)
{ cout<<np->name<<" "<<np->age<<"->";
np=np->link;
}
}
void main()
{
clrscr();
char ch;
int c;
do
{
cout<<"\n\t\t\tINSERTION AND DELETION IN A STACK";
cout<<"\n\t\t\t*********************************";
cout<<"\n1.Insertion\n2.Deletion";
cout<<"\nEnter your choice: ";
cin>>c;
switch(c)
{
case 1: cout<<"\nInsert the element:";
obj.stackpush();
break;
case 2: obj.stackpop();
break;
}
cout<<"\nDo you want to continue?(Y/N): ";
cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}
Output
OUTPUT
REVERSE A STRING
~~~~~~~~~~~~~~~~
Enter the string to be reversed: amrutha
else
{
rear->link=temp;
rear=temp;
}
}
void queue::del()
{
if(front==NULL)
cout<<"UNDERFLOW";
else
{
node *ptr=front;
cout<<front->city<<"Deleted/n";
front=front->link;
delete ptr;
{if(front==NULL)
rear=NULL;
}
}
}
void queue::display()
{
node *temp=front;
while(temp!=NULL)
{
cout<<temp->city<<"->";
temp=temp->link;
}
cout<<"!!!\n";
}
queue::~queue()
{ node *temp;
while(front!=NULL)
{
temp=front;
front=front->link;
delete temp;
}
}
void main()
{
clrscr();
queue qt;
int ch;
while(1)
{
cout<<"Menu:\n1)Insert \n2)Delete \n3)Display \n 4)Exit\n";
cout<<"Enter ur choice";
cin>>ch;
switch(ch)
{
case 1: qt.ins();
break;
case 2: qt.del();
break;
case 3: qt.display();
break;
case 4: exit(0);
default: cout<<"Wrong choice made";
}
}
}
OUTPUT
Menu:
1)Insert
2)Delete
3)Display
4)Exit
Enter ur choice1
Enter city name
chennai
Menu:
1)Insert
2)Delete
3)Display
4)Exit
Enter ur choice1
Enter city name
coimbatore
Menu:
1)Insert
2)Delete
3)Display
4)Exit
Enter ur choice3
chennai->coimbatore->!!!
Menu:
1)Insert
2)Delete
3)Display
4)Exit
Enter ur choice2
chennaiDeleted/nMenu:
1)Insert
2)Delete
3)Display
4)Exit
Enter ur choice3
coimbatore->!!!
Menu:
1)Insert
2)Delete
3)Display
4)Exit
Enter ur choice4
//Circular Queue
#include<iostream.h>
#include<process.h>
#include<conio.h>
int i;
class cqu
{ int cq[5], f, r;
public:
cqu()
{ f=r=-1; }
void ins(int item)
{ if((f==0&&r==4)||(f==r+1))
{ cout<<"\nCircular Queue is full";
return;
}
else if(r==-1)
f=r=0;
else if(r==4)
r=0;
else
r=r+1;
cq[r]=item;
}
void del(int &item)
{ item=cq[f];
if(f==r)
f=r=-1;
else if(f==4)
f=0;
else
f=f+1;
}
void display()
{ if(f==-1)
{ cout<<"\nCircular Queue is empty";
return;
}
if(f<=r)
{ cout<<"\n";
for(i=f;i<=r;i++)
cout<<cq[i]<<" ";
}
if(r<f)
{ cout<<"\n";
for(i=f;i<=4;i++)
cout<<cq[i]<<" ";
for(i=0;i<r;i++)
cout<<cq[i]<<" ";
}
}
};
void main()
{
clrscr();
int ch, item;
cqu cq;
cout<<"\n\t\t\tCIRCULAR QUEUE";
cout<<"\n\t\t\t**************";
do
{ cout<<"\n\nMenu";
cout<<"\n\n--------";
cout<<"\n1. Insertion\n2. Deletion\n3. Exit";
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{ case 1: cout<<"\nEnter item to be inserted: ";
cin>>item;
cq.ins(item);
cq.display();
break;
case 2: cq.del(item);
cout<<"\nItem deleted";
cq.display();
break;
case 3: exit(0);
}
}while(1);
getch();
}
CIRCULAR QUEUE
**************
Menu
--------
1. Insertion
2. Deletion
3. Exit
Enter your choice: 1
34
Menu
--------
1. Insertion
2. Deletion
3. Exit
Enter your choice: 1
34 78
Menu
--------
1. Insertion
2. Deletion
3. Exit
Enter your choice: 1
34 78 90
Menu
--------
1. Insertion
2. Deletion
3. Exit
Enter your choice: 1
Menu
--------
1. Insertion
2. Deletion
3. Exit
Enter your choice: 1
34 78 90 100 150
Menu
--------
1. Insertion
2. Deletion
3. Exit
Enter your choice: 1
Menu
--------
1. Insertion
2. Deletion
3. Exit
Enter your choice: 2
Item deleted
78 90 100 150
Menu
--------
1. Insertion
2. Deletion
3. Exit
Enter your choice: 2
Item deleted
90 100 150
Menu
--------
1. Insertion
2. Deletion
3. Exit
Enter your choice: 3
#include<iostream.h>
#include<conio.h>
#include<iostream.h>
#include<conio.h>
class distance
{
private:
int feet, inches;
public:
void getdata(int f, int i)
{
feet=f;
inches=i;
}
void printit(void)
{
cout<<feet<<"Feet:"<<inches<<"Inches:"<<"\n";
}
distance sum(distance d2);
};
distance distance::sum(distance d2)
{
distance d3;
d3.feet=feet+d2.feet+(inches+d2.inches)/12;
d3.inches=(inches+d2.inches)%12;
return(d3);
}
void main()
{
distance length1, length2, total;
length1.getdata(23,9);
length2.getdata(12,9);
total=length1.sum(length2);
cout<<"Length 1:";
length1.printit();
cout<<"Length 2:";
length2.printit();
cout<<"Total Length:";
total.printit();
getch();
}
OUTPUT:
Length 1: 23Feet 9Inches
Length 2: 12Feet 9Inches
Total Length: 36Feet 6Inches
RESULT
Thus the program to add two distances by passing object as parameters and to return the object
to a function is executed.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10];int n,i;
cout<<"Enter the size of the array:";
cin>>n;
cout<<"\nEnter the elements:";
for(i=0;i<n;i++)
{
cin>>a[i];
}
swap(a,n);
cout<<"\nArray after swap:";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
getch();
}
123456
123450
123400
123000
120000
100000
#include <iostream.h>
#include <conio.h>
void middle(int a[3][3],int m, int n)
{
int i,j;
cout<<"middle row :";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==(m/2))
cout<<a[i][j]<<" ";
}
}
cout<<"\n\nmiddle column:";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(j==(n/2))
cout<<a[i][j]<<" ";
}
}
void main()
{
clrscr ();
int i,j;
int a[3][3]={3,5,4,7,6,9,2,1,8};
cout<<"\nThe given matrix:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
middle(a,3,3);
getch();
}
middle column:5 6 1
#include <iostream.h>
#include <conio.h>
void main ()
{
clrscr ();
int a[7]={22,31,12,44,5,17,1};
int r;
cout<<"\nEnter the rotation factor:";
cin>>r;
cout<<"\nThe original array is:\n"<<endl;
for(int i=0;i<7;i++)
cout<<a[i]<<" ";
int b[7];
for(i=6;i>=0;i--)
{
if((i+r)>=7)
b[i+r-7]=a[i];
else
b[i+r]=a[i];
}
cout<<"\n\nArray after rotation by factor "<<r<< " is\n"<<endl;
for(i=0;i<7;i++)
{
a[i]=b[i];
cout<<a[i]<<" ";
}
getch();
}
22 31 12 44 5 17 1
5 17 1 22 31 12 44