c - 当我无法进入函数时这意味着什么? [C]

标签 c list

我正在尝试合并(交替)两个列表并创建第三个列表,而不添加新节点(问题 here )。代码执行时没有错误或警告,但似乎没有进入函数 AlternateLists(),因为没有打印“函数内部”字符串。你认为这意味着什么?您能给我快速查看和/或解释我的代码/问题吗?

谢谢..

#include <stdio.h>
#include <stdlib.h>

struct nodo {
    int inf;
    struct nodo *succ;
    struct nodo *prec;
};
typedef struct nodo nodo;

nodo *RicercaPosizione( nodo *a, int i );
nodo *Inserisci(nodo *a, int i, int x);
nodo* AlternateLists(nodo* list1, nodo* list2);
void *MostraLista(nodo *a);

int main(){

    nodo *lista1=NULL;
    nodo *lista2=NULL;
    nodo *lista3=NULL;
    int numeri[]={1,2,3,4};
    int numeri2[]={5,6,7,8};


    int i=0;
    while(i!=4){
        printf("%d",i);
        lista1=Inserisci(lista1,i, numeri[i]);
        i++;
    }


    printf("lista1 \n\n");
    MostraLista(lista1);
    lista2=lista1;
    printf("lista2 \n\n");
    MostraLista(lista2);
    printf("\n\nlista3 \n\n");
    lista3=AlternateLists(lista1,lista2);
    MostraLista(lista3);
}

nodo* AlternateLists(nodo* l1, nodo* l2){
printf("\n\n Inside the fuction");
 // Check if arrays are != NULL
  if(!l1 && !l2) return NULL;
  if(!l1) return l2;
  if(!l2) return l1;
  //----------------------
  nodo* c1 = l1;
  nodo* c2 = l2;
  nodo* next;
  nodo* next2;
  while(c1){
   next = c1->succ;
   if(c2){ // check to make sure there are still nodes in array2
     c1->succ = c2;
     next2 = c2->succ;
     c2->succ = next;
     c2 = next2;
   }else{
    c1->succ = next;
   }
   c1 = next;
 }
 while(c2){ //if there are more nodes in list 2 then there are in list 1
   c1->succ = c2;
   c2 = c2->succ;
   c1 = c2;
 }
 return l1;
 }

//Insert etc
nodo *Inserisci(nodo *a, int i, int x){
    nodo *q, *p;
    if ( i == 0 ){
        q = malloc(sizeof(nodo));
        q->succ = a; q->prec = NULL;
        q->inf = x;
        if (a != NULL)
            a->prec = q;
        a = q;
    } else {
        p = RicercaPosizione( a, i-1);
        if (p != NULL){
            q = malloc(sizeof(nodo));
            q->inf = x;
            q->succ = p->succ;
            q->prec = p;
            if ( p->succ != NULL)
                p->succ->prec = q;
            p->succ = q;
        }
    }
    return a;
}

nodo *RicercaPosizione( nodo *a, int i ){
    nodo *p = a;
    int j = 0;
    while ( j < i && p != NULL){
        p = p->succ;
        j = j+1;
    }
    return p;
}

void *MostraLista(nodo *a){
    nodo *p = a;

    while ( p != NULL ){
        printf("%d, ", p->inf);
        p = p->succ;
    }
    printf("\n");
}

最佳答案

使用调试器:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400810 in AlternateLists (l1=0x601010, l2=0x601010) at demo.c:67
67     c1->succ = c2;

问题:

while(c1){
    ...
}
/* At this point c1 is NULL */
while(c2){
   c1->succ = c2; /* Boom */
   c2 = c2->succ; 
   c1 = c2;
}

关于c - 当我无法进入函数时这意味着什么? [C],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28343374/

相关文章:

c - 如何在函数内调用函数

c - 如何在 Linux (ubuntu 16.04) 上运行 OpenCL 程序?

c++ - c/c++编译时 "compatibility"检查

algorithm - 如何使用共享元素对列表进行聚类

c - 在编译下面的代码时出现段错误,你能帮我找出错误吗

c - C语言中如何合并两个整数

Python获取列表中元素的排名奇怪的行为

python计算元组列表的一个字段中的三次重复次数

r - 将数据框拆分为列表中的多个数据框,每列分别

java - Android:使用迭代器从列表中删除元素