0

所以我的大部分代码都写下来了,问题是当我尝试将一个新节点添加到我的列表中时,如我的主目录所示,它似乎不起作用。我会添加它,但是当我尝试打印它时,什么也没有出现。

如果您出于某种奇怪的原因需要它,我需要为实验室做的事情就在那里,但它不应该是必要的。[注意:我的很多 gui 代码不起作用,因为我只是从代码的其他部分复制和粘贴]。

说明:编写一个面向对象的 C++ 程序来创建一个双向链接。每个节点都应该能够存储一个人的名字和姓氏,以及他们各自的年龄。用 5 个节点初始化列表。该程序应包含一个成员函数,该函数允许用户打印出所有年龄小于用户输入值的节点(在任一方向)。它还应该允许用户搜索特定人的姓名。它应该能够输出列表的平均年龄。最后,它应该允许删除名称。该程序应允许用户从菜单中进行选择。

#include <iostream>
#include <string>
using namespace std;

 typedef string Elem;               // list element type
  class DNode {                 // doubly linked list node
  private:
    Elem elem;                  // node element value
    DNode* prev;                // previous node in list
    DNode* next;                // next node in list
    friend class DLinkedList;           // allow DLinkedList access
  };

class DLinkedList {             // doubly linked list
  public:
    DLinkedList();              // constructor
    ~DLinkedList();             // destructor
    bool empty() const;             // is list empty?
    const Elem& front() const;          // get front element
    const Elem& back() const;           // get back element
    void addFront(const Elem& name);        // add to front of list
    void addBack(const Elem& name);     // add to back of list
    void removeFront();             // remove from front
    void removeBack();              // remove from back
    void displayViaAge();
    void displayViaName();
  private:                  // local type definitions
    DNode* header;              // list sentinels
    DNode* trailer;
  protected:                    // local utilities
    void add(DNode* v, const Elem& name);       // insert new node before v
    void remove(DNode* v);          // remove node v
  };

  DLinkedList::DLinkedList() {          // constructor
    header = new DNode;             // create sentinels
    trailer = new DNode;
    header->next = trailer;         // have them point to each other
    trailer->prev = header;
  }

    DLinkedList::~DLinkedList() {           // destructor
    while (!empty()) removeFront();     // remove all but sentinels
    delete header;              // remove the sentinels
    delete trailer;
  }
                                // insert new node before v
  void DLinkedList::add(DNode* v, const Elem& name) {
    DNode* u = new DNode;  u->elem = name;      // create a new node for e
    u->next = v;                // link u in between v
    u->prev = v->prev;              // ...and v->prev
    v->prev->next = v->prev = u;
  }

  void DLinkedList::addFront(const Elem& name)  // add to front of list
    { add(header->next, name); }

  void DLinkedList::addBack(const Elem& name)   // add to back of list
    { add(trailer, name); }

   void DLinkedList::remove(DNode* v) {     // remove node v
    DNode* u = v->prev;             // predecessor
    DNode* w = v->next;             // successor
    u->next = w;                // unlink v from list
    w->prev = u;
    delete v;
  }

  void DLinkedList::removeFront()       // remove from font
    { remove(header->next); }

  void DLinkedList::removeBack()        // remove from back
    { remove(trailer->prev); }


     bool DLinkedList::empty() const        // is list empty?
    { return (header->next == trailer); }

  const Elem& DLinkedList::front() const    // get front element
    { return header->next->elem; }

  const Elem& DLinkedList::back() const     // get back element
    { return trailer->prev->elem; }

  void DLinkedList::displayViaAge() {                     //Displays person via age

      DNode*temp = header;

       while(temp!=trailer)
       {

           //if(howold = age)
              cout << temp->elem <<endl;
              temp = temp -> next;
       }

       cout << temp->elem<<endl;
  }

int main(){
    char input = 'z';
    string entry;
    int age;
    DLinkedList person;

    person.addFront("Takkun Bradly");
    person.add("Devindra Ardnived");
    person.add("SeboY Wang");
    person.add("DoubleX Slash");
    person.add("Uncle Jelly");

    cout << "What would you like to do?" << endl;
    cout << "Enter 'A' to: Add a new person" << endl;
    cout << "Enter 'B' to: Remove a person" << endl;
    cout << "Enter 'C' to: Search for people via age" << endl;
    cout << "Enter 'D' to: Search for people via name" << endl;
    cout << "Enter 'E' to: Average all the total ages" << endl;
    cout << "Enter 'F' to: Quit" << endl;

while(input != 'f') {

    cin >> input;
    cout << endl;

        while ((input != 'a')&&(input != 'b')&&(input != 'c')&&(input != 'd')&&(input != 'e')&&(input != 'f')) {

            cout << "Please enter a valid selection" << endl;           
            cin >> input;
        }

        if ((input == 'a')){
            cout << "Please enter their name: ";
            cin.ignore();
            getline(cin, entry);

            cout << "Please enter their age: ";
            cin >> age;
            person.addFront(entry);

        }

        if ((input == 'b')){
            cout << "Who would you like to remove: ";
            cin.ignore();
            getline(cin, entry);
            person.removeFront();
        }

        if ((input == 'c')){
            cout << "What is the age of the person you are looking for?: ";
            person.displayViaAge();
        }

        if ((input == 'd')){
            cout << "What is the name of the person you are looking for?: ";
            cin.ignore();
            getline(cin, entry);
            person.addFront(entry);
        }

        if ((input == 'e')){
            return 0;
        }

        cout << endl;
    }
}
4

1 回答 1

1

我认为您的错误在您的 add 方法中的这一行中:

 v->prev->next = v->prev = u;

此行中的此分配将从右到左执行。所以v->prev设置为u,然后v->prev->next设置v->prev为 现在指向u。所以现在你u->next指向u,这不是你想要的。

我想你想要这样的东西:

// insert new node before node v
void DLinkedList::add(DNode* v, const Elem& name) {
    DNode* u = new DNode;   u->elem = name;      // create a new node and set name
    u->next = v;                 // make v the successor of u
    u->prev = v->prev;           // set u's predecessor to v's current predecessor
    u->prev->next = u;           // make u the successor of v's predecessor
    v->prev = u;                 // finally make u the predecessor of v
}
于 2012-12-23T07:30:56.753 回答