#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int SLTDataType;
typedef struct SListNode
{
SLTDataType data;
struct SListNode* next;
}SLTNode;
void SListPrint(SLTNode* phead); //单链表打印
void SListPushBack(SLTNode** pplist, SLTDataType x); //单链表尾插
void SListPushFront(SLTNode** pplist, SLTDataType x); //单链表头插
void SListPopBack(SLTNode** pplist); //单链表尾删
void SListPopFront(SLTNode** pplist); //单链表头删
void SListInsertAfter(SLTNode* pos, SLTDataType x); //单链表在pos位置之后插入x
void SListEraseAfter(SLTNode* pos); //单链表删除pos位置之后的值
void SListPrint(SLTNode* phead) //单链表打印
{
SLTNode* cur = phead;
while (cur)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
SLTNode* BuySLTNode(SLTDataType x) //创建新结点
{
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
assert(newnode);
newnode->data = x;
newnode->next = NULL;
return newnode;
}
void SListPushBack(SLTNode** pplist, SLTDataType x) //单链表尾插
{
SLTNode* newnode = BuySLTNode(x);
if (*pplist == NULL)
{
*pplist = newnode;
}
else
{
SLTNode* tail = *pplist;
while (tail->next)
{
tail = tail->next;
}
tail->next = newnode;
}
}
void SListPushFront(SLTNode** pplist, SLTDataType x) //单链表头插
{
SLTNode* newnode = BuySLTNode(x);
newnode->next = *pplist;
*pplist = newnode;
}
void SListPopBack(SLTNode** pplist) //单链表尾删
{
assert(*pplist);
SLTNode* tail = *pplist;
SLTNode* start = *pplist;
while (tail->next)
{
start = tail;
tail = tail->next;
}
if ((*pplist)->next == NULL)
{
*pplist = NULL;
free(*pplist);
}
else
{
free(tail);
start->next = NULL;
}
/*if ((*pplist)->next == NULL)
{
*pplist = NULL;
free(*pplist);
}
else
{
SLTNode* tail = *pplist;
while (tail->next->next)
{
tail = tail->next;
}
free(tail->next);
tail->next = NULL;
}*/
}
void SListPopFront(SLTNode** pplist) //单链表头删
{
assert(*pplist);
SLTNode* tail = (*pplist)->next;
free(*pplist);
*pplist = tail;
}
SLTNode* SListFind(SLTNode* pplist, SLTDataType x)
{
SLTNode* tail = pplist;
while (tail)
{
if (tail->data != x)
{
tail = tail->next;
}
else
{
return tail;
}
}
return NULL;
}
void SListInsertAfter(SLTNode* pos, SLTDataType x) //单链表在pos位置之后插入x
{
assert(pos);
SLTNode* newnode = BuySLTNode(x);
SLTNode* next = pos->next;
pos->next = newnode;
newnode->next = next;
}
void SListEraseAfter(SLTNode* pos) //单链表删除pos位置之后的值
{
assert(pos);
/*if (pos->next == NULL)
return;*/
SLTNode* tail = pos->next->next;
pos->next = tail;
free(pos->next);
}
void TestSList1()
{
SLTNode* n1 = (SLTNode*)malloc(sizeof(SLTNode));
assert(n1);
SLTNode* n2 = (SLTNode*)malloc(sizeof(SLTNode));
assert(n2);
SLTNode* n3 = (SLTNode*)malloc(sizeof(SLTNode));
assert(n3);
SLTNode* n4 = (SLTNode*)malloc(sizeof(SLTNode));
assert(n4);
n1->data = 1;
n2->data = 2;
n3->data = 3;
n4->data = 4;
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = NULL;
SLTNode* Plist = n1;
SListPrint(Plist);
}
void TestSList2()
{
SLTNode* plist = NULL;
SListPushBack(&plist, 5);
SListPrint(plist);
SListPushBack(&plist, 6);
SListPrint(plist);
SListPushBack(&plist, 7);
SListPrint(plist);
SListPushBack(&plist, 8);
SListPrint(plist); //单链表尾插
}
void TestSList3()
{
SLTNode* plist = NULL;
SListPushFront(&plist, 6); //单链表头插
SListPushFront(&plist, 7);
SListPushFront(&plist, 8);
SListPushFront(&plist, 9);
SListPrint(plist);
}
void TestSList4()
{
SLTNode* plist = NULL;
SListPushFront(&plist, 7);
SListPushFront(&plist, 8); //单链表尾删
SListPushFront(&plist, 9);
SListPopBack(&plist);
SListPopBack(&plist);
SListPopBack(&plist);
/*SListPopBack(&plist);*/
SListPrint(plist);
}
void TestSList5()
{
SLTNode* plist = NULL;
SListPushFront(&plist, 7);
SListPushFront(&plist, 8);
SListPushFront(&plist, 9);
SListPopFront(&plist);
SListPopFront(&plist);
SListPopFront(&plist); //单链表头删
SListPrint(plist);
}
void TestSList6()
{
SLTNode* plist = NULL;
SListPushFront(&plist, 4);
SListPushFront(&plist, 5);
SListPushFront(&plist, 6);
SListPushFront(&plist, 7);
SListPushFront(&plist, 8);
SListPushFront(&plist, 9);
SLTNode* pos = SListFind(plist,6);
SListInsertAfter(pos, 10); //单链表在pos位置之后插入x
SListPrint(plist);
SListEraseAfter(pos); //删除pos位置之后的值
SListPrint(plist);
}
int main()
{
TestSList4();
return 0;
}
🙇感谢大家的阅读,如有错误请指出,我们下次再见。