ListNode prev = new ListNode(-1);
时间: 2024-01-14 15:02:52 浏览: 44
这也是创建一个新的链表节点(prev)的语句,但是与上一个语句不同的是,这里将节点的值设为了-1。这个值通常用于表示一个空节点或者哨兵节点,在某些链表操作中会非常有用。哨兵节点可以帮助我们简化链表操作的实现,比如在插入或删除节点时,我们可以通过引入哨兵节点来避免处理头节点和尾节点的特殊情况,从而简化代码的实现。
相关问题
#include <iostream> #include "listnode.h" using namespace std; /*struct listNode { listNode * prev, * next; int val; listNode(): val(0), prev(nullptr), next(nullptr){} listNode(int v, listNode *p, listNode *n): val(v), prev(p), next(n) { if (prev != nullptr) prev->next = this; if (next != nullptr) next->prev = this; } };*/ class OrderedList { protected: listNode * root; public: OrderedList(){root=new listNode();} ~OrderedList(){ listNode* cur=root; if(cur->prev!=nullptr)cur=cur->prev; if(cur!=nullptr){listNode*tmp=cur;cur=cur->next;delete tmp;} delete cur;} virtual void insert(int val)=0; void printList() const { listNode* cur=root; while(cur->prev!=nullptr)cur=cur->prev; while(cur!=nullptr){cout<<cur->val<<' ';cur=cur->next;} cout<<endl; } }; class AscendOrderedList:public OrderedList { public: AscendOrderedList(){root=new listNode();} ~AscendOrderedList(){ listNode* cur=root; if(cur->prev!=nullptr)cur=cur->prev; if(cur!=nullptr){listNode*tmp=cur;cur=cur->next;delete tmp;} delete cur;} void insert(int v) { if(root->next==nullptr&&root->prev==nullptr){root->val=v;return;} listNode* pre = root; while(pre->prev!=nullptr)pre=pre->prev; if(v<=pre->val){listNode*ins=new listNode();ins->val=v;ins->next=pre;return;} while (pre->next != nullptr && v > pre->val) { pre = pre->next; } listNode*a=pre->next; listNode*ins=new listNode(v, pre, pre->next); ins->prev=pre;ins->next=a; } }; class DescendOrderedList:public OrderedList { public: DescendOrderedList(){root=new listNode();} ~DescendOrderedList(){ listNode* cur=root; if(cur->prev!=nullptr)cur=cur->prev; if(cur!=nullptr){listNode*tmp=cur;cur=cur->next;delete tmp;} delete cur;} virtual void insert(int v){ if(root->next==nullptr&&root->prev==nullptr){root->val=v;return;} listNode*pre=root; while(pre->prev!=nullptr)pre=pre->prev; if(v>=pre->val){listNode*ins=new listNode();ins->val=v;ins->next=pre;return;} while(v<pre->val&&pre!=nullptr){pre=pre->next;} listNode*a=pre->next; listNode*ins=new listNode(v, pre, pre->next); ins->prev=pre;ins->next=a; } };输出错误
在代码中,构造函数和析构函数中的删除操作存在问题。在析构函数中,应该先删除链表中的节点,再删除root节点,否则会造成内存泄漏。同时,在构造函数中,应该将root节点的prev和next指针设为nullptr,否则会导致插入操作时出现问题。另外,在DescendOrderedList的insert函数中,应该先判断pre是否为nullptr,否则在pre为nullptr时会出现访问pre->val的错误。
#include <iostream> using namespace std; // 双向链表节点 struct ListNode { int val; ListNode* prev; ListNode* next; ListNode(int x) : val(x), prev(NULL), next(NULL) {} }; // 双向链表类 class LinkedList { public: LinkedList() { head = new ListNode(-1); tail = new ListNode(-1); head->next = tail; tail->prev = head; } // 左插入 void insertLeft(int val) { ListNode* node = new ListNode(val); node->prev = head; node->next = head->next; head->next->prev = node; head->next = node; } // 右插入 void insertRight(int val) { ListNode* node = new ListNode(val); node->prev = tail->prev; node->next = tail; tail->prev->next = node; tail->prev = node; } // 删除节点 void remove(ListNode* node) { node->prev->next = node->next; node->next->prev = node->prev; delete node; } // 显示链表 void display() { ListNode* cur = head->next; while (cur != tail) { cout << cur->val << " "; cur = cur->next; } cout << endl; } private: ListNode* head; // 链表头节点 ListNode* tail; // 链表尾节点 }; int main() { LinkedList list; // 左插入示例 list.insertLeft(1); list.insertLeft(2); list.insertLeft(3); list.display(); // 输出:3 2 1 // 右插入示例 list.insertRight(4); list.insertRight(5); list.insertRight(6); list.display(); // 输出:3 2 1 4 5 6 // 删除示例 ListNode* node = list.head->next; list.remove(node); list.display(); // 输出:2 1 4 5 6 return 0; }严重性 代码 说明 项目 文件 行 禁止显示状态 错误 C2248 “LinkedList::head”: 无法访问 private 成员(在“LinkedList”类中声明) C++ experiment C:\Users\zhoubo\source\repos\C++ experiment\C++ experiment\Calculator.cpp 839
这个错误是因为你试图在类外访问私有成员head,而head是LinkedList类的私有成员,只有类内成员才能访问。如果你需要在类外访问head,可以提供一个公有的访问函数。例如,在LinkedList类中添加如下代码:
ListNode* getHead() {
return head;
}
然后在main函数中,你可以这样使用:
ListNode* node = list.getHead()->next;
这样就可以访问head了。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)