当前位置:网站首页>[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);
}边栏推荐
猜你喜欢

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

Installation of PHP FPM software +openresty cache construction

animation

MySQL 8

Open the influence list of "National Meteorological Short Videos (Kwai, Tiktok) in November" in an interactive way“

Unity2019_ Lighting system

ArrayList
![[updating] wechat applet learning notes_ three](/img/05/958b8d62d3a42b38ca1a2d8631a7f8.png)
[updating] wechat applet learning notes_ three

详解sizeof、strlen、指针和数组等组合题
![[global product discovery 2] the first pure cloud augmented reality (AR) platform - Israel](/img/51/04f5a9dbd03438fbdf25545a81b7ba.jpg)
[global product discovery 2] the first pure cloud augmented reality (AR) platform - Israel
随机推荐
【K&R】中文第二版 个人题解 Chapter1
Kwai 20200412 recruitment
Minimap plug-in
[usaco12mar]cows in a skyscraper g (state compression DP)
Haproxy+kept cluster setup 02
matlab神經網絡所有傳遞函數(激活函數)公式詳解
unity2019_ Input management
Basic operation and process control
Transplantation of freetype Library
Explain sizeof, strlen, pointer, array and other combination questions in detail
matlab神经网络所有传递函数(激活函数)公式详解
Sequence of map implementation classes
Retail philosophy retail psychological warfare after reading -- 7-11 is a good product!
Delete the last character of the string in golang
Unity2019_ Lighting system
About Wireshark's unsuccessful installation of npcap
Compilation error: "not in executable format: file format not recognized"“
Ilruntime learning - start from scratch
Advanced OSG collision detection
Conversion between golang JSON format and structure