【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
- 单链表(c++)
- 单链表(c++)
- 单链表C
- C单链表
- c 单链表
- 【c++】单链表
- c单链表
- 【C】单链表
- C 单链表
- C/C++,数据结构单链表实现约瑟夫环
- 单链表的操作(C#)
- 单链表 C程序
- 单链表(C#)实现
- 数据结构(C#)--单链表
- 数据结构(C#)--单链表
- C ++ 动态单链表 代码
- 用C实现单链表
- c 单链表操作
- CCF 201609-1 最大波动 解题报告 简单模拟
- static 变量和方法
- 内联元素的padding,margin,border等不起作用的原因
- HDU 5915 (二分 主席树)
- 本地git仓库提交远程失败的解决办法
- 【c++】单链表
- web常用标签
- 为什么C++不能有虚构造函数,却可以有虚析构函数
- Android完美满足你所有的拍照或从相册中选取照片
- Hdu1323 Perfection Time
- openstack中的rpc通信
- Mysql性能优化(1) 从新建一个表开始
- 笔试算法学习--买票找零问题(卡特兰数)
- 三十岁那年,我的梦想是年薪十万