今天写了双向链表..........写的头好晕..........看来链表还是要多加练习
这个双向链表完成了增删改查,并且最后销毁链表
环境VScode
#include <iostream>
#include <cstring>
using namespace std;
// 结点类
class Node
{
public:
string ip; // 客户端ip
string name; // 客户端名字
Node *next; // 指向下一个结点
Node *prev; // 指向上一个结点
// int len;
Node()
{
// 当前对象不用加前缀
next = nullptr;
prev = nullptr;
}
void display()
{
cout << "Ip = " << ip << " "
<< "Name = " << name << endl;
}
};
class MYList
{
public:
// 头结点
int len;
Node *head;
MYList();
~MYList();
// 增加一个结点
void addMYList(string, string);
// 删除一个结点
void deleteMYList(string);
// 修改一个结点
void modifyMYList(string, string);
// 遍历链表
void traverse();
// 销毁链表
// void destroyList(head);
};
MYList::MYList()
{
// 新建一个头结点
head = new Node();
// Node = new Node;堆空间都一样
// Node n;栈空间正确写法 ;Node n ();栈空间这样写错误,意思不一样
len = 0;
}
MYList::~MYList()
{
Node *p = head->next;
while (p)
{
Node *temp = p;
p = p->next;
delete temp;
}
head->next = nullptr;
delete head;
cout<<"List is successfully destroyed!"<<endl;
}
// 增加一个结点
void MYList::addMYList(string ip, string name)
{
// 创建数据结点
Node *pNew = new Node();
if (pNew == nullptr)
return;
pNew->ip = ip;
pNew->name = name;
// 头插
// 数据结点插在头结点后面(数据结点从无到有)
if (head->next == nullptr)
{
pNew->next = head->next;
pNew->prev = head;
head->next = pNew;
// 记录结点个数
len++;
}
// 从少到多
else
{
pNew->next = head->next;
pNew->prev = head;
head->next->prev = pNew;
head->next = pNew;
len++;
}
}
// 根据客户端ip删除
void MYList::deleteMYList(string ip)
{
// p指向第一个数据结点
Node *p = head->next;
// Node* q = head->next;
// 遍历
while (p)
{
if (p->ip == ip)
{
// 只有一个数据结点
if (p->next == nullptr)
{
p->prev->next = NULL;
p->prev = NULL;
}
// 多个
else
{
p->prev->next = p->next;
p->next->prev = p->prev;
p->next = nullptr;
p->prev = nullptr;
}
delete p;
len--;
// ip唯一
break;
// ip不唯一,需要伴随指针q
// p = q->next
}
else
p = p->next;
// q = q->next;
}
}
// 根据ip修改名字
// 修改节点
void MYList::modifyMYList(string ip, string name)
{
// p指向第一个数据结点
Node *p = head->next;
// 遍历链表
while (p)
{
if (p->ip == ip)
{
p->name = name; // 修改节点名称
break; // 找到对应IP后立即结束循环
}
p = p->next;
}
}
// 遍历打印函数
void MYList::traverse()
{
Node *p = head->next;
if (p == nullptr)
return;
// 遍历
while (p)
{
// 结点类打印方法
p->display();
p = p->next;
}
}
int main()
{
MYList h;
h.addMYList("2020", "新能源汽车");
h.addMYList("2050", "移民火星");
h.addMYList("2040", "脑机接口");
h.addMYList("0000", "马斯克");
h.traverse();
cout << "Len = " << h.len << endl;
cout << "-------------------------------------" << endl;
h.deleteMYList("0000");
h.traverse();
cout << "Len = " << h.len << endl;
cout << "-------------------------------------" << endl;
h.deleteMYList("2020");
h.traverse();
cout << "Len = " << h.len << endl;
cout << "-------------------------------------" << endl;
h.modifyMYList("2050", "飞向太阳");
h.traverse();
cout << "Len = " << h.len << endl;
cout << "-------------------------------------" << endl;
h.deleteMYList("2050");
//h.deleteMYList("2040");
h.traverse();
cout << "Len = " << h.len << endl;
cout << "-------------------------------------" << endl;
return 0;
}
运行结果:
PS E:\VScode_code> & 'c:\Users\hp\.vscode\extensions\ms-vscode.cpptools-1.19.9-win32-x64\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-4vymqctd.lfw' '--stdout=Microsoft-MIEngine-Out-sobjeroq.ve0' '--stderr=Microsoft-MIEngine-Error-5quf05eq.2vm' '--pid=Microsoft-MIEngine-Pid-ey0ko5sd.5sb' '--dbgExe=C:\Program Files (x86)\mingw64\bin\gdb.exe' '--interpreter=mi'
Ip = 0000 Name = 马斯克
Ip = 2040 Name = 脑机接口
Ip = 2050 Name = 移民火星
Ip = 2020 Name = 新能源汽车
Len = 4
-------------------------------------
Ip = 2040 Name = 脑机接口
Ip = 2050 Name = 移民火星
Ip = 2020 Name = 新能源汽车
Len = 3
-------------------------------------
Ip = 2040 Name = 脑机接口
Ip = 2050 Name = 移民火星
Len = 2
-------------------------------------
Ip = 2040 Name = 脑机接口
Ip = 2050 Name = 飞向太阳
Len = 2
-------------------------------------
Ip = 2040 Name = 脑机接口
Len = 1
-------------------------------------
List is successfully destroyed!
标签:head,Ip,Name,ip,C++,next,链表,双向,prev
From: https://blog.csdn.net/yangguanghhh/article/details/137168100