顺序链表

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
int *p,i;
int k=0;
p=head;
for(i=1;i<=length-1;i++)
{
k++;
if(compare(*p,n))
{//printf("%d",k);
return k++;}
p=p+1;
}
if(compare(*p,n))
return k+2;
return 0;
}
void visit(int n)
{}*/
int priorelem(int *head,int elem)
{ int i;
int k=0;
for(i=0;i<=length-2;i++)
if(*(head+i)==elem)
{
k=i;
break;
}
if(k!=0)
return *(head+i-1);
}
int nextelem(int *head,int elem)
p=head;
for(i=0;i<=length-2;i++)
{
printf("%5d",*p);
p++;
}
printf("***********************************************\n");
}
void listempty(int *head)
{
if(head==NULL)
head=(int*)malloc(initsize*sizeof(int));
if(head==NULL)
{
printf("malloc error");
exit(0);
}
length=0;
size=100;
//printf("when you initlist,you should typein your element as follow\n,so you'd better chose '2'!!!!\n");
size+=increa;
}
*head=n;
head++;
length++;
scanf("%d",&n);
}
printf("***********************************************\n");
}
void printlist(int *head)
{
int *p;
int i;
}
int compare(int a,int b)/*判断a是否比b大一*/
{ if(a==(b+1))
return 1;
else
return 0;
}
int locateelem(int *head,int n,int compare(int a,int b))/*找到表中比输入元素大一元素的位序*/
#include<stdio.h>
#include<stdlib.h>
#define initsize 100
#define increa 10
int length=0;/*包含标记符号-1,实际元素长度要减去一*/
int size=0;/*存储空间大小*/
int *initlist()
{
int *head;
printf("***********************************************\n");
loop:scanf("%d",&n);
switch(n)
{case 1:
{
head=initlist();
typeelem(head);
break;
}
case 2:
{typeelem(head);
{
int i;
if(length>=size)
head=(int*)realloc(head,initsize*sizeof(int));
for(i=length-1;i>=num-1;i--)
{
*(head+i+1)=*(head+i);
}
*(head+i+1)=elem;
length++;
}
int listdelete(int *head,int num)
printf("list unexist\n");
else
printf("list exist\n");
printf("***********************************************\n");
}
/*void destroylist(int *head)
{
int i;
for(i=0;i<=size-2;i++)
{
printlist(head);
break;
}
}
goto loop;
return 0;
}
}
int main()
{
int n,num,loc;
int *head=NULL;
printf("*****************plese chose*******************\n");
printf(" 1.initilst\n 2.typeelem\n 3.destroy\n 4.clearlist\n 5.listempty\n 6.listlength\n 7.getlem\n 8.locateelem\n 9.priorelem\n 10.nextelem\n 11.listinsert\n 12.listdelete\n 13.listtraverse\n 14.printlist\n");
printf("*************************************\n");
break;
}
case 13:{
listtraverse(head,visit);
printf("*************************************\n");
break;
}
case 14:
printf("type in the element\n");
scanf("%d",&num);
listinsert(head,loc,num);
break;
}
case 12:
{
printf("type in the location\n");
scanf("%d",&num);
printf("%d is deleted\n",listdelete(head,num));
{
int i,k;
k=*(head+num-1);
for(i=num-1;i<=length-2;i++)
{
*(head+i)=*(head+i+1);
}
length--;
return k;
}
void destroylist(int *head)
{
free(head+1);
head=NULL;
printf("***********************************************\n");
{
head=NULL;
head++;
}
}
void listlength()
{
printf("length is %d",length);
}
int getelem(int num,int *head)
{
return *(head+num-1);
}
/*int locateelem(int *head,int elem,void compare)
{
head++;
free(head-1);
}
free(head);
printf("***********************************************\n");
}
*/
void clearlist(int *head)
{
int i;
for(i=0;i<=length-1;i++)
{ int i;
int k=0;
for(i=0;i<=length-2;i++)
if(*(head+i)==elem)
{
k=i;
break; /*特殊情况,输入值不符合要求,超出,多个相同的值*/
}
if(k!=0)
return *(head+i+1);
}
void listinsert(int *head,int num,int elem)
{
printf("type in your element\n");
scanf("%d",&num);
printf("the prior is %d",priorelem(head,num));
break;
}
case 10:
{
printf("type in your element\n");
scanf("%d",&num);
{
if((n/2.0-n/2)==0)
printf("%d是偶数\n",n);
}
void listtraverse(int *head,void visit(int n))
{
int *p,i;
p=head;
for(i=1;i<=length-1;i++)
{
visit(*p);
p=p+1;
}
visit(*p);
if(length>=size)
{
p=(int*)realloc(p,initsize*sizeof(int));
size+=increa;
}ቤተ መጻሕፍቲ ባይዱ
scanf("%d",&n);
length++;
while(n!=-1)
{
if(length>=size)
{
p=(int*)realloc(p,initsize*sizeof(int));
break;
}
case 3:
{ destroylist(head);
break;
}
case 4:
{ clearlist(head);
break;
}
case 5:
{
listempty(head);
break;
}
case 6:
{
listlength();
printf("*************************************\n");
break;
}
case 7:
{ printf("type in the element's number you want to get\n");
scanf("%d",&num);
printf("the element is %d",getelem(num,head));
break;
}
case 8:{printf("输入元素\n");
printf("***********************************************\n");
return head;
}
void typeelem(int *p)
{
int n;
int *head;
head=p;
printf("type in you elem \nps.add -1 as an end to have a stop\n输入正整数数据,以-1作为结尾标志,-1不算入链表元素\n");
scanf("%d",&num);
if(locateelem(head,num,compare)==0)
printf("cannot find\n");
else
printf("比%d大1的元素的位序是%d \n",num,locateelem(head,num,compare)) ;
break;
}
case 9:
printf("the prior is %d\n",nextelem(head,num));
printf("*************************************\n");
break;
}
case 11:
{
printf("type in the location\n ");
scanf("%d",&loc);
相关文档
最新文档