单链表

一、SList.h

#ifndef __SList_h__
#define __SList_h__

#include <stdio.h>
#include <assert.h>
#include <Windows.h>

typedef int DataType;

typedef struct SListNode
{
	DataType _data;
	struct SListNode* _next;
}SListNode;

SListNode* Buy_SListNode(DataType x);
void SList_Print(SListNode* pHead);
void SList_Destory(SListNode** ppHead);

void SList_PushBack(SListNode** ppHead, DataType x);
void SList_PopBack(SListNode** ppHead);
void SList_PushFront(SListNode** ppHead, DataType x);
void SList_PopFront(SListNode** ppHead);

SListNode* SList_Find(SListNode* pHead, DataType x);
void SList_Insert(SListNode** ppHead, SListNode* pos, DataType x);
void SList_Erase(SListNode** ppHead, SListNode* pos);

#endif __SList_h__

二、SList.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "SList.h"

//建立结点
SListNode* Buy_SListNode(DataType x)
{
	SListNode* newNode = (SListNode*)malloc(sizeof(SListNode));
	newNode->_data = x;
	newNode->_next = NULL;
	return newNode;
}

//输出
void SList_Print(SListNode* pHead)
{
	if (pHead == NULL)
	{
		printf("该链表为空!\n");
	}
	else
	{
		SListNode* cur = pHead;
		while (cur)
		{
			printf("%d->", cur->_data);
			cur = cur->_next;
		}
		printf("NULL\n");
	}
}

//销毁
void SList_Destory(SListNode** ppHead)
{
	if (*ppHead == NULL)
	{
		printf("该链表为空!\n");
	}
	else
	{
		SListNode* cur = *ppHead;
		SListNode* tail = NULL;
		while (cur)
		{
			tail = cur->_next;
			free(cur);
			cur = tail;
		}
		*ppHead = NULL;
		printf("销毁成功!\n");
	}
}

//尾插
void SList_PushBack(SListNode **ppHead, DataType x)
{
	assert(ppHead);
	if (*ppHead == NULL)
	{
		*ppHead = Buy_SListNode(x);
	}
	else
	{
		SListNode * cur = *ppHead;
		while (cur->_next != NULL)
		{
			cur = cur->_next;
		}
		cur->_next = Buy_SListNode(x);
	}
}

//尾删
void SList_PopBack(SListNode** ppHead)
{
	assert(ppHead);
	if (*ppHead == NULL)
	{
		printf("该链表为空!\n");
	}
	else if ((*ppHead)->_next == NULL)
	{
		free(*ppHead);
		*ppHead = NULL;
	}
	else
	{
		SListNode * prev = *ppHead;
		SListNode * cur = *ppHead;
		while (cur->_next != NULL)
		{
			prev = cur; // 记录前一个结点
			cur = cur->_next;
		}
		free(cur);
		prev->_next = NULL;
	}
}

//头插
void SList_PushFront(SListNode** ppHead, DataType x)
{
	assert(ppHead);
	if (*ppHead == NULL)
	{
		*ppHead = Buy_SListNode(x);
	}
	else
	{
		SListNode * newHead = Buy_SListNode(x);
		newHead->_next = *ppHead;
		*ppHead = newHead;
	}
}

//头删
void SList_PopFront(SListNode** ppHead)
{
	assert(ppHead);
	if (*ppHead == NULL)
	{
		printf("该链表为空!\n");
	}
	else if ((*ppHead)->_next == NULL)
	{
		free(*ppHead);
		*ppHead = NULL;
	}
	else
	{
		SListNode * cur = (*ppHead)->_next;
		free(*ppHead);
		*ppHead = cur;
	}
}

//查找
SListNode* SList_Find(SListNode* pHead, DataType x)
{
	if (pHead == NULL)
	{
		printf("该链表为空!\n");
		return NULL;
	}
	else
	{
		SListNode * cur = pHead;
		while (cur)
		{
			if (cur->_data == x)
			{
				return cur;
			}
			cur = cur->_next;
		}
		return NULL;
	}
}

//插入
void SList_Insert(SListNode** ppHead, SListNode* pos, DataType x)
{
	assert(ppHead);
	if (pos == *ppHead)
	{
		SList_PushFront(ppHead, x);
	}
	else
	{
		SListNode * cur = *ppHead;
		SListNode * newNode = Buy_SListNode(x);
		while (cur->_next != pos)
		{
			cur = cur->_next;
		}
		newNode->_next = pos;
		cur->_next = newNode;
	}
}

//删除
void SList_Erase(SListNode** ppHead, SListNode* pos)
{
	assert(ppHead);
	if (*ppHead == pos)
	{
		SList_PopFront(ppHead);
	}
	else
	{
		SListNode *cur = *ppHead;
		while (cur->_next != pos)
		{
			cur = cur->_next;
		}
		cur->_next = pos->_next;
		free(pos);
	}
}

三、test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "SList.h"

void Menu()
{
	printf("********单链表********\n");
	printf("**      0.退出      **\n");
	printf("**      1.头插      **\n");
	printf("**      2.尾插      **\n");
	printf("**      3.头删      **\n");
	printf("**      4.尾删      **\n");
	printf("**      5.插入      **\n");
	printf("**      6.删除      **\n");
	printf("**      7.查找      **\n");
	printf("**      8.输出      **\n");
	printf("**      9.销毁      **\n");
	printf("**********************\n");
}

int main()
{
	SListNode* pHead = NULL;
	int op = 0;
	do{
		Menu();
		printf("请选择:");
		scanf("%d", &op);
		switch (op)
		{
		case 0:
			break;
		case 1:
		{
				  int x = 0;
				  printf("请输入插入的值:");
				  scanf("%d", &x);
				  SList_PushFront(&pHead, x);
				  printf("插入成功\n");
		}
			break;
		case 2:
		{
				  int x = 0;
				  printf("请输入插入的值:");
				  scanf("%d", &x);
				  SList_PushBack(&pHead, x);
				  printf("插入成功\n");
		}
			break;
		case 3:
			SList_PopFront(&pHead);
			break;
		case 4:
			SList_PopBack(&pHead);
			break;
		case 5:
		{
				  int x = 0;
				  int y = 0;
				  SListNode* pos = pHead;
				  printf("请输入插入的值:");
				  scanf("%d", &x);
				  printf("请输入插入的合法位置:");
				  scanf("%d", &y);
				  while (--y > 0)
				  {
					  pos = pos->_next;
				  }
				  SList_Insert(&pHead, pos, x);
				  printf("插入成功\n");
		}
			break;
		case 6:
		{
				  int y = 0;
				  SListNode* pos = pHead;
				  printf("请输入删除的合法位置:");
				  scanf("%d", &y);
				  while (--y > 0)
				  {
					  pos = pos->_next;
				  }
				  SList_Erase(&pHead, pos);
		}
			break;
		case 7:
		{
				  int x = 0;
				  printf("请输入查找的值:");
				  scanf("%d", &x);
				  if (SList_Find(pHead, x))
				  {
					  printf("找到了\n");
				  }
				  else
				  {
					  printf("未找到\n");
				  }
		}
			break;
		case 8:
			SList_Print(pHead);
			break;
		case 9:
			SList_Destory(&pHead);
			break;
		default :
			printf("请重新选择...\n");
			break;
		}
	} while (op);

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值