Why is my linked list sort changing the values of the list?
-
19-10-2022 - |
Question
Question is pretty much self-explanatory.
Here's my code:
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: Creates a structure of pointers to HuffmanTrees and pointers to the next value in the LinkedList.
#include "huffman.h"
typedef struct linkedListNode {
HuffmanTree* value;
struct linkedListNode* next;
} LinkedList;\
And the HuffmanTree structure.
#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;
When I run this code, the llSort method sorts the LinkedList correctly, but it also changes the ascii values of the Huffman chars. I've walked through it a couple different times and I can't figure out why this is happenening. Does anyone have an idea?
Thanks!
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow