기본 대기열 설계가 있지만 다중 대기열을 원합니다. 지금 당장 보이는 방법은 다른 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]
푸시 표기법 'iceCreamLine.push (13);'가 작동하려면'struct Node' 구조체에'void (* push) (int);'요소가 필요합니다. 각각의'struct Node'가 적절하게 초기화되어'push' 엘리먼트가 올바른 함수를 가리키고 있는지 확인하십시오. –
'head'와'tail' 등 다른 이름으로 파일의 새 버전을 만드는 것은 재앙입니다. +1로 구현하기 전에 의심 스럽거나 묻기가 쉽습니다. –