2016-02-18 39 views
0

我想在C中创建一个队列,它可以将字符串作为其值。C队列字符数组

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

我想的功能的输入和输出从int到字符数组改变代码(因为我存储字符串)。

但是我是新的C和没有能够解决存在于2份的代码的错误:

在排队功能: 的strcpy(Q->元素[Q->后],第);

In Front function:return Q-> elements [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); 
} 

我不知道如何去改变它来存储并打印出的字符串。帮助赞赏!

+0

不要在C中投放'malloc'和好友的结果。 – Olaf

+0

元素是一个char数组。每个元素是否只包含一个字符? –

+0

@ umamahesh-p每个元素都包含一个字符串(char数组) –

回答

1

您已经实施了一个存储char值的队列。

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

如果你想存储字符串,你的elements数组应该是char **,这是一个指向char的指针。这是C中引用动态分配指针数组的常用方式。请参阅strdup。它做同样的事情strlen + malloc + strcpy,但使用一个呼叫,而不是3

此外,请确保您的free琴弦当你与他们所做的。您可以1)使Dequeue释放字符串,在这种情况下,必须让队列用户知道front返回的指针在致电Dequeue后无效,或者2)将责任留在最终用户free无论front返回。我倾向于更喜欢我的C队列有一个弹出/退出队列操作,返回front并在一次调用中将其从队列中移除,尽管您已经完成了C++ STL容器的工作方式,这可能是目标。

+0

感谢char **信息。我已经实施了您的更改并编辑了上面的代码。现在编译好,但运行时NOK(访问冲突写入位置0xcdcdcdcd)。不知道发生了什么! (是的,我试图将出列和前台方法结合在一起,因为我更习惯于在一次调用中将它们结合在一起! - 只是代码是由在线来源提供的,我试图修复运行时错误首先在结合它们之前) –

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; 
} 

现在正在工作。