|
/****************************************************************************/
/* */
/* Copyright (C) 1998; IPL, Information Processing Ltd */
/* */
/****************************************************************************/
/* */
/* Cantata++ Demo - Unbounded Stack Example */
/* */
/****************************************************************************/
#include <iostream.h>
#include <stdlib.h>
#include <new.h>
#include "stack.h"
/*
* Default constructor
*/
stack::stack()
{
head = NULL;
}
/*
* Copy constructor
*/
stack::stack(const stack& stack1)
{
head = stack1.head;
}
/*
* Destructor
*/
stack::~stack()
{
while (head != NULL)
{
node* new_head = head->next;
delete head;
head = new_head;
}
}
/*
* Check if stack is empty
*/
bool stack::is_empty()
{
return (head == NULL);
}
/*
* Push function
*/
void stack::push(int value)
{
node* new_head;
new_head = new node;
if (new_head != NULL)
{
new_head->next = head;
new_head->data = value;
}
else
{
throw stack::new_error();
}
return;
}
/*
* Pop function
*/
int stack::pop()
{
if (is_empty())
{
throw stack::empty_pop();
}
int ret_val = head->data;
node* new_head = head->next;
delete head;
head = new_head;
return ret_val;
}
|