在队列中的node_add,只插入前3,后3和中间

时间:2014-10-31 01:20:53

标签: c linked-list queue logic abstract-data-type

我的插入功能适用于空队列,前后队列相等,还有一个。之后似乎存在逻辑错误。我只有2个小时的时间来提交。

用于测试升序和降序的输出

测试升序队列 插入: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;

        }
    }

compare函数根据以下条件返回int:

*** < 0 a < b
 = 0    a == b
 > 0    a > b***

其中&#34;&gt;&#34;和&#34;&lt;&#34;取决于被比较的数据

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 );
}

2 个答案:

答案 0 :(得分:1)

您的 que_insert

void que_insert( QueueADT queue, void *data ) {
    struct node *temp;
    temp = (struct node*)malloc(sizeof(struct node));
    temp->data= data;
    node *currNode, *prevNode = NULL;

    currNode = queue->front;

    if(currNode == NULL){
        //first node to add
        temp->next = NULL;
        queue->front = temp;
        queue->rear = temp;

        return;
    }

    while(currNode != NULL){
        if( /* comparison is negative */ ){
            temp->next = currNode;
            if(prevNode != NULL){
                prevNode->next = temp;
            }
            else{
                queue->front = temp;
            }

            return;
        }

        prevNode = currNode;
        currNode = currNode->next;
    }

    //add to the end
    prevNode->next = temp;
    temp->next = NULL;
    queue->rear = temp;
}

答案 1 :(得分:1)

这一行至少有一个问题:

if ( queue->front == queue->rear ) {

您已经将temp的值分配给queue->rear两行,所以如果测试永远不会评估为true。您需要重新排列赋值发生的位置,或者保存旧值以供以后测试,在进行赋值之前设置一个标志变量。由于这是家庭作业,你应该尝试自己解决这个问题。