利用C++简单实现顺序表和单链表的示例代码
本文主要给大家介绍了关于C++实现顺序表和单链表的相关内容,分享出来供大家参考学习,话不多说,来一起看看详细的介绍:
一、顺序表示例代码:
#include#include usingnamespacestd; typedefintDatatype; classSeqList { public: SeqList() :_array(NULL) ,_size(0) ,_capacity(0) { } SeqList(constSeqList&s) { _array=(Datatype*)malloc(s._size*(sizeof(Datatype))); memcpy(_array,s._array,s._size*(sizeof(Datatype))); _size=_capacity=s._size; } SeqList&operator=(SeqList&s) { free(_array); Swap(s); return*this; } voidSwap(SeqList&s) { _array=s._array; _size=s._size; _capacity=s._capacity; } ~SeqList() { if(_array) { free(_array); _array=NULL; _size=_capacity=0; } } voidPrint() { for(size_ti=0;i<_size;i++) { cout<<_array[i]<<""; } cout< =0) { _array[end+1]=_array[end]; end--; } _array[0]=x; } else { while(end>=(int)pos) { _array[end+1]=_array[end]; end--; } _array[pos]=x; } _size++; } //删除pos位置的元素 voidErase(size_tpos) { assert(pos<_size); //popfront的实现 if(_size>0) { if(pos==0) { intend=0; while(end<(int)_size-1) { _array[end]=_array[end+1]; end++; } _size--; } //popback的实现 elseif(pos==_size) { _size--; } //erase else { intend=pos; while(end<(int)_size-1) { _array[end]=_array[end+1]; end++; } _size--; } } return; } private: Datatype*_array; size_t_size; size_t_capacity; };
二、单链表(不含头结点)示例代码
#include#include usingnamespacestd; typedefintDataType; structSListNode { SListNode*_next; DataType_data; SListNode(DataTypex) :_data(x) ,_next(NULL) {} }; typedefSListNodeNode; classSList { public: SList() :_head(NULL) ,_tail(NULL) {} SList(constSList&s) :_head(NULL) ,_tail(NULL) { Copy(s); } SList&operator=(constSList&s) { Destroy(); Copy(s); return*this; } ~SList() { Destroy(); } voidCopy(constSList&s) { Node*cur=s._head; while(cur) { PushBack(cur->_data); cur=cur->_next; } } voidDestroy() { Node*cur=_head; while(_head!=NULL) { cur=_head; _head=cur->_next; deletecur; } _head=_tail=NULL; } voidPushBack(DataTypex) { if((_head==NULL)&&(_tail==NULL)) { _head=_tail=newNode(x); } else { _tail->_next=newNode(x); _tail=_tail->_next; } } voidPopBack() { if(_head==NULL) { return; } elseif(_head->_next==NULL) { delete_head; _head=_tail=NULL; } else { Node*tmp=_head; while(tmp->_next->_next!=NULL) { tmp=tmp->_next; } _tail=tmp; tmp->_next=NULL; } } voidPushFront(DataTypex) { if((_head==NULL)&&(_tail==NULL)) { _head=_tail=newNode(x); } else { Node*tmp=newNode(x); tmp->_next=_head; _head=tmp; } } voidPopFront() { if(_head==NULL) { return; } Node*cur=_head; _head=_head->_next; deletecur; } Node*Find(DataTypex) { Node*tmp=_head; while(tmp) { if(tmp->_data==x) returntmp; tmp=tmp->_next; } returnNULL; } //插入一个节点在pos的前面 voidInsert(Node*pos,DataTypex) { assert(pos); if(pos==0) { PushFront(x); } else { Node*cur=_head; while(cur->_next!=pos) { cur=cur->_next; } Node*tmp=newNode(x); tmp->_next=pos; cur->_next=tmp; } } voidErase(Node*pos) { assert(pos); if(pos==0) { PopFront(); } elseif(pos->_next==NULL) { PopBack(); } else { Node*cur=_head; while(cur->_next!=pos) { cur=cur->_next; } Node*tmp=cur->_next; cur->_next=tmp->_next; deletetmp; } } voidPrint() { Node*tmp=_head; while(tmp!=NULL) { cout< _data<<"->"; tmp=tmp->_next; } cout<<"NULL"< 总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。