静态链表和动态链表

1.静态链表分配在栈上,实现链表的初始化以及遍历的功能

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>

//结点的结构体
struct LinkNode
{
	int num;//数据域
	struct LinkNode *next;//指针域
};

void test01()
{
	//创建结点
	struct LinkNode node1 = { 10, NULL };
	struct LinkNode node2 = { 20, NULL };
	struct LinkNode node3 = { 30, NULL };
	struct LinkNode node4 = { 40, NULL };
	struct LinkNode node5 = { 50, NULL };

	//建立关系
	node1.next = &node2;
	node2.next = &node3;
	node3.next = &node4;
	node4.next = &node5;

	//遍历链表
	struct LinkNode *pCurrent = &node1;
	
	while (pCurrent != NULL)
	{
		printf("%d\n", pCurrent->num);
		pCurrent = pCurrent->next;
	}
}

int main(void)
{
	test01();

	system("pause");
	return EXIT_SUCCESS;
}

2.动态链表分配在堆上,实现链表的初始化以及遍历的功能

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>

struct LinkNode 
{
	int num;
	struct LinkNode * next;
};

void test01()
{
	//创建结点
	struct LinkNode * node1 = (struct LinkNode *)malloc(sizeof(struct LinkNode));
	struct LinkNode * node2 = (struct LinkNode *)malloc(sizeof(struct LinkNode));
	struct LinkNode * node3 = (struct LinkNode *)malloc(sizeof(struct LinkNode));
	struct LinkNode * node4 = (struct LinkNode *)malloc(sizeof(struct LinkNode));
	struct LinkNode * node5 = (struct LinkNode *)malloc(sizeof(struct LinkNode));

	//给数据域赋值
	node1->num = 100;
	node2->num = 200;
	node3->num = 300;
	node4->num = 400;
	node5->num = 500;

	//建立关系
	node1->next = node2;
	node2->next = node3;
	node3->next = node4;
	node4->next = node5;
	node5->next = NULL;

	//遍历链表
	struct LinkNode * pCurrent = node1;
	while (pCurrent != NULL)
	{
		printf("%d\n", pCurrent->num);
		pCurrent = pCurrent->next;
	}

	free(node1);
	free(node2);
	free(node3);
	free(node4);
	free(node5);

	node1 = NULL;
	node2 = NULL;
	node3 = NULL;
	node4 = NULL;
	node5 = NULL;

}

int main(void)
{
	test01();

	system("pause");
	return EXIT_SUCCESS;
}

3.带头结点链表和不带头结点链表

3.1 带头结点链表的优点:带头结点的链表永远固定了头节点的位置

初始化链表和遍历功能

头文件.h:

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>

struct LinkNode
{
	int num;
	struct LinkNode * next;
};

//初始化链表
struct LinkNode * initLinkList();

//遍历链表
void foreach_LinkList(struct LinkNode * pHeader);

函数实现.c

#include "linkList.h"

//初始化链表
struct LinkNode * initLinkList()
{
	//创建头节点
	struct LinkNode * pHead = (struct LinkNode *)malloc(sizeof(struct LinkNode));

	if (pHead == NULL)
	{
		return NULL;
	}

	//初始化头节点,头节点不维护数据域
	pHead->next = NULL;

	//记录尾结点的位置,方便插入新的数据
	struct LinkNode * pTail = pHead;
	int val = -1;
	while (1)
	{
		//让用户初始化几个结点,如果用户输入的是-1,代表插入结束
		printf("################请初始化您的数据################\n");
		printf("如果输入-1代表结束输入\n");

		scanf("%d", &val);

		if (val == -1)
		{
			break;
		}
		
		//如果输入的不是-1,插入结点到链表中
		struct LinkNode * newNode = (struct LinkNode *)malloc(sizeof(struct LinkNode));
		newNode->num = val;
		newNode->next = NULL;

		//更改指针的指向
		pTail->next = newNode;
		//更新新的尾结点
		pTail = newNode;
	}
	return pHead;
}

//遍历链表
void foreach_LinkList(struct LinkNode * pHeader)
{
	if (pHeader == NULL)
	{
		return;
	}

	struct LinkNode * pCurrent = pHeader->next; //指向第一个有真实数据的结点

	while (pCurrent != NULL)
	{
		printf("%d\n", pCurrent->num);
		pCurrent = pCurrent->next;
	}
}

测试文件.c

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>
#include "linkList.h"

void test01()
{
	//初始化链表
	struct LinkNode * pHead = initLinkList();

	//遍历链表
	printf("###########遍历链表的结果为#############\n");
	foreach_LinkList(pHead);
}

int main(void)
{
	test01();

	system("pause");
	return EXIT_SUCCESS;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值