You are on page 1of 21

//Linked Stack program

#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

INSERTION AND DELETION IN A STACK


*********************************
1.Insertion
2.Deletion
Enter your choice: 1

Insert the element:


Enter the name: seshadri
Enter the age: 17
Now the stack is:
seshadri 17->
Do you want to continue?(Y/N): y

INSERTION AND DELETION IN A STACK


*********************************
1.Insertion
2.Deletion
Enter your choice: 1

Insert the element:


Enter the name: ses
Enter the age: 16

Now the stack is:


ses 16->seshadri 17->
Do you want to continue?(Y/N): y

INSERTION AND DELETION IN A STACK


*********************************
1.Insertion
2.Deletion
Enter your choice: 2

Now the stack is:


seshadri 17->
Do you want to continue?(Y/N):N
//Reverse a string
#include<iostream.h>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<conio.h>
const int max=50;
class stack
{
char a[max];
int top;
public:
stack() { top=-1; }
void push(char item)
{
if(top<max-1)
{ top++;
a[top]=item;
}
else
{ cout<<"\nError!";
return;
}
}
void pop(char &item)
{
if(top>-1)
{ item=a[top];
top--;
}
else
{ cout<<"\nError!";
return;
}
}
};
void reverse(char str[], int len)
{
stack string;
char newstr[50];
int i;
for(i=0;i<len;i++)
string.push(str[i]);
for(i=0;i<len;i++)
string.pop(newstr[i]);
newstr[len]='\0';
cout<<"\nThe reversed string is: ";
puts(newstr);
}
void main()
{
clrscr();
char str[50];
int len;
cout<<"\n\t\t\tREVERSE A STRING";
cout<<"\n\t\t\t~~~~~~~~~~~~~~~~";
cout<<"\nEnter the string to be reversed: ";
gets(str);
len=strlen(str);
reverse(str,len);
getch();
}

OUTPUT

REVERSE A STRING
~~~~~~~~~~~~~~~~
Enter the string to be reversed: amrutha

The reversed string is: ahturma


//Hexadecimal conversion
#include<iostream.h>
#include<alloc.h>
#include<process.h>
#include<conio.h>
struct node
{
char info;
node *link;
};
class stack
{
node *top;
public:
stack() { top=NULL; }
int push(char c)
{
node *p=new node;
if(!p)
return 0;
p->info=c;
p->link=top;
top=p;
return 1;
}
int pop(char &item)
{
if(top==NULL)
return 0;
node *p=top;
item=p->info;
top=p->link;
delete p;
return 1;
}
};
void main()
{
clrscr();
long m, n;
char r;
stack s;
char h[]="0123456789ABCDEF";
cout<<"\n\t\t\tDECIMAL TO HEXADECIMAL CONVERSION";
cout<<"\n\t\t\t*********************************";
cout<<"\nEnter the decimal number: ";
cin>>n;
m=n;
for(;m!=0;m/=16)
if(s.push(h[m%16])==0)
{ cout<<"\nStack is full!";
getch();
exit(0);
}
cout<<"\nThe hexadecimal equivalent of "<<n<<" is: ";
for(;s.pop(r)!=0;)
cout<<r;
getch();
}

DECIMAL TO HEXADECIMAL CONVERSION


*********************************
Enter the decimal number: 123

The hexadecimal equivalent of 123 is: 7B


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
struct node
{
char city[20];
node *link;
};
class queue
{
node *rear,*front;
public:
queue()
{
rear=NULL;
front=NULL;
}
void ins();
void del();
void display();
~queue();
};
void queue::ins()
{
node *temp=new node;
cout<<Enter city name;
gets(temp->city);
temp->link=NULL;
if(rear==NULL)
{
rear=front=temp;
}

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

Enter item to be inserted: 34

34

Menu
--------
1. Insertion
2. Deletion
3. Exit
Enter your choice: 1

Enter item to be inserted: 78

34 78

Menu

--------
1. Insertion
2. Deletion
3. Exit
Enter your choice: 1

Enter item to be inserted: 90

34 78 90

Menu

--------
1. Insertion
2. Deletion
3. Exit
Enter your choice: 1

Enter item to be inserted: 100


34 78 90 100

Menu

--------
1. Insertion
2. Deletion
3. Exit
Enter your choice: 1

Enter item to be inserted: 150

34 78 90 100 150

Menu
--------
1. Insertion
2. Deletion
3. Exit
Enter your choice: 1

Enter item to be inserted: 200

Circular Queue is full


34 78 90 100 150

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 swap(int a[],int n)


{
int i,j,tmp,mid=n/2;
if(n%2==0)
j=mid;
else
j= mid+1;
for(i=0;i<mid;i++,j++)
{
tmp=a[i];
a[i]=a[j];
a[j]=tmp;
}
}

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();
}

INPUT AND OUTPUT

Enter the size of the array:8

Enter the elements:1 2 3 4 5 6 7 8

Array after swap:5 6 7 8 1 2 3 4


#include <iostream.h>
#include <conio.h>
void main ()
{
clrscr ();
int a[10],n;
int b[10][10];
int i,j;
cout<<"Enter number of elements:";
cin>>n;
cout<<"\nEnter the elements of an array:";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if((i+j)>=n)
b[i][j]=0;
else
b[i][j]=a[j];
}
}
cout<<"\nThe 2D array for the given linear array\n\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<b[i][j]<<" ";
}
cout<<"\n\n";
}
getch();
}

INPUT AND OUTPUT

Enter number of elements:6

Enter the elements of an array:1 2 3 4 5 6

The 2D array for the given linear array

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();
}

INPUT AND OUTPUT

The given matrix:


354
769
218
middle row :7 6 9

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();
}

INPUT AND OUTPUT

Enter the rotation factor:3

The original array is:

22 31 12 44 5 17 1

Array after rotation by factor 3 is

5 17 1 22 31 12 44

You might also like