link2.c
来自「自己做的常用库和实现的数据结构。public domain.」· C语言 代码 · 共 43 行
C
43 行
#include<stdio.h>
#include<stdlib.h>
struct node
{int value;
struct node *next;
};
struct node *create(int a[],int n)
{struct node *head,*q;
for(head=NULL;n;n--) /* intersting here, init a link in a rev order*/
{q=(struct node*)malloc(sizeof(struct node ));
q->value=*a++;
q->next=head;head=q;
}
return(head);
}
struct node *copy(struct node*h)
{struct node *head,*p,*q,*u,*v;
head=NULL;p=h;
while(p)
{q=(struct node *)malloc(sizeof(struct node));
q->value=p->value;
v=head;
while(v!=NULL&&v->value<q->value)
{u=v;v=v->next;}
if(v==head)head=q;
else u->next=q;
q->next=v;p=p->next;
}
return(head);
}
int d[]={5,9,3,4,5,7,8};
main()
{struct node *h1,*h2,*p;
h1=create(d,sizeof(d)/sizeof(d[0]));
for(p=h1;p;p=p->next)
printf("%5d",p->value);
printf("\n\n");
h2=copy(h1);
for(p=h2;p;p=p->next)
printf("%5d",p->value);
printf("\n");
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?