intmain(){ char input[1000];//used to keep the input
gets(input);//Get the whole line.Using sancf would stop reading when meets ' ' //create the list,head node is not used to store data ListNode* head = malloc(sizeof(ListNode)); head->frequency = 0; head->next = NULL; int i = 0; while(input[i] != '\0'){ ListNode* p = head; ListNode* pre = p;//pointer pointing the previous node of q,which can be helpful for some operations while(p != NULL && p->c != input[i]){ //iterate the list until find the target or to the end pre = p; p = p->next; } if(p != NULL){ //enter this block means having found the target element in the existing list p->frequency++;
//and after add 1 to the frequency,we need to change the position //to make sure the frequency stored in the list is decreasing
//1. take this node out of this list pre->next = p->next; //2. find the right position for the node which frequency is changed ListNode* cur = head->next;//assistant pointer pre = head;//now we reuse the 'pre' to point at the previous node of cur while(cur != NULL && cur->frequency > p->frequency){ pre = cur; cur = cur->next; printf("?"); } //3. insert the node to the new position //after the loop above, we can make sure the node should be insert after the 'pre' p->next = cur; pre->next = p; }else { //enter this block means we meet a new element,so add it to the list //here pre is the last solid node of the list
//create a new node for the new element ListNode* newNode = malloc(sizeof(ListNode)); newNode->c = input[i]; newNode->frequency = 1;
//add the new node to the tail of the list //which is pratical,in that the frequency stored in the list is decreasing newNode->next = pre->next; pre->next = newNode; } i++; } ListNode *iterator = head->next;//use this pointer to iterate the list and print the answer while(iterator != NULL){ printf("'%c' %d\n",iterator->c, iterator->frequency); iterator = iterator->next; } return0; }
//find the last two nodes and take them out of the list ListNode* FetchSecondtoLast(ListNode* head){ ListNode* fast = head; ListNode* slow = head; ListNode* pre = slow; fast = fast->next->next; while(fast != NULL){ pre = slow; slow = slow->next; fast = fast->next; } pre->next = NULL; return slow; }
//insert new node(root of the newly built subtree) to the frequency list voidInsertNode(ListNode* head, ListNode* p){ ListNode* cur = head; while(cur->next != NULL && cur->next->frequency > p->frequency){ cur = cur->next; } p->next = cur->next; cur->next = p; }
//save origin order of the list for the final display and calculate the length for other operations ListNode** SaveOriginOrder(ListNode* head, int* a){ ListNode* iterator = head->next; int length = 0; while (iterator != NULL) { length++; iterator = iterator->next; } ListNode** nodes = malloc(length * sizeof(ListNode*)); iterator = head->next; for(int i = 0; i < length; i++){ nodes[i] = iterator; iterator = iterator->next; } *a = length; return nodes; }
voidDisplayAns(ListNode** nodes, int length){ for(int i = 0; i < length; i++){ if(nodes[i]->c == '\n'){ printf("'\\n' %d %s\n", nodes[i]->frequency, nodes[i]->code); continue; } printf("'%c' %d %s\n", nodes[i]->c, nodes[i]->frequency, nodes[i]->code); } }
ListNode* CreateFrequencyList(){ char buffer[1000];// the max size of each line is 1000 size_t size = 1; // initial size is set to 1,so that we can keep the '\0' char *input = malloc(size); input[0] = '\0'; while (fgets(buffer, sizeof(buffer), stdin)) { if (buffer[0] == '\n') { if(size > 1){ input[size - 2] = '\0'; size--; } break; } size_t buffer_length = strlen(buffer); size_t new_size = size + buffer_length; char *new_result = realloc(input, new_size); input = new_result; strcat(input, buffer); size = new_size; } //create the list,head node is not used to store data ListNode* head = malloc(sizeof(ListNode)); head->frequency = 0; head->next = NULL; head->parent = NULL; head->left = NULL; head->right = NULL;
int i = 0; while(input[i] != '\0'){ ListNode* p = head; ListNode* pre = p;//pointer pointing the previous node of q,which can be helpful for some operations while(p != NULL && p->c != input[i]){ //iterate the list until find the target or to the end pre = p; p = p->next; } if(p != NULL){ //enter this block means having found the target element in the existing list p->frequency++;
//and after add 1 to the frequency,we need to change the position //to make sure the frequency stored in the list is decreasing
//1. take this node out of this list pre->next = p->next; //2. find the right position for the node which frequency is changed ListNode* cur = head->next;//assistant pointer pre = head;//now we reuse the 'pre' to point at the previous node of cur while(cur != NULL && cur->frequency > p->frequency){ pre = cur; cur = cur->next; } //3. insert the node to the new position //after the loop above, we can make sure the node should be insert after the 'pre' p->next = cur; pre->next = p; }else { //enter this block means we meet a new element,so add it to the list //here pre is the last solid node of the list
//create a new node for the new element ListNode* newNode = malloc(sizeof(ListNode)); newNode->parent = NULL; newNode->left = NULL; newNode->right = NULL; newNode->c = input[i]; newNode->frequency = 1;
//add the new node to the tail of the list //which is pratical,in that the frequency stored in the list is decreasing newNode->next = pre->next; pre->next = newNode; } i++; } return head; }
voidDispFList(ListNode* head){ ListNode *iterator = head->next;//use this pointer to iterate the list and print the answer while(iterator != NULL){ printf("'%c' %d\n",iterator->c, iterator->frequency); iterator = iterator->next; } }
//save origin order of the list for the final display and calculate the length for other operations ListNode** SaveOriginOrder(ListNode* head, int* a){ ListNode* iterator = head->next; int length = 0; while (iterator != NULL) { length++; iterator = iterator->next; } ListNode** nodes = malloc(length * sizeof(ListNode*)); iterator = head->next; for(int i = 0; i < length; i++){ nodes[i] = iterator; iterator = iterator->next; } *a = length; return nodes; }
//find the last two nodes and take them out of the list ListNode* FetchSecondtoLast(ListNode* head){ ListNode* fast = head; ListNode* slow = head; ListNode* pre = slow; fast = fast->next->next; while(fast != NULL){ pre = slow; slow = slow->next; fast = fast->next; } pre->next = NULL; return slow; }
//insert new node(root of the newly built subtree) to the frequency list voidInsertNode(ListNode* head, ListNode* p){ ListNode* cur = head; while(cur->next != NULL && cur->next->frequency > p->frequency){ cur = cur->next; } p->next = cur->next; cur->next = p; }