C语言 翁凯老师 数据结构 链表学习(1)
作者:互联网
链表的建立
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
标签:Node,node,last,number,翁凯,next,链表,C语言 来源: https://blog.csdn.net/weixin_62338898/article/details/123033866