c - 如何修复 Deque 实现

标签 c

我正在尝试使用动态分配来实现双端队列,但我遇到了一些麻烦,因为结构中变量的值与函数初始化不同,我不知道为什么。 由于某种原因,程序总是在push_front/back部分出现段错误。

// C program for vetor implementation of d 
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 

    typedef int data;
    // A structure to represent a d 
    typedef struct deque 
    { 
        int front, rear, size; 
        unsigned int capacidade; 
        data* vetor; 
    }deque; 

    // function to create a d of given capacidade. 
    // It initializes size of d as 0 
    void initialize(deque *d, unsigned int capacidade){ 
        d = (deque*) malloc(sizeof(deque)); 
        d->capacidade = capacidade; 
        d->front = 0;
        d->size = 0; 
        d->rear = capacidade-1; // This is important, see the enqueue 
        d->vetor = (data*) malloc(d->capacidade * sizeof(data));
    } 

    // deque is full when size becomes equal to the capacidade 
    int full(deque* d){
        if(d->size == d->capacidade)
        return 1;
        else return 0;
    } 

    // deque is empty when size is 0 
    int empty(deque* d){
        return (d->size == 0);
    } 

    // Function to add an item to the d. 
    // It changes rear and size 
    int push_back(deque* d, int item){ 
        if (full(&d))
            return 0; 
        d->rear = (d->rear + 1)%d->capacidade;
        d->vetor[d->rear] = item;
        d->size = d->size + 1;

    }

    int push_front(deque* d, int item){ 
        if (full(&d)) 
            return 0;
        d->front = (d->front - 1+d->capacidade)%d->capacidade;
        d->vetor[d->front] = item;
        d->size = d->size + 1;

    }  

    // Function to remove an item from d. 
    // It changes front and size 
    int pop_front(deque* d){ 
        if (empty(&d)) 
            return 0;

        int item = d->vetor[d->front];
        d->front = (d->front + 1)%d->capacidade; 
        d->size = d->size - 1; 
        return item; 
    } 

    int pop_back(deque* d){ 
        if (empty(&d)) 
            return 0; 
        int item = d->vetor[d->rear]; 
        d->rear = (d->rear - 1+d->capacidade)%d->capacidade; 
        d->size = d->size - 1; 
        return item; 
    }

    // Function to get front of d 
    int front(deque* d) 
    { 
        if (empty(d)) 
            return 0; 
        return d->vetor[d->front]; 
    } 

    // Function to get rear of d 
    int rear(deque* d) 
    { 
        if (empty(d)) 
            return 0; 
        return d->vetor[d->rear]; 
    } 

    // Driver program to test above functions./ 
    int main() 
    { 
        deque* d; 
        int operacoes=0, tamdeque=0, i=0;
        char opcao[100];

        scanf("%d %d", &operacoes, &tamdeque);
        initialize(&d, tamdeque);

        while(i<=operacoes){
            scanf("%s", opcao);
            if(!strcmp(opcao, "insereI")){
                if(full(&d)){
                    printf("cheia\n");
                }
                else{
                    data item; 
                    scanf("%d", &item);
                    printf("%u\n", &d->capacidade);
                    push_front(&d, item);
                }
            }
            else if(!strcmp(opcao,"insereF")){
                if(full(&d)){
                    printf("cheia\n");
                }
                else{
                    data item;
                    scanf("%d", &item);
                    push_back(&d, item);
                }
            }
            else if(!strcmp(opcao, "removeI")){
                if(empty(&d)){
                    printf("vazia\n");
                }
                else{
                    pop_front(&d);
                }
            }
            else if(!strcmp(opcao, "removeF")){
                if (empty(&d)){
                    printf("vazia\n");
                }
                else{
                    pop_back(&d);
                }
            }
            i++;
            }

        return 0; 
    } 

最佳答案

参数不匹配

在很多地方,出现以下类型的错误。所有警告均已启用,可轻松查找。

warning: passing argument 1 of 'pop_back' from incompatible pointer type [-Wincompatible-pointer-types]

int push_back(deque* d, int item){ 
    // if (full(&d))
    if (full(d))

main()

  // deque* d;
  deque d;
  initialize(&d, tamdeque);

混合int/unsigned数学。

建议更改设计以仅使用一个。

缺少返回值

次要:int push_back()、push_front()

<小时/>

还存在其他问题。 (总共约 30 条警告)

关于c - 如何修复 Deque 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54453110/

相关文章:

PHP exec 没有按预期运行我的脚本

Linux上的C编程: which distro and tools to use

c - 动态生成的代码在错误的地址执行

c++ - 未定义的 md5 引用

c - 别名的语法

c - C 中的互连结构和回调

C 中无法在另一个函数中打印函数的返回值

c - G-WAN 类似物

c - 在文本文件中查找关键字

c - 基础 C 快速排序