C语言,写顺序链表,能添加几个数据就行

如题所述

#include<stdio.h>
#include<stdlib.h>

typedef struct _node{
int num;
struct _node* next;
}Node;

int main()
{
int n;
Node* head = NULL;

do{
scanf("%d", &n); //输入值
if (n != -1)
{
Node* p = (Node*)malloc(sizeof(Node));
p->num = n;
p->next = NULL;

//看 链表是否 空或者 已经存在
Node * last = head;
if (last)
{
while (last->next)
{
last = last->next;
}
last->next = p;
}
else{
head = p;
}
}
} while (n != -1);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
相似回答