当前位置: 首页 > news >正文

无头单向不循环链表和带头双向循环链表的创建

 Lei宝啊:个人主页

愿所有美好不期而遇



 

前言:

 

接下来我们将会了解最基础的链表--->单链表

以及最方便也是最爽的链表--->带头双向循环链表。

 

 若有看不懂之处,可画图或者借鉴这里:反转单链表,对于数据结构而言,无非就是增删查改,当我们能够熟练应用以及画图后,其OJ题和以下代码都是小卡拉米。

 

无头单向不循环链表

单链表图:

 

头文件:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>typedef int DataType;typedef struct STLNode
{DataType data;struct STLNode* next;
}STLNode;void STL_Print(STLNode* phead);
void STL_PushBack(STLNode** pphead, DataType x);
void STL_PushFront(STLNode** pphead, DataType x);
void STL_PopBack(STLNode** pphead);
void STL_PopFront(STLNode** pphead);
STLNode* STL_Find(STLNode* phead, DataType x);
void STL_InsertAfter(STLNode* pos, DataType x);
void STL_EraseAfter(STLNode* pos);
void STL_Destroy(STLNode** pphead);

Test文件:

#include "STLNode.h"void Test1(STLNode* phead);int main()
{STLNode* plist = NULL;Test1(plist);return 0;
}void Test1(STLNode* phead)
{STL_PushBack(&phead, 1);STL_PushBack(&phead, 2);STL_PushBack(&phead, 3);STL_Print(phead);STL_PushFront(&phead, 11);STL_PushFront(&phead, 22);STL_PushFront(&phead, 33);STL_Print(phead);STLNode* ret = STL_Find(phead, 1);STL_InsertAfter(ret, 666);STL_Print(phead);STL_EraseAfter(ret);STL_Print(phead);STL_Destroy(&phead);STL_Print(phead);//STL_PopBack(&phead);//STL_PopBack(&phead);//STL_Print(phead);//STL_PopFront(&phead);//STL_PopFront(&phead);//STL_Print(phead);}

函数源文件:

对于以下函数中部分有对phead和pphead的检查,部分没有,原因是这样的:

对于是否需要对其进行断言,我们是要看他为NULL时合不合理,合理就不断言,不合理就断言,例如STL_Printf函数中,当phead为NULL时,说明该链表是空的,直接打印NULL就可以,这是很合理的。而对于STL_PushBack函数来说,phead为NULL同样合理,因为phead为NULL我们尾插其实也就相当于头插,很合理,但是对于pphead来说,他作为plist的地址,就算plist为NULL,pphead是不应该为NULL的,所以他为NULL就非常不合理,我们要对其进行断言检查。

#include "STLNode.h"void STL_Print(STLNode* phead)
{while (phead){printf("%d->", phead->data);phead = phead->next;}printf("NULL\n");}STLNode* Malloc(DataType x)
{STLNode* temp = (STLNode*)malloc(sizeof(STLNode));if (temp == NULL){perror("malloc fail");exit(-1);}temp->data = x;temp->next = NULL;return temp;
}void STL_PushBack(STLNode** pphead, DataType x)
{assert(pphead);STLNode* temp = Malloc(x);if (*pphead == NULL){*pphead = temp;}else{STLNode* cur = *pphead;while (cur->next != NULL){cur = cur->next;}cur->next = temp;}
}void STL_PushFront(STLNode** pphead, DataType x)
{assert(pphead);STLNode* temp = Malloc(x);temp->next = *pphead;*pphead = temp;}void STL_PopBack(STLNode** pphead)
{assert(pphead);assert(*pphead);STLNode* cur = *pphead;STLNode* temp = *pphead;if (cur->next == NULL){free(cur);*pphead = NULL;}else{while (cur->next){temp = cur;cur = cur->next;}free(cur);temp->next = NULL;}}void STL_PopFront(STLNode** pphead)
{assert(pphead);assert(*pphead);STLNode* cur = *pphead;*pphead = (*pphead)->next;free(cur);}STLNode* STL_Find(STLNode* phead, DataType x)
{if (phead == NULL){printf("NULL\n");return;}while (phead != NULL){if (phead->data == x){return phead;}phead = phead->next;}printf("Can not find\n");return;
}void STL_InsertAfter(STLNode* pos, DataType x)
{assert(pos);STLNode* temp = Malloc(x);temp->next = pos->next;pos->next = temp;}void STL_EraseAfter(STLNode* pos)
{assert(pos);assert(pos->next);STLNode* cur = pos->next;pos->next = pos->next->next;free(cur);
}void STL_Destroy(STLNode** pphead)
{assert(pphead);if (*pphead == NULL){return;}STLNode* temp = *pphead;STLNode* cur = *pphead;while (temp){cur = temp->next;free(temp);temp = cur;}*pphead = NULL;
}



 

带头双向循环链表

图:

 

头文件:

这个链表最爽的地方在于,他可以很轻松地找到尾,不管是尾插还是头插都非常爽,非常方便,如果我们想在半小时内写完这个链表,那么就可以对尾插和头插复用LTInsert函数,对于尾删和头删复用LTErase函数,也就是说,这两个函数是核心函数,有了他们,迅速写完该链表成为现实。

尾插的复用:LTInsert(phead,x);

头插的复用:LTInsert(phead->next,x);

尾删的复用:LTErase(phead->prev);

头删的复用:LTErase(phead->next);

​​#include <stdio.h>
#include <stdlib.h>
#include <assert.h>typedef int DataType;typedef struct LTNode
{DataType data;struct LTNode* prev;struct LTNode* next;
}LTNode;LTNode* Init();
LTNode* Malloc(DataType x);
LTNode* LTFind(LTNode* phead, DataType x);
void LTPrintf(LTNode* phead);
void LTPushBack(LTNode* phead, DataType x);
void LTPopBack(LTNode* phead);
void LTPushFront(LTNode* phead, DataType x);
void LTPopFront(LTNode* phead);
void LTInsert(LTNode* pos, DataType x);   //在pos位置前插入节点
void LTErase(LTNode* pos);                //删除pos位置节点
void LTDestroy(LTNode* phead);

 Test文件:

#define _CRT_SECURE_NO_WARNINGS 1
#include "LTNode.h"void Test1();int main()
{Test1();return 0;
}void Test1()
{LTNode* plist = NULL;plist = Init();LTPushBack(plist, 1);LTPushBack(plist, 2);LTPushBack(plist, 3);LTPushBack(plist, 4);LTPushBack(plist, 5);LTPushBack(plist, 6);LTPrintf(plist);LTPopBack(plist);LTPopBack(plist);LTPopBack(plist);LTPrintf(plist);LTPushFront(plist, 10);LTPushFront(plist, 20);LTPushFront(plist, 30);LTPrintf(plist);LTPopFront(plist);LTPrintf(plist);LTNode *pos = LTFind(plist, 10);LTInsert(pos, 66);LTPrintf(plist);LTErase(pos);LTPrintf(plist);LTDestroy(plist);plist = NULL;}

函数源文件:

#define _CRT_SECURE_NO_WARNINGS 1
#include "LTNode.h"LTNode* Malloc(DataType x)
{LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));if (newnode == NULL){perror("malloc fail");exit(-1);}newnode->data = x;return newnode;
}LTNode* Init()
{LTNode* newnode = Malloc(0);newnode->next = newnode;newnode->prev = newnode;return newnode;
}void LTPrintf(LTNode* phead)
{assert(phead);printf("phead<=>");LTNode* cur = phead->next;while (cur != phead){printf("%d<=>", cur->data);cur = cur->next;}putchar('\n');
}void LTPushBack(LTNode* phead, DataType x)
{assert(phead);LTNode *newnode = Malloc(x);LTNode* tail = phead->prev;tail->next = newnode;newnode->prev = tail;newnode->next = phead;phead->prev = newnode;}void LTPopBack(LTNode* phead)
{assert(phead);assert(phead->next != phead);LTNode* tail = phead->prev;LTNode* newtail = tail->prev;newtail->next = phead;phead->prev = newtail;free(tail);
}void LTPushFront(LTNode* phead, DataType x)
{assert(phead);LTNode* newnode = Malloc(x);LTNode* old_next = phead->next;phead->next = newnode;newnode->prev = phead;newnode->next = old_next;old_next->prev = newnode;}void LTPopFront(LTNode* phead)
{assert(phead);assert(phead->next != phead);LTNode* del = phead->next;phead->next = del->next;del->next->prev = phead;free(del);
}LTNode* LTFind(LTNode* phead, DataType x)
{assert(phead);LTNode* cur = phead->next;while (cur != phead){if (cur->data == x){return cur;}cur = cur->next;}printf("nothing!\n");return NULL;
}void LTInsert(LTNode* pos, DataType x)
{assert(pos);LTNode* newnode = Malloc(x);LTNode* posprev = pos->prev;posprev->next = newnode;newnode->prev = posprev;newnode->next = pos;pos->prev = newnode;
}void LTErase(LTNode* pos)
{assert(pos);LTNode* posprev = pos->prev;LTNode* posnext = pos->next;posprev->next = posnext;posnext->prev = posprev;free(pos);
}void LTDestroy(LTNode* phead)
{assert(phead);LTNode* cur = phead->next;while (cur != phead){LTNode* next = cur->next;free(cur);cur = next;}free(cur);
}

 

