当前位置:网站首页>(7)双向链表
(7)双向链表
2022-06-22 07:49:00 【Day-3】
来源:http://c.biancheng.net/view/3342.html
目前我们所学到的链表,无论是动态链表还是静态链表,表中各节点中都只包含一个指针(游标),且都统一指向直接后继节点,通常称这类链表为单向链表(或单链表)。
虽然使用单链表能 100% 解决逻辑关系为 “一对一” 数据的存储问题,但在解决某些特殊问题时,单链表并不是效率最优的存储结构。比如说,如果算法中需要大量地找某指定结点的前趋结点,使用单链表无疑是灾难性的,因为单链表更适合 “从前往后” 找,而 “从后往前” 找并不是它的强项。
为了能够高效率解决类似的问题,本节来学习双向链表(简称双链表)。
从名字上理解双向链表,即链表是 “双向” 的,如图 1 所示:
双向链表结构示意图

双向,指的是各节点之间的逻辑关系是双向的,但通常头指针只设置一个,除非实际情况需要。
从图 1 中可以看到,双向链表中各节点包含以下 3 部分信息(如图 2 所示):
- 指针域:用于指向当前节点的直接前驱节点;
- 数据域:用于存储数据元素。
- 指针域:用于指向当前节点的直接后继节点;

因此,双链表的节点结构用 C 语言实现为:
typedef struct line{
struct line * prior; //指向直接前趋
int data;
struct line * next; //指向直接后继
}line;
通过双向链表实现图书管理系统。主要实现的功能是增删改查。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int nCount = 0;
//书籍结构
typedef struct Node {
struct Node * Blink;
struct Node * Flink;
char BookName[50];
float BookPrice;
int BookNumber;
};
struct Node * ndHeaderNode = NULL;
//增加书籍的函数
struct Node * AppendNode(struct Node * CurrentNode, char * BookName, int BookNumber, float BookPrice)
{
struct Node * pNewNode = NULL;
struct Node * pTempNode = NULL;
struct Node * pHeadNode = CurrentNode;
pNewNode = (struct Node *)malloc(sizeof(struct Node));
if (pNewNode == NULL)
{
printf("memory malloc failed!\n");
return pNewNode;
}
if (CurrentNode == NULL)
{
CurrentNode = pNewNode;
CurrentNode->Blink = NULL;
CurrentNode->Flink = NULL;
}
else
{
while (pHeadNode->Flink != NULL)
{
pTempNode = pHeadNode;
pHeadNode = pHeadNode->Flink;
}
pHeadNode->Flink = pNewNode;
pHeadNode->Blink = pTempNode;
}
strcpy(pNewNode->BookName, BookName);
pNewNode->BookPrice = BookPrice;
pNewNode->BookNumber = BookNumber;
pNewNode->Flink = NULL;
nCount++;
return CurrentNode;
}
//查询书籍的函数
void QueryNode(struct Node * HeaderNode, char * BookName)
{
if (HeaderNode == NULL)
{
printf("ERROR:HeaderNode == NULL\n");
return;
}
if (strcmp(HeaderNode->BookName, BookName) == 0)
{
printf("书名:%s\n", HeaderNode->BookName);
printf("定价:%f\n", HeaderNode->BookPrice);
printf("书号:%d\n", HeaderNode->BookNumber);
return;
}
while (HeaderNode->Flink != NULL)
{
HeaderNode = HeaderNode->Flink;
if (strcmp(HeaderNode->BookName, BookName) == 0)
{
printf("书名:%s\n", HeaderNode->BookName);
printf("定价:%f\n", HeaderNode->BookPrice);
printf("书号:%d\n", HeaderNode->BookNumber);
return;
}
}
printf("Can Not Found!\n");
}
//修改书籍信息
void ModifyNode(struct Node * HeaderNode, char * BookName, float BookPrice)
{
if (HeaderNode == NULL)
{
printf("ERROR:HeaderNode == NULL\n");
return;
}
if (strcmp(HeaderNode->BookName, BookName) == 0)
{
HeaderNode->BookPrice = BookPrice;
printf("ModifyNode Success!\n");
return;
}
while (HeaderNode->Flink != NULL)
{
HeaderNode = HeaderNode->Flink;
if (strcmp(HeaderNode->BookName, BookName) == 0)
{
HeaderNode->BookPrice = BookPrice;
printf("ModifyNode Success!\n");
return;
}
}
printf("ModifyNode Failed!\n");
return;
}
//删除书籍的函数
void DeleteNode(struct Node * HeaderNode, char * BookName)
{
struct Node * pNode = NULL;
pNode = HeaderNode;
for (size_t i = 0; i < nCount; i++)
{
if (strcmp(pNode->BookName, BookName) == 0)
{
if (pNode == HeaderNode)
{
pNode = HeaderNode->Flink;
free(HeaderNode);
ndHeaderNode = pNode;
HeaderNode = ndHeaderNode;
nCount--;
return;
}
if (pNode->Flink == NULL)
{
pNode->Blink->Flink = NULL;
free(pNode);
nCount--;
printf("Delete Success!\n");
return;
}
pNode->Blink->Flink = pNode->Flink;
pNode->Flink->Blink = pNode->Blink;
free(pNode);
nCount--;
printf("Delete Success!\n");
return;
}
}
}
void Start()
{
while (1)
{
int ReadFlag = 0;
char szBookName[50];
float fBookPrice = 0;
float fNewBookPrice = 0;
int nBookNumber = 0;
printf("请输入需要使用的功能:\n");
printf("1.添加书籍信息\n");
printf("2.查询书籍信息\n");
printf("3.修改书籍信息\n");
printf("4.删除书籍信息\n");
scanf("%d", &ReadFlag);
switch (ReadFlag)
{
case 1:
//memset(szBookName, 0 ,50);
printf("请输入书名:");
scanf("%s", szBookName);
printf("请输入定价 :");
scanf("%f", &fBookPrice);
printf("请输入书号:");
scanf("%d", &nBookNumber);
//新增函数
ndHeaderNode = AppendNode(ndHeaderNode, szBookName, nBookNumber, fBookPrice);
break;
case 2:
printf("请输入书名:");
scanf("%s", szBookName);
//查询函数
QueryNode(ndHeaderNode, szBookName);
break;
case 3:
printf("请输入书名:");
scanf("%s", szBookName);
printf("请输入新的定价:");
scanf("%f", &fNewBookPrice);
//code 修改的函数
ModifyNode(ndHeaderNode, szBookName, fNewBookPrice);
break;
case 4:
printf("请输入书名:");
scanf("%s", szBookName);
//删除函数
DeleteNode(ndHeaderNode, szBookName);
break;
}
}
}
int main()
{
Start();
return 0;
}
边栏推荐
- 模电实验——实验一 晶体管共射极单管放大器
- lr 2022超详细安装教程「最新」
- How to backup the treasures in the store and upload them to multiple stores
- [common template problems in graph theory] four shortest path solutions and two minimum spanning tree solutions
- What are uniapp, wap2app and 5 + app in dccloud?
- Open source open source version - pintuan
- Device tree failed to add SPI device node
- Solve syntaxerror: cannot use import statement outside a module
- 微信小游戏(二)
- [multi thread programming] thread scheduling strategy and priority
猜你喜欢

