//: C06:Stack3.cpp {O}
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Constructors/destructors
#include "Stack3.h"
#include "../require.h"
using namespace std;

/* Questo esempio è simile a quello già proposto a lezione con il nome di Stack.cpp.
In questo caso però, si utilizzano costruttori e distruttori.*/

/* La funzione che segue implementa il costruttore della"struct". */

Stack::Link::Link(void* dat, Link* nxt)
{

data = dat;
next = nxt;

}

/* Distruttore della "struct" */

Stack::Link::~Link() { }

/* Costruttore della classe */

Stack::Stack() { head = 0; }

void Stack::push(void* dat)
{

head = new Link(dat,head);

}

void* Stack::peek()

require(head != 0, "Stack empty");
return head->data; 

}

void* Stack::pop()
{

if(head == 0) return 0;
void* result = head->data;
Link* oldHead = head;
head = head->next;
delete oldHead;
return result;

}

/* Distruttore della classe */

Stack::~Stack()
{

require(head == 0, "Stack not empty");

} ///:~