// 链栈.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include <iostream>
using namespace std;
template<class T>
class LinkStack;
template<class T>
class LinkNode
{
friend class LinkStack;
private:
T data;
LinkNode<T>* next;
public:
LinkNode()
{
next = nullptr;
}
LinkNode(T tempData)
{
data = p;
next = nullptr;
}
LinkNode<T>& operator = (const LinkNode<T>* p)
{
data = *p->data;
next = *p->next;
return *this;
}
};
template<class T>
class LinkStack
{
private LinkNode* head;
public:
LinkStack();
~LinkStack();
void push(T p);
void pop();
bool top(T& p);
bool isEmpty();
int size();
void print();
void clear();
};
template<class T>
LinkStack<T>::LinkStack()
{
head = new LinkNode<T>();
}
template<class T>
LinkStack<T>::~LinkStack()
{
clear();
}
template<typename T>
void LinkStack<T>::push(T p)
{
LinkNode<T>* temp;
temp = new LinkNode<T>(p);
temp->next = head->next;
head->next = temp;
}
template<typename T>
void LinkStack<T>::pop()
{
//存储将要删除的节点
LinkNode<T>* temp = nullptr;
if (head->next != nullptr)
{
temp = head->next;
head->next = head->next->next;
}
delete temp;
}
template<typename T>
bool LinkStack<T>::top(T& p)
{
if (isEmpty())
{
return false;
}
p = head->next->data;
return true;
}
template<class T>
bool LinkStack<T>::isEmpty()
{
if (head->next == nullptr)
{
return true;
}
return false;
}
template<typename T>
int LinkStack<T>::size()
{
int len = 0;
LinkNode<T> tempNode = head;
while (tempNode->next != nullptr)
{
len++;
tempNode = tempNode->next;
}
}
08-13
206
