我是初学者链接列表。最初,我创建了一个节点链接列表,并尝试显示其数据,但未显示条件的显示内容。 然后我试图采取一些在一个循环的投入,但现在我得到未处理的异常的错误,这里是我的代码:操作c/C++中的链接列表
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;
}
“免费”在哪里?为什么malloc在C++? – 2013-04-29 06:12:28