#include <iostream>
using namespace std;
class Node{
public:
int data;
Node *next, *prev;
Node();
Node(int);
};
Node::Node(){
next = NULL;
prev = NULL;
}
Node::Node(int data){
this->data = data;
next = NULL;
prev = NULL;
}
class Stack{
Node *head;
int size, maxsize;
public:
Stack(int);
void Push(int);
int Pop();
void Display();
};
Stack::Stack(int x){
head = NULL;
size = 0;
maxsize = x;
}
void Stack::Push(int data){
if(head == NULL){
head = new Node(data);
size++;
}
else if(size < maxsize){
Node *temp = new Node(data);
temp->next = head;
head = temp;
size++;
}
}
int Stack::Pop(){
if(head != NULL){
Node *temp = head, temp2 = *head;
head = head->next;
delete temp;
size--;
return temp2.data;
}
return (int)NULL;
}
void Stack::Display(){
Node* curr = head;
while(curr != NULL){
cout<<"|"<<curr->data<<"|"<<endl<<"---\n";
curr = curr->next;
Pop();
}
cout<<endl;
}
Comments
Leave a comment