#include <iostream>
using namespace std;
struct node{
int data;
node*next;
};
class Stack{
private:
node*top;
public:
Stack(){
top=nullptr;
}
void push(int value){
node*New=new node();
New->data=value;
New->next=top;
top=New;
}
void pop(){
if(isEmpty()){
cout<<"stack underflow!cannot pop "<<endl;
return;
}
node*temp=top;
cout<<temp->data<<"has been popped "<<endl;
top=top->next;
delete temp;
}
void peek(){
if(isEmpty()){
cout<<"list is empty "<<endl;
return;
}
cout<<top->data<<endl;
}
bool isEmpty(){
return top==nullptr;
}
void display(){
if(isEmpty()){
cout<<"stack is empty "<<endl;
return;
}
node*temp=top;
while(temp!=nullptr){
cout<<temp->data<<endl;
temp=temp->next;
}
}
};
int main() {
Stack s;
s.push(5);
s.push(6);
s.push(8);
s.push(89);
s.push(6123);
s.display();
return 0;
}
No comments:
Post a Comment