SList.h文件用于存放声明文件
//SList.h
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef int SLTDataType;
struct SListNode {
SLTDataType data;
struct SListNode* next;
};
typedef struct SListNode SLTNode;
//不会改变链表的头指针,传一级指针
void SListPrint(SLTNode* phead);
//会改变链表的头指针,传二级指针
void SListPushBack(SLTNode** pphead, SLTDataType x);
void SListPushFront(SLTNode** pphead, SLTDataType x);
void SListPopBack(SLTNode** pphead);
void SListPopFront(SLTNode** pphead);
SLTNode* SListFind(SLTNode* pphead, SLTDataType x);
void SListInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x);
void SListErase(SLTNode** pphead, SLTNode* pos);
SList.c文件用于存放具体函数定义文件
//SList.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "SList.h"
void SListPrint(SLTNode* phead) {
SLTNode* cur = phead;
while (cur!=NULL) {
printf("%d->",cur->data);
cur = cur->next;
}
printf("NULL\n");
}
//申请空间
SLTNode* BuySListNode(SLTDataType x) {
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
newnode->data = x;
newnode->next = NULL;
return newnode;
}
//尾插
void SListPushBack(SLTNode** pphead, SLTDataType x) {
SLTNode* newnode = BuySListNode(x);
if (*pphead == NULL) {
*pphead = newnode;
}
else {
//找尾结点的指针
SLTNode* tail = *pphead;
while (tail->next != NULL) {
tail = tail->next;
}
//尾结点,链接新结点
tail->next = newnode;
}
}
//头插
void SListPushFront(SLTNode** pphead, SLTDataType x) {
SLTNode* newnode = BuySListNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
//尾删
void SListPopFront(SLTNode** pphead) {
//为空
if (*pphead == NULL) {
return;
}
//只有一个
else if ((*pphead)->next == NULL) {
free(*pphead);
*pphead = NULL;
}
else {
SLTNode* prev = NULL;
SLTNode* tail = *pphead;
while (tail->next!=NULL) {
prev = tail;
tail = tail->next;
}
free(tail);
prev->next = NULL;
}
}
//头删
void SListPopBack(SLTNode** pphead) {
//*pphead是plist
SLTNode* next = (*pphead)->next;
free(*pphead);
*pphead = next;
}
SLTNode* SListFind(SLTNode* phead, SLTDataType x) {
SLTNode* cur = phead;
while (cur) {
if (cur->data == x) {
return cur;
}
cur = cur->next;
}
return NULL;
}
void SListInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x) {
//如果是在第一个的前边插
if (pos == *pphead) {
SListPushFront(pphead,x);
}
else {
SLTNode* newnode = BuySListNode(x);
SLTNode* prev = *pphead;
while (prev->next!=pos) {
prev = prev->next;
}
prev->next = newnode;
newnode->next = pos;
}
}
//删除pos位置的值
void SListErase(SLTNode** pphead, SLTNode* pos) {
if (pos == *pphead) {
SListPopFront(pphead);
}
else {
SLTNode* prev = *pphead;
while (prev->next != pos) {
prev = prev->next;
}
prev->next = pos->next;
free(pos);
}
}
test.c文件用于测试
//test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "SList.h"
void TestSList1() {
SLTNode* plist = NULL;
SListPushBack(&plist, 1);
SListPushBack(&plist, 2);
SListPushBack(&plist, 3);
SListPushBack(&plist, 4);
SListPushBack(&plist, 5);
//SListPrint(plist);
//SListPushFront(&plist, 0);
//SListPrint(plist);
//SListPopBack(&plist);
//SListPopBack(&plist);
//SListPopBack(&plist);
//SListPrint(plist);
//SListPrint(plist);
SListPopFront(&plist);
SListPrint(plist);
/*SLTNode* pos = SListFind(plist,3);
if (pos) {
SListInsert(&plist,pos,30);
}
SListPrint(plist);*/
SListPrint(plist);
SLTNode* pos = SListFind(plist, 1);
if (pos) {
SListErase(&plist, pos);
}
SListPrint(plist);
}
int main() {
TestSList1();
return 0;
}