当前位置:网站首页>[linear table] basic operation of bidirectional linked list specify node exchange
[linear table] basic operation of bidirectional linked list specify node exchange
2022-07-03 08:23:00 【Herk can't write code】
Catalog
1. Basic operation of double linked list
2. Creation and printing of bidirectional linked list
3. The specified value is x The node of is exchanged with its predecessor node
1. Basic operation of double linked list
#include <iostream>
using namespace std;
typedef int Elemtype;
typedef struct LinkNode {
Elemtype data;
LinkNode *next, *previous;
} * DLIST, *Node;
void Delete(Node p)
{ // Delete p node
if (p->previous != NULL) {
p->previous->next = p->next;
}
if (p->next != NULL) {
p->next->previous = p->previous;
}
delete p;
}
void MakeNull(DLIST &DL)
{ // Empty the bidirectional linked list
delete DL->next;
DL = new LinkNode;
DL->next = NULL;
DL->previous = NULL;
}
void Insert(Elemtype x, Node p)
{ // At node p Add node after q
Node q = new LinkNode;
q->data = x;
q->next = p->next;
p->next->previous = q;
p->next = q;
q->previous = p;
}
Node Locate(Elemtype x, DLIST DL)
{ // Locate the first value as x The elements of
Node p = DL;
while (p->next != NULL) {
if (p->next->data == x)
return p->next;
else
p = p->next;
}
return p->next;
}
2. Creation and printing of bidirectional linked list
void CreatDList(DLIST &DL, int n)
{
int x;
Node p = DL;
p->previous = NULL;
p->next = new LinkNode;
p->next->next = NULL;
for (int i = 0; i < n; i++) {
cin >> x;
Insert(x, p);
}
}
void PrintDList(DLIST DL)
{
Node p = DL;
while (p->next != NULL) {
cout << p->next->data << "\t";
p = p->next;
}
cout << endl;
}
3. The specified value is x The node of is exchanged with its predecessor node
int Swap(Elemtype x, DLIST &DL)
{ // If the element value in the linked list is x The node of , Exchange it with its predecessor node
Node p1, p2;
p1 = Locate(x, DL);
if (p1 == NULL)
return 0;
else {
Insert(x, p1->previous->previous);
Delete(p1); // First put the new x Insert the front of the precursor node , Delete the old x
return 1;
}
}
int main()
{
DLIST DL = new LinkNode;
cout << " Input 5 Create a two-way linked list of data : ";
CreatDList(DL, 5);
PrintDList(DL);
Swap(2, DL);
PrintDList(DL);
}
边栏推荐
- Conversion between golang JSON format and structure
- php-fpm软件的安装+openresty高速缓存搭建
- Image processing 8-cnn image classification
- Mutual call between Lua and C #
- MySQL containerization (1) docker installation MySQL
- Osgearth topographic shading map drawing
- 详解sizeof、strlen、指针和数组等组合题
- 十六进制编码简介
- [set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)
- Student educational administration management system of C # curriculum design
猜你喜欢
Redis data structure
ArrayList
Shader foundation 01
About the problem that the editor and the white screen of the login interface cannot be found after the location of unityhub is changed
the installer has encountered an unexpected error installing this package
Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
Explain sizeof, strlen, pointer, array and other combination questions in detail
Transfinite hacker cognition
C#课程设计之学生教务管理系统
Haproxy+kept cluster setup 02
随机推荐
Cesium for unreal quick start - simple scenario configuration
Unity one click AssetBundle
[end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December
Osgearth target selection
[set theory] order relation (the relation between elements of partial order set | comparable | strictly less than | covering | Haas diagram)
[cloud native] introduction and use of feign of microservices
C#课程设计之学生教务管理系统
Transplantation of freetype Library
C语言-入门-精华版-带你走进编程(一)
数据分析练习题
Shader foundation 01
swagger文档配置
Kwai 20200412 recruitment
Introduction to hexadecimal coding
详解sizeof、strlen、指针和数组等组合题
Golang 中string和int类型相互转换
[global product discovery 2] the first pure cloud augmented reality (AR) platform - Israel
【音视频】ijkplayer错误码
I want to do large screen data visualization application feature analysis
Use filechannel to copy files