c - C 实现双端队列时出现段错误

标签 c pointers

我正在尝试用 C 实现双端队列。我正在学习 C,所以这个错误可能看起来非常微不足道。这是整个程序

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

typedef struct node{
    int data;
    struct node *prev;
    struct node *next;
}Node;

Node *getNode(int data){
    Node *temp = (Node *)malloc(sizeof(Node));
    temp -> next = temp -> prev = NULL;
    temp -> data = data;
    return temp;
}

void push_right(Node **head, Node **tail, int data){
    Node *newNode = getNode(data);
    Node *temp = (*head);
    if (temp == NULL){
        *head = newNode;
    }   
    else{
        while(temp -> next != NULL){
            temp = temp -> next;
        }
        temp -> next = newNode;
        newNode -> prev = temp;
        *tail = newNode;
    }
}

void push_left(Node **head, Node **tail, int data){
    Node *newNode = getNode(data);
    Node *temp = (*head);
    if (temp == NULL){
        *head = newNode;
    }   
    else{
        newNode -> next = temp;
        newNode -> prev = NULL;
        (*head) = newNode;
    }
}

void remove_right(Node **head, Node **tail, int *val){
    Node *temp = (*tail);
    if (temp == NULL)
    {
        printf("Cannot be removed doesn't point to anything\n");
        return;
    }
    else{
        *val = temp -> data;
        temp = temp -> prev;
        (*tail) = temp;
    }
    free(temp);
}

void remove_left(Node **head, Node **tail, int *val){
    Node *temp = (*head);
    if (temp == NULL)
    {
        printf("Cannot be removed doesn't point to anything\n");
        return;
    }
    else{
        *val = temp -> data;
        temp = temp -> next;
        (*tail) = temp;
    }
    free(temp);
}

void print_all(Node *head){
    Node *temp = head;
    printf("\n");
    while(temp != NULL){
        printf("%d\n",temp->data);
        temp = temp -> next;
    }
}

int main(int argc, char const *argv[])
{
    int *val = NULL;
    Node *head = NULL;
    Node *tail = NULL;
    for (int i = 0; i < 10; ++i){
        push_right(&head, &tail,i);
        push_left(&head, &tail,i);
    }
    remove_left(&head, &tail, val);
    print_all(head);
    return 0;
}

调用 remove_left() 时似乎会出现问题。我花了很多时间来了解问题的根源,但似乎没有任何效果。请帮忙。

这是终端屏幕上的输出。

lib2s-iMac:queue admin$ ./a.out 
Segmentation fault: 11

最佳答案

问题出在这里:

*val = temp -> data;

Val 为 NULL,因此尝试取消引用它会导致段错误。

如果将 val 类型更改为 int,而不是指向 int 的指针。然后像这样调用remove_left:

int main(int argc, char const *argv[])
{   int val = 0;
    Node *head = NULL;
    Node *tail = NULL;
    for (int i = 0; i < 10; ++i){
        push_right(&head, &tail,i);
        push_left(&head, &tail,i);
    }

    remove_left(&head, &tail, &val);
    print_all(head);
    return 0;
}

这应该有效。

关于c - C 实现双端队列时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51319660/

相关文章:

c - 随机数 : either 0 or 1

c - 警告 : return makes pointer from integer without a cast using malloc sizeof struct in C

c - 通过将 char 指针设置为 NULL 来模拟 calloc 的失败

ios - 从指针接收图像

对将字符传递给函数参数时何时使用 int 类型或 char 类型感到困惑?

C 字符串表现得很奇怪

c - 生成特定于语言环境的短日期

c++ - 函数的复杂参数

c++ - C++中整数和指针之间的转换

c - 在 Arduino UNO 上通过 UART0 接收字符串的问题