SList.h
#pragma once
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
typedef int SLTDataType;
typedef struct SListNode
{
int data;
struct SListNode* next;
}SLTNode;
void SLTDestory(SLTNode** pphead);
SLTNode* BuySLTNode(SLTDataType x);
SLTNode* CreateSlist(int n);
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 SLTInsertAfter(SLTNode* pos, SLTDataType x);
void SLTEraseAfter(SLTNode* pos);
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x);
void SLTErase(SLTNode** pphead, SLTNode* pos);
SList.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "SList.h"
void SLTDestory(SLTNode** pphead)
{
SLTNode* cur = *pphead;
while (cur != NULL)
{
SLTNode* next = cur->next;
free(cur);
cur = next;
}
*pphead = NULL;
}
SLTNode* BuySLTNode(SLTDataType x)
{
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
if (newnode == NULL)
{
perror("malloc fail");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
return newnode;
}
SLTNode* CreateSlist(int n)
{
SLTNode* phead = NULL;
SLTNode* ptail = NULL;
for (int i = 0; i < n; i++)
{
SListNode* newnode = BuySLTNode(i);
if (phead == NULL)
{
ptail = phead = newnode;
}
else
{
ptail->next = newnode;
ptail = newnode;
}
}
return phead;
}
void SLTPrint(SLTNode* phead)
{
SLTNode* cur = phead;
while (cur != NULL)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
void SLTPushBack(SLTNode** pphead, SLTDataType x)
{
SLTNode* newnode = BuySLTNode(x);
if (*pphead == NULL)
{
*pphead = newnode;
}
else
{
SLTNode* tail = *pphead;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
void SLTPopBack(SLTNode** pphead)
{
assert(*pphead);
if ((* pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
SLTNode* tail = *pphead;
while (tail->next->next != NULL)
{
tail = tail->next;
}
free(tail->next);
tail->next = NULL;
}
}
void SLTPushFront(SLTNode** pphead, SLTDataType x)
{
SLTNode* newnode = BuySLTNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
void SLTPopFront(SLTNode** pphead)
{
assert(*pphead);
SLTNode* next = (*pphead)->next;
free(*pphead);
*pphead = next;
}
SLTNode* SLTFind(SLTNode* phead, SLTDataType x)
{
SLTNode* cur = phead;
while (cur != NULL)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void SLTInsertAfter(SLTNode* p, SLTDataType x)
{
assert(p);
SLTNode* newnode = BuySLTNode(x);
newnode->next = p->next;
p->next = newnode;
}
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{
assert(pos);
if(*pphead==pos)
{
SLTPushFront(pphead, x);
}
else
{
SLTNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
SLTNode* newnode = BuySLTNode(x);
prev->next = newnode;
newnode->next = pos;
}
}
void SLTEraseAfter(SLTNode* pos)
{
assert(pos);
if (pos->next == NULL)
{
return;
}
else
{
SLTNode* nextNode = pos->next;
pos->next = nextNode->next;
free(nextNode);
}
}
void SLTErase(SLTNode** pphead, SLTNode* pos)
{
assert(pos);
if (pos == *pphead)
{
SLTPopFront(pphead);
}
else
{
SLTNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
}
}
Test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "SList.h"
void TestSList1()
{
SLTNode* plist = CreateSlist(4);
SLTPrint(plist);
}
void TestSList2()
{
SLTNode* plist = CreateSlist(4);
SLTPushBack(&plist, 100);
SLTPrint(plist);
SLTDestory(&plist);
SLTPrint(plist);
}
void TestSList3()
{
SLTNode* plist = NULL;
SLTPushBack(&plist, 100);
SLTPushBack(&plist, 200);
SLTPushBack(&plist, 300);
SLTPrint(plist);
}
void TestSList4()
{
SLTNode* plist = NULL;
SLTPushBack(&plist, 100);
SLTPushBack(&plist, 200);
SLTPushBack(&plist, 300);
SLTPrint(plist);
SLTPopBack(&plist);
SLTPrint(plist);
SLTPopBack(&plist);
SLTPrint(plist);
SLTPopBack(&plist);
SLTPrint(plist);
SLTPopBack(&plist);
SLTPrint(plist);
}
void TestSList5()
{
SLTNode* plist = NULL;
SLTPushBack(&plist, 100);
SLTPushBack(&plist, 200);
SLTPushBack(&plist, 300);
SLTPrint(plist);
SLTPushFront(&plist, 20);
SLTPrint(plist);
}
void TestSList6()
{
SLTNode* plist = NULL;
SLTPushBack(&plist, 100);
SLTPushBack(&plist, 200);
SLTPushBack(&plist, 300);
SLTPrint(plist);
SLTPopFront(&plist);
SLTPrint(plist);
}
void TestSList7()
{
SLTNode* plist = NULL;
SLTPushBack(&plist, 100);
SLTPushBack(&plist, 200);
SLTPushBack(&plist, 300);
SLTPrint(plist);
SLTNode* pos = SLTFind(plist, 300);
if (pos != NULL)
{
printf("找到了\n");
}
else
{
printf("找不到\n");
}
SLTPrint(pos);
}
void TestSList8()
{
SLTNode* plist = NULL;
SLTPushBack(&plist, 100);
SLTPushBack(&plist, 200);
SLTPushBack(&plist, 300);
SLTPrint(plist);
SLTNode* pos = SLTFind(plist, 200);
if (pos != NULL)
{
SLTInsertAfter(pos, 30);
printf("找到了\n");
}
else
{
printf("找不到\n");
}
SLTPrint(plist);
}
void TestSList9()
{
SLTNode* plist = NULL;
SLTPushBack(&plist, 100);
SLTPushBack(&plist, 200);
SLTPushBack(&plist, 300);
SLTPushBack(&plist, 400);
SLTPushBack(&plist, 500);
SLTPrint(plist);
SLTNode* pos = SLTFind(plist, 300);
if (pos != NULL)
{
SLTInsert(&plist,pos, 30);
printf("找到了\n");
}
else
{
printf("找不到\n");
}
SLTPrint(plist);
}
void TestSList10()
{
SLTNode* plist = NULL;
SLTPushBack(&plist, 100);
SLTPushBack(&plist, 200);
SLTPushBack(&plist, 300);
SLTPushBack(&plist, 400);
SLTPushBack(&plist, 500);
SLTPrint(plist);
SLTNode* pos = SLTFind(plist, 500);
if (pos != NULL)
{
SLTEraseAfter(pos);
printf("找到了\n");
}
else
{
printf("找不到\n");
}
SLTPrint(plist);
}
void TestSList11()
{
SLTNode* plist = NULL;
SLTPushBack(&plist, 100);
SLTPushBack(&plist, 200);
SLTPushBack(&plist, 300);
SLTPushBack(&plist, 400);
SLTPushBack(&plist, 500);
SLTPrint(plist);
SLTNode* pos = SLTFind(plist, 300);
if (pos != NULL)
{
SLTErase(&plist, pos);
printf("找到了\n");
}
else
{
printf("找不到\n");
}
SLTPrint(plist);
}
void menu()
{
printf("***********************************************************\n");
printf("1、头插节点 2、头删节点 \n");
printf("\n");
printf("3、尾插节点 4、尾删节点 \n");
printf("\n");
printf("5、在单链表中查找pos数据 6、在单链表pos位置之后插入一个新的节点\n");
printf("\n");
printf("7、删除单链表pos位置之后的节点 8、在单链表pos位置之前插入新的节点 \n");
printf("\n");
printf("9、在单链表中删除pos位置的数据 10、打印数据 \n");
printf("\n");
printf("-1、退出 \n");
printf("***********************************************************\n");
}
int main()
{
printf("*************** 欢迎大家来到单链表的测试 ****************\n");
int option = 0;
SLTNode* plist = NULL;
SLTNode* pos = NULL;
do
{
menu();
printf("请输入你的操作:>");
scanf("%d", &option);
int sum = 0;
int x = 0;
int y = 0;
int z = 0;
int w = 0;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
switch (option)
{
case 1:
printf("请依次输入你要尾插的数据:,以-1结束\n");
scanf("%d", &sum);
while (sum != -1)
{
SLTPushBack(&plist, sum);
scanf("%d", &sum);
}
break;
case 2:
SLTPopFront(&plist);
break;
case 3:
printf("请输入想要尾插的数据:");
scanf("%d", &x);
SLTPushBack(&plist, x);
break;
case 4:
SLTPopBack(&plist);
break;
case 5:
printf("请输入想要查找的pos数据:");
scanf("%d", &y);
pos = SLTFind(plist, y);
if (pos != NULL)
{
printf("找到了\n");
}
else
{
printf("找不到pos位置\n");
}
break;
case 6:
printf("请输入想要查找的pos数据:");
scanf("%d", &z);
pos = SLTFind(plist, z);
if (pos != NULL)
{
printf("请输入想要在pos位置后插入的数据:");
scanf("%d", &w);
SLTInsertAfter(pos, w);
}
else
{
printf("找不到pos位置,无法插入\n");
}
break;
case 7:
printf("请输入想要查找的pos数据:");
scanf("%d", &a);
pos = SLTFind(plist, a);
if (pos != NULL)
{
SLTEraseAfter(pos);
}
else
{
printf("找不到pos位置,无法删除\n");
}
break;
case 8:
printf("请输入想要查找的pos数据:");
scanf("%d", &b);
pos = SLTFind(plist, b);
if (pos != NULL)
{
printf("请输入想要在pos位置后插入的数据:");
scanf("%d", &c);
SLTInsert(&plist, pos, c);
}
else
{
printf("找不到pos位置,无法插入\n");
}
break;
case 9:
printf("请输入想要查找的pos数据:");
scanf("%d", &d);
pos = SLTFind(plist, d);
if (pos != NULL)
{
SLTErase(&plist, pos);
}
else
{
printf("找不到pos位置,无法删除\n");
}
case 10:
SLTPrint(plist);
break;
default:
if (option == -1)
{
exit(0);
}
else
{
printf("输入错误,请重新输入\n");
}
break;
}
} while (option != -1);
return 0;
}