#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
时间: 2024-01-08 09:03:03 浏览: 29
这个错误是因为你试图在类外访问私有成员head,而head是LinkedList类的私有成员,只有类内成员才能访问。如果你需要在类外访问head,可以提供一个公有的访问函数。例如,在LinkedList类中添加如下代码:
ListNode* getHead() {
return head;
}
然后在main函数中,你可以这样使用:
ListNode* node = list.getHead()->next;
这样就可以访问head了。
相关问题
#include <iostream> using namespace std; struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; ListNode* insert(ListNode* head, int x) { ListNode* prev = NULL; ListNode* curr = head; while (curr != NULL && curr->val <= x) { prev = curr; curr = curr->next; } ListNode* node = new ListNode(x); if (prev == NULL) { node->next = head; return node; } else { node->next = prev->next; prev->next = node; return head; } } void printList(ListNode* head) { while (head != NULL) { cout << head->val << " "; head = head->next; } cout << endl; } int main() { int n, x; cin >> n; ListNode* head = NULL; for (int i = 0; i < n; i++) { int val; cin >> val; head = insert(head, val); } cin >> x; head = insert(head, x); printList(head); return 0; } 用c语言实现
以下是使用C语言实现的代码:
```c
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode* next;
};
struct ListNode* insert(struct ListNode* head, int x) {
struct ListNode* prev = NULL;
struct ListNode* curr = head;
while (curr != NULL && curr->val <= x) {
prev = curr;
curr = curr->next;
}
struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
node->val = x;
if (prev == NULL) {
node->next = head;
return node;
} else {
node->next = prev->next;
prev->next = node;
return head;
}
}
void printList(struct ListNode* head) {
while (head != NULL) {
printf("%d ", head->val);
head = head->next;
}
printf("\n");
}
int main() {
int n, x;
scanf("%d", &n);
struct ListNode* head = NULL;
for (int i = 0; i < n; i++) {
int val;
scanf("%d", &val);
head = insert(head, val);
}
scanf("%d", &x);
head = insert(head, x);
printList(head);
return 0;
}
```
这个C语言版本的代码与C++版本的代码非常相似,只是语法和一些细节上有所不同。
#include <iostream> 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; } };根据 listnode.h 中对链表 listNode 的定义,在 orderedList.hpp 中实现以下三个类: 抽象基类 OrderedList,表示一个整数链表,有以下方法: 构造函数:无参数,创建一个空的链表; void insert(int val) 向链表中插入一个元素,在 OrderedList 中该方法应当为纯虚函数; void printList() const 依次在屏幕上输出链表中的元素,元素之间用空格分隔,输出完整个链表后换行; 析构函数:释放链表所占用的空间。 此外,OrderedList还有一个保护成员:listNode * root; 具体类 AscendOrderedList,表示一个升序的链表,继承自 OrderedList,需要重载 void insert(int val) 函数,在插入的时候实现升序。 具体类 DescendOrderedList,表示一个降序的链表,继承自 OrderedList,需要重载 void insert(int val) 函数,在插入的时候实现降序。
orderedList.hpp 实现如下:
```c++
#include "listnode.h"
class OrderedList {
public:
OrderedList() : root(nullptr) {}
virtual ~OrderedList() {
listNode *p = root;
while (p != nullptr) {
listNode *tmp = p->next;
delete p;
p = tmp;
}
}
virtual void insert(int val) = 0;
void printList() const {
listNode *p = root;
while (p != nullptr) {
std::cout << p->val << " ";
p = p->next;
}
std::cout << std::endl;
}
protected:
listNode *root;
};
class AscendOrderedList : public OrderedList {
public:
void insert(int val) override {
listNode *p = root;
listNode *prev = nullptr;
while (p != nullptr && p->val <= val) {
prev = p;
p = p->next;
}
listNode *node = new listNode(val, prev, p);
if (prev == nullptr) {
root = node;
}
}
};
class DescendOrderedList : public OrderedList {
public:
void insert(int val) override {
listNode *p = root;
listNode *prev = nullptr;
while (p != nullptr && p->val >= val) {
prev = p;
p = p->next;
}
listNode *node = new listNode(val, prev, p);
if (prev == nullptr) {
root = node;
}
}
};
```
在 AscendOrderedList 和 DescendOrderedList 类中,重载了 insert 函数,实现插入的时候按照升序和降序排列。在 insert 函数中,首先遍历链表,找到插入位置,然后创建新的节点并插入到链表中。如果链表为空,则新节点作为根节点。需要注意的是,在插入节点时,需要更新节点的前驱和后继节点。