数据结构---详解单链表-三、链表的代码总览

1、SList.h

#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

typedef int SLTDataType;

typedef struct SListNode
{
	SLTDataType data;
	struct SListNode* next;
}SLTNode;

//打印
void SLTPrint(SLTNode *phead);
//尾插
void SLTPushBack(SLTNode** pphead, SLTDataType x);
//头插
void SLTPushFront(SLTNode** pphead, SLTDataType x);
//尾删
void SLTPopBack(SLTNode** pphead);
//头删
void SLTPopFront(SLTNode** pphead);
//查找
SLTNode* SLTFind(SLTNode* phead,SLTDataType x);
//在指定位置之前插入数据
void SLInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x);
//在指定位置之后插入数据
void SLInsertAfter(SLTNode* pos, SLTDataType x);
//删除pos结点
void SLTErase(SLTNode** pphead, SLTNode* pos);
//删除pos之后的结点
void SLTEraseAfter(SLTNode* pos);
//销毁链表
void SListDestroy(SLTNode** pphead);

2、SList.c

#define  _CRT_SECURE_NO_WARNINGS 1
#include "SList.h"

//创建一个节点
SLTNode* SLTBuyNode(SLTDataType x)
{
	//给节点开辟一个空间
	SLTNode* node = (SLTNode*)malloc(sizeof(SLTNode));
	if (node == NULL)
	{
		perror("malloc fail!");
		exit(1);
	}
	//如果不是NULL,就把x放在新的节点上
	node->data = x;
	node->next = NULL;

	return node;
}
//打印
void SLTPrint(SLTNode *phead) 
{
	//重新创建一个pcur指针是为了避免指针指向的改变
	//导致无法重新找到首节点
	SLTNode* pcur = phead;
	//链表遍历和数组有很大区别
	while (pcur != NULL)
	{
		printf("%d -> ", pcur->data);
		//打印完后要指向下一个地址
		pcur = pcur->next;
	}
	printf("NULL\n");
}
//尾插
void SLTPushBack(SLTNode **pphead, SLTDataType x)
{
	SLTNode* newnode = SLTBuyNode(x);
	//判断是不是空链表
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	else
	{
		//如果链表非空,找尾节点
		SLTNode* ptail = *pphead;
		while (ptail->next != NULL) //可以写成ptail->next
		{
			//遍历找到尾节点
			ptail = ptail->next;
		}
		//找到后跳出循环,将新创建的节点赋值过去
		ptail->next = newnode;
	}
}
//头插
void SLTPushFront(SLTNode** pphead, SLTDataType x)
{
	assert(pphead);
	SLTNode* newnode = SLTBuyNode(x);
	newnode->next = *pphead;
	*pphead = newnode;
}
//尾删
void SLTPopBack(SLTNode** pphead)
{
	//分两种情况,只有一个节点的时候
	if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else {
		//第一个是传参的时候不能为空
	   //第二个人是链表不能为空
		assert(pphead && *pphead);
		//创建两个指针遍历
		SLTNode* ptail = *pphead;
		SLTNode* prec = NULL;
		while (ptail->next)
		{
			prec = ptail;
			ptail = ptail->next;
		}
		prec->next = NULL;
		free(ptail);
		ptail = NULL;
	}
}
//头删
void SLTPopFront(SLTNode** pphead)
{
	assert(*pphead && *pphead);

	SLTNode* next = (*pphead)->next;
	free(*pphead);
	*pphead = next;
}
//查找
SLTNode* SLTFind(SLTNode* phead, SLTDataType x)
{
	SLTNode* pcur = phead;
	while (pcur)
	{
		if (pcur->data == x)
		{
			return pcur;
		}
		pcur = pcur->next;
	}
	//没找到
	return NULL;
}
//在指定位置之前插入数据
void SLInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{
	assert(pphead && pos);

	if (pos == *pphead)
	{
		//头插
		SLTPushFront(pphead, x);
	}
	else {
		//创建一个节点
		SLTNode* newnode = SLTBuyNode(x);
		SLTNode* prev = *pphead;
		while (prev->next != pos)
		{
			//next不是pos的时候继续往下遍历
			prev = prev->next;
		}
		//prev->next是pos的时候
		newnode->next = pos;
		prev->next = newnode;
	}
}
//在指定位置之后插入数据
void SLInsertAfter(SLTNode* pos, SLTDataType x)
{
	assert(pos);
	SLTNode* newnode = SLTBuyNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}
//删除pos结点
void SLTErase(SLTNode** pphead, SLTNode* pos)
{
	assert(pphead && pos);

	if (pos == *pphead)
	{
		//要删除的节点就是头节点时
		SLTPopFront(pphead);
	}
	else {
		SLTNode* prev = *pphead;
		while (prev->next != pos)
		{
			prev = prev->next;
		}
		prev->next = pos->next;
		free(pos);
		pos = NULL;
	}
}
//删除pos之后的结点
void SLTEraseAfter(SLTNode* pos)
{
	assert(pos && pos->next);
	SLTNode* del = pos->next;
	pos->next = del->next;
	free(del);
	del = NULL;
}
//销毁链表
void SListDestroy(SLTNode** pphead)
{
	assert(pphead);
	SLTNode* pcur = *pphead;
	while (pcur)
	{
		SLTNode* next = pcur->next;
		free(pcur);
		pcur = next;
	}
	*pphead = NULL;
}

3、test.c

