#ifndef STACK_LINKED_H
#define STACK_LINKED_H
#include <iostream>
#include <iomanip>
#include <stdexcept>
using namespace std;
template<typename T>
struct Node_stack
{
T entry;
Node_stack* pNext;
};
template<typename T>
class Stack_linked
{
public:
Stack_linked();
// Safeguards
Stack_linked(const Stack_linked&);
~Stack_linked();
Stack_linked& operator = (const Stack_linked&);
// Stack operations
void push(const T&);
void pop();
T top() const;
bool empty() const;
int size() const;
void clear();
void display() const;
private:
Node_stack<T> *pTop;
};
template<typename T>
Stack_linked<T>::Stack_linked()
{
pTop = NULL;
}
// Copy Constructor
template<typename T>
Stack_linked<T>::Stack_linked(const Stack_linked<T>& ano)
{
Node_stack<T> *temp, *ano_temp;
ano_temp = ano.pTop;
if (ano_temp == NULL)
pTop = NULL;
else
{
pTop = temp = new Node_stack<T>;
temp->entry = ano_temp->entry;
ano_temp = ano_temp->pNext;
while (ano_temp != NULL)
{
temp->pNext = new Node_stack<T>;
temp = temp->pNext;
temp->entry = ano_temp->entry;
ano_temp = ano_temp->pNext;
}
temp->pNext = NULL;
}
}
// Destructor
template<typename T>
Stack_linked<T>::~Stack_linked()
{
Node_stack<T> *temp;
while (pTop != NULL)
{
pTop = pTop->pNext;
delete temp;
temp = pTop;
}
}
// Equivalent operator overload
template<typename T>
Stack_linked<T>& Stack_linked<T>::operator = (const Stack_linked<T>& ano)
{
Node_stack<T> *temp, *ano_temp,*new_top;
ano_temp = ano.pTop;
if (ano_temp == NULL)
new_top = NULL;
else
{
new_top = temp = new Node_stack<T>;
temp->entry = ano_temp->entry;
ano_temp = ano_temp->pNext;
while (ano_temp != NULL)
{
temp->pNext = new Node_stack<T>;
temp = temp->pNext;
temp->entry = ano_temp->entry;
ano_temp = ano_temp->pNext;
}
temp->pNext = NULL;
}
clear();
pTop = new_top;
return *this;
}
template<typename T>
void Stack_linked<T>::push(const T& item)
{
Node_stack<T> *temp = new Node_stack<T>;
temp->entry = item;
temp->pNext = pTop;
pTop = temp;
}
template<typename T>
void Stack_linked<T>::pop()
{
if (pTop == NULL)
throw runtime_error("Error: Pop an item from an empty stack.");
Node_stack<T> *temp = pTop;
pTop = pTop->pNext;
delete temp;
}
template<typename T>
T Stack_linked<T>::top() const
{
if (pTop == NULL)
throw runtime_error("Error: Pop from an empty stack.");
return pTop->entry;
}
template<typename T>
bool Stack_linked<T>::empty() const
{
if (pTop == NULL)
return true;
return false;
}
template<typename T>
int Stack_linked<T>::size() const
{
int count = 0;
Node_stack<T> *temp = pTop;
while (temp != NULL)
{
count++;
temp = temp->pNext;
}
return count;
}
template<typename T>
void Stack_linked<T>::clear()
{
Node_stack<T> *temp = pTop;
while (pTop != NULL)
{
pTop = pTop->pNext;
delete temp;
temp = pTop;
}
}
template<typename T>
void Stack_linked<T>::display() const
{
cout << "Top -> ";
if (pTop == NULL)
{
cout << endl;
return;
}
cout << pTop->entry << endl;
for (Node_stack<T> *temp = pTop->pNext;temp != NULL;temp = temp->pNext)
{
cout << " " << temp->entry << endl;
}
}
#endif
链式堆栈实现
最新推荐文章于 2021-07-13 11:34:00 发布