C - 使用链表的队列 |代码 运行 永远在某一时刻
C - Queue using linked list | Code run forever at one point
这是我的main.c:
int main()
{
Queue* list = initialize();
if (!isEmpty(list))
{
fprintf(stderr, "Error!\n");
}
enqueue(list, 10);
enqueue(list, 20);
display(list); // Output: 10 -> 20 -> nil
if (front(list) != 10)
{
fprintf(stderr, "Error!\n");
}
if (back(list) != 20)
{
fprintf(stderr, "Error!\n");
}
enqueue(list, 30);
display(list); // Output: 10 -> 20 -> 30 -> nil
int len = length(list);
if (len == 3)
{
fprintf(stdout, "Number of elements in Queue: %d.\n", len);
}
else
{
fprintf(stderr, "Error!\n");
}
dequeue(list);
display(list); // Output: 20 -> 30 -> nil
if (front(list) != 20)
{
fprintf(stderr, "Error!\n");
}
if (back(list) != 30)
{
fprintf(stderr, "Error!\n");
}
len = length(list);
if (len == 2)
{
fprintf(stdout, "Number of elements in Queue: %d.\n", len);
}
else
{
fprintf(stderr, "Error!\n");
}
empty(list);
display(list); // Output: Queue is empty.
free(list);
return 0;
}
这是我的queue.c:
Queue* initialize()
{
Queue* q = (Queue*)malloc(sizeof(Queue));
q->front = q->back = NULL;
return q;
}
struct Node* newNode(int item)
{
Node* temp = (Node*)malloc(sizeof(Node));
if (temp != NULL)
{
temp->value = item;
temp->next = NULL;
return temp;
}
}
int enqueue(Queue* q, int item)
{
Node *temp = newNode(item);
temp->value = item;
if (q->front == NULL)
q->front = temp;
else
q->back->next = temp;
q->back = temp;
q->back->next = q->front;
return item;
}
int dequeue(Queue *q)
{
Node *temp = q->front;
printf("\nRemoving %d", temp->value);
q->front = q->front->next;
if (q->front == NULL)
q->back = NULL;
int item = temp->value;
free(temp);
return item;
}
Queue* display(Queue *q)
{
Node *temp;
temp=q->front;
if(temp==NULL)
printf("\n The queue is empty");
else
{
printf("\n");
while(temp != q->back)
{
printf("%d -> ",temp->value);
temp=temp->next;
}
printf("%d -> nil",temp->value);
}
return q;
}
int front(Queue *q)
{
if ((q->front != NULL) && (q->back != NULL))
return (q->front->value);
else
return -1;
}
int back(Queue *q)
{
if ((q->front != NULL) && (q->back != NULL))
return (q->back->value);
else
return -1;
}
bool isEmpty(Queue *q)
{
if((q->front == NULL) || (q->back == NULL))
return -1;
else
return 0;
}
void empty(Queue *q)
{
q->front = NULL;
q->back = NULL;
}
int length(Queue *q)
{
int count=0;
if(q->front!=NULL){
Node *temp = q->front;
while(temp->next != NULL)
{
count++;
temp = temp -> next;
}
}
return count;
}
输出应如下所示:
10 -> 20 -> nil
10 -> 20 -> 30 -> nil
Number of elements in Queue: 3.
20 -> 30 -> nil
Number of elements in Queue: 2.
Queue is empty.
我的输出:
10 -> 20 -> nil
10 -> 20 -> 30 -> nil
在这里它会一直保持 运行 直到我手动终止它。我不知道为什么,有人可以帮忙吗?我的长度函数可能是错误的,但我不知道这是否是错误的原因以及如何修复它。此外,main.c 已修复,我无法更改其中的任何内容。
感谢您的帮助和阅读所有这些内容。
当你enqueue
时,你用这个语句创建了一个循环列表
q->back->next = q->front;
您不需要设置 q->back->next
,它应该并且已经是 null
,因为 newNode()
将其设置为“null”。
这是我的main.c:
int main()
{
Queue* list = initialize();
if (!isEmpty(list))
{
fprintf(stderr, "Error!\n");
}
enqueue(list, 10);
enqueue(list, 20);
display(list); // Output: 10 -> 20 -> nil
if (front(list) != 10)
{
fprintf(stderr, "Error!\n");
}
if (back(list) != 20)
{
fprintf(stderr, "Error!\n");
}
enqueue(list, 30);
display(list); // Output: 10 -> 20 -> 30 -> nil
int len = length(list);
if (len == 3)
{
fprintf(stdout, "Number of elements in Queue: %d.\n", len);
}
else
{
fprintf(stderr, "Error!\n");
}
dequeue(list);
display(list); // Output: 20 -> 30 -> nil
if (front(list) != 20)
{
fprintf(stderr, "Error!\n");
}
if (back(list) != 30)
{
fprintf(stderr, "Error!\n");
}
len = length(list);
if (len == 2)
{
fprintf(stdout, "Number of elements in Queue: %d.\n", len);
}
else
{
fprintf(stderr, "Error!\n");
}
empty(list);
display(list); // Output: Queue is empty.
free(list);
return 0;
}
这是我的queue.c:
Queue* initialize()
{
Queue* q = (Queue*)malloc(sizeof(Queue));
q->front = q->back = NULL;
return q;
}
struct Node* newNode(int item)
{
Node* temp = (Node*)malloc(sizeof(Node));
if (temp != NULL)
{
temp->value = item;
temp->next = NULL;
return temp;
}
}
int enqueue(Queue* q, int item)
{
Node *temp = newNode(item);
temp->value = item;
if (q->front == NULL)
q->front = temp;
else
q->back->next = temp;
q->back = temp;
q->back->next = q->front;
return item;
}
int dequeue(Queue *q)
{
Node *temp = q->front;
printf("\nRemoving %d", temp->value);
q->front = q->front->next;
if (q->front == NULL)
q->back = NULL;
int item = temp->value;
free(temp);
return item;
}
Queue* display(Queue *q)
{
Node *temp;
temp=q->front;
if(temp==NULL)
printf("\n The queue is empty");
else
{
printf("\n");
while(temp != q->back)
{
printf("%d -> ",temp->value);
temp=temp->next;
}
printf("%d -> nil",temp->value);
}
return q;
}
int front(Queue *q)
{
if ((q->front != NULL) && (q->back != NULL))
return (q->front->value);
else
return -1;
}
int back(Queue *q)
{
if ((q->front != NULL) && (q->back != NULL))
return (q->back->value);
else
return -1;
}
bool isEmpty(Queue *q)
{
if((q->front == NULL) || (q->back == NULL))
return -1;
else
return 0;
}
void empty(Queue *q)
{
q->front = NULL;
q->back = NULL;
}
int length(Queue *q)
{
int count=0;
if(q->front!=NULL){
Node *temp = q->front;
while(temp->next != NULL)
{
count++;
temp = temp -> next;
}
}
return count;
}
输出应如下所示:
10 -> 20 -> nil 10 -> 20 -> 30 -> nil Number of elements in Queue: 3. 20 -> 30 -> nil Number of elements in Queue: 2. Queue is empty.
我的输出:
10 -> 20 -> nil 10 -> 20 -> 30 -> nil
在这里它会一直保持 运行 直到我手动终止它。我不知道为什么,有人可以帮忙吗?我的长度函数可能是错误的,但我不知道这是否是错误的原因以及如何修复它。此外,main.c 已修复,我无法更改其中的任何内容。 感谢您的帮助和阅读所有这些内容。
当你enqueue
时,你用这个语句创建了一个循环列表
q->back->next = q->front;
您不需要设置 q->back->next
,它应该并且已经是 null
,因为 newNode()
将其设置为“null”。