一、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;
}