c精彩编程

来源:百度知道 编辑:UC知道 时间:2024/09/20 17:31:53
数据结构C语言版 创建一个不定长链表
以最简代码编写,不超过30行。

以前写的,发一下。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct list
{ int data;
struct list *next;
};
typedef struct list node;
typedef node *link;
int main()
{ int i,n;
link ptr,q,head;
head=(link)malloc(sizeof(node));
ptr=head;
printf("Please input array. When input isn't digit, end.\n");
while(scanf("%d",&n)==1)
{ ptr->data=n;
ptr->next=(link)malloc(sizeof(node));
q=ptr;
ptr=ptr->next;
}
q->next=NULL;/* 释放最后一次多申请的空间 */
free(ptr);
ptr=head;
while(ptr!=NULL)
{ printf("The value is ==>%d\n",ptr->data);
ptr=ptr->next;
}
free(head);/* 释放链表空间 */
system("pause");
return 0;
}