准备把基本数据结构以模板类的形式都敲一遍以准备秋招和PAT考试
/***********************************************
> Filename: list.cpp
> Author: Pyt
> Create: 2017-08-04 09:36:04
************************************************/
#include <iostream>
using namespace std;
template<typename T>
class List{
public:
List();
~List();
List(const List &t);
List& operator=(const List &t);
void insert(size_t pos, const T &val);
void push_back(const T &val);
void push_front(const T &val);
void pop_back();
void pop_front();
void remove(const T &val);
void clear();
void print_list() const;
bool empty() const;
T back();
T front();
private:
struct Node{
T val;
Node *next;
Node *pre;
Node(T x) : val(x), next(NULL), pre(NULL){}
};
Node *head;
Node *tail;
size_t cursize;
void copy_list(const List &t);
};
/***构造***/
template<typename T>
List<T>::List()
: head(NULL), tail(NULL), cursize(0)
{
}
/***析构***/
template<typename T>
List<T>::~List()
{
clear();
}
/****拷贝构造****/
template<typename T>
List<T>::List(const List &t)
{
copy_list(t);
}
/***赋值重载***/
template<typename T>
List<T>& List<T>::operator=(const List &t)
{
if(this != &t)
{
clear();
copy_list(t);
}
return *this;
}
/***指定位置前插入从0开始***/
template<typename T>
void List<T>::insert(size_t pos, const T &val)
{
if(pos == 0)
{
Node *t = new Node(val);
t->next = head;
head->pre = t;
head = t;
cursize++;
}
else if(pos > 0)
{
Node *cur = head->next;
while(--pos && cur)
{
cur = cur->next;
}
Node *t = new Node(val);
if(cur == NULL)
{
push_back(val);
}
else{
t->pre = cur->pre;
cur->pre->next = t;
cur->pre = t;
t->next = cur;
}
cursize++;
}
}
/***在链表尾插入**/
template<typename T>
void List<T>::push_back(const T &val)
{
Node *t = new Node(val);
if(empty())
head = tail = t;
else
{
tail->next = t;
t->pre = tail;
tail = t;
}
cursize++;
}
/**在链表头插入***/
template<typename T>
void List<T>::push_front(const T &val)
{
Node *t = new Node(val);
if(empty())
head = tail = t;
else{
t->next = head;
head->pre = t;
head = t;
}
cursize++;
}
/***删除最后一个元素***/
template<typename T>
void List<T>::pop_back()
{
if(!empty())
{
cursize--;
Node *obj = tail;
if(cursize == 0)
head = tail = NULL;
else
tail = tail->pre;
delete obj;
}
}
/***删除第一个元素***/
template<typename T>
void List<T>::pop_front()
{
if(!empty())
{
cursize--;
Node *obj = head;
if(cursize == 0)
head = tail = NULL;
else
head = head->next;
delete obj;
}
}
/***删除所有指定元素***/
template<typename T>
void List<T>::remove(const T &val)
{
Node *cur = head;
while(cur)
{
if(cur->val == val)
{
if(cur == head)
cur = cur->next, pop_front();
else if(cur == tail)
cur = cur->next, pop_back();
else{
Node *obj = cur;
cur->pre->next = cur->next;
cur->next->pre = cur->pre;
cur = cur->next;
delete obj;
}
cursize--;
}
else{
cur = cur->next;
}
}
}
/***清空链表*****/
template<typename T>
void List<T>::clear()
{
while(head)
{
Node *t = head;
head = head->next;
delete t;
}
head = tail = NULL;
cursize = 0;
}
/***获得最后一个元素**/
template<typename T>
T List<T>::back()
{
if(tail)
return tail->val;
}
/***获得第一个元素***/
template<typename T>
T List<T>::front()
{
if(head)
return head->val;
}
/***私有函数深拷贝***/
template<typename T>
void List<T>::copy_list(const List &t)
{
cursize = t.cursize;
Node *cur = t.head;
if(cur == NULL)
head = tail = NULL;
else{
Node *pre = new Node(cur->val);
head = tail = pre;
cur = cur->next;
while(cur)
{
Node *t = new Node(cur->val);
pre->next = t;
t->pre = pre;
pre = t;
cur = cur->next;
}
tail = pre;
}
}
/***打印链表****/
template<typename T>
void List<T>::print_list() const
{
Node *cur = head;
while(cur)
{
cout << cur->val << " " ;
cur = cur->next;
}
cout << endl;
/* cur = tail;
while(cur)
{
cout << cur->val << " ";
cur = cur->pre;
}
cout << endl;*/
}
/***判空****/
template<typename T>
bool List<T>::empty() const
{
return cursize == 0;
}
int main()
{
List<int> a;
a.push_back(1);
a.push_front(2);
a.pop_back();
a.push_back(1);
a.insert(0, 3);
a.insert(10, 5);
cout << a.front() << endl;
cout << a.back() << endl;
a.print_list();
List<int> b(a);
b.push_front(9);
b.print_list();
a.print_list();
a.remove(1);
a.print_list();
a.clear();
cout << a.empty() << endl;
a.print_list();
List<int> c;
c = b;
c.print_list();
return 0;
}