1、Data.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLLDatatype;
typedef struct SListNode
{
SLLDatatype data;
struct SListNode* next;
}SLLNode;
SLLNode* BuySLLNode(int x);
void SLLPushFront(SLLNode** pphead, int x);
void SLLPushBack(SLLNode** pphead, int x);
void SLLPopFront(SLLNode** pphead);
void SLLPopBack(SLLNode** pphead);
SLLNode* SLLFind(SLLNode* phead, SLLDatatype find);
void SLLInsertAfter(SLLNode** pphead, SLLNode* pos, SLLDatatype x);
//我们希望在pos的前一个链表插入数据
//单向链表的前向插入非常麻烦,我们一般使用后置插入
void SLLInsertBack(SLLNode* pos, SLLDatatype x);
void SLLEraseFront(SLLNode** pphead, SLLNode* pos);
void SLLEraseBack(SLLNode* pos);
void SLLLink(SLLNode** pphead);
void SLLPrint(const SLLNode* phead);
2、main.cpp
#include"Data.h"
int main()
{
SLLNode* phead = NULL;
SLLPushBack(&phead, 8);
SLLPushBack(&phead, 13);
SLLPushBack(&phead, 55);
SLLPushBack(&phead, 7);
SLLPushBack(&phead, 8);
SLLPushBack(&phead, 13);
SLLPushBack(&phead, 55);
SLLPushBack(&phead, 7);
SLLPrint(phead);
SLLPopFront(&phead);
SLLPrint(phead);
SLLEraseBack(phead->next);
SLLPrint(phead);
SLLLink(&phead);
//形成有环链表
SLLPrint(phead);
return 0;
}
3、Data.cpp
//#include"Data.h"
//SLLNode* BuySLLNode(int x)
//{
// SLLNode* newnode = (SLLNode*)malloc(sizeof(SLLNode));
// if (newnode == NULL)
// {
// perror("newnode fail");
// }
// newnode->data = x;
// newnode->next = NULL;
// return newnode;
//}
//
//void SLLPushFront(SLLNode** pphead, int x)
//{
// SLLNode* newnode = BuySLLNode(x);
// newnode->next = *pphead;
// *pphead = newnode;
//}
//
//void SLLPushBack(SLLNode** pphead, int x)
//{
// SLLNode* newnode = BuySLLNode(x);
// if (*pphead == NULL)
// {
// *pphead = newnode;
// }
// else
// {
// SLLNode* tail = *pphead;
// while (tail->next)
// {
// tail = tail->next;
// }
// tail->next = newnode;
// }
//}
//
//void SLLPopFront(SLLNode** pphead)
//{
// if (*pphead == NULL)
// {
// return;
// }
// else
// {
// SLLNode* next = (*pphead)->next;
// free(*pphead);
// *pphead = next;
// }
//}
//void SLLPopBack(SLLNode** pphead)
//{
// if (*pphead == NULL)
// {
// return;
// }
// else if ((*pphead)->next == NULL)
// {
// free(*pphead);
// *pphead = NULL;
// }
// else
// {
// SLLNode* tail = *pphead;
// while (tail->next->next)
// {
// tail = tail->next;
// }
// free(tail->next);
// tail->next = NULL;
// }
//
//}
//
//void SLLPrint(const SLLNode* phead)
//{
// const SLLNode* cur = phead;
// while (cur)
// {
// printf("%d->", cur->data);
// cur = cur->next;
// }
// printf("NULL\n");
//}
#include"Data.h"
SLLNode* BuySLLNode(int x)
{
SLLNode* newnode = (SLLNode*)malloc(sizeof(SLLNode));
if (newnode == NULL)
{
perror("newnode fail");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
return newnode;
}
void SLLPushFront(SLLNode** pphead, int x)
{
assert(pphead);
SLLNode* newnode = BuySLLNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
void SLLPushBack(SLLNode** pphead, int x)
{
assert(pphead);
SLLNode* newnode = BuySLLNode(x);
if (*pphead == NULL)
{
*pphead = newnode;
}
else
{
SLLNode* tail = *pphead;
while (tail->next)
{
tail = tail->next;
}
tail->next = newnode;
}
}
void SLLPopFront(SLLNode** pphead)
{
assert(pphead);
assert(*pphead);
/*if (*pphead == NULL)
{
return;
}*/
if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
SLLNode* next = (*pphead)->next;
free(*pphead);
*pphead = next;
}
}
void SLLPopBack(SLLNode** pphead)
{
assert(pphead);
assert(*pphead);
/*if (*pphead == NULL)
{
return;
}*/
if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
SLLNode* tail = *pphead;
while (tail->next->next)
{
tail = tail->next;
}
free(tail->next);
tail->next = NULL;
}
}
SLLNode* SLLFind(SLLNode* phead, int find)
{
SLLNode* pfind = phead;
while (pfind)
{
if (pfind->data == find)
{
return pfind;
}
pfind = pfind->next;
}
return NULL;
}
void SLLInsertAfter(SLLNode** pphead, SLLNode* pos, SLLDatatype x)
{
assert(pphead);
if (*pphead == pos)
{
SLLPushFront(pphead,x);
}
else
{
SLLNode* prev = *pphead;
while ((prev->next) != pos)
{
prev = prev->next;
}
SLLNode* newnode = BuySLLNode(x);
prev->next = newnode;
newnode->next = pos;
//不能写成newnode->next = nownode->next;
//不然就自己链接自己了(乐)
}
}
void SLLEraseFront(SLLNode** pphead, SLLNode* pos)
{
SLLNode* prev = *pphead;
while (prev->next->next != pos)
{
prev = prev->next;
}
SLLNode* del = prev->next;
prev->next = prev->next->next;
free(del);
}
void SLLEraseBack(SLLNode* pos)
{
assert(pos);
assert(pos->next);
SLLNode* del = pos->next;
pos->next = pos->next->next;
free(del);
}
void SLLLink(SLLNode** pphead)
{
SLLNode* cur = *pphead;
while (cur->next)
{
cur = cur->next;
}
cur->next = *pphead;
}
void SLLPrint(const SLLNode* phead)
{
while (phead)
{
printf("%d->", phead->data);
phead = phead->next;
}
printf("NULL\n");
}