如何修复Deque实现

时间:2019-01-31 04:06:57

标签: 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; 
    } 

1 个答案:

答案 0 :(得分:0)

<强>失配参数

在许多地方,以下类型的错误。随时可以找到所有警告。

  

警告:从不兼容的指针类型[-Wincompatible-pointer-types]传递“ pop_back”的参数1

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条警告)