欢迎来到天天文库
浏览记录
ID:11406018
大小:54.00 KB
页数:4页
时间:2018-07-11
《用链表排序删除相同项》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、问题分析:因为是非降幂排序的,所以接连比较相邻的两位,相同则删掉后一个算法分析:step1设置三个指针,两个分别指向相邻的两个数,第三个指针暂存要被删的结点Step2用while语句循环依次向后比较相邻的两位,相同删掉一个,不同则两个指针分别后移,直到结束源程序:#include//cout,cin#include"process.h"//exit()#defineMax20typedefstruct{inte;}PolyArray[Max];structPolyNode{inte;PolyNode*next;};
2、classPoly{private:public:PolyNode*head;Poly();//构造函数,建立空数值~Poly();//析构函数,释放数值voidCreate(PolyArraya,intn);//键盘输入,创建数值链表voidDisp();//数值显示voidSort();//有序表排序//intGetMax();};Poly::Poly(){//创建一空数值head=newPolyNode;head->next=NULL;}Poly::~Poly(){//释放数值所占空间PolyNode*p;while(head!=N
3、ULL){//从头结点开始,依次释放结点p=head;head=head->next;deletep;}head=NULL;//头结点指向空}voidPoly::Disp(){PolyNode*p;p=head->next;while(p!=NULL){cout<e<<'t';p=p->next;}cout<4、->e=a[i].e;s->next=NULL;r->next=s;r=s;}}voidPoly::Sort(){PolyNode*p,*q,*r;p=head->next;if(p!=NULL){r=p->next;//r指向p的后继结点while(r!=NULL){if(p->e==r->e){q=p->next;p->next=q->next;//摘链//q->next=r;//deleteq;r=p->next;}else{p=p->next;r=p->next;}}}}voidmain(){PolyLA;PolyNode*p;/5、/p1=LA.head;PolyArraya={{1},{2},{2},{3}};LA.Create(a,4);cout<<"原数值A为:";LA.Disp();cout<<"删除后A为:";LA.Sort();LA.Disp();while(LA.head!=NULL){//从头结点开始,依次释放结点p=LA.head;LA.head=LA.head->next;deletep;}LA.head=NULL;//头结点指向空}运行截图:
4、->e=a[i].e;s->next=NULL;r->next=s;r=s;}}voidPoly::Sort(){PolyNode*p,*q,*r;p=head->next;if(p!=NULL){r=p->next;//r指向p的后继结点while(r!=NULL){if(p->e==r->e){q=p->next;p->next=q->next;//摘链//q->next=r;//deleteq;r=p->next;}else{p=p->next;r=p->next;}}}}voidmain(){PolyLA;PolyNode*p;/
5、/p1=LA.head;PolyArraya={{1},{2},{2},{3}};LA.Create(a,4);cout<<"原数值A为:";LA.Disp();cout<<"删除后A为:";LA.Sort();LA.Disp();while(LA.head!=NULL){//从头结点开始,依次释放结点p=LA.head;LA.head=LA.head->next;deletep;}LA.head=NULL;//头结点指向空}运行截图:
此文档下载收益归作者所有