C++ 在c/c中操作链接列表++

C++ 在c/c中操作链接列表++,c++,c,data-structures,C++,C,Data Structures,我是链表的初学者。最初,我创建了一个单节点链接列表,并试图显示其数据,但由于 呜呜=空条件。 然后我尝试在循环中获取多个输入,但现在我得到了未处理异常的错误,下面是我的代码: struct node { int data; node* next; }; //Initializing a NULL pointer for head node *head=NULL; //create a temporary node node *temp; //allocate space

我是链表的初学者。最初,我创建了一个单节点链接列表,并试图显示其数据,但由于 呜呜=空条件。 然后我尝试在循环中获取多个输入,但现在我得到了未处理异常的错误,下面是我的代码:

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

//Initializing a NULL pointer for head
    node *head=NULL;

//create a temporary node 
    node *temp; 

//allocate space for node 
    temp = (node*)malloc(sizeof(node));

//Initilaizing avariable of node type to store data
node info;
for (int i=0;i<3;i++){
cout<<"Enter Data\t";
cin>>info.data;

//Store data(First Field)
temp->data=info.data;

//Store the address of the head pointer(Second Field)
temp->next=head;

//Converting temp into head since we are adding data from front
    temp=head;
}
  //==============Traversing the Link List=====================//
//Declaring a temporary pointer
 node *temp1;

//Assigning the address of head to temp1
temp1=head;

//Loop to traverse the list
cout<<"the data"<<endl;
while(temp1!=NULL)
{
    cout<<"the data is"<<endl;
    cout<<temp1->data<<endl;
    temp1=temp1->next;
}
问题出在这里

temp = (node*)malloc(sizeof(node)); 
//Initilaizing avariable of node type to store data
node info;
for (int i=0;i<3;i++)
{
    cout<<"Enter Data\t";
    cin>>info.data;
    //Store data(First Field)
    temp->data=info.data;
    //Store the address of the head pointer(Second Field)
    temp->next=head;
    //Converting temp into head since we are adding data from front
    temp=head;
}
您正试图构建一个包含三项的列表,因此必须分配三个节点。但是上面的代码只分配一个节点。您需要在循环中移动对malloc的调用,以便它被调用三次。

问题出在这里

temp = (node*)malloc(sizeof(node)); 
//Initilaizing avariable of node type to store data
node info;
for (int i=0;i<3;i++)
{
    cout<<"Enter Data\t";
    cin>>info.data;
    //Store data(First Field)
    temp->data=info.data;
    //Store the address of the head pointer(Second Field)
    temp->next=head;
    //Converting temp into head since we are adding data from front
    temp=head;
}
temp = (node*)malloc(sizeof(node)); 
您正试图构建一个包含三项的列表,因此必须分配三个节点。但是上面的代码只分配一个节点。您需要在循环中移动对malloc的调用,以便它被调用三次

temp = (node*)malloc(sizeof(node)); 
您只分配一个节点,而需要分配三个节点 因此,该语句应该在循环中,以便可以为每个节点分配内存

您只分配一个节点,而需要分配三个节点 因此,该语句应该在循环中,以便可以为每个节点分配内存

node *head=NULL;
node *temp,*ptr;
temp=ptr=NULL
for (int i=0;i<3;i++)
{
     temp = (node*)malloc(sizeof(node));
     cout<<"Enter Data\t";
     cin>>info.data;
     //Store data(First Field)
     temp->data=info.data;
     //Store the address of the head pointer(Second Field)

      //Converting temp into head since we are adding data from front
      if(head== NULL)
      {
        head=temp;
        temp->next=NULL;
        ptr=temp;
       }
      else
      {
          ptr->next=temp;
          temp->next=NULL;
          ptr=temp;
      }
}
现在列表已填充&head指向第一个节点,以便您可以访问它。

按-->

node *head=NULL;
node *temp,*ptr;
temp=ptr=NULL
for (int i=0;i<3;i++)
{
     temp = (node*)malloc(sizeof(node));
     cout<<"Enter Data\t";
     cin>>info.data;
     //Store data(First Field)
     temp->data=info.data;
     //Store the address of the head pointer(Second Field)

      //Converting temp into head since we are adding data from front
      if(head== NULL)
      {
        head=temp;
        temp->next=NULL;
        ptr=temp;
       }
      else
      {
          ptr->next=temp;
          temp->next=NULL;
          ptr=temp;
      }
}

现在列表已满,头指向第一个节点,所以你可以访问它。

如果这是C++不使用Maloc,如果这是C,不要使用cOUT。你把这两种语言的编码搞混了

每次打电话给malloc,你首先要考虑的是我什么时候有空?在输入new之前,你必须做的第一件事是我什么时候删除

第二件要考虑的事情是谁对物品的寿命负责:是主物品拥有整个清单,还是物品相互亏欠?或者是其他东西,即列表本身是一个对象,而不仅仅是项目


在这一点上,考虑两个类:一个节点携带一个值,一个节点承载节点并提供一个方法来购买和解散一个节点,一个节点通过。

如果这是C++不使用Maloc,如果这是C,不要使用cOUT。你把这两种语言的编码搞混了

每次打电话给malloc,你首先要考虑的是我什么时候有空?在输入new之前,你必须做的第一件事是我什么时候删除

第二件要考虑的事情是谁对物品的寿命负责:是主物品拥有整个清单,还是物品相互亏欠?或者是其他东西,即列表本身是一个对象,而不仅仅是项目


此时,请考虑两个类:一个是带有值的节点,一个是承载节点并提供购买和取消节点方法的列表,一个是遍历节点的列表。

@John,谢谢:输入问题已经解决,但是我的代码仍然没有显示数据,据我所知,while循环没有执行,你能帮我一下吗???如果你看一下你的代码,你的head=NULL;。代码中没有任何地方更改head的值。因此,当您进入while循环时,它仍然是空的。温度=水头;应为压头=温度;我想说。@John,谢谢:输入问题已经解决,但是我的代码仍然没有显示数据,据我所知,while循环没有被执行,你能帮我一下吗???如果你看你的代码,你的head=NULL;。代码中没有任何地方更改head的值。因此,当您进入while循环时,它仍然是空的。温度=水头;应为压头=温度;我想问一下,哪里有免费的?为什么C++中的Maloc??为什么C++中的Malc????AKP,你不认为你的代码是从链接列表的后面插入数据吗?@ Aayman KalID是,它从后台插入数据……但是U没有指定节点应该插入哪里!!!你可以随心所欲地修改…>>你能帮我理解从前面插入的内容吗,因为从我的理解来看,在插入数据之后,head位于列表的末尾,对吗?@akp,你不认为你的代码是从链接列表的后面插入数据吗?@AaymanKhalid是的,它是从后面插入数据的…但是你没有指定节点应该插入的位置!!!你可以随意修改…>>你能帮我理解从前面插入的内容吗,因为从我的理解来看,插入数据后,头部在列表的末尾,对吗?