1、单链表
如下方式创建了一个单链表,注意链表的节点需要使用 malloc 申请,避免使用局部变量
#include <stdio.h>
#include <stdlib.h>
typedef struct TAG {
int value;
struct TAG *ptr;
} node;
int main ()
{
node rootnode = {0, NULL};
node *root, *temp, *help;
help = root = temp = &rootnode;
// printf("help->value = %d \n", help -> value);
for(int i = 1; i < 11; i++)
{
node *new_node = (node *)malloc(sizeof(node));
if (NULL == new_node)
{
return 0;
}
// node new_node;
new_node->value = i;
new_node->ptr = NULL;
temp->ptr = new_node;
temp = temp->ptr;
}
while(help != NULL)
{
printf("help->value = %d \n", help->value);
help = help -> ptr;
}
return 0;
}
链表的插入有四种情况:插入链表中间位置、插入链表起始位置、插入链表结束位置、插入空链表
单链表替代方案就是双链表,双链表的每个节点都包含两个指针—指向前一个节点的指针和指向后一个节点的指针