我的插入函數適用於空隊列,前後排隊等於一個的隊列。之後,似乎有一個邏輯錯誤。我只有2個小時才能提交。在隊列中的node_add,只插入前3個,前後中間
輸出繼電器用於測試升降
測試升序隊列 插入:42 17 -12 9982 476 2912 -22 3291213 7782
卸下:17 3291213 7782
測試下行隊列 插入:42 17 -12 9982 476 2912 -22 3291213 7782
刪除:42 -22 7782
測試FIFO隊列 插入:42 17 -12 9982 476 2912 -22 3291213 7782
卸下:42 17 -12 9982 476 2912 -22 3291213 7782
void que_insert(QueueADT queue, void *data) {
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->data= data;
node *currNode;
//currNode = (struct node*)malloc(sizeof(struct node));
currNode = queue->front;
//cmp = &(queue->cmprFunc);
if (queue->front != NULL) {
if (queue->cmprFunc == NULL) { //if the cmp_func is FIFO
queue->rear->next = temp;
queue->rear= temp;
queue->rear->next=NULL;
if (queue->front == queue->rear) {
currNode->next = temp;
queue->rear = temp;
temp->next= NULL;
}
} else {
while (currNode->next != NULL){
if (((*(queue->cmprFunc))(currNode->next->data, temp->data) < 0) ||
((*(queue->cmprFunc))(currNode->next->data, temp->data) == 0)) {
temp->next = currNode->next;
currNode->next = temp;
break;
} else {
currNode = currNode->next;
if (currNode->next != NULL) {
currNode->next = temp;
queue->rear = temp;
temp->next = NULL;
}
//exit_failure
}
}
if (queue->front == queue->rear) {
if (((*(queue->cmprFunc))(currNode->data, temp->data) < 0) ||
((*(queue->cmprFunc))(currNode->data, temp->data) == 0)) {
queue->rear = temp;
queue->front->next = queue->rear;
temp->next= NULL;
} else {
queue->front = temp;
temp->next = temp;
}
}
//printf("Front is equal to next %i\n", (queue->front == queue->rear));
}
} else { //(queue->front == NULL)
queue->front = temp;
queue->rear= queue->front;
queue->front->next= queue->rear->next = NULL;
}
}
比較功能返回int基於以下標準:
*** < 0 a < b
= 0 a == b
> 0 a > b***
其中「>」和在數據被比較「<」依賴
typedef struct node {
void* data;
struct node *next;
}node;
struct queueStruct {
struct node *front; /* pointer to front of queue */
struct node *rear; /* pointer to rear of queue */
int (*cmprFunc)(const void*a,const void*b);
//size_t num; /* The compare function used for insert */
};
typedef struct queueStruct *QueueADT; //typedef inserted for pointers, name is QueueADT
#define _QUEUE_IMPL_
#include "queueADT.h"
/// create a queue that is either sorted by cmp or FIFO
//function with two void
QueueADT que_create(int (*cmp)(const void*a,const void*b)) {
QueueADT new;
new = (QueueADT)malloc(sizeof(struct queueStruct));
if (cmp == NULL) {
//do I create a new pointer to the cmp_int64?
new->front = NULL;
new->rear = NULL;
new->cmprFunc = NULL;
} else {
new->cmprFunc = cmp;
new->front = NULL;
new->rear = NULL;
}
return (new);
}
您是在結尾還是在開始列表中添加新節點? – 2014-10-31 01:57:44
我通常會在開始時添加節點,如果先進先出,但如果compare(currNode,tempData)返回負數,那麼它會在currNode之前。如果compare(currNode,tempData)是正數,那麼它應該移動隊列的後面 – arrowinfedex 2014-10-31 02:28:09