我已经编写了一个C++程序,将插入到排序的链表中-给出错误:进程终止,状态为-1073741510 我在这里使用了双指针来访问头节点。
程序中没有其他错误
#include<iostream>
using namespace std;
struct node
{
int data;
struct node* next;
};
void ins(int d,node** head)
{
node* temp=new node;
node* prev=new node;
temp->next=NULL;
temp->data=d;
if(head==NULL)
{
*head=temp;
}
else
{
node* ptr=*head;
while(ptr!=NULL && ptr->data<d)
{ prev=ptr;
ptr=ptr->next;
}
prev->next=temp;
temp->next=ptr;
}
}
void print(node* head)
{
node* ptr=head;int i=0;
while(ptr!=NULL)
{
cout<<i<<" "<<ptr->data<<" ";
i++;
ptr=ptr->next;
}
}
int main()
{
node* head=NULL;
ins(4,&head);
ins(10,&head);
ins(9,&head);
print(head);
return 0;
}`
最佳答案
您在if (head == NULL)
中输入了错误,因为head根本不应该为null,所以它应该是if (*head == NULL)
。但这不应该导致进程终止。
关于c++ - 进程终止,状态为-1073741510,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37904427/