数据结构_单链表
2017-12-15 23:51
162 查看
单链表定义:
对单链表“增删查改”的函数:
函数的代码实现:
测试用例:
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; }
相关文章推荐
- [数据结构]线性表之单链表的类模板实现
- 左程云_算法与数据结构 — 链表问题 — 05反转部分单链表
- 数据结构C语言单链表的实现和几点注意的问题
- 数据结构学习笔记4——单链表的实现
- Henry手记—.NET数据结构对象补遗之单链表(一)
- 数据结构(14)单链表
- 笔试,面试,C/C++,数据结构单链表排序(改进冒泡排序)
- 数据结构 -- 单链表
- 数据结构和算法笔记 lesson4 线性表链式存储结构 单链表
- 单链表-----数据结构
- Java模拟单链表和双端链表数据结构的实例讲解
- 4.数据结构单链表之链表成员的插入与删除
- 算法设计和数据结构学习_8(单链表的递归逆序)
- 数据结构-单链表的实现
- 数据结构题典018:三个有序单链表求交(ANSI C)
- 数据结构和算法设计专题之---单链表的逆序
- java 数据结构单链表的实现
- (摘)c++数据结构---单链表的节点类和单链表的查找---(凌风)
- 数据结构基础之单链表基本操作实现
- 线性表之单链表学习小结(初学数据结构必看)