c++实现顺序表、单链表和双向链表
2017-07-23 23:12
651 查看
c++实现顺序表、单链表和双向链表
1.顺序表//SeqList.h #pragma once #include<iostream> #include<assert.h> using namespace std; typedef int DataType; class SeqList { public: SeqList() :_array(NULL) ,_size(0) ,_capacity(0) {} SeqList(const SeqList& s) { _array=(DataType*)malloc(sizeof(DataType)*s._size); memcpy(_array,s._array,sizeof(DataType)*s._size); _size=_capacity=s._size; } SeqList& operator=(SeqList& s) { Swap(s); return *this; } void Swap(SeqList& s) { swap(_array,s._array); swap(_size,s._size); swap(_capacity,s._capacity); } ~SeqList() { if(_array) { free(_array); _size=_capacity=0; _array=NULL; } } void CheckCapcacity() { if(_size==_capacity) { _capacity=_capacity*2+3; _array=(DataType*)realloc(_array,_capacity*sizeof(DataType)); } } void PushBack(DataType x) { CheckCapcacity(); _array[_size++]=x; } void PopBack() { assert(_size); --_size; } void PushFront(DataType x) { CheckCapcacity(); int end=_size-1; while(end>=0) { _array[end+1]=_array[end]; --end; } _array[0]=x; ++_size; } void PopFront() { assert(_size); for(size_t i=1;i<_size;++i) { _array[i-1] = _array[i]; } --_size; } inline void Insert(size_t pos, DataType x) { assert(pos<_size); for(int end=_size-1;(int)pos<end;--end) { _array[end+1]=_array[end]; } _array[pos] = x; ++_size; } inline void Erase(size_t pos) { assert(pos<_size); for(size_t i=pos+1;i<_size;++i) { _array[i-1]=_array[i]; } --_size; } void Print() { assert(_size); for(size_t i=0;i<_size;++i) { cout<<_array[i]<<" "; } cout<<endl; } inline DataType& operator[](size_t pos) { assert(pos<_size); return _array[pos]; } private: DataType* _array; size_t _size; size_t _capacity; };
//SeqList.cpp #include<iostream> #include"SeqList.h" using namespace std; void TestSeqList() { SeqList s1; s1.PushBack(1); s1.PushBack(2); s1.PushBack(3); s1.PushBack(4); s1.Print(); s1.PushFront(0); s1.Print(); s1.Insert(2,10); s1.Print(); s1.Insert(0,20); s1.Print(); s1.Erase(2); s1.Print(); SeqList s(s1); s.PushBack(5); s.Print(); s1=s; s1.Print(); } int main() { TestSeqList(); return 0; }
2.单链表
//SList.h #pragma once #include<iostream> #include<assert.h> using namespace std; typedef int DataType; struct SListNode { DataType _data; SListNode* _next; SListNode(DataType x) :_data(x) ,_next(NULL) { } }; class SList { typedef SListNode Node; public: SList() :_head(NULL) ,_tail(NULL) { } SList(const SList& s) :_head(NULL) ,_tail(NULL) { Node *cur=s._head; while(cur) { PushBack(cur->_data); cur=cur->_next; } } SList& operator=(SList s) { swap(_head, s._head); swap(_tail, s._tail); return *this; } ~SList() { Node *cur=_head; while(cur) { Node* next=cur->_next; delete cur; cur=next; } _head=_tail=NULL; } void PushBack(DataType x) { //链表为空 //链表有一个以上节点 if(_head==NULL) { _head=_tail=new Node(x); } else { _tail->_next=new Node(x); _tail=_tail->_next; } } void PopBack() { //无节点 //有一个节点 //有一个以上节点 if(_head==NULL) { return; } else if(_head==_tail) { delete _head; _head=_tail=NULL; } else { Node* prev=_head; while(prev->_next!=_tail) { prev=prev->_next; } delete _tail; _tail=prev; _tail->_next=NULL; } } void PushFront(DataType x) { //无节点 //有一个以上节点 if(_head==NULL) { _head=_tail=new Node(x); } else { Node* tmp=new Node(x); tmp->_next=_head; _head=tmp; } } void PopFront() { //无节点 //有一个节点 //有一个以上节点 if(_head==NULL) { return; } else if(_head==_tail) { delete _head; _head=_tail=NULL; } else { Node * tmp=_head; _head=_head->_next; delete tmp; } } //插入一个节点在pos的前面 void Insert(Node* pos, DataType x) { assert(pos); if(pos==_head) { PushFront(x); } else { Node* prev=_head; while(prev->_next!=pos) { prev=prev->_next; } Node* tmp=new Node(x); prev->_next=tmp; tmp->_next=pos; } } void Erase(Node* pos) { assert(pos); if(pos==_head) { PopFront(); } else if(pos==_tail) { PopBack(); } else { Node *prev=_head; while(prev->_next!=pos) { prev=prev->_next; } prev->_next=pos->_next; delete pos; } } SListNode* Find(DataType x) { Node* cur=_head; while(cur) { if(cur->_data==x) { return cur; } cur=cur->_next; } return NULL; } void Print() { Node* cur=_head; while(cur) { cout<<cur->_data<<" "; cur=cur->_next; } cout<<endl; } private: Node* _head; Node* _tail; };
//SListNode.cpp #include<iostream> #include"SList.h" using namespace std; void TestSList1() { SList s1; s1.PushBack(1); s1.PushBack(2); s1.PushBack(3); s1.PushBack(4); s1.Print(); s1.PopBack(); s1.PopBack(); s1.PopBack(); s1.PopBack(); s1.PopBack(); s1.Print(); s1.PushFront(0); s1.PushFront(1); s1.PushFront(2); s1.PushFront(3); s1.Print(); s1.PopFront(); s1.PopFront(); s1.PopFront(); s1.PopFront(); s1.PopFront(); s1.Print(); } void TestSList2() { SList s2; s2.PushBack(1); s2.PushBack(2); s2.PushBack(3); s2.PushBack(4); s2.Print(); SListNode* pos=s2.Find(2); s2.Insert(pos,10); s2.Print(); s2.Erase(pos); s2.Print(); } void TestSlist3() { SList s1; s1.PushBack(1); s1.PushBack(2); s1.PushBack(3); s1.PushBack(4); s1.Print(); SList s2(s1); s2.PushBack(10); s2.PushBack(11); s2.Print(); s1 = s2; s1.Print(); } int main() { //TestSList1(); //TestSList2(); TestSlist3(); return 0; }
TestSList1();
TestSList2();
TestSList3();
3.双向链表
//ListNode.h #pragma once #include<iostream> #include<assert.h> using namespace std; typedef int DataType; struct ListNode { ListNode* _next; ListNode* _prev; DataType _data; ListNode(const DataType d) :_data(d) ,_next(NULL) ,_prev(NULL) { } }; class List { typedef ListNode Node; public: List(); //构造 List(const List& d); //拷贝构造 List& operator=(const List& d); ~List(); void PushBack(DataType x); void PopBack(); void PushFront(DataType x); void PopFront(); // 在pos的前面插入一个 void Insert(Node* pos, DataType x); void Erase(Node* pos); Node* Find(DataType x); void Reverse(); void Print(); private: Node* _head; Node* _tail; };
//ListNode.cpp #include"ListNode.h" #include<iostream> List::List()//构造 :_head(NULL) ,_tail(NULL) { } List::List(const List& d)//拷贝构造 :_head(NULL) ,_tail(NULL) { if(d._head==NULL) { return; } Node* tmp=d._head; while(tmp) { PushBack(tmp->_data); tmp=tmp->_next; } } //List& List::operator=(const List& d)//赋值运算符重载 //{ // if(this!=&d) // { // swap(_head,d._head); // swap(_tail,d._tail); // } // return *this; //} List::~List()//析构 { Node* cur=_head; while(cur!=_tail) { _head=_head->_next; delete cur; cur=_head; } _head=NULL; _tail=NULL; } void List::PushBack( DataType d)//尾插 { if(_head==NULL) { _head=_tail=new Node(d); } else { Node* tmp=new Node(d); _tail->_next=tmp; tmp->_prev=_tail; _tail=_tail->_next; } } void List::PopBack() //尾删 { if(_head==NULL||_tail==_head) { delete _tail; _head=_tail=NULL; } else { _tail=_tail->_prev; _tail->_next=NULL; } } void List::PushFront(DataType x)//头插 { if(_head==NULL) { _head=_tail=new Node(x); } else { Node* tmp=new Node(x); tmp->_next=_head; _head->_prev=tmp; _head=tmp; } } void List::PopFront() //头删 { if(_head==NULL||_head==_tail) { delete _tail; _head=_tail=NULL; } else { _head=_head->_next; delete _head->_prev; } } void List::Insert(Node* pos, DataType x) //任意位置插入一个节点 { assert(pos); if(pos==_head) { PushFront(x); } else { Node* prev=_head; while(prev->_next!=pos) { prev=prev->_next; } Node* tmp=new Node(x); prev->_next=tmp; tmp->_next=pos; } } ListNode* List::Find(DataType x) //在链表中查找一个节点 { Node* cur=_head; while(cur) { if(cur->_data==x) { return cur; } cur=cur->_next; } return NULL; } void List::Erase(Node* pos) //删除任意节点 { assert(pos); if(pos==_head) { PopFront(); } else if(pos==_tail) { PopBack(); } else { Node* prev=_head; while(prev->_next!=pos) { prev=prev->_next; } prev->_next=pos->_next; delete pos; } } void List:: Reverse()//双向链表逆置 { if(_head==_tail) { return; } else { ListNode* cur=_head; ListNode* tmp=cur; ListNode* NewHead=NULL; while(cur) { tmp=cur; cur=cur->_next; tmp->_next=NewHead; tmp->_prev=cur; NewHead=tmp; } _head=NewHead; } } void List::Print() //打印链表 { if(_head==NULL) { cout<<"The List is Empty"<<endl; return; } else { Node* tmp=_head; while(tmp) { cout<<tmp->_data<<" " ; tmp=tmp->_next; } cout<<endl; } }
//Test.cpp #include"ListNode.h" #include<stdlib.h> #include<iostream> using namespace std; void test1() { List list1; list1.PushBack(0); list1.PushBack(1); list1.PushBack(2); list1.PushBack(3); list1.Print(); list1.PopBack(); list1.Print(); list1.PushFront(4); list1.Print(); list1.PopFront(); list1.Print(); list1.Reverse(); list1.Print(); } void test2() { List s1; s1.PushBack(1); s1.PushBack(2); s1.PushBack(3); s1.PushBack(4); s1.Print(); ListNode* pos=s1.Find(2); s1.Insert(pos,10); s1.Print(); s1.Erase(pos); s1.Print(); } int main() { //test1(); test2(); return 0; }
text1();
test2();
相关文章推荐
- c++实现单链表,双向链表,顺序表的接口(非模板)
- 【C++】实现顺序表、单链表和双向链表
- 双向循环链表的头插、中插、尾插、删除、逆序顺序显示(C++实现)
- C++实现顺序表及双向链表
- C++实现双向链表
- 双向链表的C++实现
- C++实现顺序表、单链表、双链表
- c++实现线性表和双向链表
- C++实现简单的双向链表
- C++实现数据结构三 双向循环链表
- C++实现双向链表
- 双向循环链表删除算法的C++程序实现
- Structure.List 线性表(包含顺序链表,单链表,双链表,双向循环链表的实现)
- C++模板及用模板实现双向循环链表,顺序表
- 链表的顺序表示和实现(C++模板类实现)
- (C++模板类)顺序表、双向链表
- C++ 学习练手 - 双向链表的模板实现
- C++实现双向链表
- 双向循环链表插入算法的C++程序实现