#ifndef LINKED_H_
#define LINKED_H_
//---------------------------------------------
// file:Linked.h
// 链式结构的自定义容器类<本实例都用栈空间, 实际使用应该用堆空间, 用new>
// 设计:从类用户角度去考虑,需要提供的接口
// 1. 元素的类型: 类型希望在使用的时候指定, 也就是能适应元素是int or double or 自定义类...,
// 把我们的容器类设计成模板类,可以实现这一点. 像这样使用:Linked<int> intlist;
// 2. 容器中元素的数量
// 3. 在容器的前面插入新元素:push_front
// 4. 容易构造新容器对象, 构造函数:缺省构造函数Linked(), 拷贝构造函数
// Linked(const Linked<T>& otherLinked), 赋值操作operator=
// Linked<int> intList1;调用构造函数Linked()
// Linked<int> intList2 = intList1;会隐式转换成Linked<int> intList2(intList1),
// 这里我们不要这样, 要像下面这样显式使用, 用上explicit关键字
// Linked<int> intList3(intList1);拷贝构造函数
// intList3 = intList2;赋值操作符
//---------------------------------------------
#include <iostream>
using namespace std;
template<typename T>
class Linked{
protected:
struct Node{
T item;
Node* next;
};//每个元素(节点)包含2项:用户指定类型的对象, 指向下一个节点的指针
Node* head;//头指针
unsigned long length;//容器元素个数
public:
// Postcondition(后置条件): 初始化一个空的容器
Linked()
{
head = NULL;
length = 0;
}
// Postcondition: 返回元素的个数
unsigned long size() const
{
return length;
}
// Postcondition: 在所有元素的前面插入新节点
void push_front(const T& newItem)
{
Node* newHead = new Node;
newHead->item = newItem;
newHead->next = head;
head = newHead;
length++;
}
// Precondition(前置条件): 容器不是空的
// Postcondition: 从容器中删除最前面的一个元素
void pop_front()
{
if(NULL != head){
Node* oldHead = head;
head = head->next;
delete oldHead;
length--;
}
//这里有一个坑, 如果只有下面2行代码:
//head = head->next;
//length--;
//容器是没问题, 第一个元素丢掉了, 但是丢掉的那个元素, 没有删除,
//也不可能再访问的到, 就变野指针了, 存在内存泄露, 所以这里一定要delete掉
}
// Postcondition: 这个容器是指定容器的拷贝(
// 将另一个容器赋值给本容器:Linked<int> intList1; Linked<int> intList2 = intList1;)
void operator=(const Linked<T>& otherLinked)
{
Linked<T> tempLinked;
Node* tempPtr;
// 删除容器已有的元素
while (head != NULL)
pop_front();
for (tempPtr = otherLinked.head; tempPtr != NULL; tempPtr = tempPtr -> next)
tempLinked.push_front (tempPtr -> item);
for (tempPtr = tempLinked.head; tempPtr != NULL; tempPtr = tempPtr -> next)
push_front (tempPtr -> item);
} // 重写操作符 =
// Postcondition: 这个容器是指定容器的拷贝
// 将另一个容器赋值给本容器:Linked<int> intList1; Linked<int> intList2(intList1);
explicit Linked(const Linked<T>& otherLinked)
{
head = NULL;
length = 0;
Linked<T> tempLinked;
Node* tempPtr;
for (tempPtr = otherLinked.head; tempPtr != NULL; tempPtr = tempPtr->next)
tempLinked.push_front(tempPtr->item);
for (tempPtr = tempLinked.head; tempPtr != NULL; tempPtr = tempPtr->next)
push_front(tempPtr->item);
} // 拷贝构造函数
// Postcondition: 删除所有元素, 释放占用的空间
virtual ~Linked()
{
cout << "~Linked() called." << endl;
while (head != NULL)
pop_front();
} // 析构函数
};
#endif
//file:main.cpp
//------------- 测试 ----------------
#include "Linked.h"
#include <iostream>
using namespace std;
int main(){
Linked<int> intList1;
for(int i=1; i<=100; ++i){
intList1.push_front(i);
}
cout << "intList1 length:" << intList1.size() << endl;
//测试拷贝构造函数
//Linked<int> intList2 = intList1; 编译错误, 不允许隐式转换, 因为我们用了explicit关键字
Linked<int> intList2(intList1);
cout << "intList2 length:" << intList2.size() << endl;
int ch;
cin >> ch;
return 0;
}
//现在这个类, 还没有遍历或说提供迭代器的能力.
//迭代器才是我做练习的目的, 下一篇就实现它, 现在只是做个基础.
转载于:https://my.oschina.net/SethFeng/blog/300808