Skip to content

Commit

Permalink
模板实现List----容器适配器以及模板的模板参数使用
Browse files Browse the repository at this point in the history
  • Loading branch information
MissYingYingHu committed May 14, 2018
1 parent 5a25335 commit 0b3c1c6
Show file tree
Hide file tree
Showing 4 changed files with 293 additions and 48 deletions.
166 changes: 166 additions & 0 deletions List.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#include"List.h"

template<class T>
LinkList<T>::LinkList()
:_head(new Node(T()))
{
_head->_next = _head;
_head->_prev = _head;
}

template<class T>
LinkList<T>::LinkList(const LinkList<T>& l)
:_head(new Node(T()))
{
_head->_next = _head;
_head->_prev = _head;

Node* cur = l._head->_next;
while(cur != l._head)
{
Node* new_node = new Node(cur->data);
Node* prev = _head->_prev;

new_node->_next = _head;
_head->_prev = new_node;

prev->_next = new_node;
new_node->_prev = prev;
cur = cur->_next;
}
}

template<class T>
LinkList<T>& LinkList<T>::operator=(const LinkList<T>& l)
{
if(this != &l)
{
clear();
Node* cur = l._head->_next;
while(cur != l._head)
{
Node* new_node = new Node(cur->data);
Node* prev = _head->_prev;

new_node->_next = _head;
_head->_prev = new_node;

prev->_next = new_node;
new_node->_prev = prev;
cur = cur->_next;
}
}
return *this;
}

template<class T>
void LinkList<T>::Insert(Node* pos,const T& value)
{
assert(pos);
Node* prev_node = pos->_prev;
Node* new_node = new Node(value);
prev_node->_next = new_node;
new_node->_prev = prev_node;
new_node->_next = pos;
pos->_prev = new_node;
}

template<class T>
void LinkList<T>::PushBack(const T& value)
{
Insert(_head,value);
}

template<class T>
void LinkList<T>::PushFront(const T& value)
{
Insert(_head->_next,value);
}

template<class T>
void LinkList<T>::Erase(Node* pos)
{
assert(pos);
assert(pos != _head);
if(_head->_next == _head)
{
return;
}
Node* prev = pos->_prev;
Node* next = pos->_next;
prev->_next = next;
next->_prev = prev;
delete pos;
}

template<class T>
void LinkList<T>::PopBack()
{
if(_head->_next == _head)
{
return;
}
Erase(_head->_prev);
}

template<class T>
void LinkList<T>::PopFront()
{
if(_head->_next == _head)
{
return;
}
Erase(_head->_next);
}

template<class T>
T& LinkList<T>::Front()
{
return (_head->_next)->data;
}

template<class T>
bool LinkList<T>::Empty()
{
return _head->_next == _head;
}

template<class T>
void LinkList<T>::clear()
{
if(_head->_next == _head)
{
return;
}
Node* cur = _head->_next;
while(cur != _head)
{
Node* next = cur->_next;
_head->_next = next;
next->_prev = _head;
delete cur;
cur = next;
}
}


template<class T>
LinkList<T>::~LinkList()
{
clear();
delete _head;
_head = NULL;
}

template<class T>
void LinkList<T>::Show(const char* msg)
{
cout<<msg<<" ";
Node* cur = _head->_next;
while(cur != _head)
{
cout<<cur<<":"<<cur->data<<" ";
cur = cur->_next;
}
cout<<endl;
}
40 changes: 40 additions & 0 deletions List.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;

template<class T>
struct LinkNode
{
T data;
LinkNode<T>* _prev;
LinkNode<T>* _next;

LinkNode(T value)
:data(value)
,_prev(NULL)
,_next(NULL)
{}
};
template<class T>
class LinkList
{
public:
typedef LinkNode<T> Node;
LinkList();
LinkList(const LinkList<T>& l);
LinkList<T>& operator=(const LinkList<T>& l);
void Insert(Node* pos,const T& value);
void PushBack(const T& value);
void PushFront(const T& value);
void Erase(Node* pos);
void PopBack();
void PopFront();
T& Front();
bool Empty();
void clear();
~LinkList();
void Show(const char* msg);
protected:
Node* _head;
};
50 changes: 50 additions & 0 deletions Queue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include"List.h"
#include"List.cpp"
using namespace std;

