0

我正在实现一个类似于队列的双向链表。因此,当我将节点添加到列表(例如 5 个节点)并清空列表并尝试将新节点添加到列表时,它会给我一个分段错误(核心转储)。我不知道它为什么这样做。你可以解释吗?

链表.h

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

typedef struct node{
    int d;
    struct node *prev;
    struct node *next;
}node;

typedef struct linkedlist{
    int size;
    struct node *first;
    struct node *last;
}linkedlist;

链表.c

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

#include "linkedlist.h"

linkedlist* createList(){
    linkedlist* myList = (linkedlist*)calloc(1,sizeof(linkedlist));
    myList->first = NULL;
    myList->last = NULL;
    myList->size =0;

    return myList;
}

static node* createNode(int n){
    node *myNode = (node*)calloc(1,sizeof(node));

    myNode->d = n;

    myNode->prev = NULL;
    myNode->next = NULL;

    return myNode;
}

void insertNode(linkedlist* l, int num){
    node *temp, *newNode;

    newNode = createNode(num);

    if (l->size == 0){
        newNode->next = NULL;
        newNode->prev = NULL;

        l->first = newNode;
        l->last = newNode;

        l->size++;
    }
    else{
        temp = l->first;
        while (temp->next != NULL){
            temp = temp->next;
        }   

        newNode->prev = temp;
        temp->next = newNode;
        newNode->next = NULL;

        l->size++;
    }
}

int deleteNode(linkedlist* l){
    node *temp = calloc(1,sizeof(node));

    if (l->first ==NULL){
        return -1;
    }
    else if (l->size ==1){

        free(l->first);
        l->first= NULL;
        l->last = NULL;

        l->size--;
    }
    else if (l->size > 1){
        temp = l->first;
        l->first = temp->next;          

        free(temp);
    }
}

void display(linkedlist *l){
    node *temp = calloc(1,sizeof(node));
    temp = l->first;

    if (temp == NULL){
        printf("The list is empty\n");
    }
    while (temp != NULL) {
        printf("-> %d ", temp->d);
        temp = temp->next;
    }
}

int main(){

    linkedlist *myList = createList();

    int choice, temp=0, numb;
    printf("(1) Insert \n (2) Delete \n");

    for (temp; temp<10; temp++){
    printf("Choice :");
    scanf ("%d", &choice);
    switch(choice) {
        case 1: {
            printf("Enter a Number: ");
            scanf("%d", &numb);
            insertNode(myList, numb);
            display(myList);
            break;
        }
        case 2:{
             deleteNode(myList);
            display(myList);
            break;

        }
    }

    }       
}
4

2 回答 2

0

在以下情况下,您不会减少列表的大小l->size > 1

int deleteNode(linkedlist* l){

    node *temp = calloc(1,sizeof(node));

    if (l->first ==NULL){
        return -1;
    }
    else if (l->size ==1){
        free(l->first);
        l->first= NULL;
        l->last = NULL;
        l->size--;               \\ <--- Here is OK
    }
    else if (l->size > 1){
        temp = l->first;
        l->first = temp->next;          
        free(temp);
                                \\ <--- Here should have another l->size--
    }
}

您也可以将递减指令移出 if 语句。

于 2013-05-16T17:28:57.683 回答
-1

我没有完全通过你的程序,但从我在这里看到的

 linkedlist *myList = createList();

您在开始时初始化列表,然后是删除节点

int deleteNode(linkedlist* l){

    node *temp = calloc(1,sizeof(node));

    if (l->first ==NULL){
        return -1;
    }
    else if (l->size ==1){
        free(l->first);
        l->first= NULL;
        l->last = NULL;
        l->size--;
    }
    else if (l->size > 1){
        temp = l->first;
        l->first = temp->next;          
        free(temp);
    }
}

释放整个列表

那么当你插入节点时

void insertNode(linkedlist* l, int num){
   node *temp, *newNode;

   newNode = createNode(num);

    if (l->size == 0){
       newNode->next = NULL;
       newNode->prev = NULL;
       l->first = newNode;
       l->last = newNode;
       l->size++;
    }
    else{
       temp = l->first;
       while (temp->next != NULL){
            temp = temp->next;
        }   
        newNode->prev = temp;
        temp->next = newNode;
        newNode->next = NULL;

        l->size++;
    }
}

l->size 可能正在创建段错误,您需要修复删除逻辑或插入逻辑

于 2013-05-16T15:12:33.297 回答