Моя функция вставки отлично работает для пустой очереди, очередь с передней и задней равными и еще одна. После этого возникает логическая ошибка. У меня есть только 2 часа, чтобы представить это.node_add в очереди, только вставляет первые 3 как спереди, сзади и посередине
Ouput для тестирования восходящих и нисходящих
Тестирование очередь нужно по возрастанию Вставка: 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);
}
Вы добавляете новый узел в конце или в начале списка? –
Обычно я добавляю узел в начале, если сначала первый, но если сравнение (currNode, tempData) возвращает отрицательный, то оно идет до currNode. Если compare (currNode, tempData) положителен, то он должен перемещать заднюю часть очереди – arrowinfedex