带有虚节点的双向链表

时间:2015-07-07 11:25:28

标签: c++

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

1 个答案:

答案 0 :(得分:1)

  

我试图至少修复insertLast func

你的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;
}

我所做的更改

  • 删除了模糊的类位置:公共节点
  • int Size(){返回大小; }
  • Node * toPosition(int rank){...} - 最后移动了return语句

N.B。:我没有检查代码的其他部分。我只是让你的代码正确编译并按预期工作,因为&#34;你试图至少修复insertLast func&#34;