向Linked模板类中放入数据的过程图(逆向取出数据):
Linked模板类头文件:
#ifndef _LINK_CLASS
#define _LINK_CLASS
#include <iostream>
using namespace std;
template <class T>
class Linked // 容器类(链式结构--堆栈)
{
struct Node{
T item;
Node *next;
}*head; // 容器的项(链的结点--堆栈的顶部)
long length; // 容器中项的数目
public:
class iterator{ // 为此容器类创建iterator类
Node *nodeptr;
public:
iterator() {nodeptr = NULL;}
iterator(Node *newptr) {nodeptr = newptr;} // 复制构造函数
iterator operator++(int){ // 将迭代器前进到容器的下一个位置
if (nodeptr == NULL)
{cout << "Out of Stack!\n"; return NULL;}
else
{nodeptr = nodeptr->next; return *this;}
}
iterator operator=(Node* newptr)
{nodeptr = newptr; return *this;}
bool operator!=(Node* newptr) {
if (nodeptr == NULL)
if (newptr == NULL) return false;
else return true;
else if (newptr == NULL) return true;
else if (nodeptr->item != newptr->item) return true;
else return false;
}
friend ostream& operator<<(ostream& out, iterator& iter){
if (iter.nodeptr != NULL)
out << iter.nodeptr->item;
return out;
}
}; // operator!=、operator++ 与 operator= 不能以友元函数形式重载,只能以函数成员形式重载
Linked();
~Linked();
Node* begin() {return head;}
Node* end();
long size() const; // 返回这个Linked对象中项的数量
void push_front(const T& newitem);
// 在这个Linked对象的前面插入newitem(入栈)
void pop_front();
// 删除这个Linked对象最前面的item(出栈)
template<class T> friend ostream& operator<<(ostream& out, Linked<T>& ink);
// 在C++里面通过模板特化的函数和没有通过模板特化的函数是不同的(template<class T>不能丢掉)
};
// 模板类的定义和声明不能分开,要创建特例,编译器不但要看到模板的声明,还要看到模板的定义
// Linked模板类的定义
// 返回值前面必须使用 typename 告诉编译器Linked<T>::Node*是一种类型而不是类的成员变量
template <class T>
typename Linked<T>::Node* Linked<T>::end(){
return NULL;
}
template <class T>
ostream& operator<<(ostream& out, Linked<T>& ink) // 输出整个容器
{
Linked<T>::Node *temp = ink.head;
while (ink.head != NULL){
out << ink.head->item << " | ";
ink.head = ink.head->next;
}
ink.head = temp;
return out;
}
template <class T>
Linked<T>::Linked()
{
head = NULL;
length = 0;
}
template <class T>
Linked<T>::~Linked()
{
while (length == 0){
pop_front();
length--;
}
}
template <class T>
long Linked<T>::size() const
{
return length;
}
template <class T>
void Linked<T>::push_front(const T& newitem)
{
Node *temp = head;
head = new Node;
head->item = newitem;
head->next = temp;
length++;
}
template <class T>
void Linked<T>::pop_front()
{
if (length == 0){
cout << "Out of Stack!\n";
}
else{
Node *temp = head;
head = temp->next;
delete temp;
length--;
}
}
#endif
Linked模板类的测试.cpp:
#include "3 Linked class.h"
int main()
{
// 测试Linked类
Linked<char> stack1;
stack1.push_front('a');
stack1.push_front('b');
stack1.push_front('c');
cout << "the lenght = " << stack1.size() << endl;
cout << "stack1 = " << stack1 << endl;
stack1.pop_front();
cout << "now, stack1 = " << stack1 << endl << endl;
// 测试iterator类
Linked<char> stack2;
stack2.push_front('z');
stack2.push_front('y');
stack2.push_front('x');
Linked<char>::iterator iter = stack2.begin();
// iterator的复制构造函数起作用(初始化),不是iterator的operator=起作用
cout << "the lenght = " << stack2.size() << endl;
cout << "stack1 = " << stack2 << endl;
for (; iter != stack2.end(); iter++){
cout << "iter = " << iter << endl;
}
cout << endl;
// 其他测试
Linked<char> stack3;
char ch;
cout << "Please enter some characters, to quit, enter:\n";
while ((ch = cin.get()) != '\n'){
stack3.push_front(ch);
}
cout << "the lenght = " << stack3.size() << endl;
cout << "stack1 = " << stack3 << endl;
return 0;
}
结果: