[英]doubly linked list insert at middle
Can anyone identify what is happening in my code that is causing the segmentation fault? 谁能识别我的代码中引起分段错误的内容? Please modify/correct the wrong part.
请修改/纠正错误的部分。
void InsertAtMid (Node *head){
int num,count=0,i;
Node *ptr=head;
Node *newnode=NULL;
Node *newnode2=head;
printf("Enter node to be inserted: ");
scanf("%d", &num);
if (head==NULL){
newnode = head;
newnode=(Node *)malloc(sizeof(Node));
newnode->x=num;
newnode->next=NULL;
newnode->prev=NULL;
} else {
ptr=head->next;
while(ptr->x!=(count/2)){
ptr=ptr->next;
}
newnode->next=ptr->next;
newnode->prev=ptr;
ptr->next->prev=newnode;
ptr->next=newnode;
}
}
So, based on my understanding of your code - the following should [mostly] work: 因此,根据我对您的代码的理解-以下应该[大部分]有效:
void InsertAtMid (Node **head){
int num = 0;
int count = 0
int advance = 0;
Node *ptr = *head;
Node *newnode = NULL;
printf("Enter node to be inserted: ");
scanf("%d", &num);
if (*head == NULL) {
*head = (Node *)malloc(sizeof(Node));
ptr = *head;
ptr->x = num;
ptr->next = NULL;
ptr->prev = NULL;
} else {
// *** Count the number of items
ptr = *head;
while (ptr != NULL) {
ptr = ptr->next;
count++;
}
// *** Move to the middle of the list
ptr = *head;
while (advance < (count/2)){
ptr = ptr->next;
advance++;
}
// *** Insert the new value
newnode = (Node *)malloc(sizeof(Node));
newnode->x = num;
newnode->next = ptr->next;
newnode->prev = ptr;
ptr->next->prev = newnode;
ptr->next = newnode;
}
}
The following are the issues I fixed: 以下是我解决的问题:
Hope that helps some. 希望对您有所帮助。 Best of luck!
祝你好运!
int num,count=0,i;
...
ptr=head->next;
while(ptr->x!=(count/2)){
ptr=ptr->next;
count is initialized to 0 and never changed. count初始化为0,并且从未更改。
So unless you enter "0" for x, that while loop is just going to walk off the end of the list, every time. 因此,除非您为x输入“ 0”,否则while循环每次都会走出列表的末尾。
Test to work out under what circumstances your code segfaults. 测试以找出在什么情况下您的代码段错误。
You'll find that it works OK when head == NULL
, but fails if head
is not null. 您会发现,当
head == NULL
时,它可以正常工作,但是如果head
不为null,则会失败。
So you know that your error is somewhere in the else
block. 因此,您知道您的错误在
else
块中的某处。
Step through your running code in a debugger (if you don't know how, it's never too early to learn: whenever you solve a problem with a debugger, you think "why didn't I turn to this sooner?"). 在调试器中逐步执行正在运行的代码(如果您不知道怎么做,那么学习它永远不会太早:每当您使用调试器解决问题时,您都会想到“为什么不早点解决?”。)
Work out what you expect to happen, watch the variables in the debugger, and when the actual values deviate from your expectations, reason about why. 弄清您期望发生的情况,观察调试器中的变量,以及当实际值偏离您的期望时,说明原因。
It's not clear to me what you expect to happen in your code, but what will actually happen for a list with one node, is that: 对我来说尚不清楚您期望在代码中发生什么,但是对于具有一个节点的列表,实际发生的是:
ptr=head->next;
ptr=head->next;
-- so ptr
is now NULL
. ptr
现在为NULL
。 while
condition it will try to dereference ptr->x
, and since ptr
is NULL, it will segfault. while
条件,它将尝试取消对ptr->x
引用,并且由于ptr
为NULL,它将进行段错误。 A quick fix for that would be: 一个快速的解决方法是:
while(ptr != NULL && ptr->x ....) {
But you need to think about whether that's the actual logic you want; 但是您需要考虑这是否是您想要的实际逻辑; and once you get past that, you'll hit other problems (for example,
count
never changes), which can be sorted out with a debugger in the same way. 而一旦您克服了这些麻烦,就会遇到其他问题(例如,
count
永不改变),这些问题可以用调试器以相同的方式加以解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.