2014-10-09 2 views
3

기본 대기열 설계가 있지만 다중 대기열을 원합니다. 지금 당장 보이는 방법은 다른 queue.h 파일이 필요하고 머리와 꼬리를 다른 이름으로 대체하는 것입니다. 그러나 더 나은 방법이있을 것이라고 확신합니까?C의 다중 대기열

queue.h * 내가이 같은 여러 개의 큐를 만들 수있는 방법

#include<stdlib.h> // malloc 

struct Node { 
    int data; 
    struct Node* next; 
}; 

struct Queue { 
    struct Node *head, *tail; 
}; 

struct Queue *QueueInit() { 
    //allocate and initialize a queue 
    struct Queue *thisQueue = malloc(sizeof *thisQueue); 
    thisQueue->head = NULL; 
    thisQueue->tail = NULL; 
    return thisQueue; 
} 


void push(struct Queue *myQueue, int x) { 
    struct Node *temp; 
    temp = malloc(sizeof(struct Node)); 
    temp->data = x; 
    temp->next = NULL; 
    if(myQueue->head == NULL && myQueue->tail == NULL) { //empty 
     myQueue->head = myQueue->tail = temp; 
     return; 
    } 
    myQueue->tail->next = temp; 
    myQueue->tail = temp; 
} 

void pop(struct Queue *myQueue) { 
    struct Node* temp = myQueue->head; 
    if(myQueue->head == NULL) return; //empty 
    if(myQueue->head == myQueue->tail) { 
     myQueue->head = myQueue->tail = NULL; 
    } 
    else { 
     myQueue->head = myQueue->head->next; 
    } 
    free(temp); 
} 

을 편집?

main.c를

int main() { 
    struct Node iceCreamLine; 
    struct Node bathroomLine; 

    iceCreamLine.push(13); 
    bathroomLine.push(2); 


    //It looks like I will have to use this syntax then instead? 
    struct Queue *droneQueue; //(THIS IS LINE 5) 
    push(&droneQueue,1666); 
    push(&droneQueue,100); 

    printf("--> %d",&droneQueue->head->data); 
    printf("--> %d",&droneQueue->head->next->data); 

} 

첫 번째 printf와 작품,하지만 두 번째는 나에게 분할 덤프를 제공합니다. 또한 여기에 경고가 있습니다

main.c : 함수 'main'에서 : main.c : 6 : 2 : 경고 : 호환되지 않는 포인터 유형에서 'push'인수 1을 전달 함 [기본적으로 활성화 됨] 파일 포함 queue.c : 2 : 0 : queue.h : 21 : 6 : 참고 : 'struct Queue *'가 예상되지만 인수는 'struct Queue **'유형입니다. main.c : 7 : 2 : 경고 : 전달 인수 1 : 호환되지 않는 포인터 유형에서 'push'중 하나를 선택 함 [기본값으로 활성화] queue.c : 2 : 0 : queue.h : 21 : 6에서 파일에 있음 : 'struct Queue *'가 필요하지만 인수의 형식은 ' struct Queue ** ' main.c : 9 : 2 : 경고 : 형식'% d '은'int '형식의 인수를 필요로하지만 인수 2는'int * '형식을 가짐 [-Wformat] 을 main.c가 10 : 2 :주의 : 포맷 '% (D)가'INT '형태의 인수를 기대하지만, 인수 2 입력 보유'INT * '[-Wformat]

+0

푸시 표기법 'iceCreamLine.push (13);'가 작동하려면'struct Node' 구조체에'void (* push) (int);'요소가 필요합니다. 각각의'struct Node'가 적절하게 초기화되어'push' 엘리먼트가 올바른 함수를 가리키고 있는지 확인하십시오. –

+0

'head'와'tail' 등 다른 이름으로 파일의 새 버전을 만드는 것은 재앙입니다. +1로 구현하기 전에 의심 스럽거나 묻기가 쉽습니다. –

답변

3
struct Queue { 
    struct Node *head, *tail; 
}; 

을 할당하는 QueueInit 기능 추가 대기열을 초기화하고 struct Queue에 대한 포인터를 반환합니다. struct Queue에 대한 포인터를 pushpop에 전달하고 headtail을 제거하십시오.

+0

위의 코드를 편집했지만 C 배경이 아닙니다. 머리와 꼬리는 무엇을 얻을 것입니까? 푸시와 팝은 머리와 꼬리가 어떨지를 어떻게 알 수 있습니까? 감사합니다 – user3838436

+0

다른 코드를 기반으로'head'와'tail'을'NULL'으로 초기화합니다. push 및 pop - void push (struct Queue * myQueue, int x) 및 void pop (struct Queue * myQueue)에 대한 매개 변수 목록에'struct Queue * '를 추가합니다. 'push'와'pop'에서'myQueue-> head'와'myQueue-> tail'을 참조하십시오. 또한,'QueueInit'은'return thisQueue;'를 가져야합니다. –

+0

고마워, 나는 거의 효과가 있다고 생각한다. 나는 iceCreamLine.push (13)와 같은 문법을 원했다. 대신 push (& iceCreamLine, 13)하지만 그렇게 할 것입니다. 나는 모두 당신이 말한 코드를 편집했지만 두 번째 printf에서 경고와 세분화 덤프를 얻는다. 나는 포인터를 싫어했습니다! – user3838436

관련 문제