2014-10-31 47 views
0

我的插入函數適用於空隊列,前後排隊等於一個的隊列。之後,似乎有一個邏輯錯誤。我只有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); 
} 
+0

您是在結尾還是在開始列表中添加新節點? – 2014-10-31 01:57:44

+0

我通常會在開始時添加節點,如果先進先出,但如果compare(currNode,tempData)返回負數,那麼它會在currNode之前。如果compare(currNode,tempData)是正數,那麼它應該移動隊列的後面 – arrowinfedex 2014-10-31 02:28:09

回答

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; 
} 
+0

我正在閱讀這篇文章,但我現在必須提交,這可能比我的方法更清晰。離開正面! – arrowinfedex 2014-10-31 03:36:46

1

至少有一個問題是這一行:

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

您已經分配的temp價值queue->rear幾行了,所以如果測試永遠不會評估爲真。您需要重新排列分配發生的位置,或者保存舊值以供以後測試,請在分配之前設置標誌變量。由於這是功課,你應該嘗試自己解決這個問題。