#include <iostream>
#include <list>
using namespace std;
struct Node
{
int data;
Node* next;
Node* pre;
};
Node* initList()
{
Node* node = (Node*)malloc(sizeof(Node));
node->data = 0;
node->next = nullptr;
node->pre = nullptr;
return node;
}
void headInsert(Node* list, int data)
{
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = list->next;
node->pre = list;
list->next = node;
if(node->next)
node->next->pre = node;
list->data++;
}
void tailInsert(Node* list, int data)
{
Node* head = list;
list->data++;
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = nullptr;
while (list->next!=nullptr)
{
list = list->next;
}
list->next = node;
node->pre = list;
}
void remove(Node* list, int data)
{
Node* head = list;
Node* cur = list->next;
Node* temp = nullptr;
while (cur!=nullptr)
{
if (cur->data == data)
{
cur->pre->next = cur->next;
cur->next->pre = cur->pre;
temp = cur;
cur = cur->next;
free(temp);
head->data--;
}
else {
cur = cur->next;
}
}
}
void printList(Node* node)
{
cout << "size:" << node->data << endl;
Node* head = node;
while (node->next!=nullptr)
{
node = node->next;
cout << node->data << endl;
}
cout << "null" << endl;
}
int main()
{
Node* list = initList();
headInsert(list, 1);
headInsert(list, 2);
headInsert(list, 3);
headInsert(list, 4);
headInsert(list, 5);
headInsert(list, 3);
tailInsert(list, 6);
tailInsert(list, 6);
tailInsert(list, 7);
tailInsert(list, 8);
tailInsert(list, 9);
tailInsert(list, 10);
printList(list);
remove(list, 3);
remove(list, 8);
printList(list);
}
线性表-双链表
最新推荐文章于 2024-08-05 17:54:35 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)