Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Node {
- int key, maxcurr;
- Node *next;
- Node (int data): key (data), maxcurr (data), next (nullptr) {
- }
- };
- class Stiva {
- public:
- Stiva (): peak (nullptr) {
- }
- void Push (int data) {
- Node *temp = new Node (data);
- if (!temp)
- return;
- if (isEmpty ())
- peak = temp;
- else {
- temp->next = peak;
- temp->maxcurr = max (temp->maxcurr, peak->maxcurr);
- peak = temp;
- }
- }
- void Pop () {
- if (isEmpty ())
- return;
- Node *temp = peak;
- peak = peak->next;
- delete temp;
- }
- bool isEmpty () const{
- return !peak;
- }
- int Top () const{
- if (!isEmpty ())
- return peak->key;
- else
- return - 1;
- }
- int Max () {
- if (!isEmpty ())
- return peak->maxcurr;
- else
- return - 1;
- }
- void Display () {
- if (!isEmpty ()) {
- stack <int> s;
- while (!isEmpty ())
- cout << Top () << " ", s.push (Top ()), Pop ();
- while (!s.empty ())
- Push (s.top ()), s.pop ();
- } else
- exit (EXIT_FAILURE);
- }
- ~Stiva () {
- while (!isEmpty ())
- Pop ();
- }
- private:
- int key;
- Node *peak;
- };
Add Comment
Please, Sign In to add comment