C队列字符数组

时间:2016-02-18 16:11:07

标签: c queue

我正在尝试在C中创建一个可以将字符串作为其值的队列。

我引用了http://www.thelearningpoint.net/computer-science/data-structures-queues--with-c-program-source-code

中的代码

我正在尝试将函数的输入和输出从int更改为char数组(因为我正在存储字符串)。

但是我是C的新手并且无法解决代码的两部分中出现的错误:

在入队功能中:   strcpy(Q->元素[Q-> rear],p);

在Front功能中:返回Q->元素[Q-> front];

给出的错误消息是与char *

不兼容的char类型
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

/*Queue has five properties.

capacity stands for the maximum number of elements Queue can hold.
  Size stands for the current size of the Queue and elements is the array of elements.
  front is the index of first element (the index at which we remove the element)
  rear is the index of last element (the index at which we insert the element) */
typedef struct Queue
{
        int capacity;
        int size;
        int front;
        int rear;
        char **elements;
}Queue;

/* crateQueue function takes argument the maximum number of elements the Queue can hold, creates
   a Queue according to it and returns a pointer to the Queue. */
Queue * createQueue(int maxElements)
{
        /* Create a Queue */
        Queue *Q;
        Q = (Queue *)malloc(sizeof(Queue));
        /* Initialise its properties */
        Q->elements = (char**)malloc(sizeof(char*)*maxElements);
        Q->size = 0;
        Q->capacity = maxElements;
        Q->front = 0;
        Q->rear = -1;
        /* Return the pointer */
        return Q;
}

void Dequeue(Queue *Q)
{
        if(Q->size!=0)
        {
                Q->size--;
                Q->front++;
                /* As we fill elements in circular fashion */
                if(Q->front==Q->capacity)
                {
                        Q->front=0;
                }
        }
        return;
}

char* front(Queue *Q)
{
        if(Q->size!=0)
        {
                /* Return the element which is at the front*/
                return Q->elements[Q->front];
        }
        return NULL;
}

void Enqueue(Queue *Q,char *element)
{
        //char *p = (char *) malloc(strlen(element)+1);

        /* If the Queue is full, we cannot push an element into it as there is no space for it.*/
        if(Q->size == Q->capacity)
        {
                printf("Queue is Full\n");
        }
        else
        {
                Q->size++;
                Q->rear = Q->rear + 1;
                /* As we fill the queue in circular fashion */
                if(Q->rear == Q->capacity)
                {
                        Q->rear = 0;
                }
                /* Insert the element in its rear side */ 
                strcpy(Q->elements[Q->rear], element);
        }
        return;
}

int main()
{
        Queue *Q = createQueue(5);
        Enqueue(Q,"test");  // now runtime fails at this line
        Enqueue(Q,"test");
        Enqueue(Q,"test");
        Enqueue(Q,"test");
        printf("Front element is %s\n",front(Q));
        Enqueue(Q,"test");
        Dequeue(Q);
        Enqueue(Q,"test");
        printf("Front element is %s\n",front(Q));
        Sleep(10000);
}

我不知道如何更改它来存储和打印字符串。帮助赞赏!

2 个答案:

答案 0 :(得分:1)

您已实施存储char值的队列。

在C中,字符串是char *,它是指向以char结尾的一系列连续\0值的指针。

如果要存储字符串,则elements数组应为char **,这是指向char指针的指针。这是C中引用动态分配的指针数组的常用方法。

/* Allocate an array of 10 "char *" values.  NOTE: we're taking sizeof
   a char * NOT a char. */
char **entries = malloc(sizeof(char*) * 10);

要复制字符串,请查看strdup。它与strlen + malloc + strcpy的功能相同,但使用一次调用而不是3次。

此外,请确保在完成字符串后free front。您可以1)使Dequeue释放字符串,在这种情况下,必须让队列的用户知道Dequeue返回的指针在调用free后失效,或者2)保留责任最终用户front无论front返回什么。我倾向于更喜欢我的C队列有一个pop / dequeue操作返回 <div id="partialForm"> @Html.Partial("_AudienceFaxComposit", Model.AudienceFaxes[start]) </div> <ul class="pager"> <li><a href="#" id="prev">Prev</a></li> <li><a href="#" id="next">Next</a></li> </ul> 并在一次调用中将它从队列中删除,尽管你所做的事情近似于C ++ STL容器的工作方式,这可能是目标..

答案 1 :(得分:1)

你忘了分配Q->elements[Q->rear]

void Enqueue(Queue *Q , char *element)
{
        //char *p = (char *) malloc(strlen(element)+1);

        /* If the Queue is full, we cannot push an element into it as there is no space for it.*/
        if(Q->size == Q->capacity)
        {
                printf("Queue is Full\n");
        }
        else
        {
                Q->size++;
                Q->rear = Q->rear + 1;
                /* As we fill the queue in circular fashion */
                if(Q->rear == Q->capacity)
                {
                        Q->rear = 0;
                }
                /* Insert the element in its rear side */ 

                //printf("testing\n");

                Q->elements[Q->rear] = (char *) malloc((sizeof element + 1)* sizeof(char));

                strcpy(Q->elements[Q->rear], element);
        }
        return;
}

现在它正在运作。