#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int DataType;
typedef struct SListNode {
struct SListNode* next;
DataType data;
}Node;
//申请结点并初始化
extern Node* BuySListNode(DataType data);
//头插
extern void SListPushFront(Node** pplist, DataType x);
//头删
extern void SListPopFront(Node** pplist);
//尾插
extern void SListPushBack(Node** pplist, DataType x);
//尾删
extern void SListPopBack(Node** pplist);
//查找
extern Node* SListFind(Node* plist, DataType x);
//指定位置插入
extern void SListInsertAfter(Node* pos, DataType x);
//指定位置删除
extern void SListEraseAfter(Node* pos);
// 链表长度
extern int SListSize(Node* plist);
//销毁链表
extern void SListDestory(Node** pplist);
//打印链表
void PrintSList(Node* pplist);
#include "SList.h"
//申请结点并初始化
extern Node* BuySListNode(DataType data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (NULL == newNode) {
assert(0);
return NULL;
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
//头插
void SListPushFront(Node** pplist, DataType x) {
assert(pplist);
Node* newNode = BuySListNode(x);
newNode->next = *pplist;
*pplist = newNode;
}
//头删
void SListPopFront(Node** pplist) {
assert(pplist);
if (NULL == *pplist)
return;
Node* delNode = *pplist;
*pplist = delNode->next;
free(delNode);
}
//尾插
void SListPushBack(Node** pplist, DataType x) {
assert(pplist);
Node* cur = *pplist;
Node* newNode = BuySListNode(x);
if (NULL == *pplist) {
*pplist = newNode;
}
else {
while (cur->next) {
cur = cur->next;
}
cur->next = newNode;
}
}
//尾删
void SListPopBack(Node** pplist) {
assert(pplist);
if (NULL == *pplist) {
return;
}
else if (NULL == (*pplist)->next ){
free(*pplist);
*pplist = NULL;
}
else {
Node* cur = *pplist;
Node* prev = NULL;
while (cur->next) {
prev = cur;
cur = cur->next;
}
free(cur);
prev->next = NULL;
}
}
//查找
Node* SListFind(Node* plist, DataType x) {
Node* cur = plist;
while (cur) {
if (cur->data == x) {
return cur;
}
cur = cur->next;
}
return NULL;
}
//指定位置插入
void SListInsertAfter(Node* pos, DataType x) {
if (NULL == pos) {
return;
}
Node* newNode = BuySListNode(x);
newNode->next = pos->next;
pos->next = newNode;
}
//指定位置删除
void SListEraseAfter(Node* pos) {
if (NULL == pos || NULL == pos->next) {
return;
}
Node* delNode = pos->next;
pos->next = delNode->next;
free(delNode);
}
// 链表长度
int SListSize(Node* plist) {
Node* cur = plist;
int count = 0;
while (cur) {
cur = cur->next;
count++;
}
return count;
}
//销毁链表
void SListDestory(Node** pplist) {
assert(pplist);
Node* cur = *pplist;
while (cur) {
*pplist = cur->next;
free(cur);
cur = *pplist;
}
}
//打印链表
void PrintSList(Node* pplist) {
Node* cur = pplist;
while (cur) {
printf("%d-->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
#include "SList.h"
int main() {
Node* plist = NULL;
SListPushBack(&plist, 1);
SListPushBack(&plist, 2);
SListPushBack(&plist, 3);
SListPushBack(&plist, 4);
SListPushBack(&plist, 5);
PrintSList(plist);//1-->2--> 3--> 4--> 5--> NULL
SListPopBack(&plist);
SListPopBack(&plist);
PrintSList(plist);//1-->2-->3-->NULL
SListPushFront(&plist, 0);
PrintSList(plist);//0-->1-->2-->3-->NULL
SListPopFront(&plist);
PrintSList(plist);//1-->2-->3-->NULL
SListInsertAfter(SListFind(plist, 2), 22);
PrintSList(plist);//1-->2-->22-->3-->NULL
SListEraseAfter(SListFind(plist, 2));
PrintSList(plist);//1-->2-->3-->NULL
printf("链表长度为%d", SListSize(plist));//3
SListDestory(&plist);
}