C++ 双链表反向打印?
我必须将名为“reversePrint”的成员函数添加到这个双链接列表中。并反向打印姓名。 例如,输入: 杰森 赖安 迪伦 路易 输出应为: 路易 迪伦 赖安 杰森 我的想法是先向下移动到列表的末尾,即最后一个节点。然后使用“Previous”指针一次返回到前一个节点,同时打印出名称。因此,我将有一个反向打印。 然而,我的指针有点问题,我想,我就是不知道如何修复它。一些帮助真的很感激 **更新:此代码最初是单独链接的。我试图将其转换为双链接并添加“reversePrint”函数。但是我认为问题在于,我没有正确修改“Add”方法,因此“prev”指针断开了连接。那么,有没有关于如何修改“添加”的帮助,以便我可以将其添加到一个完全双链接的列表中 这是它现在提供的输出: 上一个0x0 地址0x100200000数据a 下一个0x100300000 上一个0x0 地址0x100300000数据b 下一个0x100400000 上一个0x0 地址0x100400000数据c 下一个0x0 这是我们需要的输出: 上一个0x0 地址0x100200000数据a 下一个0x100300000 上一个0x100200000 地址0x100300000数据b 下一个0x100400000 prev 0x100300000 地址0x100400000数据c 下一个0x0 以下是我的代码:C++ 双链表反向打印?,c++,linked-list,doubly-linked-list,C++,Linked List,Doubly Linked List,我必须将名为“reversePrint”的成员函数添加到这个双链接列表中。并反向打印姓名。 例如,输入: 杰森 赖安 迪伦 路易 输出应为: 路易 迪伦 赖安 杰森 我的想法是先向下移动到列表的末尾,即最后一个节点。然后使用“Previous”指针一次返回到前一个节点,同时打印出名称。因此,我将有一个反向打印。 然而,我的指针有点问题,我想,我就是不知道如何修复它。一些帮助真的很感激 **更新:此代码最初是单独链接的。我试图将其转换为双链接并添加“reversePrint”函数。但是我认为问题在
#include <iostream>
#include <string>
using namespace std;
// define a node for storage and linking
class node{
public:
string name;
node *next;
node *prev; // to be implemented by students
};
class linkedList{
public:
linkedList():top(NULL){}
bool empty(){return top == NULL;}
node *getTop(){return top;}
void setTop(node *n){top = n;}
void add(string);
int menu();
void remove(string);
~linkedList();
void reversePrint(); // to be implemented by students
friend ostream& operator << (ostream&, const linkedList&); // default output is in-order print.
private:
node *top;
node *end; // to be used for reverse print and implemented by students
};
int main(void){
linkedList l;
//cout << l.empty() << endl;
int option = 0;
string s;
bool go = true;
while(go){
option = l.menu();
switch(option){
case 1: cout << "enter a name: ";cin >> s; l.add(s); break;
case 2: cout << "enter name to be deleted: "; cin >> s; l.remove(s);break;
case 3: cout << l; break;
case 4: l.reversePrint(); break;
case 5: cout << "exiting" << endl; go = false; break;
}
}
// l goes out of scope and calls ~linkedList()
return(NULL);
}
// can not call this method "delete" - "delete" is a reserved keyword.
void linkedList::remove(string s){
bool found = false;
node *curr = getTop(), *prev=NULL;
while(curr != NULL){
// match found, delete
if(curr->name == s){
found = true;
// found at top
if(prev == NULL){
node *temp = getTop(); // delete the current node(which is the head), and set the "new head" to as the next node
setTop(curr->next);
delete(temp);
// found in list - not top
}else{
prev->next = curr->next; //Skip the current deleted node, connect previous node directly to the next node
delete(curr);
} }
// not found, advance pointers
if(!found){
prev = curr;
curr = curr->next; }
// found, exit loop
else curr = NULL; }
if(found)cout << "Deleted " << s << endl;
else cout << s << " Not Found "<< endl; }
void linkedList::add(string s){
node *n = new node();
n->name = s;
n->next = NULL;
// take care of empty list case
if(empty()){ top = n;
// take care of node belongs at beginning case
} else if(getTop()->name > s)
{
n->next = getTop();
setTop(n);
// take care of inorder and end insert
}else{
// insert in order case
node *curr = getTop(), *prev = curr;
while(curr != NULL){
if(curr->name > s)break;
prev = curr;
curr = curr->next;
}
if(curr != NULL){ // search found insert point
n->next = curr;
prev->next = n; }
// take care of end of list insertion
else if(curr == NULL){// search did not find insert point
prev->next = n; }
} }
void linkedList::reversePrint(){
node *curr = getTop(), *prev=NULL;
// jump to the last node
while(curr->next != NULL){
prev = curr;
curr = curr->next;
//curr = curr->next;
cout << "!!!!hahaha" << curr->name <<" Prev:" <<curr->prev << " " << prev->name <<endl ;//testing purpose
}
//print the name then jump back to the previous node, stops at the first node which curr->prev = NULL
while(prev != 0 ){
cout << "NULL is not 0!";
prev->prev = curr->next;
//cout << curr->name;
curr = prev;
}
}
/*ostream& operator << (ostream& os, const linkedList& ll){
//linkedList x = ll; // put this in and the code blows up - why?
node *n = ll.top;
if(n == NULL)cout << "List is empty." << endl;
else
while(n != NULL){
os << n->name << endl;
n = n->next;
} return os;
}*/
ostream& operator << (ostream& os, const linkedList& ll){
//linkedList x = ll; // put this in and the code blows up - why?
node *n = ll.top;
if(n == NULL)cout << "List is empty." << endl;
else
while(n != NULL){
os << " prev " << n->prev << endl;
os << " address " << n << " data " << n->name << endl;
os << " next " << n->next << endl;
n->prev = n;
n = n->next;
} return os;
}
// return memory to heap
linkedList::~linkedList(){
cout << "~linkedList called." << endl;
node *curr = getTop(), *del;
while(curr != NULL){
del = curr;
curr = curr->next;
delete(del);
}
}
int linkedList::menu(){
int choice = 0;
while(choice < 1 || choice > 5){
cout << "\nEnter your choice" << endl;
cout << " 1. Add a name." << endl;
cout << " 2. Delete a name." << endl;
cout << " 3. Show list." << endl;
cout << " 4. Show reverse list. " << endl; // to be implemented by students
cout << " 5. Exit. " << endl;
cin >> choice;
}
return choice;
}
#包括
#包括
使用名称空间std;
//定义用于存储和链接的节点
类节点{
公众:
字符串名;
节点*下一步;
node*prev;//由学生实现
};
类链接列表{
公众:
linkedList():top(NULL){}
bool empty(){return top==NULL;}
node*getTop(){return top;}
void setTop(node*n){top=n;}
无效添加(字符串);
int菜单();
脱空(串);
~linkedList();
void reversePrint();//由学生实现
friend ostream&operator next;//跳过当前删除的节点,将上一个节点直接连接到下一个节点
删除(货币);
} }
//未找到,前进指针
如果(!找到){
上一次=当前;
curr=curr->next;}
//找到,退出循环
else curr=NULL;}
如果(发现)无法命名>s)中断;
上一次=当前;
当前=当前->下一步;
}
如果(curr!=NULL){//搜索找到插入点
n->next=当前;
上一个->下一个=n;}
//注意列表末尾的插入
如果(curr==NULL){//search未找到插入点,则为else
上一个->下一个=n;}
} }
void linkedList::reversePrint(){
节点*curr=getTop(),*prev=NULL;
//跳转到最后一个节点
while(当前->下一步!=NULL){
上一次=当前;
当前=当前->下一步;
//当前=当前->下一步;
cout这看起来有问题:
prev->prev = curr->next;
您正在设置上一个节点的上一个节点。您可能只想设置上一个节点,对吗
您可能希望执行以下操作:
while(curr != NULL)
{
cout << curr->name;
curr = curr->prev
}
while(curr!=NULL)
{
姓名;
当前=当前->上一个
}
由于此时您已确定curr
是列表中的最后一个元素,现在可以通过将curr
分配给它的上一个元素来“递减”curr
。由于第一个元素没有上一个节点,它的prev
将为NULL
。这样,循环将在curr时停止ODE已被分配给第一个元素的前一个元素,它再次是<代码> null 。< p>您的函数Add是无效的。例如,考虑列表空的情况。下面的代码片段将对应于此情况。
void linkedList::add(string s){
node *n = new node();
n->name = s;
n->next = NULL;
// take care of empty list case
if(empty()){ top = n;
// take care of node belongs at beginning case
} else if(getTop()->name > s)
如您所见,未设置列表的数据成员结尾
因此,在编写函数reversePrint的实现之前,您应该确保函数add正确工作。这怎么可能是您的代码?它就像“正向打印”。只需从“尾部”(vs“头部”)开始,并通过“previousNode”(vs“nextNode”)导航即可。如果这不起作用,那么在创建列表时,上一个节点指针可能没有正确设置。我假设您给我们的是您的程序的输出?哈哈,伙计们,很抱歉我试图编辑它…第一篇帖子…所以…感谢您的及时评论…不要否决,他似乎是新来的,可能是在尝试格式化问题,他说无法正确发布代码。但如果您将其放入并查看输出,您可以看到“prev”指针有点断开连接。因此,在到达最后一个节点后,我无法返回。我想我必须修改“Linkedlist::Add”一点来将它们链接起来?这是正确的。您的add函数应该确保上次添加的节点的next
和prev
指针以及新节点指向正确的节点。我不明白,您的代码似乎缺少了某些部分。@user3103980,我认为您的任务是编写缺少的代码。看起来你所做的只是删除了我的一些代码。好的,非常有用。