单链表C语言实现

2024-04-30 21:20:40 浏览数 (2)

SList.h文件

代码语言:javascript复制
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.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 SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x);

void SLTInsertAfter(SLTNode* pos, SLTDataType x);

void SLTErase(SLTNode** pphead,SLTNode*pos);

void SLTEraseAfter(SLTNode** pphead);

void SListDesTroy(SLTNode** pphead);

SList.c文件

代码语言:javascript复制
#define _CRT_SECURE_NO_WARNINGS
#include "SList.h"

void SLTPrint(SLTNode* phead)
{
	SLTNode * pcur = phead;
	while (pcur)
	{
		printf("%d->", pcur->data);
		pcur = pcur->Next;
	}
	printf("NULL");
}

SLTNode* SLTBuyNode(SLTDataType x)
{
	SLTNode* NewNode = (SLTNode*)malloc(sizeof(SLTNode));
	if (NewNode == NULL)
	{
		perror("malloc fail!");
		exit(1);
	}
	NewNode->data = x;
	NewNode->Next = NULL;
	return NewNode;
}

void SLTPushBack(SLTNode** pphead, SLTDataType x)
{
	assert(pphead);
	SLTNode* NewNode = SLTBuyNode(x);
	//空链表与非空
	if (*pphead == NULL)
	{
		*pphead = NewNode;
	}
	else
	{		
		//find尾巴
		SLTNode* ptail = *pphead;
		while (ptail->Next != NULL)
		{
			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)
{
	assert(pphead && *pphead);
	if ((*pphead)->Next == NULL)// -> 高于 *
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		SLTNode* prev = *pphead;
		SLTNode* ptail = *pphead;
		while (ptail->Next)
		{
			prev = ptail;
			ptail = ptail->Next;
		}
		free(ptail);
		ptail = NULL;
		prev->Next = 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 SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{
	assert(pphead && *pphead && pos);
	SLTNode* prev = *pphead;
	if (pos == *pphead)
	{
		SLTPushFront(pphead, x);
	}
	else 
	{
		SLTNode* NewNode = SLTBuyNode(x);
		while (prev->Next != pos)
		{
			prev = prev->Next;
		}
		NewNode->Next = pos;
		prev->Next = NewNode;
	}
}

void SLTInsertAfter(SLTNode* pos, SLTDataType x)
{
	assert(pos);
	SLTNode* NewNode = SLTBuyNode(x);
	NewNode->Next = pos->Next;
	pos->Next = NewNode;
}

void SLTErase(SLTNode** pphead, SLTNode* pos)
{
	assert(*pphead && pphead && pos);
	//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;
	}
}

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 && *pphead);
	SLTNode* pcur = *pphead;
	while (pcur)
	{
		SLTNode* next = pcur->Next;
		free(pcur);
		pcur = next;
	}
	*pphead = NULL;
}

测试文件

代码语言:javascript复制
#define _CRT_SECURE_NO_WARNINGS
#include "SList.h"

void SListTestA()
{

	SLTNode* node1 = (SLTNode*)malloc(sizeof(SLTNode));
	node1->data = 1;

	SLTNode* node2 = (SLTNode*)malloc(sizeof(SLTNode));
	node2->data = 2;

	SLTNode* node3 = (SLTNode*)malloc(sizeof(SLTNode));
	node3->data = 3;

	SLTNode* node4 = (SLTNode*)malloc(sizeof(SLTNode));
	node4->data = 4;

	//连接
	node1->Next = node2;
	node2->Next = node3;
	node3->Next = node4;
	node4->Next = NULL;

	//调用打印
	SLTNode* plist = node1;
	SLTPrint(plist);

}
void SListTestB()
{
	SLTNode* plist = NULL;
	SLTPushFront(&plist, 1);
	SLTPushFront(&plist, 2);
	SLTPushFront(&plist, 3);
	SLTPushFront(&plist, 4);
	SLTPopBack(&plist);
	SLTPrint(plist);
	printf("n");
	SLTPopBack(&plist);
	SLTPrint(plist);
	printf("n");
	SLTPopBack(&plist);
	SLTPrint(plist);
	printf("n");
	SLTPopFront(&plist);
	SLTPrint(plist);
	while (1);
}
void SListTestC()
{
	SLTNode* plist = NULL;
	SLTPushFront(&plist, 1);
	SLTPushFront(&plist, 2);
	SLTPushFront(&plist, 3);
	SLTPushFront(&plist, 4);
	SLTPrint(plist);
	printf("n");
	SLTNode* find= SLTFind(plist, 4);
	//SLTInsert(&plist, find, 2);
	SLTInsertAfter(find,11);
	SLTPrint(plist);
	printf("n");
	SLTEraseAfter(find);
	SLTPrint(plist);
	printf("n");
	SListDesTroy(&plist);
	SLTPrint(plist);
}
int main()
{
	//SListTestA();
	SListTestC();
}

0 人点赞