I'm having major trouble figuring out how to do this correctly. The output was until shortly riddles with errors. I managed to fix most of them but I'm still left with two, and likely a bunch of logic errors.
I also have trouble with my hash algorithm, so I have replaced it with simple temp code. The instructions for the correct one are:
The hash function to be used is h(k) = m(k · A mod 1) where
A = (√5 − 1)/2 and k · A mod 1 returns the fractional part of k · A.
I assume I've not implemented it properly.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define TABLE_SIZE 8
typedef struct stItem item;
struct stItem {
int key;
item *next;
};
void init(item * H[]) {
int i = 0;
for (i; i < TABLE_SIZE; i++)
H[i] = NULL;
}
int h(int k) {
// this does not work at all, currently using testcode
/*
int m = TABLE_SIZE;
int A = ( sqrt(5.0) - 1) / 2;
return m * (k * A % 1);
*/
return k % TABLE_SIZE;
}
void insert(int key, item * H[]) {
int keyHashed = h(key);
if (H[keyHashed] == NULL) {
item * temp = malloc(sizeof(item));
temp->key = key;
temp->next = NULL;
H[keyHashed] = temp;
free(temp);
}
else {
item * temp = malloc(sizeof(item));
temp->next = H[keyHashed]->next;
while (temp != NULL) {
temp = temp->next;
}
temp->key = key;
temp->next = NULL;
}
}
int search(int key, item * H[]) {
int keyHashed = h(key);
if (H[keyHashed] == NULL)
return -1;
else if (H[keyHashed]->key != key) {
item * temp = malloc(sizeof(item));
temp->next = H[keyHashed]->next;
while (temp->key != key && temp != NULL)
temp = temp->next;
if (temp->key == key) {
free(temp);
return keyHashed;
}
else {
free(temp);
return -1;
}
}
else
return keyHashed;
}
void printHash(item * H[]) {
printf("Table size: %d", TABLE_SIZE);
int i = 0;
for (i; i < TABLE_SIZE; i++) {
if (H[i] != NULL) {
printf("i: %d key: %d",i,H[i]->key);
if (H[i]->next != NULL) {
item * temp = malloc(sizeof(item));
temp->next = H[i]->next;
while (temp != NULL) {
printf(" -> %d", temp->key);
}
printf("\n");
}
else
printf("\n");
}
}
}
void test() {
// a)
int array[7] = {111,10112,1113,5568,63,1342,21231};
item *h[TABLE_SIZE];
init(h);
int i = 0;
for (i; i < 7; i++)
insert(array[i], h);
// b)
printHash(h);
// c)
printf("Search result for 1: %d", search(1, h));
printf("Search result for 10112: %d", search(10112, h));
printf("Search result for 1113: %d", search(1113, h));
printf("Search result for 5568: %d", search(5568, h));
printf("Search result for 337: %d", search(337, h));
}
int main() {
test();
}
EDIT: Thanks to user3386109's fix the code now compiles with no errors but what happens is that the command prompt just pops up with nothing displayed in it and nothing happening at all. It doesn't close either. Not even after several minutes of waiting.
EDIT2: After some more testing it looks to be hanging up on the insert-function. Nothing after the for-loop in test() is executed.
If I add this printf("init done %d", h[1]); to just after the init() in the test function, I get "init done 0" instead of "init done NULL", might that be one of the issues?
The structure definition is malformed. I would suggest the following
typedef struct stItem item;
struct stItem {
int key;
item *next;
};
Related
Im trying to write a very simple compressor in C and I ran into the error "Heap has been corrupted" in my code. I looked into it, and the error seems to come because of this line:
ptr = (char*)malloc(count * sizeof(char));
when i change the size from count to 1000 it works, and i tried debugging and seeing if there is a difference somewhere but i couldnt find it, i understand there might be somekind of an overflow but i dont understand why and whats the solution instead of just writing a big number to fix it
this is my code for now:
#include <stdio.h>
#include <stdlib.h>
errno_t err;
int count =0;
struct node {
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data) {
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
};
int* frequency(char* str) {
FILE* fptr;
err = fopen_s(&fptr, str, "r");
if (err != 0)
{
printf("The file wasn't opened\n");
exit(0);
}
int* ptr;
ptr = (int*)malloc(95 * sizeof(int));
if (ptr == NULL) {
printf("Error! memory not allocated.");
exit(0);
}
for (int i = 0; i < 95; i++) {
*(ptr + i) = 0;
}
char ch;
int index;
while ((ch = fgetc(fptr)) != EOF) {
index = (int)ch - 32;
(*(ptr+index))++;
}
err = fclose(fptr);
if (err != 0)
{
printf("The file wasn't closed\n");
exit(0);
}
for (int i = 0; i < 95; i++) {
printf("%d ", *(ptr+i));
}
return ptr;
}
void* letFrequency(int* occLet) {
for (int i = 0; i < 95; i++) // counts how many actually occur
{
if (*(occLet+i) > 0)
{
count++;
}
}
int* ptr;
ptr = (char*)malloc(count * sizeof(char));
if (ptr == NULL) {
printf("Error! memory not allocated.");
exit(0);
}
int max = 0;
int placement = 0;
for (int j = 0; j < count; j++) {
for (int i = 0; i < 95; i++) {
if (*(occLet+i) >= max)
{
max = *(occLet+i);
placement = i;
}
}
*(ptr+j) = (char)(placement + 32);
printf("%c", *(ptr +j));
*(occLet+placement) = 0;
max = 1;
}
return ptr;
}
void binaryMap(char* letFrq) {
struct node* rootzero = newNode(1);
struct node* rootone = newNode(0);
int leaveszero = 0;
int leavesone = 0;
if (count % 2 == 0) {
leavesone = count / 2;
leaveszero = count / 2;
}
else
{
leavesone = count / 2;
leaveszero = count / 2 + 1;
printf("%d", leaveszero);
}
}
int main() {
char str[70];
printf("Enter the name of the text file you want to compress: ");
scanf_s("%s", str, sizeof(str));
int* ptrFr;
char* ptrLetFr;
ptrFr = frequency(str);
ptrLetFr = letFrequency(ptrFr);
free(ptrFr);
binaryMap(ptrLetFr);
}
The lines;
int* ptr;
ptr = (char*)malloc( count * sizeof(char));
are clearly erroneous. If you intended count integers, then sizeof(char) will allocate too small a block. I suggest as a habit:
Do not cast void* - just assign it.
Use sizeof the object pointed to, not an explicit type.
Do not leave dangling pointers unnecessary.
To that end:
int* ptr = malloc(count * sizeof(*ptr) ) ;
Or if it were _intended to be char* then:
char* ptr = malloc(count * sizeof(*ptr) ) ;
Note how minimal the change is - you don't have to correct it three different places.
I have a project, that generates a Linked List, delete them and show this at the User. Now, I want to sort the list.
My struct:
typedef struct YugiohCard {
char Name[100];
char CardType[20];
int Level;
int Rank;
int PendulumStage;
int Link;
int ATK;
int DEF;
char Property[20];
char MonsterType[40];
char CardType2[30];
char Description[500];
struct YugiohCard* pNext;
struct YugiohCard* pPrev;
} struYugiohCard;
When the User says: "CardType2 Ascending" then the program sort the list by CardType2 and Ascending.
In this case alphematicaly. It is also possible to sort by the other struct contents(Monstertyp, ATK, DEF, etc.). Ascending or Descending.
How can I do it without things from C++?
Sorry for my bad English. I'm not very well at this.
Edit:
Here is my complete Code:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "time.h"
typedef struct YugiohCard {
char Name[100];
char CardType[20];
int Level;
int Rank;
int PendulumStage;
int Link;
int ATK;
int DEF;
char Property[20];
char MonsterType[40];
char CardType2[30];
char Description[500];
struct YugiohCard* pNext;
struct YugiohCard* pPrev;
} struYugiohCard;
bool OutputList(struYugiohCard* pStart)
{
int count = 0;
struYugiohCard* current = pStart; // Initialize current
while (current != NULL)
{
count++;
current = current->pNext;
}
char answer[265];
int CountetCardsThatWillBeOutputet;
printf("How many Yugioh cards would you like to spend? 0 means all,
otherwise the number counts. Number of elements in list: %i Input:",
count);
fgets(answer, 265, stdin);
CountetCardsThatWillBeOutputet = atoi(answer);
int countOutputetCards = 0;
if (CountetCardsThatWillBeOutputet > count)
{
printf("Please enter a correct number!");
system("pause");
return false;
}
else if (CountetCardsThatWillBeOutputet == 0)
{
CountetCardsThatWillBeOutputet = count;
}
system("cls");
printf("%10s %20s %10s %10s %20s %10s %10s %10s %20s %20s %20s %20s\n",
"Name", "CardType", "Level", "Rank", "PendulumStage", "Link", "ATK",
"DEF", "Property", "MonsterType", "CardType2", "Description");
for (struYugiohCard* pOut = pStart; pOut != NULL; pOut = pOut->pNext)
{
printf("%10s %20s %10i %10i %20i %10i %10i %10i %20s %20s %20s
%20s\n", pOut->Name, pOut->CardType, pOut->Level, pOut-
>Rank, pOut->PendelumStage, pOut->Link, pOut->ATK, pOut->DEF,
pOut->Property, pOut->MonsterType, pOut->CardType2, pOut-
>Description);
countOutputetCards++;
if (countOutputetCards == CountetCardsThatWillBeOutputet )
{
break;
}
}
system("pause");
}
void DeleteList(struYugiohCard** head_ref)
{
struct YugiohCard* prev = *head_ref;
while (*head_ref)
{
*head_ref = (*head_ref)->pNext;
free(prev);
prev = *head_ref;
}
}
struYugiohCard* CreateList()
{
system("cls");
char answer[265];
int countedCards;
printf("\nHow many Yugioh cards would you like to create? Please enter
only enter numbers, otherwise you'll crash.");
fgets(answer, 265, stdin);
countedCards = atoi(answer);
struYugiohCard* pFirst = NULL;
for (int i = 0; i < countedCards; i++)
{
struYugiohCard* pNew =
(struYugiohCard*)malloc(sizeof(struYugiohCard));
if (pNew == NULL) break;
pNew->Name[0] = 'A' + rand() % 26;
pNew->Name[1] = '\0';
pNew->CardType[0] = 'A' + rand() % 26;
pNew->CardType[1] = '\0';
pNew->Level = 1 + rand() % 12;
pNew->Rank = 1 + rand() % 13;
pNew->PendulumStage = 1 + rand() % 12;
pNew->Link = 1 + rand() % 8;
pNew->ATK = rand() % 10001;
pNew->DEF = rand() % 10001;
pNew->Property[0] = 'A' + rand() % 26;
pNew->Property[1] = '\0';
pNew->MonsterType[0] = 'A' + rand() % 26;
pNew->MonsterType[1] = '\0';
pNew->CardType2[0] = 'A' + rand() % 26;
pNew->CardType2[1] = '\0';
pNew->Description[0] = 'A' + rand() % 26;
pNew->Description[1] = '\0';
if (pFirst != NULL)
{
pNew->pNext = pFirst;
}
else
{
pNew->pNext = NULL;
}
pFirst = pNew;
}
return pFirst;
}
int main()
{
struYugiohCard* pStart = NULL;
printf("\nIMPORTANT: Please maximize the window, otherwise it will not
represents everything correctly.");
do
{
system("cls");
printf("\nDo you want to create a Yugioh card list (YKE) that
Delete Yugioh card list(YKL), a single Yugioh card
delete(EYKL), sort the list(YKS), the Yugioh-
Output card list(YKA) or the program
close(Prsc):");
char answer[265];
fgets(answer, 265, stdin);
if (strcmp(answer, "YKE\n") == 0)
{
pStart = CreateList();
}
else if (strcmp(answer, "YKS\n") == 0)
{
//SortList(pStart);
}
else if (strcmp(answer, "EYKL\n") == 0)
{
//DeleteOneCard(pStart);
}
else if (strcmp(answer, "YKL\n") == 0)
{
DeleteList(&pStart);
}
else if (strcmp(answer, "YKA\n") == 0)
{
OutputList(pStart);
}
else if (strcmp(answer, "Prsc\n") == 0)
{
return 0;
}
else
{
printf("Please enter a shortcut!");
}
} while (true);
}
Make an array
Create an array of pointers. Have the array members point to the list members. The code uses a little trick so that the last element of the list will get its pNext member set to NULL properly.
struYugiohCard *arr[size_of_list + 1], **arrp = arr, *iter;
for (iter = list; iter != NULL; iter = iter->pNext) {
*arrp++ = iter;
}
*arrp = NULL;
Write a comparison function, then call qsort
There are lots of examples of how to use qsort, but the trick is writing an appropriate comparison function. It is important to realize that qsort will pass in the addresses of the array elements it is comparing. Since our array contains pointers, the comparison function will be passed pointers to pointers.
In your case, I would guess CardType2 Ascending might be implemented using strcmp, which has return values that match up to what qsort expects (negative if a is less than b, positive if greater than, zero if equal):
int cmp_CardType2_Ascending(const void *a, const void *b) {
const struYugiohCard * const *aa = a;
const struYugiohCard * const *bb = b;
return strcmp((*aa)->CardType2, (*bb)->CardType2);
}
//...
qsort(arr, size_of_list, sizeof(*arr), cmp_CardType2_Ascending);
Fix your list
Now, rewire your list to the sorted order. Notice the pNext of the last iteration is making use of the extra array member which was set to NULL.
arr[0]->pNext = arr[1];
arr[0]->pPrev = NULL;
for (int i = 1; i < size_of_list; ++i) {
arr[i]->pNext = arr[i+1];
arr[i]->pPrev = arr[i-1];
}
list = arr[0];
Putting it into a function
Below is a function that puts most of this logic into a single function. The sorting function is passed in, and this function is then passed to qsort.
void sort_YugiohCard(struYugiohCard **pList, int size_of_list,
int (*By)(const void *, const void *)) {
if (size_of_list == 0) return;
struYugiohCard *list = *pList;
struYugiohCard *arr[size_of_list+1], **arrp = arr, *iter;
for (iter = list; iter != NULL; iter = iter->pNext)
*arrp++ = iter;
*arrp = NULL;
qsort(arr, size_of_list, sizeof(*arr), By);
arr[0]->pNext = arr[1];
arr[0]->pPrev = NULL;
for (int i = 1; i < size_of_list; ++i) {
arr[i]->pNext = arr[i+1];
arr[i]->pPrev = arr[i-1];
}
list = arr[0];
*pList = list;
}
Then, you can call this function like this:
sort_YugiohCard(&list, 5, cmp_CardType2_Ascending);
And the list will be returned in sorted order as determined by the comparison function.
A demo
Try it online!
I am coding a deck and when I try to run it to assign values to each node and then print it, I get this error when going through the addcard method:
exception thrown: write access violation. p was nullptr.
Why is it not okay to work with NULL?
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#define RAND_MAX 51
typedef struct card_s {
char suit;
int face;
struct card_s *next, *previous;
} card;
void addcard(card *p, card **hl, card **hr, int i, char c) {
card *temp;
temp = (card *)malloc(sizeof(card));
temp->face = i;
temp->suit = c;
if (*hl == NULL) {
temp->previous = NULL;
temp->next = NULL;
*hl = temp;
*hr = temp;
} else if (p == NULL) {
temp->previous = p;
temp->next = NULL;
p->next = temp;
*hr = temp;
} else {
temp->next = p->next;
temp->previous = p;
p->next = temp;
temp->next->previous = temp;
}
}
void delectecard(card *p, card **hl, card **hr) {
if (p == *hl) {
*hl = p->next;
} else {
p->previous->next = p->next;
}
if (p == *hr) {
*hr = p->previous;
} else {
p->next->previous = p->previous;
}
free(p);
}
void createdeck(card *p, card **hl, card **hr) {
int i = 1;
int j;
while (i <= 13) {
j = 1;
while (j <= 4) {
if (j == 1)
addcard(p, hl, hr, i, 'S');
if (j == 2)
addcard(p, hl, hr, i, 'H');
if (j == 3)
addcard(p, hl, hr, i, 'D');
if (j == 4)
addcard(p, hl, hr, i, 'C');
}
}
}
void printdeck(card *currentNode) {
while (currentNode != NULL) {
printf("Face: %d, Suit: %c\n", currentNode->face, currentNode->suit);
currentNode = currentNode->next;
}
}
int main(void) {
card *headl = NULL, *headr = NULL;
createdeck(headr, &headl, &headr);
printdeck(headl);
}
In your function addcard(); You check to see if p is NULL:
else if (p == NULL) {
temp->previous = p;
temp->next = NULL;
p->next = temp;
*hr = temp;
Then, you try and access the NULL p in the line:
p->next = temp;
How can you access something which doesn't exist! That is where the null pointer exception error is coming from.
Maybe you want to initialize p to be a new card_s struct like you did with temp. Using calloc will initialize both the next and previous pointers to NULL.
p = calloc(1, sizeof(card));
You are stuck in an infinite loop after this, specifically in your createdeck function. The reason is that you never increment or decrement either j or i so the loop never breaks.
while (i <= 13) {
j = 1;
while (j <= 4) {
if (j == 1)
addcard(p, hl, hr, i, 'S');
if (j == 2)
addcard(p, hl, hr, i, 'H');
if (j == 3)
addcard(p, hl, hr, i, 'D');
if (j == 4)
addcard(p, hl, hr, i, 'C');
}
}
I'm having issues to correct my code so that it works as I want it.
I have three arrays given in this example:
char arr[MAX_ELEMENTS][MAX_LENGTH] = {"ABS","ABS","ABS","ACT","ACT","PPB","PPB","QQQ","QQQ"};
char race[MAX_ELEMENTS][MAX_LENGTH] = {"PARI", "PARI", "LOND", "PARI", "PARI", "CYKA", "LOND", "CYKA", "PARI"};
int freq[MAX_ELEMENTS];
I wish to create a function that can count the amount of occurrences of string elements in arr[] and store them in freq[]. Apart from that I also wish to know in what race[] there have been the most occurrences of given arr[].
To demonstrate this here is an example of what output I wish to receive when the function works:
In Race [PARI] the highest occurence was [ABS] with 3 occurences!
In Race [LOND] the highest occurence was [ACT] with 1 occurences!
.....
Currently, I am able to count the occurrences of arr[] in freq[] but I can't associate them with their respective race[] and give that output..
for(i=0; i<size; i++)
{
count = 1;
for(j=i+1; j<size; j++)
{
/* If duplicate element is found */
if(strcmp(arr[i], arr[j])==0)
{
count++;
/* Make sure not to count frequency of same element again */
freq[j] = 0;
}
}
/* If frequency of current element is not counted */
if(freq[i] != 0)
{
freq[i] = count;
}
}
Giving me currently :
ABS occurs 3 times.
ACT occurs 2 times.
etc. etc...
But I don't know how I can associate them with the race[] and only count them if a given race.
You probably have to use struct here to format your data.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define true 1
#define len 100
#define elms 10
struct NODE;
#define Node struct NODE
struct NODE {
unsigned long int val;
int count;
char name[len];
Node *left;
Node *right;
};
Node * makeNode(char * str, unsigned long int val){
Node * tmp = (Node *)malloc(sizeof(Node));
strcpy(tmp->name, str);
tmp->val = val;
tmp->left = NULL;
tmp->right = NULL;
tmp->count = 1;
return tmp;
}
unsigned long int getHash(char * name){
int prime = 19;
int i = 0;
unsigned long int val = 0;
while(name[i]!='\0'){
val += (name[i] * pow(prime, i) );
++i;
}
return val;
}
void insert(Node * root, char * name){
Node * newnode;
int val = getHash(name);
Node * tmp = root;
while(tmp != NULL) {
if ( tmp->val == val){
tmp->count += 1;
break;
}
if (val > tmp->val){
if( tmp->right != NULL)
tmp = tmp->right;
else{
tmp->right = makeNode(name, val);
break;
}
}else {
if( tmp->left != NULL)
tmp = tmp->left;
else{
tmp -> left = makeNode(name, val);
break;
}
}
}
}
Node * find(Node * root, char * name){
int val = getHash(name);
Node * tmp = root;
while(tmp != NULL){
if(tmp -> val == val){
return tmp;
}else if (val > tmp->val){
tmp = tmp->right;
}else{
tmp = tmp->left;
}
}
return NULL;
}
struct Race {
char name[len];
char elements[elms][len];
};
char arr[elms][len] = {"ABS","ABS","ABS","ACT","ACT","PPB","PPB","QQQ","QQQ"};
char race[elms][len] = {"PARI", "PARI", "LOND", "PARI", "PARI", "CYKA", "LOND", "CYKA", "PARI"};
int freq[elms];
void copyArray(char dest[elms][len], char src[elms][len] ){
int i = 0;
while(strlen(src[i]) > 0){
strcpy(dest[i],src[i]);
++i;
}
}
int main(){
Node * root = makeNode("root", 0);
int i = 0;
while(strlen(arr[i]) > 0){
insert(root,arr[i]);
++i;
}
i = 0;
while(strlen(arr[i]) > 0){
Node * r = find(root,arr[i]);
printf("found %s, count = %ld\n", r->name, r->count);
++i;
}
// make representation of race
struct Race r1, r2;
strcpy(r1.name, "PARI");
{
char tmp[elms][len] = { "ABS", "PPB", "QQQ" };
copyArray(r1.elements, tmp);
}
strcpy(r2.name, "LOND");
{
char tmp[elms][len] = { "ACT" };
copyArray(r2.elements, tmp);
}
struct Race races[2] = {r1, r2};
i = 0;
while(i < 2){
struct Race * current = &races[i];
printf("for %s", current->name);
Node * max = NULL;
int m = -1;
int j = 0;
while(strlen(current->elements[j]) > 0){
Node * tmp = find(root, current->elements[j]);
if( tmp != NULL && tmp->count > m) {
max = tmp;
m = tmp->count;
}
++j;
}
if (max != NULL){
printf(" max is %s : %d\n", max->name, max->count);
}else{
printf(" max is None\n");
}
++i;
}
return 0;
}
Basically you have to format you data, and specify link between them. Here I used Binary tree and Rabin karp hashing technique to store data efficiently.
Binary tree is best way to solve counting problem, since the search operation fairly cheap. and Rabin karp hashing technique will avoid string comparison every time.
And I create a struct called Race to store all related elements of that race. so the algorithm is going to be.
let arr be array of elements
let races be array of races
for each race in races
define related element
#find occurrence now
#Binary tree will increment count if element already exist.
let binary_tree be a Binary Tree
for each element in arr
add element to binary_tree
# now we have all the elements with it's count
# let's iterate through races now
for each race in races
m = null
for element in race.elements
node = find_element_in_binary_tree(element)
if node is not null
m = max(m, node)
if m is not null then
print m
else
print not found
First, initializations, note the []s
char arr[][MAX_LENGTH] = {"ABS","ABS","ABS","ACT","ACT","PPB","PPB","QQQ","QQQ"};
char race[][MAX_LENGTH] = {"PARI","PARI","LOND","PARI","PARI","CYKA","LOND","CYKA","PARI"};
int freq[MAX_ELEMENTS];
int n = sizeof(arr)/sizeof(*arr); // get actual number of used items
int i,j;
int max = 0; // init max to 0
The main loop goes through arr and race, and whenever a dupe is found at [j] (after [i]), "invalidate" the dupe ("already processed") by setting its first char to 0 (empty string).
Note that j starts from i and not i+1 to ensure freq is at least 1, even for the first non-dupes items.
for(i=0 ; i<n ; i++) {
freq[i] = 0; // ensure freq is 0 for any item
if ( ! *arr[i]) continue; // skip already processed items
for(j=i ; j<n ; j++) { // j=i, not i+1!
if (!strcmp(arr[i],arr[j]) && !strcmp(race[i],race[j])) {
freq[i]++; // update max if necessary
if (freq[i] > max) max = freq[i];
if (j > i) *arr[j] = 0; // invalidate that arr element
}
}
}
Finally display the max appearances, including ties
printf("Items at max=%d:\n", max);
for(i=0 ; i<n ; i++) {
if (freq[i] == max) { // skipped items are never displayed (max cannot be 0)
printf("%s / %s\n", arr[i],race[i]);
}
}
(no need to check for "invalidation" as max will be >0, and all invalidated items have freq[i] == 0)
I got a segmentation fault 11 with my strncmp() function. I knew where the bug is, but have no idea what cause it.
Here is what i am trying to solve. I input a txt file that contains lots of words. Then i need to compute the frequency of each word and then sort the words. At last, output the sorted words with their frequencies. So since it is a C program, i use linked list to store the word and the frequency. Adding the word to linked list and Count the freq of each word both work well. The bug happens in my quick sort, which i use to sort words.
My quick sort:
struct node *quick_sort(struct node *head, int l, int r){
int i, j;
int jval;
int pivot;
int min;
char* test1;
char* test2;
i = l + 1;
if (l + 1 < r) {
test1 = get_char(head, l);
pivot = get_freq(head, l);
for (j = l + 1; j <= r; j++) {
jval = get_freq(head, j);
test2 = get_char(head, j);
printf("test 1: %s test 2: %s\n",test1,test2);
min = strlen(test1) < strlen(test2) ? strlen(test1) : strlen(test2);
printf("Length 1 :%ld Length 2: %ld Max is: %d\n",strlen(test1),strlen(test2), min);
// HERE is where the bug is
if (strncmp(test2,test1,min)<0 && jval != -1) {
swap(head, i, j);
i++;
}
}
swap(head, i - 1, l);
quick_sort(head, l, i);
quick_sort(head, i, r);
}
return head;
}
And other associated functions:
int get_freq(struct node *head, int l){
while(head && l) {
head = head->next;
l--;
}
if (head != NULL)
return head->freq;
else
return -1;
}
void swap(struct node *head, int i, int j){
struct node *tmp = head;
int tmpival;
int tmpjval;
char* tmpiStr;
char* tmpjStr;
int ti = i;
while(tmp && i) {
i--;
tmp = tmp->next;
}
tmpival = tmp->freq;
tmpiStr = tmp->str;
tmp = head;
while(tmp && j) {
j--;
tmp = tmp->next;
}
tmpjval = tmp->freq;
tmpjStr = tmp->str;
tmp->freq = tmpival;
tmp->str = tmpiStr;
tmp = head;
i = ti;
while(tmp && i) {
i--;
tmp = tmp->next;
}
tmp->freq = tmpjval;
tmp->str = tmpjStr;
}
char* get_char(struct node *head, int l){
char* res;
while(head && l) {
head = head->next;
l--;
}
if (head != NULL){
char * arr = head->str;
return arr;
}
else
return res;
}
If i change the number of min in the strncmp(), sometimes the program works. I have no idea what the wrong is.
Thanks in advance.
You never assign to the variable declared in get_char function on this line
char* res;
Segmentation fault 11 error is generally getting called when the program accesses memory it has not allocated. In your case you are probably trying to compare a string and some random place in memory.