今天来讲创建节点和必要的头文件,后续操作博主会持续更新。
首先我们需要几个头文件,其中包括:
#include<stdio.h>
#include<stdlib.h>//这个是链表创建所需要的,其中包括几个特殊函数(动态内存分配“malloc”函数,以及“free”函数来释放节点中的数据)
//#include<string.h>,若有字符串的相关操作,就需要这个文件。
下面是链表操作中的万能模板:
//第一种是最普通最简单的
struct node
{
int data;//数据域
struct Node *next;//指针域,指向节点的指针。
};
//第二种相对简便后续操作
typedef struct node
{
int data;//数据域
struct Node *next;//指针域,指向节点的指针。
}Node;//此处的Node就相当于struct node。
然后定义全局变量:
struct node*head = NULL;//全局定义链表的头尾指针,方便调用,在后续的函数中就可以直接使用了,就不需要每创建一个函数就要重新定义头尾指针。
struct node*tail = NULL;
具体代码如下:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node*head = NULL;
struct node*tail = NULL;