带假节点的双链表
doubly linked list with dummy node
我在这里花了很多时间,但没有在这个双重链表的实现上取得进展。我试图至少修复insertLast函数,我根本没有运气。部分原因是我缺乏在c++中如何处理指针的经验。在这一点上,你的建议是非常有价值的。当执行时,我将得到0作为列表的大小。
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *prev, *next;
Node(int x,Node *p,Node *q) {
data = x; prev = p; next = q;
}
};
public class Position :public Node {
//};
class List {
public:
Node *head;
int size;
public:
List() {
head = new Node(NULL,NULL,NULL);
head->prev = head;
head->next = head;
size = 0;
}
int Size(){
return List().size;
}
void insertFirst(int x)
{
insertBefore(first(), x);
}
void insertLast(int x)
{
insertAfter(last(), x);
}
void insertAfter(Node *p, int x) {
Node *q = new Node(x,p,p->next);
p->next->prev = q;
p->next = q;
size += 1;
}
void insertBefore(Node *p, int x) {
Node *q = new Node(x,p->prev,p);
p->prev->next = q;
p->prev = q;
size += 1;
}
void insertAtRank(int rank, int x)
{
insertBefore(toPosition(rank), x);
}
void remove(Node *p) {
p->prev->next = p->next;
p->next->prev = p->prev;
p->prev = p->next = NULL;
size -= 1;
}
void removeAtRank(int rank)
{
remove(toPosition(rank));
}
int element(Node *p)
{
return p->data;
}
int elementAtRank(int rank)
{
return toPosition(rank)->data;
}
int toRank(Node *p) {
int r = 0;
for (Node *q = first(); q != p; q = q->next) r += 1;
return r;
}
Node *toPosition(int rank) {
if (rank > size){
throw "exception";
}
Node *q = first();
for (int r = 0; r != rank; r += 1){
q = q->next;
return q;
}
}
Node *first() { return head->next; }
Node *last() { return head->prev; }
Node *after(Node *p) { return p->next; }
Node *before(Node *p) { return p->prev; }
bool isEmpty() { return head->next == head; }
Node *removeLast(){
if (last() == NULL){
throw "exception";
}
Node *p = last();
p->prev->next = p->next;
p->next->prev = p->prev;
p->prev = p->next = NULL;
return p;
}
};
void main(){
int x;
List l;
int a[]{163, 179, 103, 91, 404,
531, 745, 405, 686,
858, 898, 926, 266, 867, 865,
91, 103, 163, 179, 2,
66, 404, 405, 531, 686, 745,
858, 865, 867, 898, 926};
int size = sizeof(a) / sizeof(a[0]);
for (int i = 0; i < size; i++){
l.insertLast(i);
}
for (int j = 0; j < l.Size(); j++){
cout << l.elementAtRank(j) << endl;
}
cout << l.Size();
cin >> x;
}
我正在尝试至少修复insertLast函数
你的inserLast()看起来很好。试试下面的代码:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *prev, *next;
Node(int x,Node *p,Node *q) {
data = x; prev = p; next = q;
}
};
class List {
public:
Node *head;
int size;
public:
List() {
head = new Node(0,NULL,NULL);
head->prev = head;
head->next = head;
size = 0;
}
int Size(){
return size;
}
void insertFirst(int x)
{
insertBefore(first(), x);
}
void insertLast(int x)
{
insertAfter(last(), x);
}
void insertAfter(Node *p, int x) {
Node *q = new Node(x,p,p->next);
p->next->prev = q;
p->next = q;
size += 1;
}
void insertBefore(Node *p, int x) {
Node *q = new Node(x,p->prev,p);
p->prev->next = q;
p->prev = q;
size += 1;
}
void insertAtRank(int rank, int x)
{
insertBefore(toPosition(rank), x);
}
void remove(Node *p) {
p->prev->next = p->next;
p->next->prev = p->prev;
p->prev = p->next = NULL;
size -= 1;
}
void removeAtRank(int rank)
{
remove(toPosition(rank));
}
int element(Node *p)
{
return p->data;
}
int elementAtRank(int rank)
{
return toPosition(rank)->data;
}
int toRank(Node *p) {
int r = 0;
for (Node *q = first(); q != p; q = q->next) r += 1;
return r;
}
Node *toPosition(int rank) {
if (rank > size){
throw "exception";
}
Node *q = first();
for (int r = 0; r != rank; r += 1){
q = q->next;
}
return q;
}
Node *first() { return head->next; }
Node *last() { return head->prev; }
Node *after(Node *p) { return p->next; }
Node *before(Node *p) { return p->prev; }
bool isEmpty() { return head->next == head; }
Node *removeLast(){
if (last() == NULL){
throw "exception";
}
Node *p = last();
p->prev->next = p->next;
p->next->prev = p->prev;
p->prev = p->next = NULL;
return p;
}
};
void main(){
int x;
List l;
int a[]{163, 179, 103, 91, 404,
531, 745, 405, 686,
858, 898, 926, 266, 867, 865,
91, 103, 163, 179, 2,
66, 404, 405, 531, 686, 745,
858, 865, 867, 898, 926};
int size = sizeof(a) / sizeof(a[0]);
for (int i = 0; i < size; i++){
l.insertLast(i);
}
for (int j = 0; j < l.Size(); j++){
cout << l.elementAtRank(j) << endl;
}
cout << l.Size();
cin >> x;
}
:
- 删除模糊的类位置:public Node
- int Size(){返回Size;}
- 节点*位置(int rank){…} -移动了返回语句的末尾
注意::我没有检查你代码的其他部分。我只是使您的代码正确编译并按预期工作,因为"您试图至少修复insertLast函数"
相关文章:
- 如何按数字顺序插入链表节点?
- 插入一个基本的单向链表节点似乎破坏了我的 c++ 代码?
- 在 c++ 中,在链表节点上使用括号有什么意义?
- C++链表节点是指它自己
- 不显示链表节点
- 如何取消引用作为指针的链表节点数据
- 删除链表节点,C++函数不起作用
- 基本C++链表节点和指针探针
- 链表节点上的分段错误
- 链表节点合并器函数
- 如何将链表节点中的值相加,C++
- 释放链表节点时出现分段错误
- 链表节点类中的链接数
- 链表 c++ 节点构造函数
- 当链表节点是用"new"构造时,如何删除列表?
- 双向链表节点的下一个是私有的
- 分段 C++中链表节点分配的错误
- 双向链表节点交换问题
- 在单向链表中添加双向链表节点
- 关于链表节点的说明