当前位置:网站首页>[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);
}边栏推荐
- P2622 light off problem II (state compression search)
- [set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)
- oracle中的 (+)是什么意思
- Oracle insert single quotation mark
- Explain sizeof, strlen, pointer, array and other combination questions in detail
- Unity4.3.1 engine source code compilation process
- Redis的数据结构
- Unity change default editor
- unity2019_ Input management
- Chocolate installation
猜你喜欢

Minimap plug-in

How to establish rectangular coordinate system in space

Introduction to hexadecimal coding
![P1596 [USACO10OCT]Lake Counting S](/img/a7/07a84c93ee476788d9443c0add808b.png)
P1596 [USACO10OCT]Lake Counting S
![[set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)](/img/de/1c75b5e7ed79aca47462de365428a7.jpg)
[set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)

Data analysis exercises

the installer has encountered an unexpected error installing this package

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

Pulitzer Prize in the field of information graphics - malofiej Award

Storage of data
随机推荐
详解sizeof、strlen、指针和数组等组合题
I want to do large screen data visualization application feature analysis
图像处理8-CNN图像分类
Sequence of map implementation classes
Luaframwrok handles resource updates
Open the influence list of "National Meteorological Short Videos (Kwai, Tiktok) in November" in an interactive way“
php-fpm软件的安装+openresty高速缓存搭建
YOLO系列 --- xml2txt脚本
Chocolate installation
Delete the last character of the string in golang
Why can void * be a general pointer
Scite change background color
Redis data structure
Advanced OSG collision detection
Mutual call between Lua and C #
oracle中的 (+)是什么意思
matlab神经网络所有传递函数(激活函数)公式详解
Mall management system of database application technology course design
Transmit pictures with Base64 encoding
Unity2019_ Natural ambient light_ Sky box