gpt4 book ai didi

c++ - 带有指针的循环不起作用

转载 作者:行者123 更新时间:2023-11-28 02:36:16 24 4
gpt4 key购买 nike

我有一个问题,我正在尝试创建一个列表,该列表删除持有最高值(value)的数字,或者如果该值在列表中最高,则删除具有相同值的所有数字。感谢您提供任何类型的提示。

// n,n1,head,next - are pointers
int j = 0; //this number helps to put pointer forward by one place
while(n!=0){//should go through every digit of the list
if(head == 0){
cout << "list is empty" << endl;
}
else{
n = head;
n1=0; // n1 and n are pointers
while(n!=0){
if(n->sk == maxx){//searches for maximum digit in the list
break;
}
else{
n1=n;
n=n->next;
}
}
if(head == n){
head = head->next;
}
else{
n1->next = n->next;
}
delete n; // deletes the pointer holding the highest value
}
n = head; //problem is here or somewhere below
j++;
for(int i=0; i<j;i++){ // this loop should make the pointer point to the first
n = n->next; // number, then the second and so on until the end of list
} // and all the numbers inside the list with the value that
} // equals "maxx" should be deleted

最佳答案

您应该取消对指针的引用。现在,您正指向他们的地址。看看这是否有助于解决您的问题。

关于c++ - 带有指针的循环不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27325693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com