在双链表的末尾插入节点时,为什么我的代码出错?

时间:2016-03-26 22:42:34

标签: c

我正在学习C和我需要创建 - 删除双列表的头部和尾部,当我想删除所有节点后我想创建一个尾部时遇到麻烦。 这是我添加和删除的功能:

struct nodo{
int code;
char name[30];
char last[30];
struct nodo *next, *prev;
};// fin struct nodo
static struct nodo *head = NULL, *tail = NULL;
struct nodo *inserthead(struct nodo *new_aux){
    if(head == NULL && tail == NULL){
        new_aux->next =NULL;
        new_aux->prev =NULL;
        head = new_aux;
        tail = new_aux;
     }// fin if
    else{
        new_aux->prev = NULL;
        new_aux->next = head;
    head = new_aux;
    }// fin else
    return head;
}
struct nodo *insert_tail(struct nodo * new_aux){
    if(head == NULL && tail == NULL){
     new_aux->next =NULL;
        new_aux->prev =NULL;
        head = new_aux;
        tail = new_aux;
}// fin if
    else{
        struct nodo *tmp =head;
        while(tmp->next!=NULL){
            tmp = tmp->next;
        }// fin while
        new_aux->prev = tmp;
        tmp->next = new_aux;
        tail = new_aux;
    }// fin else
return tail;
}// fin insertar cola
struct nodo *removehead(){
    if(head!=NULL){
        struct nodo *temp = head;
        head = head->next;
        free(temp);
    }// fin if
    else{
        return NULL;
    }// fin else
    return head;
}// fin elimina head
struct nodo *remove_tail(){
    if(head == NULL){
        return NULL;
    }//fin if
    else if(head->next == NULL){
        struct nodo *tmp = head;
        free(tmp);
        head = NULL;
    }// fin else if
    else{
        struct nodo *aux_one = head;
        struct nodo *aux_two = head->next;
        struct nodo *aux_three = head;
        while(aux_two->next!=NULL){
            aux_two = aux_two->next;
            aux_one = aux_one->next;
        }//fin while
        aux_one->next = NULL;
        while(aux_three->next!=aux_one){
          aux_three = aux_three->next;
       }
     aux_one->prev = aux_three; 
    tail = aux_one;
    free(aux_two);    
    }//fin else
}//fin remove cola

这是主要的:

int main(){
    struct nodo *nuevo;
    int option;
    int continuar = 1;
    printf("\n\t BIENVENIDO\n");
    while(continuar==1){
        printf("\n->1.-Ingresar nodo al principio\n->2.-Ingresar nodo al     final\n->3.-Eliminar primer nodo\n->4.-Eliminar ultimo nodo\n->5.-Mostrar     nodos\n->");
       scanf("%d",&option);
        switch(option){
           case 1:
               nuevo = (struct nodo*)malloc(1*sizeof(struct nodo));
                printf("\n->Ingresa un codigo: ");
                scanf("%d", &(*nuevo).code);
                printf("\n->Ingresa un nombre: ");
                scanf("%s",(*nuevo).name);
                printf("\n->Ingresa un apellido: ");
                scanf("%s",(*nuevo).last);
                inserthead(nuevo);
        break;
        case 2:
            nuevo = (struct nodo*)malloc(1*sizeof(struct nodo));
            printf("\n->Ingresa un codigo: ");
            scanf("%d", &(*nuevo).code);
            printf("\n->Ingresa un nombre: ");
            scanf("%s",(*nuevo).name);
            printf("\n->Ingresa un apellido: ");
            scanf("%s",(*nuevo).last);
            insert_tail(nuevo);
        break;
        case 3:
            removehead();
        break;
        case 4:
            remove_tail();
        break;
        case 5:
            printlist();
        break;
    };//fin switch
    printf("\n->Deseas continuar? 1.-Si 2.-No ");
    scanf("%d", &continuar);
};//fin while
return 0;
}// fin main :D

当我尝试在删除所有节点后添加尾部或者在删除所有节点后尝试添加头部时,问题是“分段错误”。当我第一次创建尾部或头部时,我没有这个问题,对于没有节点的删除也是如此。

谢谢你的时间:D

0 个答案:

没有答案