C++ 预结束不起作用。我的代码适用于从双链接列表正常打印。然而,我想预先准备一下
我的代码适用于从双链接列表正常打印。但是,我想预先输入(或按与输入相反的顺序打印数据)我的输入。因此,我对代码的非工作部分进行了注释。当我运行整个代码时,由于某种原因,循环不会关闭以进行反向顺序打印。它只会继续打印我输入的第一个数字。你能建议我如何删除我分配的所有内存吗C++ 预结束不起作用。我的代码适用于从双链接列表正常打印。然而,我想预先准备一下,c++,C++,我的代码适用于从双链接列表正常打印。但是,我想预先输入(或按与输入相反的顺序打印数据)我的输入。因此,我对代码的非工作部分进行了注释。当我运行整个代码时,由于某种原因,循环不会关闭以进行反向顺序打印。它只会继续打印我输入的第一个数字。你能建议我如何删除我分配的所有内存吗 struct node{ int data; node* next; node* prev; }; struct node* head; void printF(node* tail); //vo
struct node{
int data;
node* next;
node* prev;
};
struct node* head;
void printF(node* tail);
//void printR(node* head);
int main(){
node* tail;
node* n;
int input;
do {
cout<<"Enter a positive number to add in the list or -1 to terminate the list: ";
cin>>input;
cout<<endl;
n= new node;
if (input >=0){
if(head == NULL){
n->data=input;
n->next=NULL;
head=n;
tail = n;
}
n->data = input;
n->prev =NULL;
n->next=head;
head->prev=n;
head = n;
}
else if (input == -1)
{
head->prev=NULL;
cout<<"Program terminated!!"<<endl;
}
else{
cout<<"Your input is invalid!! Enter a positive number to add in the list or -1 to terminate the list"<<endl;
}
}while(input != -1);
// printR(head);
printF(tail);
//delete node;
}
/*
void printR(node* head){
node* temp = head;
while(temp!=NULL){
cout<< temp->data<<", ";
temp= temp->next;
}
cout<<endl;
}
*/
///*
void printF(node* tail){
node* temp = tail;
while(temp!=NULL){
cout<< temp->data<<", ";
temp = temp->prev;
}
cout<<endl;
结构节点{
int数据;
节点*下一步;
节点*prev;
};
结构节点*头部;
void printF(节点*尾部);
//无效打印器(节点*头部);
int main(){
节点*尾部;
节点*n;
int输入;
做{
库廷普特;
coutdata=输入;
n->next=NULL;
水头=n;
尾=n;
}
n->数据=输入;
n->prev=NULL;
n->next=头部;
头部->上一个=n;
水头=n;
}
else if(输入==-1)
{
head->prev=NULL;
请仔细查看添加第一个数据点时代码的功能。此外,您还应学习如何使用调试器。一旦使用调试器检查程序创建的所有对象,并检查所有next
和prev
指针,并准确查看它们的错误,显示的代码中的错误将非常明显。