链表查找排序.docx

上传人:b****5 文档编号:29041694 上传时间:2023-07-20 格式:DOCX 页数:33 大小:19.12KB
下载 相关 举报
链表查找排序.docx_第1页
第1页 / 共33页
链表查找排序.docx_第2页
第2页 / 共33页
链表查找排序.docx_第3页
第3页 / 共33页
链表查找排序.docx_第4页
第4页 / 共33页
链表查找排序.docx_第5页
第5页 / 共33页
点击查看更多>>
下载资源
资源描述

链表查找排序.docx

《链表查找排序.docx》由会员分享,可在线阅读,更多相关《链表查找排序.docx(33页珍藏版)》请在冰豆网上搜索。

链表查找排序.docx

链表查找排序

//顺序查找和折?

半ã?

查¨¦找¨°

//折半查找两种?

都?

可¨¦以°?

#include

#include

#include

#include

#include

#include

usingnamespacestd;

intSeqsearch(inta[],intn,intkey)

{

inti=0;

while(i

{

if(a[i]==key)

{

return(i+1);

break;

}

i++;

}

return0;

}

/*intBinarysearch(inta[],intn,intkey)

{

intlow,high,mid;

low=0;

high=n-1;

mid=(int)((low+high)/2);

while(a[mid]!

=key)

{

if(key>a[mid])

{

low=mid+1;

mid=(int)((low+high)/2);

}

else

{

high=mid-1;

mid=(int)((low+high)/2);

}

if(low==high&&n!

=1)

{

//return0;

break;

}

}

if(a[mid]==key)

{

return(mid+1);

}

elsereturn0;

}*/

intBinarysearch(inta[],intn,intkey)

{

intlow,high,mid;

low=0;

high=n-1;

mid=(int)((low+high)/2);

while(low<=high)

{

if(key>a[mid])

{

low=mid+1;

mid=(int)((low+high)/2);

}

elseif(key

{

high=mid-1;

mid=(int)((low+high)/2);

}

elsereturn(mid+1);

}

return0;

}

intmain()

{

while(true)

{

//inty[20];

intdata;

intn,i;

intkey;

intflag;

cout<<"plsinputthearray'slength:

"<

cin>>n;

int*y=newint[n];

for(i=0;i

{

cout<<"plsinputthe"<

"<

cin>>data;

y[i]=data;

}

for(i=0;i

{

cout<

}

cout<

cout<<"plsinputthearray'skeydata:

"<

cin>>key;

//flag=Seqsearch(y,n,key);

flag=Binarysearch(y,n,key);

cout<

system("pause");

}

}

//单Ì£¤向¨°链¢¡ä表À¨ª

#include

#include

#include

#include

#include

usingnamespacestd;

typedefstructstu

{

intdata;

structstu*next;

}node;

node*creat()

{

node*head,*p,*s;

intx,cycle=1;

head=(node*)malloc(sizeof(node));

p=head;

while(cycle)

{

cout<<"plsinputthedata"<

cin>>x;

if(x!

=0)

{

 

s=(node*)malloc(sizeof(node));

s->data=x;

p->next=s;

p=s;

}

else

{

cycle=0;

}

}

head=head->next;

p->next=NULL;

//cout<data<

return(head);

}

voidprint(node*text)

{

node*q;

q=text;

while(q!

=NULL)

{

cout<data<

q=q->next;

}

}

intlength(node*text)

{

node*p;

inti=0;

p=text;

while(p->next!

=NULL)

{

p=p->next;

i++;

}

returni;

}

node*del(node*text,intnum)

{

inti=0;

intj=0;

node*p1,*p2;

booldel;

del=false;

p1=text;

while(p1&&p1->next)

{

if(text->data==num)

{

text=p1->next;

j=1;

}

if(del==false)

{

p2=p1;

}

p1=p1->next;

del=false;

if(p1->data==num)

{

p2->next=p1->next;

//free(p1);

i++;

del=true;

}

}

if(i==0)

{

cout<<"链¢¡ä表À¨ª里¤?

面?

没?

有®D这a个?

数ºy据Y"<

}

else

{

cout<<"已°?

删¦?

除y的Ì?

"<

个?

数ºy是º?

"<

}

return(text);

}

node*insert(node*text,intnum)

{

node*p0,*p1,*p2;

p1=text;

p0=(node*)malloc(sizeof(node));

p0->data=num;

while(p1->data!

=num&&p1->next!

=NULL)

{

p2=p1;

p1=p1->next;

}

if(p1->next==NULL)

{

p1->next=p0;

p0->next=NULL;

}

elseif(text->data==num)

{

p0->next=text;

text=p0;

}

else

{

p2->next=p0;

p0->next=p1;

}

return(text);

}

node*sort(node*text)

{

node*p;

intn;

inttemp;

n=length(text);

/*while(p->next==NULL||p==NULL)

{

p=text;

}*/

for(inti=0;i

{

p=text;

for(intj=1;j

{

if(p->data>p->next->data)

{

temp=p->data;

p->data=p->next->data;

p->next->data=temp;

}

p=p->next;

}

}

returntext;

}

node*reverse(node*text)

{

node*p1,*p2,*p3;

if(text==NULL||text->next==NULL)

{

returntext;

}

p1=text;

p2=p1->next;

//text->next=NULL;

while(p2)

{

p3=p2->next;

p2->next=p1;

p1=p2;

p2=p3;

//if(p3->next!

=NULL)

//{

//p3=p3->next;

//}

}

//p2->next=p1;

text->next=NULL;

text=p1;

returntext;

}

node*merge(node*a,node*b)

{

node*c=NULL;

node*cur=NULL;

node*head=NULL;

//c=(node*)malloc(sizeof(node));

while(a&&b)

{

if(a->datadata)

{

c=a;

a=a->next;

}

else

{

c=b;

b=b->next;

}

if(head==NULL)

{

cur=c;

head=cur;

}

else

{

cur->next=c;

cur=c;

}

}

if(a==NULL)

{

c=b;

}

else

{

c=a;

}

while(c)

{

cur->next=c;

c=c->next;

}

//head=cur;

cur->next=NULL;

returnhead;

}

intmain()

{

//cout<data<

while(true)

{

inti,num;

node*head;

node*a,*b;

/*head=creat();

cout<<"指?

针?

的Ì?

数ºy据Y是º?

êo"<

print(head);

i=length(head);

cout<<"指?

针?

的Ì?

长¡è度¨¨是º?

êo"<

cout<<"请?

输º?

入¨?

欲®?

删¦?

除y的Ì?

数ºy据Y"<

cin>>num;

head=del(head,num);

cout<<"请?

输º?

入¨?

欲®?

插?

入¨?

的Ì?

数ºy据Y"<

cin>>num;

head=insert(head,num);

i=length(head);

cout<<"指?

针?

的Ì?

长¡è度¨¨是º?

êo"<

head=sort(head);*/

a=creat();

cout<<"指?

针?

的Ì?

数ºy据Y是º?

êo"<

print(a);

b=creat();

cout<<"指?

针?

的Ì?

数ºy据Y是º?

êo"<

print(b);

head=merge(a,b);

cout<<"指?

针?

的Ì?

数ºy据Y是º?

êo"<

print(head);

system("pause");

}

}

//双?

向¨°链¢¡ä表À¨ª的Ì?

相¨¤关?

知a识º?

#include

#include

#include

#include

#include

usingnamespacestd;

typedefstructstudent

{

intdata;

structstudent*next;

structstudent*pre;

}dnode;

dnode*creat()

{

dnode*head,*p,*s;

head=(dnode*)malloc(sizeof(dnode));

intx,cycle;

x=1;

cycle=1;

p=head;

while(x)

{

cout<<"plsinputthedata"<

cin>>x;

if(x!

=0)

{

s=(dnode*)malloc(sizeof(dnode));

s->data=x;

p->next=s;

s->pre=p;

p=s;

}

else

{

cycle=0;

}

}

head=head->next;

p->next=NULL;

//head->pre;

return(head);

}

voidprint(dnode*head)

{

dnode*p;

p=head;

while(p!

=NULL)

{

cout<data<

p=p->next;

}

}

dnode*insert(dnode*head,intnum)

{

dnode*p,*s;

s=(dnode*)malloc(sizeof(dnode));

s->data=num;

p=head;

while(s->data>p->data&&p->next!

=NULL)

{

p=p->next;

}

if(s->data<=p->data)

{

if(head==p)

{

s->next=p;

p->pre=s;

s->pre=NULL;

head=s;

}

else

{

p->pre->next=s;

p->pre=s;

s->next=p;

s->pre=p->pre;

}

}

else

{

p->next=s;

s->pre=p;

s->next=NULL;

}

return(head);

}

dnode*del(dnode*head,intnum)

{

dnode*p;

p=head;

//for(p=head;p->next!

=NULL;p=p->next)

while(p->next)

{

/*if(p->data==num)

{

if(p==head)

{

p->next->pre=NULL;

head=head->next;

p=p->next;

}

elseif(p->next!

=NULL)

{

p->pre->next=p->next;

p->next->pre=p->pre;

p=p->next;

}

else

{

p->pre->next=NULL;

free(p);

}

}

elseif(p->data!

=num&&p->next!

=NULL)

{

p=p->next;

}*/

p=p->next;

if(head->data==num)

{

head=head->next;

head->pre=NULL;

}

if(p->data==num&&p->next!

=NULL)

{

p->next->pre=p->pre;

if(p!

=head)

{

p->pre->next=p->next;

}

}

if(p->data==num&&p->next==NULL)

{

p->pre->next=NULL;

}

}

//while(p&&p->next)

//{

//if(p->data)

//}

return(head);

}

intmain()

{

while(true)

{

dnode*head;

intnum;

head=creat();

cout<<"thecreatedlistis:

"<

print(head);

//cout<<"plsinputtheinsertnum:

"<

//cin>>num;

//head=insert(head,num);

//print(head);

cout<<"plsinputthedeletenum:

"<

cin>>num;

head=del(head,num);

print(head);

system("pause");

}

}

//循-环¡¤链¢¡ä表À¨ª

#include

#include

#include

#include

#include

usingnamespacestd;

typedefstructnode

{

intdata;

structnode*link;

}node;

node*creat(intm,intn)

{

intk=n;

inti=1;

node*head,*p,*s;

head=(node*)malloc(sizeof(node));

p=head;

while(i!

=k+1)

{

s=(node*)malloc(sizeof(node));

s->data=i;

p->link=s;

p=s;

i++;

}

head=head->link;

p->link=head;

returnhead;

}

voidcycle_list(intm,intn)

{

node*head,*p,*s;

intk,j,o;

boolaa=false;

inti=0;

k=n;

j=m;

head=creat(j,k);

p=head;

while(p->link!

=p)

{

i++;

if(aa==false)

{

s=p;

p=p->link;

}

aa=false;

if(i==j)

{

cout<data<

o=p->data;

o=s->data;

s->link=p->link;

o=s->link->data;

p=s->link;

o=p->data;

i=0;

aa=true;

}

}

}

voidprint(node*head)

{

node*p;

p=head;

boolflag;

flag=true;

while(flag==true)

{

cout<data<

p=p->link;

if(p==head)

flag=false;

}

}

intmain()

{

//node*head;

intm,n;

m=2;

n=6;

//head=creat(m,n);

//print(head);

cycle_list(m,n);

system("pause");

}

链表逆置

#include

#include

#include

#include

#include

usingnamespacestd;

typedefstructstu

{

intdata;

structstu*next;

}node;

node*creat()

{

node*head,*p,*s;

intx,cycle=1;

head=(node*)malloc(sizeof(node));

p=head;

while(cycle)

{

cout<<"plsinputthedata"<

cin>>x;

if(x!

=0)

{

s=(node*)malloc(sizeof(node));

s->data=x;

p->next=s;

p=s;

}

else

{

cycle=0;

}

}

head=head->next;

p->next=NULL;

//cout<data<

re

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 销售营销

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1