Qualcomm platform LCM summary

ES6 set data type de duplication of array, intersection, union and difference

Qualcomm platform msm8953 display subsystem learning

Expérience électrique en mode - - expérience 2 circuit d'amplification de source commune JFET

数据可视化优秀案例

How to backup the treasures in the store and upload them to multiple stores

An example shows the difference between let and VaR

【宋红康 MySQL数据库 】【高级篇】【07】MySQL的存储引擎

Canvastotempfilepath of wechat

Wx applet vant UI call interface to realize two-level cascade
随机推荐
User defined pop-up use
Ugui text spacing textspacing
Open version - user level
The applet uses the step bar vant steps in vant
[standard version 4.3] marketing activities (group bargaining second kill) error reporting under orders
FileTool
Blob format problems involved in image uploading
Idea cannot connect to sqlsms
Usage and understanding of async/await in JS
The width of each picture in the description of the mobile terminal baby should be between 480 and 1500, and the maximum height is 2500. The following pictures are not satisfied
The ranking of websites is very important for websites
JS 数组扁平化 (递归写法)
Qualcomm platform LCM summary
模电实验——实验二 JFET共源极放大电路
lr 2022超详细安装教程「最新」
5、 Image component
Introduction to several mainstream and easy-to-use rich text editors (WYSIWYG common editors)
Get through version - bargain activity
[v4.3] the applet fails to receive the subscription message, and the coupon time on the product details page is displayed incorrectly
JS to assign values to two objects with the same attributes