当前位置:网站首页>[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);
}边栏推荐
- 【云原生】微服务之Feign的介绍与使用
- [end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December
- Installation of PHP FPM software +openresty cache construction
- Mall management system of database application technology course design
- Unity change default editor
- Use of ue5 QRcode plug-in
- P1596 [USACO10OCT]Lake Counting S
- 十六进制编码简介
- Osgearth target selection
- Conversion between golang JSON format and structure
猜你喜欢

About the problem that the editor and the white screen of the login interface cannot be found after the location of unityhub is changed

KunlunBase MeetUP 等您来!

100 GIS practical application cases (78) - Multi compliance database design and data warehousing

数据分析练习题

Introduction to Base64 coding

Redis data structure

GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库

Simple demo of solving BP neural network by gradient descent method

Kunlunbase meetup is waiting for you!

数据的存储
随机推荐
了解小程序的笔记 2022/7/3
Why can void * be a general pointer
Encoding and decoding of golang URL
C#课程设计之员工信息管理系统
Compilation error: "not in executable format: file format not recognized"“
Redis data structure
Initial unity
KunlunBase MeetUP 等您来!
Sequence of map implementation classes
Display terrain database on osgearth ball
Golang's range
Use filechannel to copy files
Get to know unity2 for the first time
Cesium for unreal quick start - simple scenario configuration
Detailed explanation of all transfer function (activation function) formulas of MATLAB neural network
Lua framwrok framework starts
Conversion between golang JSON format and structure
Chain length value
matlab神经网络所有传递函数(激活函数)公式详解
Youyou1 of xlua knapsack system