-1

我正在尝试实现一个像队列一样的双向链表(我希望它像队列一样)。

[编辑]

当我将节点添加到列表中(例如 5 个节点)并清空列表(删除所有元素)并尝试再次将另一个节点添加到列表中时,它给了我一个分段错误(核心转储)错误。

linkedlist.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;




 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

4 回答 4

2

在您的删除节点功能中:

else if (l->size > 1){
        temp = l->first;
        l->first = NULL;       //this is problem
        l->first->next = NULL;
        temp->next = l->first;

        l->first->prev = NULL;

您正在分配l->first = NULL然后在下一条语句中访问它l->first->next = NULL;,这将失败并给您分段错误。

另外,什么时候l->size == 1你也应该l->first = NULL在释放它之后设置。

于 2013-05-16T07:26:18.767 回答
1

在 deleteNode 中,如果大小为 1,则首先指向已释放的内存

它应该是:

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

temp 也是一个指针,您不需要使用 malloc 为其分配内存

于 2013-05-16T07:24:20.463 回答
1

访问“NULL”位置时会出现问题。让我们修改一下代码:

temp = l->first;
l->first = NULL;        // here, you set l->first = 0
l->first->next = NULL;  // here, you access to 0->next: this is not correct.
temp->next = l->first;

将其更改为:

temp = l->first;
l->first = temp->next;
delete temp;
于 2013-05-16T07:26:51.313 回答
1
int deleteNode(linkedlist* l){
    node *temp= (node*)malloc(sizeof(node)) ;

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

    else 
{
temp= l->first;
l->first= temp->next;
l->first->previous= temp;
l->size--;
free(l->first->previous);
}
}
于 2013-05-16T07:37:27.160 回答