From 0b3c1c6c6ad9e9e6026162cd03c5fc1627a76f62 Mon Sep 17 00:00:00 2001 From: MissHu <34367530+MissYingYingHu@users.noreply.github.com> Date: Mon, 14 May 2018 10:19:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E6=9D=BF=E5=AE=9E=E7=8E=B0List----?= =?UTF-8?q?=E5=AE=B9=E5=99=A8=E9=80=82=E9=85=8D=E5=99=A8=E4=BB=A5=E5=8F=8A?= =?UTF-8?q?=E6=A8=A1=E6=9D=BF=E7=9A=84=E6=A8=A1=E6=9D=BF=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- List.cpp | 166 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ List.h | 40 +++++++++++++ Queue.cpp | 50 ++++++++++++++++ test.cpp | 85 ++++++++++++---------------- 4 files changed, 293 insertions(+), 48 deletions(-) create mode 100644 List.cpp create mode 100644 List.h create mode 100644 Queue.cpp diff --git a/List.cpp b/List.cpp new file mode 100644 index 0000000..4d2f208 --- /dev/null +++ b/List.cpp @@ -0,0 +1,166 @@ +#include"List.h" + +template +LinkList::LinkList() + :_head(new Node(T())) +{ + _head->_next = _head; + _head->_prev = _head; +} + +template +LinkList::LinkList(const LinkList& 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 +LinkList& LinkList::operator=(const LinkList& 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 +void LinkList::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 +void LinkList::PushBack(const T& value) +{ + Insert(_head,value); +} + +template +void LinkList::PushFront(const T& value) +{ + Insert(_head->_next,value); +} + +template +void LinkList::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 +void LinkList::PopBack() +{ + if(_head->_next == _head) + { + return; + } + Erase(_head->_prev); +} + +template +void LinkList::PopFront() +{ + if(_head->_next == _head) + { + return; + } + Erase(_head->_next); +} + +template +T& LinkList::Front() +{ + return (_head->_next)->data; +} + +template +bool LinkList::Empty() +{ + return _head->_next == _head; +} + +template +void LinkList::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 +LinkList::~LinkList() +{ + clear(); + delete _head; + _head = NULL; +} + +template +void LinkList::Show(const char* msg) +{ + cout<_next; + while(cur != _head) + { + cout<data<<" "; + cur = cur->_next; + } + cout< +#include +using namespace std; + +template +struct LinkNode +{ + T data; + LinkNode* _prev; + LinkNode* _next; + + LinkNode(T value) + :data(value) + ,_prev(NULL) + ,_next(NULL) + {} +}; +template +class LinkList +{ +public: + typedef LinkNode Node; + LinkList(); + LinkList(const LinkList& l); + LinkList& operator=(const LinkList& 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; +}; diff --git a/Queue.cpp b/Queue.cpp new file mode 100644 index 0000000..c51f068 --- /dev/null +++ b/Queue.cpp @@ -0,0 +1,50 @@ +#include"List.h" +#include"List.cpp" +using namespace std; + +template class Container> +//template +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 con; +// Container con; +}; + +int main() +{ + Queue q; +// Queue > q; + q.Push(1); + q.Push(2); + q.Push(3); + q.Push(4); + cout< -using namespace std; - -void Test() -{ - Vector v; - cout< v1; - v1.Insert(0,1); - v1.Insert(0,2); - v1.Insert(0,4); - v1.Show("插入三个元素"); - v1 = v; - v1.Show("赋值v:"); - Vector v2(v1); - v2.Show("拷贝构造:"); - v.Erase(0); - v.Show("删除首元素后"); - v.Erase(5); - cout< 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 s1(s); - s1.Show("拷贝构造:"); - Vector s2; - s2 = s; - s2.Show("赋值:"); - cout< 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 L1(L); + L1.PushBack(6); + L1.Show("拷贝构造:"); + LinkList L3; + L3 = L1; + L3.Show("赋值运算符重载:"); + + LinkList 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 s1(s); + s1.PushBack("qqqqqqqqqqqqqqq"); + s1.Show("拷贝构造并尾插一个元素:"); + LinkList s2; + s2 = s1; + s2.PushBack("qqqqqqqqqqqqqqq"); + s2.Show("赋值运算符重载并尾插:"); return 0; } -