Multipop
#include<iostream>
#define MS 100
using namespace std;
class stack{
int top,arr[MS];
public:
stack()
{
top = -1;
}
void push(int data);
int pop();
bool isempty();
bool isfull();
int peek();
void multipop(int n);
};
bool stack::isfull()
{
if(top==MS-1){
return true;
}
else{
return false;
}
}
bool stack::isempty()
{
if (top==-1)
{
return true;
}
else
{
return false;
}
}
void stack::push(int data)
{
if(stack::isfull())
return ;
else
{
++top;
arr[top] = data;
}
}
int stack ::pop()
{
if (stack::isempty())
{
return 999999;
}
else
{
int temp=arr[top];
--top;
return temp;
}
}
int stack ::peek()
{
if(!stack::isempty())
{
return arr[top];
}
}
void stack::multipop(int n)
{
while(n > 0 && !isempty())
{
pop();
n--;
}
}
int main()
{
stack s1;
s1.push(10);
s1.push(20);
s1.push(30);
s1.push(40);
s1.push(50);
cout << "Top element is: " << s1.peek() << endl;
s1.multipop(3);
cout << "After popping 3 elements, the top element is: " << s1.peek() << endl;
return 0;
}
Comments
Post a Comment