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

网站项目设计与制作综合实训网络服务器下载

网站项目设计与制作综合实训,网络服务器下载,网站开发年薪,微信网页视频怎么下载Lei宝啊:个人主页 愿所有美好不期而遇 前言: 接下来我们将会了解最基础的链表--->单链表 以及最方便也是最爽的链表--->带头双向循环链表。 若有看不懂之处,可画图或者借鉴这里:反转单链表,对于数据结构而言&am…

 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.yayakq.cn/news/524648/

相关文章:

  • 做网站点子在广州注册一个公司要多少钱
  • 手机网站漂浮怎么做硬件开发有哪些方向
  • 做虚拟币网站需要什么手续茶叶公司网站的建设
  • 百度企业网站建设产品备案查询
  • 手机上能不能制作网站开发wordpress移动端导航
  • 过年做那个网站能致富巴中做网站的公司
  • 微网站建设第一步是进行什么的设置深圳市建设集团和恒大的关系
  • 建站神器跟wordpress哪个好深圳软件开发工程师
  • 门户网站申请设计网站推荐视频
  • 济宁网站建设 悍诺天河区门户网站招生考试
  • 普陀网站建设做关于卖宠物饲料网站有什么名字吗
  • 乐清市建设规划局网站福建做网站的公司
  • 秦皇岛做网站公司排名北京轨道交通建设管理有限公司网站
  • 可以做渐变色块拼接的网站上海做网站的公司电话
  • 东莞网站自动化推广书生商友软件怎么样
  • 微商城网站建设报价wordpress 分类置顶
  • 黑龙江做网站手机网站返回跳转
  • 中国建设银行网站查询密码是什么意思商丘市做网站
  • 做封面图的网站vs哪个版本做网站好
  • 网站建站网站域名申请成都本地做网站的
  • 设计类专业网站新网站制作公司
  • 东莞网站制作百度联系方式
  • 连江可门港建设发展有限公司网站最好看免费观看高清大全老师补课
  • 网站设计内容wordpress的特点()
  • asp网站源码安装教程京东网站建设案例论文
  • 黑龙江专业网站建设游戏云电脑
  • 做网页收款网站wordpress都可以干什么
  • 网站浏览器兼容性通用博罗惠州网站建设
  • 网站换主题傻瓜式在线做网站
  • 域名注册以后怎样做网站安溪住房和城乡规划建设局网站