#include<iostream>
using namespace std;
typedef struct Node {
int data;
Node* next;
Node* pre;
}Node;
Node* initlist(int data)
{
Node* node = new Node;
node->data = data;
node->next = node;
node->pre = node;
return node;
}
void headinsert(Node*& head, int data)
{
Node* node = new Node;
node->data = data;
node->next = head;
node->pre = head->pre;
head->pre->next = node;
head->pre = node;
head = node;
}
void tailinsert(Node* head, int data)
{
Node* node = new Node;
node->data = data;
node->pre = head->pre;
node->next = head;
head->pre->next = node;
head->pre = node;
}
void printlist(Node* head)
{
Node* node = head->next;
cout << head->data << "->";
while (node != head)
{
cout << node->data << "->";
node = node->next;
}
cout << "NULL" << endl;
}
void nodedelete(Node*& head, int data)
{
Node* node = head;
if (node->data == data)
{
node->pre->next = node->next;
node->next->pre = node->pre;
head = node->next;
}
node = node->next;
while (node != head)
{
if (node->data == data)
{
node->pre->next = node->next;
node->next->pre = node->pre;
}
node = node->next;
}
}
int main()
{
Node* a = initlist(5);
headinsert(a, 4);
headinsert(a, 3);
headinsert(a, 2);
headinsert(a, 1);
headinsert(a, 0);
tailinsert(a, 7);
tailinsert(a, 8);
tailinsert(a, 9);
tailinsert(a, 10);
tailinsert(a, 11);
printlist(a);
nodedelete(a, 10);
nodedelete(a, 0);
nodedelete(a, 11);
nodedelete(a, 4);
printlist(a);
return 0;
}
标签:node,pre,head,双循环,next,链表,打卡,data,Node
From: https://www.cnblogs.com/wlxdaydayup/p/17418975.html