#单链表头文件
//SList
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define _CRT_SECURE_NO_WARNINGS 1
typedef int DataType;
typedef struct SListNode
{
DataType data;
struct Node *next;
} SListNode ;
// 初始化链表
void SListInit(SListNode** pHead);
// 单链表尾插
void SListPushBack(SListNode** pHead, DataType data);
// 单链表尾删
void SListPopBack(SListNode** pHead);
// 单链表头插
void SListPushFront(SListNode** pHead, DataType data);
// 单链表头删
void SListPopFront(SListNode** pHead);
// 单链表销毁
void SListDestroy(SListNode** pHead);
// 单链表中查找值为data的元素,查到后返回该结点,否则返回NULL
SListNode* SListFind(SListNode* pHead, DataType data);
// 获取单链表最后一个节点
SListNode* SListBack(SListNode* pHead);
SListNode* BuySListNode(DataType data);
#所需要的操作
// 初始化链表
SListNode* BuySListNode(DataType Data)
{
SListNode *cur = (SListNode*)malloc(sizeof(SListNode));
if (cur == NULL)
{
printf(" BuyNode :: malloc failed ! \n");
return;
}
cur->data = Data;
cur->next = NULL;
return cur;
}
void SListInit(SListNode** pHead)
{
assert(pHead);
(*pHead) = NULL;
}
// 单链表尾插
void SListPushBack(SListNode** pHead, DataType data)
{
assert(pHead);
SListNode *cur = BuySListNode(data);
if (cur == NULL)
{
printf("SListPushBack :: BuySListNode falied ! \n");
return;
}
if ((*pHead) == NULL)
{
(*pHead) = cur;
return;
}
else
{
SListNode *pcur = (*pHead);
while (pcur->next != NULL)
{
pcur = pcur->next;
}
pcur->next = cur;
}
return;
}
// 单链表尾删
void SListPopBack(SListNode** pHead)
{
assert(pHead);
if ((*pHead) == NULL)
{
printf("单链表为空无法尾删 !\n");
return;
}
SListNode *cur = (*pHead);
if(cur ->next == NULL)
{
free(cur);
(*pHead) = NULL;
}
else
{
SListNode *pre = cur;
cur = cur->next;
while (cur->next != NULL)
{
pre = pre->next;
cur = cur->next;
}
pre->next = NULL;
free(cur);
cur = NULL;
}
}
// 单链表头插
void SListPushFront(SListNode** pHead, DataType data)
{
assert(pHead);
SListNode *cur = BuySListNode(data);
if (cur == NULL)
{
printf("SListPushBack :: BuySListNode falied ! \n");
return;
}
cur->next = (*pHead);
(*pHead) = cur;
return;
}
// 单链表头删
void SListPopFront(SListNode** pHead)
{
assert(pHead);
if ((*pHead) == NULL)
{
printf("链表为空无法删除 ! \n");
return;
}
else
{
SListNode *temp = (*pHead);
(*pHead) = (*pHead)->next;
free(temp);
temp = NULL;
}
}
void SListDestroy(SListNode** pHead)
{
assert(pHead);
SListNode *cur = (*pHead);
SListNode *tmp = NULL;
if (cur == NULL)
{
return;
}
while (cur->next != NULL)
{
tmp = cur;
cur = cur->next;
free(tmp);
tmp = NULL;
}
(*pHead) = NULL;
return;
}
SListNode* SListFind(SListNode* pHead, DataType data)
{
assert(pHead);
SListNode *cur = pHead;
if (cur == NULL)
{
printf("链表为空 ! \n");
return NULL;
}
while (cur != NULL)
{
if (cur->data == data)
{
break;
}
cur = cur->next;
}
return cur;
}
SListNode* SListBack(SListNode* pHead)
{
assert(pHead);
if (pHead == NULL)
{
printf("链表为空,无法获取队尾元素 !\n");
return NULL;
}
SListNode *cur = pHead;
if (cur->next == NULL)
{
return cur;
}
else
{
while (cur->next != NULL)
{
cur = cur->next;
}
return cur;
}
}
```c
#验证操作
`#include "SList.h"
void Print(SListNode *pHead)
{
SListNode *cur = pHead;
while (cur != NULL)
{
printf(" %d ", cur->data);
cur = cur->next;
}
printf("\n");
}
int main()
{
SListNode *pHead;
SListInit(&pHead);
SListPushBack(&pHead, 1);
SListPushBack(&pHead, 2);
SListPushBack(&pHead, 3);
Print(pHead);
SListPopBack(&pHead);
Print(pHead);
SListPushFront(&pHead, 1);
SListPushFront(&pHead, 2);
SListPushFront(&pHead, 3);
Print(pHead);
SListPopFront(&pHead);
Print(pHead);
SListNode *pcur = SListFind (pHead , 1);
printf("pcur = %d \n", pcur->data);
pcur = SListBack(pHead);
printf("pcur = %d \n", pcur->data);
system("pause");
return 0;
}