2016-02-18 29 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; 
} 

現在正在工作。