1.创建结点
//创建结点 SLTNode* BuySLTNode(SListDataType x) { SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode)); newnode->data = x; newnode->next = NULL; return newnode; }
2.打印
// 打印 void SListPrint(SLTNode* phead) { SLTNode* cur = phead; while (cur != NULL) { printf("%d->", cur->data); cur = cur->next; } printf("NULL\n"); }
3.尾插
//尾插 void SListPushBack(SLTNode** pphead, SListDataType x) { //1.要插入一个结点 首先要malloc一个结点 SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode)); newnode->data = x; //创建后要初始化 newnode->next = NULL; //2.如果链表无结点 就把newnode直接做表头 if (*pphead == NULL) { *pphead = newnode; } //3.如果链表有结点 找到尾 else { SLTNode* tail = *pphead;//尾 while (tail->next != NULL) { tail = tail->next; //尾一直找到最后一个节点 } tail->next = newnode; //最后一个结点的next为newnode } }
4.头插
//头插 void SListPushFront(SLTNode** pphead, SListDataType x) { //也要创建一个结点 SLTNode* newnode = BuySLTNode(x); //newnode->next 存放第一个结点 把表头改为newnode newnode->next = *pphead; *pphead = newnode; }
5.头删
//头删 void SListPopFront(SLTNode** pphead) { if (*pphead == NULL) { return; } //不能上来直接free头 SLTNode* tmp = *pphead; //先存链表头 一会free *pphead = (*pphead)->next;//把头换到下一个 free(tmp); }
6.尾删
//尾删 void SListPopBack(SLTNode** pphead) { if (*pphead == NULL) { return; } else if ((*pphead)->next == NULL) { free(*pphead); //把pList指向的那块内存空间释放 *pphead = NULL;//把pList指针置为空 } else { SLTNode* tail = *pphead; //尾删 需要知道尾和尾前的结构体(置为空) SLTNode* prev = NULL; while (tail->next != NULL) { prev = tail; //小跟班指针 tail = tail->next; } free(tail); prev->next = NULL; } }
7.查找
//查找 SLTNode* SListFind(SLTNode* phead, SListDataType x) { SLTNode* cur = phead; while (cur != NULL) { if (cur->data == x) { return cur; } cur = cur->next; } return NULL; }
8.在pos前面插入x
//在pos前面插入x void SListInsert(SLTNode** pphead, SLTNode* pos, SListDataType x) { SLTNode* newnode = BuySLTNode(x); if (*pphead == pos) { newnode->next = *pphead; *pphead = newnode; } else { SLTNode* cur = *pphead; while (cur->next != pos) { cur = cur->next; } newnode->next = pos; cur->next = newnode; } }
9.删除pos位置的值
//删除pos位置的值 void SListErase(SLTNode** pphead, SLTNode* pos) { if (pos == *pphead) { void SListPopFront(SLTNode * *pphead); } else { SLTNode* cur = *pphead; while (cur->next != pos) { cur = cur->next; } cur->next = pos->next; free(pos); } }
10.头文件
#pragma once #include<stdio.h> #include<stdlib.h> typedef int SListDataType; //重命名data类型 以后要改char double 直接就改 typedef struct SListNode { SListDataType data; struct SListNode* next; }SLTNode; //打印 void SListPrint(SLTNode* phead); //尾插 (可能会改变链表的头指针就要传二级指针) void SListPushBack(SLTNode** pphead, SListDataType x);//传入一个二级指针链表头 和 插入的数据 //头插 void SListPushFront(SLTNode** pphead, SListDataType x); //头删 void SListPopFront(SLTNode** pphead); //尾删 void SListPopBack(SLTNode** pphead); //查找 SLTNode* SListFind(SLTNode* phead, SListDataType x); //在pos前面插入x void SListInsert(SLTNode** pphead, SLTNode* pos, SListDataType x); //删除pos位置的值 void SListErase(SLTNode** pphead, SLTNode* pos);
11.测试代码
#define _CRT_SECURE_NO_WARNINGS 1 #include"Slist.h" void SLTNodeTest1() { SLTNode* Plist = NULL; SListPushBack(&Plist, 1); SListPushBack(&Plist, 2); SListPushBack(&Plist, 3); SListPushFront(&Plist, 0); //SListPopFront(&Plist); //SListPrint(Plist); SListPushBack(&Plist, 4); //SLTNode* pos = SListFind(Plist, 0); //if (pos != NULL) //{ // SListInsert(&Plist, pos, 30); //} SListPrint(Plist); SLTNode* pos = SListFind(Plist, 2); if (pos != NULL) { SListErase(&Plist, pos); } SListPrint(Plist); } int main() { SLTNodeTest1(); return 0; }