为什么我的链表排序会更改列表的值?

时间:2014-03-03 22:13:20

标签: c sorting linked-list

问题几乎是不言自明的。

这是我的代码:

LinkedList* llSort(LinkedList* l) { // this function is changing the ascii values for some reason

LinkedList* tmpPtr = l; // tmpPtr is pointing to the beginning of the LinkedList
LinkedList* tmpNext = l->next; // tmpNext is pointing to the second entry in the LinkedList

int tmp;

/* while we are not at the end of the LinkedList */
while(tmpNext != NULL) {

/* and while tmpNext isn't pointing backwards */
while(tmpNext != tmpPtr) {

  /* if the frequency of tmpNext is less than the frequency of tmpPtr */
  if(tmpNext->value->freq < tmpPtr->value->freq) {

    /* set tmp = to tmpPtr's freq */
    tmp = tmpPtr->value->freq;

    /* set tmpPtr's freq equal to tmpNext's freq */
    tmpPtr->value->freq = tmpNext->value->freq;

        /* set tmpNext's freq equal to tmp, which is the value of the original frequency */
        tmpNext->value->freq = tmp;
      }

      /* set tmpPtr equal to it's next value */
      tmpPtr = tmpPtr->next;
    }
tmpPtr = l;
    tmpNext = tmpNext->next;
  }
return tmpPtr;
}

LinkedListsH.h:创建指向HuffmanTrees的指针结构和指向LinkedList中下一个值的指针。

#include "huffman.h"

typedef struct linkedListNode {
    HuffmanTree* value;
    struct linkedListNode* next;
} LinkedList;\

和HuffmanTree结构。

#include <stdio.h> //printf
#include <stdlib.h> //standard library
#include <string.h>

typedef struct huffmanNode {
  char c;
  int freq;
  struct huffmanNode* left;
  struct huffmanNode* right;
} HuffmanTree;

当我运行此代码时,llSort方法正确排序LinkedList,但它也会更改Huffman字符的ascii值。我已经走了几次不同的时间,我无法弄清楚为什么会发生这种情况。有没有人有想法?

谢谢!

0 个答案:

没有答案