Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在列表C+中查找第一个和第二个元素+; 这是我在C++中实现列表的程序。我键入元素直到0。程序正确地显示了第一个元素,但第二个元素错误。我可能会在第二种情况下出错 if (p -> next == first) { secondElement = first -> data; }_C++_Algorithm_List - Fatal编程技术网

在列表C+中查找第一个和第二个元素+; 这是我在C++中实现列表的程序。我键入元素直到0。程序正确地显示了第一个元素,但第二个元素错误。我可能会在第二种情况下出错 if (p -> next == first) { secondElement = first -> data; }

在列表C+中查找第一个和第二个元素+; 这是我在C++中实现列表的程序。我键入元素直到0。程序正确地显示了第一个元素,但第二个元素错误。我可能会在第二种情况下出错 if (p -> next == first) { secondElement = first -> data; },c++,algorithm,list,C++,Algorithm,List,。你能说它有什么毛病吗。谢谢 #include "stdafx.h" #include "iostream" using namespace std; struct Node { int data; Node *next; }; int firstElement; int secondElement; int main() { Node *first = 0; Node *p; cout << "Enter a list" <&l

。你能说它有什么毛病吗。谢谢

#include "stdafx.h"
#include "iostream"
using namespace std;

struct Node {
    int data;
    Node *next;
};

int firstElement;
int secondElement;

int main()
{
    Node *first = 0;
    Node *p;

    cout << "Enter a list" << endl;
    int i;
    while (true) {

        cin >> i;
        if (i == 0) break;


        p = new Node;
        p -> data = i;


        p -> next = first;

        if (first == 0) {
            first = p;
            firstElement = first -> data;
        }

        if (p -> next == first) {
            secondElement = first -> data;
        }       

        first = p;

    }

    cout << "First element is: " << firstElement << endl;
    cout << "Second element is: " << secondElement << endl;
    cout << "List: ";

    p = first;
    while (p) {
        cout << p -> data << " ";
        p = p -> next;
    }
    cout << endl;

    return 0;
}
#包括“stdafx.h”
#包括“iostream”
使用名称空间std;
结构节点{
int数据;
节点*下一步;
};
int第一元素;
int第二元素;
int main()
{
节点*first=0;
节点*p;
我不能;
如果(i==0)中断;
p=新节点;
p->data=i;
p->next=第一;
如果(第一个==0){
第一个=p;
firstElement=first->data;
}
如果(p->next==第一个){
第二个元素=第一个->数据;
}       
第一个=p;
}
cout您可以这样做(我刚刚编辑了您的while循环):


希望这是您想要实现的…

每次通过循环将元素指针设置为第一个

p -> next = first;
然后,在检查第二个元素时,您要检查指针是否设置为第一个元素,它总是这样

 if (p -> next == first) // This is always true
您必须使用一些不同的检查来查看它是否是列表中的第二个条目,例如:

if (p->next && !p->next->next) // only true second time around
{
    secondElement = p -> data; // also note the change here
}
你应该有

 if (p -> next == 0) {

为什么不直接使用std::list呢?这是我的任务。我必须手动操作。我相信它打印的第一个和第二个元素是相同的。您是否试图获得后进先出的行为?一些一般性意见:不要使用
new
/
delete
,而是使用智能指针,例如
std::unique\u ptr
。我看到你已经在避免删除,但那只意味着你在泄密。不要将
0
用作空指针常量。改用
nullptr
。在实际的程序中,应该确保像
cin>>i
这样的输入操作成功,并且故障得到适当的处理。最好使用
“\n”
而不是
endl
endl
执行通常不需要的额外操作。像
一样包含iostream#包含
,而不是使用引号。
p -> next = first;
......
enter code here
 if (p -> next == first) {  //it's always true here
 if (p -> next == 0) {