gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:09:34 25 4
gpt4 key购买 nike

我尝试使用链接实现在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com