template<class T,template<class T> class Container>
//template<class T,class Container>
class Queue
{
public:
void Push(const T& value)
{
con.PushBack(value);
}
void Pop()
{
con.PopFront();
}
T& Top()
{
con.Front();
}
bool Empty()
{
con.Empty();
}
protected:
Container<T> con;
// Container con;
};

int main()
{
Queue<int,LinkList> q;
// Queue<int,LinkList<int> > q;
q.Push(1);
q.Push(2);
q.Push(3);
q.Push(4);
cout<<q.Empty()<<endl;
cout<<q.Top()<<" ";
q.Pop();
cout<<q.Top()<<" ";
q.Pop();
cout<<q.Top()<<" ";
q.Pop();
cout<<q.Top()<<" "<<endl;
q.Pop();
cout<<q.Empty()<<endl;
return 0;
}
85 changes: 37 additions & 48 deletions test.cpp
Original file line number Diff line number Diff line change
@@ -1,53 +1,42 @@
#include"vector.cpp"
#include"List.cpp"
#include<string>
using namespace std;

void Test()
{
Vector<int> v;
cout<<v.Empty()<<endl;
//cout<<v.Front()<<endl;
v.Insert(0,1);
v.Insert(0,2);
v.Insert(0,3);
v.Show("插入三个元素");
v.PushBack(4);
v.Show("尾插一个元素");
cout<<v.Front()<<endl;
Vector<int> v1;
v1.Insert(0,1);
v1.Insert(0,2);
v1.Insert(0,4);
v1.Show("插入三个元素");
v1 = v;
v1.Show("赋值v:");
Vector<int> v2(v1);
v2.Show("拷贝构造:");
v.Erase(0);
v.Show("删除首元素后");
v.Erase(5);
cout<<v.Empty()<<endl;
v.Show("删除尾元素后");
Vector<string> s;
s.Show("尾插5个字符串:");
s.PushBack("aaa");
s.Show("尾插5个字符串:");
s.PushBack("bbbb");
s.PushBack("ccccc");
s.PushBack("dddddd");
s.PushBack("eeeeeee");
s.Show("尾插5个字符串:");
Vector<string> s1(s);
s1.Show("拷贝构造:");
Vector<string> s2;
s2 = s;
s2.Show("赋值:");
cout<<s2.Front()<<endl;
}

int main()
{
Test();
LinkList<int> L;
L.PushBack(1);
L.PushBack(2);
L.PushBack(3);
L.PushBack(4);
L.PushFront(5);
L.Show("尾插4个元素头插一个元素:");
L.PopBack();
L.Show("尾删一个元素:");
L.PopFront();
L.Show("头删一个元素:");
LinkList<int> L1(L);
L1.PushBack(6);
L1.Show("拷贝构造:");
LinkList<int> L3;
L3 = L1;
L3.Show("赋值运算符重载:");

LinkList<string> s;
s.PushBack("aaaaa");
s.PushBack("bbbbbb");
s.PushBack("ccccccc");
s.PushBack("dddddddd");
s.PushFront("wwwwwwwwwwwwwww");
s.Show("尾插4个字符串,头插一个字符串:");
s.PopBack();
s.Show("尾删一个字符串:");
s.PopFront();
s.Show("头删一个字符串:");
LinkList<string> s1(s);
s1.PushBack("qqqqqqqqqqqqqqq");
s1.Show("拷贝构造并尾插一个元素:");
LinkList<string> s2;
s2 = s1;
s2.PushBack("qqqqqqqqqqqqqqq");
s2.Show("赋值运算符重载并尾插:");
return 0;
}

0 comments on commit 0b3c1c6

Please sign in to comment.