创建
#include<stdio.h>
#include<stdlib.h>
typedef int Item;
typedef struct node
{
Item data;
struct node *next;
}Node;
Node *first = NULL; //初始化头指针
Node *last = NULL; //初始化尾指针
//在链表开始处插入结点
//添加尾指针简化链表操作
void add_to_list(Item x)
{
Node *new_node;
new_node = (Node *)malloc(sizeof(Node));
if (new_node == NULL)
terminate("Error: malloc failed in add_to list!");
new_node->data = x;
new_node->next = first;
first = new_node;
new_node->next = first; //尾结点指向头结点
last = new_node->next; //移动尾指针,使其保持在链表尾端
}