带假节点的双链表

doubly linked list with dummy node

本文关键字:链表 节点      更新时间:2023-10-16

我在这里花了很多时间,但没有在这个双重链表的实现上取得进展。我试图至少修复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函数"