sq->a[i]=0;
sq->length=0;
}
voidcreat(structsqlist*sq)/*建顺序表*/
{inti;
printf("pleaseinputlength");
scanf("%d",&sq->length);
printf("pleaseinput%dnums\n",sq->length);
for(i=1;i<=sq->length;i++)
scanf("%d",&sq->a[i]);
}
voidprint(structsqlist*sq)/*输出顺序表*/
{inti;
for(i=1;i<=sq->length;i++)
printf("%d",sq->a[i]);
printf("\n");
}
voidinsert(structsqlist*sq,intpos,intx)/*顺序表插入元素*/
{inti;
for(i=sq->length;i>=pos;i--)
sq->a[i+1]=sq->a[i];
sq->a[pos]=x;
sq->length=sq->length+1;
}
intdelete(structsqlist*sq,intpos)/*顺序表删除元素*/
{inti,x;
x=sq->a[pos];
for(i=pos+1;i<=sq->length;i++)
sq->a[i-1]=sq->a[i];
sq->length=sq->length-1;
return(x);
}
main()
{intposition,x;
structsqlist*list;
structsqlistslist;
intxz=0;
list=&slist;
while
(1)
{printf("1.init\n");
printf("2.creat\n");
printf("3.insert\n");
printf("4.delete\n");
printf("5.locate_value\n");
printf("6.locate_pos\n");
printf("7.print\n");
printf("0.exit\n");
printf("pleaseinputyourchoice");
scanf("%d",&xz);
switch(xz)
{case1:
init(list);break;
case2:
creat(list);break;
case3:
printf("pleastinputinsetposition(pos)andvalue(x)");
scanf("%d%d",&position,&x);
if(position<1||position>list->length+1||list->length>=LEN)
printf("positionerror\n");
elseinsert(list,position,x);
break;
case4:
printf("pleastinputdeleteposition(pos)");
scanf("%d",&position);
if(position<1||position>list->length||list->length==0)
printf("positionerror\n");
else
printf("deleteposition=%d,deletedata=%d\n",position,delete(list,position));
break;;
case5:
;
case6:
;
case7:
print(list);break;
case0:
exit(0);
}}}
2.三种方法建立链表
#include
typedefstructnode
{intdata;
structnode*link;}NODE;
NODE*creat1()
/*按输入数据的顺序建立链表,输入数据通过个数控制*/
{inti,data,n;
NODE*h=NULL,*p,*last=NULL;
printf("pleaseinputthenum:
");
scanf("%d",&n);
printf("pleaseinput%ddatas:
",n);
for(i=1;i<=n;i++)
{p=(NODE*)malloc(sizeof(NODE));
scanf("%d",&p->data);
if(i==1)h=p;
elselast->link=p;
last=p;
}
last->link=NULL;
return(h);
}
NODE*creat2()
/*按输入数据的逆序建立链表,输入数据以0结束*/
{intdata;
NODE*h=NULL,*p;
printf("pleaseinputdatas(0end)\n");
scanf("%d",&data);
while(data)
{p=(NODE*)malloc(sizeof(NODE));
p->data=data;
if(h==NULL){h=p;h->link=NULL;}
else{p->link=h;h=p;}
scanf("%d",&data);
}
return(h);
}
NODE*creat3()
/*按输入数据的大小顺序建立带头结点的链表,输入数据以0结束*/
{intdata;
NODE*h,*p,*q,*r;
h=(NODE*)malloc(sizeof(NODE));h->link=NULL;
printf("pleaseinputdatas(0end)\n");
scanf("%d",&data);
while(data)
{p=(NODE*)malloc(sizeof(NODE));
p->data=data;p->link=NULL;
if(h->link==NULL)h->link=p;
else
{r=h;q=r->link;
while(p->data>q->data&&q)
{r=q;q=q->link;}
if(q)
p->link=q;
r->link=p;
}
scanf("%d",&data);
}
return(h->link);
}
main()
{NODE*h,*p;
intx;
do
{printf("=====================\n");
printf("1.zhengxujianlianbiao\n");
printf("2.nixujianlianbiao\n");
printf("3.jianliyouxulianbiao\n");
printf("0.tuichu\n");
printf("=====================\n");
printf("pleaseinputyourchosice");
scanf("%d",&x);
switch(x)
{case1:
h=creat1();break;
case2:
h=creat2();break;
case3:
h=creat3();break;
case0:
return;
}
p=h;
while(p)
{printf("%5d",p->data);
p=p->link;
}
printf("\n\n");
}
while(x);
}
3.试写出逆转线性单链表的算法
要逆转一个线性单链表,只需从头指针指向的结点开始扫描该链表,在扫描过程中改变各结点的指针(由指向后件改为指向原来的前件)即可。
Structnode/*ET位数据元素类型*/
{ETd;
structnode*next
};
invlst(head)
structnode**head;
structnode*p,*q,*r;
{if(*head==NULL)return;
p=*head;q=p->next;
p->next=NULL;
while(q!
=NULL)
{r=q->next;q->next=p;
p=q;q=r;
}
*head=p;
return;
}
4.设有两个有序线性单链表,头指针分别为AH和BH。
试写出将两个有序线性单链表合并为一个头指针为CH的有序线性单链表的算法,要求去掉重复元素。
Structnode/*ET位数据元素类型*/
{ETd;
structnode*next
};
#include“stdio.h”
mglst1(ah,bh,ch)
structnodeah,bh,**ch;
{structnode*i,*j,*k,*p;
etx;
i=ah;j=bh;*ch=NULL;k=NULL;
while((i!
=NULL)&&(j!
=NULL))
{if(j->d>=i->d){x=i->d;i=i->next;}
else{x=j->d;j=j->next;}
if(*ch==NULL)
{p=(structnode*)malloc(sizeof(structnode));
p->d=x;*ch=p;k=p;
}
elseif(d!
=k->d)
{p=(structnode*)malloc(sizeof(structnode));
p->d=x;k->next=p;k=p;}}}
if(j==NULL)
while(i!
=NULtructL)
{x=i->d;i=i->next;
if(*ch==NULL)
{p=(structnode*)malloc(sizeof(structnode));
p->d=x;*ch=p;k=p;
}
elseif(d!
=k->d)
{p=(structnode*)malloc(sizeof(structnode));
p->d=x;k->next=p;k=p;
}}
else
while(j!
=NULL)
{x=j->d;j=j->next;
if(*ch==NULL)
{p=(structnode*)malloc(sizeof(structnode));
p->d=x;*ch=p;k=p;
}
elseif(d!
=k->d)
{p=(structnode*)malloc(sizeof(structnode));
p->d=x;k->next=p;k=p;
}}
if(k!
=NULL)k->next=NULL;
return;
}
5.试编写在二叉排序树中插入一个元素的算法。
#include“stdlib.h”
structbtnode
{ETd;
structbtnode*lchild;
structbtnode*rchild;
};
structbtnode*insort(bt,b)
structbtnode*bt;