双链表。逻辑错误

时间:2016-10-29 16:40:06

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

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

 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);
     }

1 个答案:

答案 0 :(得分:1)

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

#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;
}