II PUC C++ Progs (13-16)
II PUC C++ Progs (13-16)
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define n 3
class stack
{
int a[n],top,i;
public :
stack()
{
top=-1;
}
void push(int ele)
{
if(top==n-1)
{
cout<<"stack is full\n";
display();
getch();
exit(0);
}
a[++top]=ele;
}
void display()
{
if(top==-1)
cout<<"stack is empty \n";
else
cout<<"stack contents are \n";
for(i=top;i>=0;i--)
cout<<a[i]<<"\t";
}
};
void main()
{
char ch;
int ele;
stack s;
clrscr();
cout<<"enter your choice to push an elements(y/n)\n";
cin>>ch;
while(ch=='y')
{
cout<<"enter an element to push \n";
cin>>ele;
s.push(ele);
cout<<"enter your choice to push another element(y/n)\n";
cin>>ch;
}
s.display();
getch();
}
C++ Program-14
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define n 3
class stack
{
int a[n],i,top;
public :
stack()
{
top=-1;
}
void push(int ele)
{
if(top==n-1)
{
cout<<"stack is full";
display();
}
else
a[++top]=ele;
}
void pop()
{
if(top==-1)
cout<<"stack is empty";
else
{
cout<<a[top]<<" is popped";
top--;
}
}
void display()
{
if(top==-1)
cout<<"stack is empty";
else
{
cout<<"stack contents are \n";
for(i=top; i>=0 ;i--)
cout<<a[i]<<"\t";
}
}
};
void main()
{
stack s;
int ele,ch;
clrscr();
while(1)
{
cout<<"\n enter the choice 1.Push\t2.Pop\t3.Display\t4.Exit\n";
cin>>ch;
switch(ch)
{
case 1: cout<<"enter the element to insert:\n";
cin>>ele;
s.push(ele);
break;
case 2: s.pop();
break;
case 3: s.display();
break;
default: cout<<"End of stack operation";
getch();
exit(0);
}
}
}
C++ Program-15
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define n 3
class que
{
int a[n],i,r,f;
public:
que()
{
f=0;
r=-1;
}
void insert(int ele)
{
if(r==n-1)
{
cout<<"queue is full";
display();
}
else
a[++r]=ele;
}
void del()
{
if(f>r)
cout<<"queue is empty";
else
{
cout<<a[f]<<" is deleted";
f++;
}
}
void display( )
{
if(f>r)
cout<<"queue is empty";
else
{
cout<<"queue contents are \n";
for(i=f;i<=r; i++)
cout<<a[i]<<"\t";
}
}
};
void main()
{
que q;
int ele,ch;
clrscr();
while(1)
{
cout<<"\n enter the choice:1.Insert\t 2.Delete\t 3.Display\t
4. Exit\n";
cin>>ch;
switch(ch)
{
case 1:cout<<"enter the element to insert:\n";
cin>>ele;
q.insert(ele);
break;
case 2:q.del( );
break;
case 3:q.display( );
break;
default:cout<<"end of queue operation";
getch( );
exit(0);
}
}
}
C++ Program 16
#include<iostream.h>
#include<conio.h>
class list
{
struct node
{
int data;
node *link;
}*start;
public:
list()
{
start=NULL;
}
void add(int);
void print();
};
void main()
{
list *obj=new list;
char ch;
int ele;
clrscr();
cout<<"enter your choice to push an element(y/n)\n";
cin>>ch;
while(ch=='y')
{
cout<<"enter an element to push\n";
cin>>ele;
obj->add(ele);
cout<<"enter your choice push another element(y/n) \n";
cin>>ch;
}
obj->print();
getch();
}