2017-12-03 255 views
0

這是一個結構,我給了一個返回指向新的空隊列的指針的隊列。我知道隊列 - >後端指向隊列後面的節點,隊列 - >後 - >下一個指向隊列前端的節點。我如何稱呼隊列的前端?每當我使用queue-> rear-> next時,我都會遇到運行時錯誤。如何在鏈表中調用隊列的前端?

intqueue_t *intqueue_construct(void) 
{ 
intqueue_t *queue = malloc(sizeof(intqueue_t)); 
assert(queue != NULL); 

queue->rear = NULL; 
queue->size = 0; 
return queue; 
} 

回答

0

我不知道我是否正確,但我認爲你看起來像這樣。 Pleas提供所有代碼,尤其是結構定義。

typedef struct node_t 
{ 
    int data; 
    struct node_t* next; 
} node; 

void intqueue_addelement(node* n, int d) //add new element to linked list 
{ 
    if(n->next == NULL) 
    { 
     n->next = malloc(sizeof(node)); 
     assert(n->next != NULL); 
     n->next->next = NULL; 
     n->next->data = d; 
    } 
    else 
    { 
     intqueue_addelement(n->next, d); 
    } 
} 

node* intqueue_lastelement(node* n) 
{ 
    if(n->next == NULL) 
    { 
     return n; 
    } 
    else 
    { 
     return intqueue_lastelement(n->next); 
    } 
} 

而且在主要應該是這個樣子:

node *q = malloc(sizeof(node)); 
q->next = NULL; 
q->data = 0; 

您現在有一個鏈表。 q指向開頭,intqueue_lastelement給出最後一個元素的指針,intqueue_addelement添加一個元素。