c++ - 双向链表。逻辑错误

标签 c++ linked-list doubly-linked-list

这是一个从用户那里获取学生姓名和注册号并将其存储在双向链表中的程序。我在最后插入节点。但是while循环中存在逻辑错误。它需要名称和注册。数但随后停止。请帮助

 class dlist{

        struct node{
      string name;
      string regno;
      node *next;
      node *prev;
   };
    public:
      node *head,*tail;
      void insert(string,string);
      void search(string);
};

    void dlist::insert(string nn, string r)
{
       node *ptr,*newNode;
       tail=head;
       newNode= new node;
       newNode->name=nn;
       newNode->regno=r;
       newNode->next=NULL;
       newNode->prev=NULL;

  if(!head)
  {
      head=newNode;
       tail=head;
  }

 else 
  {
          ptr = head;
       while(ptr)
          ptr=ptr->next;
     newNode->next=tail;
     newNode->prev=ptr;
     ptr->next-newNode;
    }
  }

  void dlist::search(string n){

         node *ptr;
         ptr=head;
   while(ptr->next!=NULL)
   {
        if(ptr->name==n)
             cout<<"Name found..." <<ptr->name<<endl;
         ptr=ptr->next;
   }
  }


  int _tmain(int argc, _TCHAR* argv[])
   {
         dlist dl;
         string nn;
         string n;
         string r;
       for(int i=0;i<5;i++)
           {
              cout<<"Enter student's name: ";
              cin>>nn;
              cout<<"Enter student's Registration Number: ";
              cin>>r;
              dl.insert(nn,r);
           }
         cout<<"Enter a name to search: ";
         cin>>n;
         dl.search(n);
     }

最佳答案

您在使用尾指针时遇到问题。我在下面改进了您的代码并添加了注释来解释我所做的事情。如果您对我所做的还有任何疑问,请在评论中提问,我很乐意澄清。

#include <tchar.h>
#include <string>
#include <iostream>

class dlist {

    struct node {
        string name;
        string regno;
        node* next;
        node* prev;
    };
public:
    node* head, * tail;

    void insert(string, string);

    void search(string);
    /**
     * You're using classes, so go ahead and provide a definition for the
     * constructor you're using. This one will initialize head and tail to
     * be null
     */
    dlist() : head(NULL), tail(NULL) {}

    /**
     * It's important to delete any memory you allocate so that you don't create a memory
     * leak.
     */
    virtual ~dlist() {
        while (head) {
            node* deleteMe = head;
            head = head->next;
            delete deleteMe;
        }
    }
};

void dlist::insert(string nn, string r) {
    // there is no good reason to set tail=head at this time.
    // and you don't need a ptr, that's why we have tail.
    node* newNode = new node;
    newNode->name = nn;
    newNode->regno = r;
    // no need to set newNode->prev now, as that will be taken care of later
    newNode->next = NULL;

    // if we don't have any nodes yet, we need to set head = newNode
    // and make head point to tail, which is also head.
    if (!head) {
        head = newNode;
        tail = head;
        head->next = tail;
        tail->prev = head;
        // head points to itself both ways
    } else {
        // we already have some nodes, so go ahead and
        // set newNode to go right after tail, then set tail = newNode
        // this will work even when head == tail
        tail->next = newNode;
        newNode->prev = tail;
        tail = newNode;
    }
}

void dlist::search(string n) {
    // it's nice to assign variables when you declare them, when you can
    node* ptr = head;
    // basically the same as what you had, but we are checking in the case
    // that tail has the node we want, which your code did not do.
    while (ptr) {
        if (ptr->name == n) {
            cout << "Name found... " << ptr->name << endl;
        }
        ptr = ptr->next;
    }
}


int _tmain(int argc, _TCHAR* argv[]) {
    dlist dl;
    string nn;
    string n;
    string r;
    for (int i = 0; i < 5; i++) {
        cout << "Enter student's name: ";
        cin >> nn;
        cout << nn << endl;
        cout << "Enter student's Registration Number: ";
        cin >> r;
        cout << r << endl;
        dl.insert(nn, r);
    }
    cout << "Enter a name to search: ";
    cin >> n;
    dl.search(n);
    return 0;
}

关于c++ - 双向链表。逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40321300/

相关文章:

c++ - C++ 中的 case 表达式返回错误

c++ - 如何引用 C++ 中所谓的函数?

java - 双向链表逻辑

c++ - 静态函数调用错误 "C++ requires a type specifier for all declarations"

.net - 不能在另一个项目的 dll 中使用一个类的 typedef

c++ - 与蒙特卡洛的定积分没有可接受的误差

c++ - 我的循环链表中的 remove 方法是否定义明确?

python - 循环链表代码陷入无限循环

c - 使用 gdb 进行调试时,如何打印整个链接结构列表中的所有特定属性?

c - 无法返回双向链表