#define  _CRT_SECURE_NO_WARNINGS 1
#include "SList.h"

void test1()
{
	//手动构建一个链表
	//创建空间
	SLTNode* node1 = (SLTNode*)malloc(sizeof(SLTNode));
	SLTNode* node2 = (SLTNode*)malloc(sizeof(SLTNode));
	SLTNode* node3 = (SLTNode*)malloc(sizeof(SLTNode));
	SLTNode* node4 = (SLTNode*)malloc(sizeof(SLTNode));
	//初始化
	node1->data = 1;
	node2->data = 2;
	node3->data = 3;
	node4->data = 4;
	//连接节点
	node1->next = node2;
	node2->next = node3;
	node3->next = node4;
	node4->next = NULL;
	//打印
	SLTNode* plist = node1;
	SLTPrint(plist);
}
//尾插测试
void test2()
{
	SLTNode* plist = NULL;
	//这里是取指针的地址,所以要二级指针来接收
	SLTPushBack(&plist, 1);
	SLTPushBack(&plist, 2);
	SLTPushBack(&plist, 3);
	SLTPushBack(&plist, 4);
	SLTPushBack(&plist, 5);
	//打印
	SLTPrint(plist);
}
//尾删测试
void test3()
{
	//先插入数据
	SLTNode* plist = NULL;
	
	SLTPushBack(&plist, 1);
	SLTPushBack(&plist, 2);
	SLTPushBack(&plist, 3);
	SLTPushBack(&plist, 4);
	SLTPushBack(&plist, 5);
	//打印
	SLTPrint(&plist);
	//尾删
	SLTPopBack(&plist);
	//打印
	SLTPrint(plist);
}
//头插测试
void test4()
{
	SLTNode* plist = NULL;

	SLTPushFront(&plist, 1);
	SLTPushFront(&plist, 2);
	SLTPushFront(&plist, 3);
	SLTPushFront(&plist, 4);
	SLTPushFront(&plist, 5);
	//打印
	SLTPrint(plist);
}
//头删测试
void test5()
{
	SLTNode* plist = NULL;

	SLTPushFront(&plist, 1);
	SLTPushFront(&plist, 2);
	SLTPushFront(&plist, 3);
	SLTPushFront(&plist, 4);
	SLTPushFront(&plist, 5);
	//打印
	SLTPrint(plist);
	//头删
	SLTPopFront(&plist);
	//打印
	SLTPrint(plist);
}

//查找测试
int test6()
{
	SLTNode* plist = NULL;

	SLTPushFront(&plist, 1);
	SLTPushFront(&plist, 2);
	SLTPushFront(&plist, 3);
	SLTPushFront(&plist, 4);
	SLTPushFront(&plist, 5);
	//查找
	SLTNode* ret = SLTFind(plist, 6);
	if (ret == NULL)
	{
		printf("未找到!");
	}
	else {
		printf("找到了!");
	}
}

//在指定位置之前插入数据测试
void test7()
{
	SLTNode* plist = NULL;

	SLTPushFront(&plist, 1);
	SLTPushFront(&plist, 2);
	SLTPushFront(&plist, 3);
	SLTPushFront(&plist, 4);
	SLTPushFront(&plist, 5);
	//查找
	SLTNode* find = SLTFind(plist, 5);
	SLInsert(&plist, find, 99);
	//打印
	SLTPrint(plist);
}
//在指定位置之后插入数据测试
void test8()
{
	SLTNode* plist = NULL;

	SLTPushFront(&plist, 1);
	SLTPushFront(&plist, 2);
	SLTPushFront(&plist, 3);
	SLTPushFront(&plist, 4);
	SLTPushFront(&plist, 5);
	//查找
	SLTNode* find = SLTFind(plist, 1);
	SLInsertAfter(find, 88);
	//打印
	SLTPrint(plist);
}
//删除pos结点测试
void test9()
{
	SLTNode* plist = NULL;

	SLTPushFront(&plist, 1);
	SLTPushFront(&plist, 2);
	SLTPushFront(&plist, 3);
	SLTPushFront(&plist, 4);
	SLTPushFront(&plist, 5);
	//查找
	SLTNode* find = SLTFind(plist, 5);
	SLTErase(&plist, find);
	//打印
	SLTPrint(plist);
}
//删除pos之后的结点
void test10()
{
	SLTNode* plist = NULL;

	SLTPushFront(&plist, 1);
	SLTPushFront(&plist, 2);
	SLTPushFront(&plist, 3);
	SLTPushFront(&plist, 4);
	//查找
	SLTNode* find = SLTFind(plist, 1);
	SLTEraseAfter(find);
	//打印
	SLTPrint(plist);
}
//销毁链表测试
void test11()
{
	SLTNode* plist = NULL;

	SLTPushFront(&plist, 1);
	SLTPushFront(&plist, 2);
	SLTPushFront(&plist, 3);
	SLTPushFront(&plist, 4);
	//打印
	SLTPrint(plist);
	//销毁
	SListDestroy(&plist);
	//打印
	SLTPrint(plist);
}

int main()
{
	//test1();
	//test2();
	//test3();
	//test4();
	//test5();
	//test6();
	//test7();
	//test8();
	//test9();
	//test10();
	test11();

	return 0;
}

上一篇:【架构论文-2】架构设计中存在的问题和改进方向-二、可扩展性方面


下一篇:利用dem和DOM生成路线算法的实现