创建一个链表

 不知道为什么总是忘了对链表的操作, 主要就是平时用的少, 希望自己通过写这编文章能加深对链表操作的印象

目录

1.首先得要有两个基本的头文件

2.再然后得要有个结构体

 3. 这部分是函数前置声明

4.链表初始化

 5.插入节点

 6.打印整个链表

7. 释放整个链表的内存

 8.整个程序示例

9.打印结果


1.首先得要有两个基本的头文件

#include<stdio.h>       //用于scanf, printf 标准输入输出

#include<stdlib.h>      //用于malloc, free 内存申请及释放

2.再然后得要有个结构体

一个节点就像火车的一节车厢, data是车厢里面的东西, next相当于一个钩子, 用于将车厢之间的连接起来

typedef struct node          //typedef 用于取struct node 的别名
{
        int data;
        struct node *next;
}*PNODE,NODE;                //别名*PNODE 就是 struct node *   别名NODE就是 struct node

 3. 这部分是函数前置声明

PNODE init();                           //链表初始化
void insert(PNODE head, int newdata);   //插入节点
void print(PNODE head);                 //打印整个链表
void freeAllNode(PNODE head);           //释放整个链表的内存

4.链表初始化

定义一个节点, 其在整个链表操作中都不存储数据, 比较特殊, 与head直接相连

PNODE init()
{
	PNODE temp = (PNODE)malloc(sizeof(NODE));  //定义一个节点
	temp->next = NULL;                    //这个节点比较特殊, 它不用于存储数据
	return temp;
}

 5.插入节点

插入节点有两种方式: 1是头插, 2是尾插

先看头插

void insert(PNODE head,int newdata)
{

	PNODE temp=(PNODE)malloc(sizeof(struct node));  //先开辟一块内存用于存放要插入的数据
	temp->data = newdata;
        
        //头插
	temp->next = head->next;    //head->next就是节点A, 将temp连向A
	head ->next= temp;          //将head相向temp
}

再看尾插

void insert(PNODE head,int newdata)
{

	PNODE temp=(PNODE)malloc(sizeof(struct node));
	temp->data = newdata;

	//尾插
	PNODE p = (PNODE)malloc(sizeof(NODE));
	p = head;
	while (p->next != NULL) p = p->next;  //先定义一个p, 使它指向最后一个节点
	p->next = temp;                       //将p连向temp
	temp->next = NULL;                    //temp连向NULL
}

 6.打印整个链表

链表的打印只能从head开始遍历, 不能像数组可以使用下标那么便利

void print(PNODE head)
{
	PNODE temp=head->next;   //定义temp指向A, 即第一个有内容的节点('空的'的下一个节点)
	while (temp!=NULL)       //在temp不为NULL是, 一直循环, 直到temp==NULL, 即遍历完了
	{
		printf("%d->", temp->data);  //打印节点里面的data
		temp = temp->next;          //指针的移动, 指向下一个节点
	}
	printf("NULL\n");
}

7. 释放整个链表的内存

void freeAllNode(PNODE head)
{
	PNODE p = head,temp;
	while (p != NULL)
	{
		temp = p->next;
		free(p);
		p = temp;
	}
}

 8.整个程序示例

#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
	int data;
	struct node *next;
}*PNODE,NODE;
void insert(PNODE head, int newdata);
void print(PNODE head);
PNODE init();
void freeAllNode(PNODE head);
int main()
{
	PNODE head = init();    //接受init()的返回值, 即指向了temp
	for (int i = 0; i < 10;i++)
	 insert(head,i);
	print(head);
        freeAllNode(head);
	return 0;
}
PNODE init()
{
	PNODE temp = (PNODE)malloc(sizeof(NODE));
	temp->next = NULL;
	return temp;
}
void insert(PNODE head,int newdata)
{

	PNODE temp=(PNODE)malloc(sizeof(struct node));
	temp->data = newdata;
	//头插
	temp->next = head->next;
	head ->next= temp;
#if 0
	//尾插
	PNODE p = (PNODE)malloc(sizeof(NODE));
	p = head;
	while (p->next != NULL) p = p->next;
	p->next = temp;
	temp->next = NULL;
#endif
}
void print(PNODE head)
{
	PNODE temp=head->next;
	while (temp!=NULL)
	{
		printf("%d->", temp->data);
		temp = temp->next;
	}
	printf("NULL\n");
}
void freeAllNode(PNODE head)
{
	PNODE p = head,temp;
	while (p != NULL)
	{
		temp = p->next;
		free(p);
		p = temp;
	}
}

9.打印结果

9->8->7->6->5->4->3->2->1->0->NULL
请按任意键继续. . .

猜你喜欢

转载自blog.csdn.net/Mr_HCW/article/details/82026333