当前位置:网站首页>[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);
}边栏推荐
- P1896 [scoi2005] non aggression (shape pressure DP)
- [updating] wechat applet learning notes_ three
- Haproxy+kept build 01
- Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
- Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données
- Cesium service deployment, and import and display local 3dfiles data
- Unity2019_ Lighting system
- Ilruntime learning - start from scratch
- Osgearth topographic shading map drawing
- Image processing 8-cnn image classification
猜你喜欢

图像处理8-CNN图像分类

unity2019_ Input management

Explain sizeof, strlen, pointer, array and other combination questions in detail

Solution détaillée de toutes les formules de fonction de transfert (fonction d'activation) du réseau neuronal MATLAB

MXone Pro自适应2.0影视模板西瓜视频主题苹果cmsV10模板

How to establish rectangular coordinate system in space

Unity2019_ Lighting system

Wpf: solve the problem that materialdesign:dialoghost cannot be closed

jupyter远程服务器配置以及服务器开机自启

Oracle insert single quotation mark
随机推荐
Lua framwrok framework starts
Image processing 8-cnn image classification
Golang中删除字符串的最后一个字符
Multi traveling salesman problem -- overview of formula and solution process
Use of ue5 QRcode plug-in
2021-10-19
Abstract classes and interfaces
Scite change background color
C#课程设计之学生教务管理系统
Classes and objects
796 · unlock
数据分析练习题
Golang 时间格式整理
Unity change default editor
the installer has encountered an unexpected error installing this package
Shader foundation 01
數據庫應用技術課程設計之商城管理系統
Dotween plug-in
Display terrain database on osgearth ball
Golang 中string和int类型相互转换