[英]I was trying to create a Queue ADT in C but something is wrong in my code… can Anyone Fix this?
Declaring the headers in.c file or header file doesn't change anything in the errors在.c 文件或 header 文件中声明标题不会更改错误中的任何内容
#ifndef QUEUE
#define QUEUE
#include <stdio.h>
#include <stdlib.h>
typedef struct QNodeType
{
char ch;
struct QNodeType *next;
}QNode;
typedef struct QueueType
{
QNode *head,*tail;
}Queue;
Queue **Create_Queue(); // Initialize the queue
void ClearQueue(); // Remove all items from the queue
int Enqueue(Queue **q,char ch); // Enter an item in the queue
char Dequeue(Queue **q); // Remove an item from the queue
int isEmpty(Queue **q); // Return true if queue is empty
int isFull(Queue **q); // Return true if queue is full
// Define TRUE and FALSE if they have not already been defined
#ifndef FALSE
#define FALSE (0)
#endif
#ifndef TRUE
#define TRUE (!FALSE)
#endif
#endif
#include "queue.h"
Queue** Create_Queue()
{
Queue **q;
(*q)->head = (*q)->tail = NULL;
return q;
}
void ClearQueue(Queue **q)
{
QNode *temp;
while((*q)->head != NULL)
{
temp = (*q)->head;
(*q)->head = (*q)->head->next;
free(temp);
}
(*q)->head = (*q)->tail = NULL; // Reset indices to start over
}
int Enqueue(Queue **q,char ch)
{
QNode *temp;
if(isFull(q)) return FALSE;
temp = (QNode *)malloc(sizeof(QNode));
temp->ch = ch;
temp->next = NULL;
if((*q)->head == NULL)
(*q)->head = (*q)->tail = temp;
else
{
(*q)->tail->next = temp; // Insert into the queue
(*q)->tail = temp; // Set tail to new last node
}
return TRUE;
}
char Dequeue(Queue **q)
{
char ch;
QNode *temp;
if(isEmpty(q)) return '\0';
else
{
ch = (*q)->head->ch;
temp = (*q)->head;
(*q)->head = (*q)->head->next;
free(temp);
if(isEmpty(q))
{
(*q)->head = (*q)->tail = NULL;
}
}
return ch;
}
int isEmpty(Queue **q)
{
return ((*q)->head == NULL);
}
int isFull(Queue **q)
{
return FALSE;
}
#include "queue.h"
int main()
{
char testString[27];
int i;
char ch;
Queue **q=Create_Queue();
Enqueue(q,'A');
printf("Enqueued: %c\n", Dequeue(q));
strcpy(testString, "abcdefghijklmnopqrasuvwxyz");
i = 0;
printf("Testing enqueuing of string: %s\n", testString);
while(testString[i] != '\0')
{
if(!Enqueue(q,testString[i]))
{
printf("Queue is full. Unable to enqueue %c\n", testString[i]);
}
i++;
}
printf("Dequeued letters are...\n");
while((ch = Dequeue(q)) != '\0') // Dequeue returns null terminator
printf("%c", ch); // when queue is empty
printf("\nEnd of queue encountered...\n");
return 0;
}
But when I run the code without the QUEUE Structure( basically this means declaring a *head and *tail pointer of static type in the queue.c file which allows for only one queue to be created at a time. And thus the CreateQueue function will be of void type and other functions doesn't require an argument of type Queue**) ) it ran without any bugs. But when I run the code without the QUEUE Structure( basically this means declaring a *head and *tail pointer of static type in the queue.c file which allows for only one queue to be created at a time. And thus the CreateQueue function will是 void 类型,其他函数不需要 Queue**) 类型的参数)它运行时没有任何错误。
I think that the problem is in create_Queue() you are not allocating any memory in it, after that in void ClearQueue() you are using free().我认为问题出在 create_Queue() 中,您没有在其中分配任何 memory,之后在 void ClearQueue() 中您使用的是 free()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.