c - 如何使用 C 中的链接实现将元素插入和显示到 Dequeue

标签 c data-structures

我尝试使用链接实现在 C 中编写基本的出队程序。但它显示用于将元素插入出队的函数存在一些错误。我还想显示出队中的元素。下面是我尝试过的代码。运行代码时 CLI 不显示任何输出。由于插入函数中的错误,我不确定删除函数是否也正确或不正确。

#include<stdio.h>
#include<stdlib.h>
typedef int DeQueueElement;
typedef enum{FALSE,TRUE} Boolean;
typedef struct node{
    DeQueueElement entry;
    struct node *next, *prev;
}Node;
typedef struct dequeue{
    int count;
    Boolean full;
    Node *front;
    Node *rear;
}DeQueue;
void CreateDeQueue(DeQueue *dq){
    dq->count = 0;
    dq->full = FALSE;
    dq->front = dq->rear = -1;
}
Boolean IsEmpty(DeQueue *dq){
    return (dq->front == NULL && dq->rear == NULL);
}
Boolean IsFull(DeQueue *dq){
    return(dq->full);
}
void InsertRear(DeQueueElement x, DeQueue *dq){
    Node *np;
    np = (Node* )malloc(sizeof(Node));
    if(np == NULL){
        printf("Not enough space\n");
    }
    else{
        if(dq->rear == NULL)
            dq->front = dq->rear = np;
        else{
            np->prev = dq->rear;
            dq->rear->next = np;
            dq->rear = NULL;
            np->next = np;
            np->entry = x;
        }
    }
}
void InsertFront(DeQueueElement x, DeQueue *dq){
    Node *np;
    np = (Node* )malloc(sizeof(Node));
    if(np == NULL)
        printf("Not enough space\n");
    else{
        if(dq->front == NULL)
            dq->rear = dq->front = np;
        else{
            np->next = dq->front;
            dq->front->prev = np;
            dq->front = np;
            np->prev = NULL;
            np->entry = x;
        }
    }
}
void DeleteFront(DeQueue *dq){
    if(dq->front == NULL)
        printf("Underflow\n");
    else{
        Node *temp;
        temp = dq->front;
        dq->front = dq->front->next;
        if(dq->front == NULL)
            dq->rear = NULL;
        else
            dq->front->prev = NULL;
        free(temp);
    }
}
void DeleteRear(DeQueue *dq){
    if(dq->front == NULL)
        printf("Underflow\n");
    else{
        Node *temp;
        temp = dq->rear;
        dq->rear = dq->rear->prev;
        if(dq->rear == NULL)
            dq->front == NULL;
        else
            dq->rear->next = NULL;
        free(temp);
    }
}
 void display(DeQueue *dq) {
        Node *temp;

        if (dq->front->next == dq->rear) {
                printf("Queue is empty\n");
                return;
        }

        temp = dq->front->next;
        while (temp != dq->rear) {
                printf("%d", temp->entry);
                temp = temp->next;
        }
        printf("\n");
  }

int main(){
    DeQueue dq;
    CreateDeQueue(&dq);
    InsertFront(21,&dq);
    InsertFront(1,&dq);
    InsertFront(221,&dq);
    InsertRear(23,&dq);
    InsertRear(36,&dq);
    display(&dq);
}

最佳答案

代码有一些错误,我尝试更正它们并评论我所做的更改。 问题出在 CreateDeQueueInsertFrontInsertReardisplay 函数中。

void CreateDeQueue(DeQueue *dq){
    dq->count = 0;
    dq->full = FALSE;
    // dq->front = dq->rear = -1;
    // Change 0: It should be NULL or 0 not -1
    dq->front = NULL;
    dq->rear = NULL;
}
void InsertRear(DeQueueElement x, DeQueue *dq){
    Node *np;
    np = (Node* )malloc(sizeof(Node));
    if(np == NULL){
        printf("Malloc failed\n");
    }
    else{
        np->entry = x;
        np->next = NULL;
        np->prev = NULL;
        if(dq->rear == NULL)
            dq->front = dq->rear = np;
        else{
            np->prev = dq->rear;
            dq->rear->next = np;
            //dq->rear = NULL;
            //Change 1: Why dq->rear to NULL it should point to valid mem location
            dq->rear = np;
        }
        dq->count++;
    }
}
void InsertFront(DeQueueElement x, DeQueue *dq){
    Node *np;
    np = (Node* )malloc(sizeof(Node));

    if(np == NULL)
        printf("Malloc failed. \n");
    else{
        //Change 3: Initialize the newly created struct Node, np
        np->entry = x;
        np->next = NULL;
        np->prev = NULL;

        if(dq->front == NULL)
            dq->rear = dq->front = np;
        else{
            np->next = dq->front;
            dq->front->prev = np;
            dq->front = np;
        }
        dq->count++;
    }
}
void display(DeQueue *dq) {
       Node *temp;
       if (dq->front->next == dq->rear) {
               printf("Queue is empty\n");
               return;
       }
       temp = dq->front;
       // Change 4: Checking temp against dq->rear is not logical i guess
       // temp should  not be NULL. Printing from the front
       while (temp!=dq->rear->next) {
           printf("%d ", temp->entry);
           printf("\n");
           temp = temp->next;
       }
}

关于c - 如何使用 C 中的链接实现将元素插入和显示到 Dequeue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59105432/

相关文章:

c - 并行运行代码的潜在危险

algorithm - 为什么我们通常在分而治之算法中分成两部分?

linux - 如何在Linux中将大量带有数据(相同格式)的PDF转换为Excel?

c - 打印解决方案(C 语言新手)

c - fgets()/scanf() do-while 循环

sql - 在关系数据库中存储大量点(x,y,z)

c++ - 选择矩阵中距离另一点 30m 以内的所有点

data-structures - 如何在安全的 Rust 中表达相互递归的数据结构?

c - 尝试编写一个从用户处获取名称的函数

c++ - 在循环中使用 dup2() 和 create() 在 Linux 中重定向