2017-12-03 8 views
0

이것은 새로운 빈 큐에 대한 포인터를 반환하는 큐에 주어진 구조입니다. queue-> rear가 queue의 뒤쪽에있는 노드를 가리키고 queue-> rear-> next가 queue의 앞에있는 노드를 가리킨다는 것을 알고 있습니다. 대기열의 앞면을 어떻게 부르겠습니까? 대기열 -> 뒤 -> 다음을 사용할 때마다 런타임 오류가 발생합니다.링크 된 목록에서 큐의 앞을 호출하는 방법?

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

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

답변

0

내가 맞는지는 잘 모르겠지만, 이런 식으로 생각합니다. 플레에서는 모든 코드, 특히 구조체 정의를 제공합니다.

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는 요소를 추가합니다.