单链表的所有操作实现
// 动态申请一个节点
SListNode* BuySListNode(SLTDateType x)
//链表初始化
void SListInit(Head* plist)
// 单链表打印
void SListPrint(Head* plist)
// 单链表尾插
void SListPushBack(Head* pplist, SLTDateType x)
// 单链表的头插
void SListPushFront(Head* pplist, SLTDateType x)
// 单链表的尾删
void SListPopBack(Head* pplist)
// 单链表头删
void SListPopFront(Head* pplist)
// 单链表查找
SListNode* SListFind(Head* plist, SLTDateType x)
// 单链表在pos位置之后插入x
void SListInsertAfter(SListNode* pos, SLTDateType x)
// 单链表删除pos位置之后的值
void SListEraseAfter(SListNode* pos)
//释放链表所有空间
void SListDestory(Head* plist)
#include<stdio.h>
#include<stdlib.h>
typedef int SLTDateType;
typedef struct SListNode
{
SLTDateType data;
struct SListNode* next;
}SListNode;
typedef struct Head
{
struct SListNode* head;
}Head;
SListNode* BuySListNode(SLTDateType x) {
SListNode* node = (SListNode*)malloc(sizeof(SListNode));
node->data = x;
node->next = NULL;
return node;
}
void SListInit(Head* plist) {
plist->head = NULL;
}
void SListPrint(Head* plist) {
SListNode* cur = plist->head;
while (cur) {
printf("%d ", cur->data);
cur = cur->next;
}
}
void SListPushBack(Head* pplist, SLTDateType x) {
SListNode* node = BuySListNode(x);
if (pplist == NULL) {
pplist->head = node;
}
else {
SListNode* cur = pplist->head;
while (cur->next) {
cur = cur->next;
}
cur->next = node;
}
}
void SListPushFront(Head* pplist, SLTDateType x) {
SListNode* node = BuySListNode(x);
if (pplist == NULL) {
pplist->head = node;
}
else {
node->next = pplist->head;
pplist->head = node;
}
}
void SListPopBack(Head* pplist) {
if (pplist->head) {
SListNode* cur = NULL;
SListNode* last = pplist->head;
while (last->next) {
cur = last;
last = last->next;
}
if (last == pplist->head) {
pplist->head = NULL;
}
else {
cur->next = NULL;
}
free(last);
}
}
void SListPopFront(Head* pplist) {
if (pplist->head) {
SListNode* cur = pplist->head;
pplist->head = cur->next;
free(cur);
}
}
SListNode* SListFind(Head* plist, SLTDateType x) {
SListNode* cur = plist->head;
while (cur) {
if (cur->data == x) {
return cur;
}
cur = cur->next;
}
return NULL;
}
void SListInsertAfter(SListNode* pos, SLTDateType x) {
SListNode* node = BuySListNode(x);
node->next = pos->next;
pos->next = node;
}
void SListEraseAfter(SListNode* pos) {
SListNode* next;
if (pos == NULL){
return;
}
next = pos->next;
if (next) {
pos->next = next->next;
free(next);
}
}
void SListDestory(Head* plist) {
SListNode* cur = plist->head;
while (cur) {
SListNode* next = cur->next;
free(cur);
cur = next;
}
}
int main() {
Head plist;
SListInit(&plist);
SListPushFront(&plist, 1);
SListPushFront(&plist, 2);
SListPushFront(&plist, 3);
SListPushFront(&plist, 4);
SListPushFront(&plist, 5);
SListPrint(&plist);
printf("\n");
SListPushBack(&plist, 100);
SListPushBack(&plist, 200);
SListPushBack(&plist, 300);
SListPrint(&plist);
printf("\n");
SListPopBack(&plist);
SListPrint(&plist);
printf("\n");
SListPopFront(&plist);
SListPopFront(&plist);
SListPrint(&plist);
printf("\n");
SListNode* pos = SListFind(&plist, 2);
SListInsertAfter(pos, 666);
SListPrint(&plist);
printf("\n");
SListNode* pos1 = SListFind(&plist, 3);
SListEraseAfter(pos1);
SListPrint(&plist);
printf("\n");
return 0;
}