这是一个程序,它从用户获取学生姓名和注册号,并将其存储在一个双重链接列表中。我在最后插入节点。但是在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);
}
使用尾部指针有问题。我已经改进了下面的代码,并添加了注释来解释我所做的事情。如果你对我所做的还有任何问题,请在评论中提问,我很乐意澄清。
#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;
}