Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.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/1/list/4.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++_List_Hyperlink_Insert - Fatal编程技术网

C++ 使用循环在链接列表的前面插入节点

C++ 使用循环在链接列表的前面插入节点,c++,list,hyperlink,insert,C++,List,Hyperlink,Insert,我试过这个代码。这个代码的输出是11098765432。 但是我想要输出109875654321。 什么是我的错,我不明白。我看到这个问题之前问过,但为了我的概念澄清,我又问了一次。有没有更好的解决方案这个代码 #include <iostream> using namespace std; struct NODE { int data; NODE *next; }; int main() {

我试过这个代码。这个代码的输出是11098765432。 但是我想要输出109875654321。 什么是我的错,我不明白。我看到这个问题之前问过,但为了我的概念澄清,我又问了一次。有没有更好的解决方案这个代码

    #include <iostream>
    using namespace std;
    struct NODE
    {
        int data;
        NODE *next;
    };
    int main()
    {
        int i,j=0;
        NODE *start=NULL,*ptr,*temp;
        for (i=1; i<=10; i++)
        {
            ptr = new NODE;
            ptr->data=i;
            if(start==NULL)
            {
                ptr->next=NULL;
                start=ptr;
            }
            else
            {
                ptr->next=start->next;
                start->next=ptr;
            }
        }
        temp=start;
        for ( temp = start; temp; temp = temp->next )
            cout << temp->data << ' ';
        return 0;
    }
#包括
使用名称空间std;
结构节点
{
int数据;
节点*下一步;
};
int main()
{
int i,j=0;
节点*start=NULL,*ptr,*temp;
对于(i=1;idata=i;
if(start==NULL)
{
ptr->next=NULL;
启动=ptr;
}
其他的
{
ptr->next=开始->下一步;
开始->下一步=ptr;
}
}
温度=启动;
用于(临时=开始;临时;临时=临时->下一步)

cout data循环有不必要的逻辑。它可以是:

   for (i=1; i<=10; i++)
   {
      ptr = new NODE;
      ptr->data=i;

      // It doesn't matter what start is.
      // The new NODE is added to the front.
      ptr->next=start;

      // Make the new NODE the front(start).
      start=ptr;
   }
for(i=1;idata=i;
//开始是什么并不重要。
//新节点将添加到前面。
ptr->next=开始;
//使新节点位于前面(开始)。
启动=ptr;
}

看到它在。

@πάντα下工作了吗ῥεῖ 它不工作,只是调试一下,看看为什么会这样。你想在末尾插入一个节点吗?然后更改title@FirstStep谢谢。我理解我的错误。试着将链接添加到其他答案