【发布时间】:2014-01-22 04:54:27
【问题描述】:
这是我使用双向链表实现双端队列的代码。它不工作..你能给我一些关于我哪里出错的指示吗?指针未初始化,代码卡在 addqatend 函数中。
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string.h>
#include<time.h>
#include<math.h>
#include<ctype.h>
#include <malloc.h>
#include <Windows.h>
struct node
{
struct node *prev;
int data;
struct node *next;
};
struct queue
{
struct node *front;
struct node *rear;
};
void initqueue(struct queue *);
void addqatbeg(struct queue *,int);
void addqatend(struct queue *, int);
int delqatbeg(struct queue *);
int delqatend(struct queue *);
void delqueue(struct queue *);
int main()
{
struct queue a;
int i;
system("cls");
initqueue(&a);
addqatbeg(&a,11);
addqatbeg(&a,23);
addqatbeg(&a,-5);
addqatbeg(&a,45);
addqatend(&a,34);
addqatend(&a,78);
i = delqatbeg(&a);
if(i!=NULL)
printf("item deleted from front:%d",i);
i = delqatbeg(&a);
if(i!=NULL)
printf("item deleted from front:%d",i);
i = delqatend(&a);
if(i!=NULL)
printf("item deleted from end:%d",i);
i = delqatend(&a);
if(i!=NULL)
printf("item deleted from end:%d",i);
delqueue(&a);
system("pause");
return 0;
}
//initialise the queue
void initqueue(struct queue *q)
{
q->front = q->rear = NULL;
}
//add at the beginning of the queue
void addqatbeg(struct queue *q,int item)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
if(temp == NULL)
printf("queue is full\n");
//temp->data = item;
//temp->link = NULL;
if(q->front == NULL)
{
q->rear = q->front = temp;
q->front->prev = NULL;
q->front->next = NULL;
q->rear->next = NULL;
q->rear->prev = NULL;
return;
}
q->front->data = item;
q->front->next = temp;
q->front = temp;
q->front->prev = q->front;
//q->rear->next = q->front->next;
//q->front->prev = q->rear;
}
//add at the end of the queue
void addqatend(struct queue *q, int item)
{
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
if(temp == NULL)
printf("queue is full\n");
if(q->front == NULL)
{
q->rear = q->front = temp;
q->front->prev = NULL;
q->front->next = NULL;
return;
}
while(q->front->next!=NULL)
q->rear = q->front->next;
q->rear->data = item;
q->rear->next =temp;
q->rear = q->rear->next;
q->rear->prev = q->rear;
}
//delete at the beginning of the queue
int delqatbeg(struct queue *q)
{
struct node *temp;
int item;
if(q->front ==NULL)
{
printf("queue is empty:\n");
return NULL;
}
item = q->front->data;
temp = q->front;
q->front = q->front->next;
free(temp);
return item;
}
//delete at the end of the queue
int delqatend(struct queue *q)
{
struct node *temp;
int item;
if(q->rear == NULL)
{
printf("queue is empty\n");
return NULL;
}
item = q->rear->data;
temp = q->rear;
q->rear = q->rear->prev;
free(temp);
return item;
}
//free the nodes
void delqueue(struct queue *q)
{
struct node *temp;
if(q->front == NULL)
return;
while(q->front!=NULL)
{
temp = q->front;
q->front = q->front->next;
free(temp);
}
}
【问题讨论】:
-
所以您希望我们梳理您的代码,理解它,找到您的错误,调试您的错误,并为您重新编码? “努力产生努力。”
-
我是这里的新手..所以请你帮忙..我也是数据结构的新手
-
@jitendrakumar 您必须更具体地了解您所面临的确切问题。 “它不工作......”没有多大帮助。告诉我们哪个部分起作用以及它挂起或卡住的确切位置。
-
@AbhishekBansal,它卡在 while 循环中的第二个函数 addatend 中。它陷入了无限循环,并且指针 next、prev 和 data 似乎都未初始化。
-
@jitendrakumar 现在是学习如何调试代码的好时机。如果任何循环卡在 while 循环中,则表示未满足终止条件。如果您说指针未初始化,则回溯并检查代码的哪一部分按预期工作,最好使用调试器或至少通过将调试消息输出到控制台。
标签: c++ c pointers data-structures struct