I'm trying to learn Insertion techniques in Linked list. During execution, it crashes everytime saying the program has stopped working. It showed no errors whatsoever. I'm new to Stack Overflow. So pardon me if this question was already asked. Here is my code:
我正在尝试学习链接列表中的插入技术。在执行过程中,每次说程序停止工作时都会崩溃。它没有显示任何错误。我是Stack Overflow的新手。如果这个问题已被提出,请原谅我。这是我的代码:
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
void push(struct node** head_ref, int new_data)
{
struct node* new_node= (struct node*)malloc(sizeof(struct node));
new_node->data=new_data;
new_node->next=(*head_ref);
(*head_ref)=new_node;
}
void insertAfter(struct node* prev_node, int new_data)
{
if(prev_node==NULL)
{printf("The previous node cannot be NULL");
return;
}
struct node* new_node=(struct node*)malloc(sizeof(struct node));
new_node->data=new_data;
new_node->next=prev_node->next;
prev_node->next=new_node;
}
void append(struct node** head_ref, int new_data)
{
struct node* new_node= (struct node*)malloc(sizeof(struct node));
struct node *last= *head_ref;
new_node->data=new_data;
new_node->next=NULL;
if(*head_ref==NULL)
{
*head_ref=new_node;
}
else
while(last->next!=NULL)
{
last=last->next; /* Segmentation fault */
}
last->next=new_node;
return;
}
void printlist(struct node *node)
{
while(node!=NULL)
{
printf("%d",node->data);
node=node->next;
}
}
int main()
{
struct node* head=NULL;
append(&head,6);
push(&head,7);
push(&head,11);
append(&head,4);
insertAfter(head->next,12);
printf("\n Created Linked list is:");
printlist(head);
return 0;
}
2 个解决方案
#1
2
You check the case where the head is NULL
, but the else
clause contains only the while
loop. The assignment to last
is carried out in both cases.
检查head为NULL的情况,但else子句仅包含while循环。在两种情况下都执行最后的分配。
You should place braces around the else
clause:
你应该在else子句周围放置大括号:
void append(struct node **head_ref, int new_data)
{
struct node *new_node = (struct node *) malloc(sizeof(struct node));
struct node *last = *head_ref;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref = new_node;
} else {
while (last->next != NULL) {
last = last->next;
}
last->next = new_node;
}
}
Proper indentation will make such errors stand out. In my opinion, it is also a good idea to use braces throughout, perhaps with the exception of very short if
s without else
in the inner loop.
适当的缩进将使这些错误脱颖而出。在我看来,在整个过程中使用大括号也是一个好主意,也许除了在内循环中没有其他的非常短的ifs之外。
#2
0
At least, you are trying to dereference a NULL pointer (in append
).
至少,您尝试取消引用NULL指针(在追加中)。
You probably want if (head_ref==NULL)
instead of if (*head_ref==NULL)
.
你可能想要if(head_ref == NULL)而不是if(* head_ref == NULL)。
#1
2
You check the case where the head is NULL
, but the else
clause contains only the while
loop. The assignment to last
is carried out in both cases.
检查head为NULL的情况,但else子句仅包含while循环。在两种情况下都执行最后的分配。
You should place braces around the else
clause:
你应该在else子句周围放置大括号:
void append(struct node **head_ref, int new_data)
{
struct node *new_node = (struct node *) malloc(sizeof(struct node));
struct node *last = *head_ref;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref = new_node;
} else {
while (last->next != NULL) {
last = last->next;
}
last->next = new_node;
}
}
Proper indentation will make such errors stand out. In my opinion, it is also a good idea to use braces throughout, perhaps with the exception of very short if
s without else
in the inner loop.
适当的缩进将使这些错误脱颖而出。在我看来,在整个过程中使用大括号也是一个好主意,也许除了在内循环中没有其他的非常短的ifs之外。
#2
0
At least, you are trying to dereference a NULL pointer (in append
).
至少,您尝试取消引用NULL指针(在追加中)。
You probably want if (head_ref==NULL)
instead of if (*head_ref==NULL)
.
你可能想要if(head_ref == NULL)而不是if(* head_ref == NULL)。