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