单链表(无头节点)的实现
来源:互联网 发布:java web基础 编辑:程序博客网 时间:2024/04/30 07:51
链表:链表是一种顺序表,但并不是顺序存储,每个节点都存储着一个指向下一个节点的指针,把存储的数据元素连接起来
//SlistNode.h#pragma once#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>#include <stdlib.h>#include <Windows.h>#include <assert.h>typedef int Datatype;typedef struct SlistNode{ Datatype data; struct SlistNode* next;}SlistNode;void InitSlist(SlistNode*& pHead);//初始化void PrintList(SlistNode*& pHead);//打印void PushBack(SlistNode*& pHead,Datatype a);//尾插void PopBack(SlistNode*& pHead);//尾删void PushFront(SlistNode*& pHead, Datatype a);//头插void PopFront(SlistNode*& pHead);//头删void Insert(SlistNode*& pHead, SlistNode*& pos, Datatype a);//指定位置插入void Erase(SlistNode*& pHead, SlistNode*& pos);//删除指定位置数据void Remove(SlistNode*& pHead,Datatype a);//删除指定数SlistNode* Find(SlistNode* pHead, Datatype a);//查找void Destory(SlistNode*& pHead);//销毁SlistNode* BuyNode(Datatype a);//申请节点
//SlistNode.cpp#include "SlistNode.h"void InitSlist(SlistNode*& pHead){ pHead = NULL;}void PrintList(SlistNode*& pHead){ SlistNode* temp = pHead; if (temp == NULL) { return; } while (temp) { printf("%d ", temp->data); temp = temp->next; } printf("\n");}SlistNode* BuyNode(Datatype a){ SlistNode* node = (SlistNode*)malloc(sizeof(SlistNode)); assert(node); node->data = a; node->next = NULL; return node;}void PushBack(SlistNode*& pHead, Datatype a){ if (pHead == NULL) { pHead = BuyNode(a); } else { SlistNode* pcur = pHead; while (pcur->next) { pcur = pcur->next; } pcur->next = BuyNode(a); }}void PopBack(SlistNode*& pHead){ assert(pHead); if (pHead->next == NULL) { free(pHead); pHead = NULL; } else { SlistNode* pcur = pHead; SlistNode* pre = pcur; while (pcur->next) { pre = pcur; pcur = pcur->next; } free(pcur); pcur = NULL; pre->next = NULL; }}void PushFront(SlistNode*& pHead, Datatype a){ assert(pHead); if (pHead == NULL) { pHead = BuyNode(a); } else { SlistNode* newnode = BuyNode(a); newnode->next = pHead; pHead = newnode; }}void PopFront(SlistNode*& pHead){ assert(pHead); if (pHead == NULL) { return; } else if (pHead->next==NULL) { free(pHead); pHead = NULL; } else { SlistNode* pcur = pHead; pHead = pHead->next; free(pcur); pcur = NULL; }}void Insert(SlistNode*& pHead, SlistNode*& pos, Datatype a)//在pos之前插入数据{ assert(pHead); assert(pos); if (pHead == pos) { SlistNode* newnode = BuyNode(a); newnode->next = pHead; pHead = newnode; } else { SlistNode* pcur = pHead; SlistNode* pre = pcur; while (pcur != pos) { pre = pcur; pcur = pcur->next; } SlistNode* newnode = BuyNode(a); newnode->next = pcur; pre->next = newnode; }}SlistNode* Find(SlistNode* pHead, Datatype a){ assert(pHead); SlistNode* temp = pHead; while (temp) { if (temp->data == a) { return temp; } temp = temp->next; } return NULL;}void Erase(SlistNode*& pHead, SlistNode*& pos){ assert(pHead&&pos); if (pos == pHead) { SlistNode* temp = pHead; pHead = pHead->next; free(temp); temp = NULL; } else { SlistNode* pcur = pHead; while (pcur->next != pos) { pcur = pcur->next; } pcur->next = pos->next; free(pos); pos = NULL; }}void Remove(SlistNode*& pHead, Datatype a){ assert(pHead); SlistNode* pos = Find(pHead, a); if (pos == pHead) { SlistNode* temp = pHead; pHead = pHead->next; free(temp); temp = NULL; } else { SlistNode* pcur = pHead; while (pcur->next != ``os) { pcur = pcur->next; } pcur->next = pos->next; free(pos); pos = NULL; }}void Destory(SlistNode*& pHead){ assert(pHead); free(pHead); pHead = NULL;}
注:这里用到了c++的引用
0 0
- 单链表(无头节点)的实现
- C语言实现无头节点的单链表
- C语言实现无头节点单链表
- 单链表 无头节点
- 简单的无头节点单链表
- 【数据结构】单链表(无头节点)
- 2.17-比较有头节点的单链表与无头节点插入元素的区别;
- 有/无头节点的单链表的创建(尾插法)
- 线性表的链式存储结构(无头节点)
- C++双向链表的建立(无头节点)
- 单链表中的冒泡排序(无头节点)
- 无头结点的单链表(java实现)
- 无头节点,删除已知节点的前驱(循环链表)
- C语言实现无头节点链表
- 在无头节点的单链表里删除元素
- 实现无头结点单链表的基本操作函数
- 16 - 10 - 31 无头节点 单链表 ------《c primer plus》
- 数据实验7倒置无头节点单链表
- IOS学习之旅之3 页面跳转及绘制
- A Simple Problem with Integers(线段树 做的我想哭)
- AsyncTask机制详解
- 闭包
- 第15周 oj Problem J: C语言习题 字符串比较
- 单链表(无头节点)的实现
- 《工业通信网络与系统集成》——读后记
- java数据类型
- getchar() putchar()
- JAVA实现银行系统
- Bootstrap轮播
- 正式开启博客之路!
- Linux操作常用命令(整理)
- 快速使用FileProvider解决Android7.0文件权限问题