http://www.lryc.cn/news/107633.html

相关文章:

  • 超简单的fastapi链接websocket用例
  • MySQL详解
  • Vue [Day2]
  • 【前端|Javascript第1篇】一文搞懂Javascript的基本语法
  • 【Linux命令200例】cp用于复制文件和目录(常用)
  • C高级_第二讲_shell指令和shell脚本_递归练习
  • 静态路由综合实验
  • Spring核心IOC控制反转思想-----Spring框架
  • 中小企业如何做好MES管理系统实施建设
  • java环境搭建 Ubuntu Linux
  • 微信小程序使用mp-html遇到的问题并解决
  • 【VTK】基于读取出来的 STL 模型,当用户点击鼠标左键时,程序将获取点击位置的点,显示其坐标,并设置它为模型的旋转原点
  • 【第一阶段】kotlin的when表达式
  • C#中Convert.ToInt32() 和 int.Parse()的区别
  • 安全学习DAY14_JS信息打点
  • windows下配置vue开发环境
  • AndroidTV 获取焦点View放大效果实现方式
  • 访问者模式——操作复杂对象结构
  • 指针经典笔试题强训(附图详解)
  • Python:列表(list)与元组(tuple)
  • 常见的相似性度量方法
  • Day06-JS高级编程
  • 针对高可靠性和高性能优化的1200V硅碳化物沟道MOSFET
  • 开发框架软件公司:与之携手,共同开启办公流程化之路!
  • openCV C++环境配置
  • 8.3 作业 c高级
  • django实现部门表的增删改查界面
  • Tomcat的介绍和安装配置、eclipse中动态web项目的创建和运行、使用IDEA创建web项目并运行
  • idea操作——已经push到远程的代码回滚(不保留本地更改)
  • 无涯教程-Lua - 垃圾回收