数据结构_单链表
来源:互联网 发布:四分位数java 编辑:程序博客网 时间:2024/06/14 16:25
单链表定义:
typedef int DataType;typedef struct SListNode{ struct SListNode* _next; DataType _data;}SListNode;
对单链表“增删查改”的函数:
#ifndef __CODE_H__#define __CODE_H__#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>#include <Windows.h>typedef int DataType;typedef struct SListNode{ struct SListNode* _next; DataType _data;}SListNode;SListNode* BuySListNode(DataType x); //创建一个结点void SListPrint(SListNode* pHead); //打印单链表void SListDestory(SListNode** ppHead); //删除单链表void SListPushBack(SListNode** ppHead, DataType x); //尾插void SListPopBack(SListNode** ppHead); //尾删void SListPushFront(SListNode** ppHead, DataType x); //头插void SListPopFront(SListNode** ppHead); //头删SListNode* SListFind(SListNode* pHead, DataType x); //查找void SListInsest(SListNode** ppHead, SListNode* pos, DataType x); //指定位置插入void SListErase(SListNode** ppHead, SListNode* pos); //指定位置删除#endif //__CODE_H__
函数的代码实现:
#include "code.h"SListNode* BuySListNode(DataType x){ SListNode* cur = (SListNode*)malloc(sizeof(SListNode)); cur->_data = x; cur->_next = NULL; return cur;}void SListPrint(SListNode* pHead){ //assert(pHead); while (pHead) { printf("%d ", pHead->_data); pHead = pHead->_next; } printf("\n");}void SListDestory(SListNode** ppHead){ assert(ppHead); if (*ppHead == NULL) { return; } else { while (*ppHead) { SListPopFront(ppHead); } }}void SListPushBack(SListNode** ppHead, DataType x){ assert(ppHead); if (*ppHead == NULL) { *ppHead = BuySListNode(x); } else { SListNode* cur = *ppHead; while (cur->_next) { cur = cur->_next; } cur->_next = BuySListNode(x); }}void SListPopBack(SListNode** ppHead){ assert(ppHead); assert(*ppHead); SListNode* cur = *ppHead; SListNode* tail = NULL; if ((*ppHead)->_next == NULL) { SListPopFront(ppHead); } else { while (cur->_next) { tail = cur; cur = cur->_next; } free(cur); tail->_next = NULL; }}void SListPushFront(SListNode** ppHead, DataType x){ assert(ppHead); if (*ppHead == NULL) { *ppHead = BuySListNode(x); } else { SListNode* cur = BuySListNode(x); cur->_next = *ppHead; *ppHead = cur; }}void SListPopFront(SListNode** ppHead){ assert(ppHead); if (*ppHead != NULL) { SListNode* cur = *ppHead; *ppHead = (*ppHead)->_next; free(cur); }}SListNode* SListFind(SListNode* pHead, DataType x){ assert(pHead); while (pHead) { if (pHead->_data == x) { return pHead; } else { pHead = pHead->_next; } } return NULL;}void SListInsest(SListNode** ppHead, SListNode* pos, DataType x){ assert(ppHead); assert(pos); if (*ppHead == pos) { SListPushFront(ppHead, x); } else { SListNode* cur = *ppHead; SListNode* tail = NULL; while (cur->_next != pos) { cur = cur->_next; } tail = BuySListNode(x); cur->_next = tail; tail->_next = pos; }}void SListErase(SListNode** ppHead, SListNode* pos){ assert(ppHead); assert(pos); if (*ppHead == pos) { SListPopFront(ppHead); } else { SListNode* cur = *ppHead; while (cur->_next != pos) { cur = cur->_next; } SListPopFront(&pos); cur->_next = pos; }}
测试用例:
#include "code.h"SListNode* Node;void test1(){ SListPrint(Node); SListPushFront(&Node, 3); SListPushFront(&Node, 2); SListPushFront(&Node, 1); SListPushFront(&Node, 0); SListPrint(Node);}void test2(){ SListPushBack(&Node, 4); SListPushBack(&Node, 5); SListPushBack(&Node, 6); SListPrint(Node);}void test3(){ SListPopFront(&Node); SListPopBack(&Node); SListPrint(Node);}void test4(){ SListInsest(&Node, Node->_next, 10); SListPrint(Node);}void test5(){ SListErase(&Node, Node->_next); SListPrint(Node);}void test6(){ SListErase(&Node, SListFind(Node, 2)); SListPrint(Node);}void test7(){ SListDestory(&Node); SListPrint(Node);}int main(){ test1(); test2(); test3(); test4(); test5(); test6(); test7(); return 0;}
阅读全文
0 0
- 数据结构_单链表
- 数据结构_单链表
- 数据结构_单链表
- 数据结构_单链表
- 数据结构_单链表
- 数据结构_单链表
- c语言_数据结构_单链表
- 数据结构-线性表_单链表
- 数据结构-线性表_单链表
- (数据结构)线性表_单链表反转 _模仿
- 数据结构与算法_链表1_单链表
- 实战数据结构(1)_单链表的操作
- 数据结构_单链表实现选择排序
- 数据结构_单链表的建立与反转
- 数据结构_单链表的逆置
- 数据结构基础2_单链表的实现
- 【数据结构_链表_List_0954】单链表的链接
- 数据结构_单链表(带头节点)
- Linux.ProxyM僵尸网络再次发起疯狂攻击,感染过万台设备
- Android Studio优化编译速度
- 开源数据库工具DBeaver能支持的数据库种类
- 集合中的质数(容斥原理&&位操作)
- [Raspberrypi] 树莓派 搭建mysql+tomcat+jdk环境
- 数据结构_单链表
- Deep Learning(深度学习)学习笔记整理系列(七)
- navicat连接远程数据库
- 《文法俱乐部》读书笔记之动词时态--LG二进制
- java 生成二维码
- gdb调试
- codeforces Educational Round 34 D Almost Difference
- 坚持#第243天~shell脚本5
- [Linux] CentOS7安装Chrome后yum不可以使用的解决方法