链表可以有效解决可变数组在使用的过程中内存浪费的情况,一下是最基础的链表结构 。
q其中包括链表中最后一个结构的寻找与处理方法。
#ifndef __NODE_H__
#define __NODE_H__
typedef struct _node {
int value;
struct _node* next;
}Node;
#endif
include<stdio.h>
#include<stdlib.h>
#include"node.h"
/*typedef struct _node {
int value;
struct _node* next;
}Node;*/
int main(int argv, char const* argc[])
{
Node* head = NULL;
int number;
do {
scanf_s("%d", &number);
if (number != -1) {
//add to linked-list
Node* p = (Node*)malloc(sizeof(int));
p->value = number;
p->next = NULL;
//find the last
Node* last = head;
if (last) {
while (last->next) {
last = last->next;
}
//attach
last->next = p;
}
else {
head = p;
}
}
} while (number != -1);
return 0;
}