#include <stdio.h>
#include <stdlib.h>
struct Test
{
int data;
struct Test *next;
};
void printflink(struct Test *head)
{
struct Test *p=head;
while(p != NULL){
printf("%d ",p->data);
p=p->next;
}
putchar('\n');
}
struct Test *sf1(struct Test *new,struct Test *head)//第一种函数分装
{
struct Test *p=head;
if(p == NULL){
head=new;
return head;
}
while(p->next != NULL){
p=p->next;
}
p->next=new;
return head;
}
struct Test *dwc(struct Test *head)//第二种
{
struct Test *p=head;
int i=0;
int j=0;
struct Test *new=NULL;
while(1){
new=(struct Test *)malloc(sizeof(struct Test));
printf("plese input your number\n");
scanf("%d",&(new->data));
if(new->data == 0){
free(new);
return head;
}
/* if(p == NULL){
p=head=new;
printf(" i= %d\n",i++);
continue;
}
while(p->next != NULL){
printf("j=%d\n",j++);
p=p->next;
}
p->next=new;*/
head=sf1(new,head);
}
}
int main()
{
struct Test *head=NULL;
head=dwc(head);
printflink(head);
return 0;
}
linux 创建动态链表尾插法两种方法
最新推荐文章于 2024-08-30 14:39:56 发布