/*Name: "Inserisci in ordine"
* Author: fra trement
* task ->
* creates a list of integers,
* in ascending order.
* Then print the list and
* free up the memory
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct nodo{
int dato;
struct nodo*next;
}nodo_t;
typedef nodo_t*Ptr_nodo;
Ptr_nodo Insinordine(Ptr_nodo l, int num); /* Insert the value in order inside the list */
Ptr_nodo destroy(Ptr_nodo list); /* Destroy the list to free HEAP */
void stampa(Ptr_nodo l); /* Print the list */
int main(){
Ptr_nodo head=NULL;;
int val;
printf("Lista di interi positivi; inserisci 0 per terminaren"); /* eng-> List of positive integers; enter 0 to finish */
do{
printf("> ");
scanf("%d", &val);
if(val>0){
head = Insinordine(head, val);
// printf("Inserimenton");
} else if(val==0){
printf("**FINE**n"); /* eng-> **END** */
} else {
printf("Valore non valido!n"); /* eng-> Value not valid */
}
} while (val != 0);
if(head!=NULL){
stampa(head);
// printf("PRINTEDn");
head = destroy(head);
// printf("DISTROYEDn");
} else {
printf("list: -|n");
}
return 0;
}
Ptr_nodo Insinordine (Ptr_nodo l, int num){
Ptr_nodo tmp=NULL, curr=l, prec=NULL;
int count;
tmp = (Ptr_nodo)malloc(sizeof(nodo_t));
if(tmp){
tmp->dato = num;
tmp->next = NULL;
if(l==NULL){
l=tmp;
} else {
while(curr->dato < num && curr->next != NULL){
prec = curr;
curr = curr->next;
}
tmp->next = curr;
prec->next = tmp;
}
} else printf("Errore memorian"); /* eng-> Memory error */
}
Ptr_nodo destroy (Ptr_nodo list){
Ptr_nodo tmp;
while(list!=NULL){
tmp = list;
list = list->next;
free(tmp);
}
return NULL;
}
void stampa (Ptr_nodo l){
printf("list: ");
while(l!=NULL){
printf("%d -> ", l->dato);
l = l->next;
}
printf("n");
}
嗨,有人能告诉我哪里错了吗?程序似乎可以工作,但只要我输入第二个值,就会出现错误"分段故障(堆芯倾倒(">出现。我想知道这是一个与代码相关的错误,还是堆内存问题。在此处输入图像描述我希望一切都清楚。
不确定您在这里要做什么,您的代码也有逻辑问题。您似乎正在尝试创建一个排序的链表,但逻辑似乎不正确。无论哪种方式,获得segfault的原因都是因为将prev
初始化为NULL
,并且如果while
条件在第一次迭代中失败,则尝试访问NULL
的元素,从而导致segfault。
使用像gdb
这样的调试工具来查看发生了什么。使用gdb进行简单的检查可以指示line 67
的问题,然后您可以尝试从那里找出问题。
对于初学者,也可以将prev
初始化为头部。
prev = l;
有几个问题。
- 当您将元素添加到列表中时,不会返回更新的
head
(在您的情况下,l
来自Insinordine
( - 您的
Insinordine
函数在根据其比较和插入查找下一个元素时出错
我刚刚用head
重命名了您的l
。
浏览下面的代码,如果有任何澄清,请返回,并在内联中添加注释。
Ptr_nodo Insinordine (Ptr_nodo head, int num){
Ptr_nodo tmp=NULL, curr=NULL, prec=NULL;
int count;
tmp = (Ptr_nodo)malloc(sizeof(nodo_t));
if(tmp){
tmp->dato = num;
tmp->next = NULL;
if(head == NULL){
head = tmp;
return head;
} else {
// first check whether head->data is less than new element (num)
if( head->dato < num )
{
tmp -> next = head;
head = tmp;
return head;
}
// assign prec pointer the head
prec = head;
// curr is head -> next
curr = head -> next;
// check until you reach NULL or curr->dato > num
// there will be two outcomes of while
while( curr && curr->dato > num ){
prec = curr;
curr = curr->next;
}
//1. if curr is NULL then we have reached the end, so add the tmp at end
if(!curr) {
prec->next = tmp;
return head;
}
//2. curr is not NULL, but curr->dato < num
// so, we store curr node(prec->next) in tmp->next and tmp in curr node(prec->next)
else {
tmp->next = prec->next;
prec->next = tmp;
return head;
}
}
} else printf("Errore memorian"); /* eng-> Memory error */
}