【c++】单链表

来源:互联网 发布:细说php第四版 编辑:程序博客网 时间:2024/05/16 13:38

单链表的内容C语言博客已经描述过,这里只贴上c++代码

#include<iostream>#include<stdlib.h>#include<assert.h>using namespace std;typedef int DataType;class LinkNode{friend ostream& operator<<(ostream&os,LinkNode& n);public:LinkNode(DataType x):data(x),next(NULL){}//private:    DataType data;    LinkNode* next;};ostream& operator<<(ostream&os,LinkNode& n){os<<n.data<<" ";return os;}class LinkList{friend ostream& operator<<(ostream& os,LinkList& list);public:LinkList():pHead(NULL)    ,pTail(NULL){}~LinkList(){LinkNode* cur=pHead;while(cur){LinkNode* tmp=cur;cur=cur->next;delete tmp;}}void PushBack(const DataType& x)//尾插 {LinkNode* tmp=new LinkNode(x);if(pTail!=NULL){pTail->next=tmp;pTail=tmp;}else{pHead=tmp;pTail=tmp;}}void PopBack()//尾删 {LinkNode* cur=NULL;cur = pHead;if(pHead==NULL){return;}else if(cur->next==NULL){   pHead=cur->next;free(cur);cur=NULL;}else{while(cur->next){pTail=cur;cur=cur->next;}free(cur);cur=NULL;pTail->next=NULL;}}void PushFront(const DataType& x)//头插 {{LinkNode* cur=pHead;LinkNode* tmp=new LinkNode(x);tmp->data=x;tmp->next=cur;pHead=tmp;}}void PopFront()//头删 {LinkNode* cur=NULL;cur=pHead;if(cur==NULL)printf("链表为空\n");pHead=cur->next;free(cur);cur=NULL;}LinkNode* Find(const DataType x){LinkNode* cur=pHead;while(cur){if(cur->data==x)break;cur=cur->next;}return cur;}void Insert(LinkNode* pos,const DataType& x)//指定为插入 {LinkNode* cur=NULL;LinkNode* pre=NULL;DataType tmp=x;LinkNode* newNode=new LinkNode(x);newNode->data=tmp;cur=pHead;if(cur==NULL){PushFront(x);return;}if(pos==NULL){printf("无此位置\n");return;}newNode->next=pos->next;pos->next=newNode;tmp=pos->data;pos->data=newNode->data;newNode->data=tmp;}void Remove(const DataType& x)//指定数删除 {LinkNode* cur;cur=pHead;while(cur){if(cur->data==x){LinkNode* p=NULL;cur->data=cur->next->data;p=cur->next;cur->next=cur->next->next;free(p);p=NULL;}cur=cur->next;}return;}void RemoveAll(const DataType& x)//删除所有x {LinkNode* cur=NULL;LinkNode* pre=NULL;cur=pHead;while(cur){if(cur->data==x){ pre=cur->next;if(cur==pHead){PopFront();cur=pre;}else if(cur->next==NULL){PopBack();cur=NULL;break;}        else{LinkNode* p=NULL;cur->data=cur->next->data;p=cur->next;cur->next=cur->next->next;free(p);p=NULL;}}else {cur=cur->next;}}}void Erase(LinkNode* pos)//指定位删除{LinkNode* cur;LinkNode* pre;cur=pHead;if(pos==cur){PopFront();return;}while(pos!=cur){pre=cur;cur=cur->next;}pre->next=pos->next;free(pos);pos=NULL;}void BubbleSort()//排序 {LinkNode* i;LinkNode* j;for(i=pHead;i!=NULL;i=i->next){for(j=i->next;j!=NULL;j=j->next){if(j->data<i->data){DataType tmp=j->data;j->data=i->data;i->data=tmp;}}}}void InsertFrontNode(LinkNode* pos,const DataType& x)//当前节点插入{LinkNode* newNode=NULL;DataType tmp;newNode=BuyNode(x);newNode->next=pos->next;pos->next=newNode;tmp=pos->data;pos->data=newNode->data;newNode->data=tmp;}LinkNode* BuyNode(const DataType& x)//开辟新结点  {   LinkNode* tmp = new LinkNode(x);   tmp->data = x;   tmp->next = NULL;   return tmp;  }  private:LinkNode* pHead;LinkNode* pTail;};ostream& operator<<(ostream& os,LinkList& list){LinkNode* cur=list.pHead;while(cur){os<<(*cur)<<" ";cur=cur->next;}os<<endl;return os;}
测试用例void test(){LinkList List;List.PushBack(1);List.PushBack(2);List.PushBack(3);List.PushBack(4);List.PushBack(5);List.PushBack(6);cout<<List<<endl;List.PopBack();cout<<List<<endl;List.PushFront(9);cout<<List<<endl;List.PushFront(10);cout<<List<<endl;List.PopFront();cout<<List<<endl;List.Insert(List.Find(2),2);cout<<List<<endl;List.Remove(4);cout<<List<<endl;List.RemoveAll(2);cout<<List<<endl;List.Erase(List.Find(3));cout<<List<<endl;List.BubbleSort();cout<<List<<endl;List.InsertFrontNode(List.Find(1),5);cout<<List<<endl;}


0 0
原创粉丝点击