链表的建立
linked-list.c
#include"node.h"
#include<stdio.h>
#include<stdlib.h>
// typedef struct _node
// {
// int value;
// struct _node *next;
// } Node;
int main()
{
Node * head = NULL;//初始化
int number;
do{
scanf("%d",&number);
if(number != 1){
//add to linked-list
Node *p = (Node*)malloc(sizeof(Node));
p->value = number;
p->next = NULL;
//find the last
Node *last = head;
if(last){
while (last->next){//遍历到last->next=NULL为止,保证数据存储在链表的最后一位
last = last->next;
}
//attach
last->next = p;
}else{
head = p;
}
}
}while (number != -1);
printf("%d",head->value);//为了验证链表的有效性
return 0;
}
node.h
#ifndef _NODE_H_
#define _NODE_H_
typedef struct _node
{
int value;
struct _node *next;
} Node;
#endif