create tree with static and pre-determined elements in c - c

I have a problem that I decided to solve using binary tree, however:
I can't think of a way to fill the tree with predetermined elements so that it looks like the following in the image
I used a vector as follows, and then I inserted it into the tree, I don't know if I just leave it in the order the tree will be assembled as in the image, but what I did was the following:
char* dict[] = {
"Mamifero","aves","repteis",
"quadrupede", "bipede", "voadores", "aquaticos",
"nao-voadoras", "nadadoras", "de rapina", "com casco", "carnivoros", "sem patas",
"carnivoro", "herbivoro", "onivoro", "afrutifero", "tropical", "polar",
"leao", "cavalo", "homem", "macaco", "morcego", "baleia", "avestruz", "pinguim",
"pato", "aguia", "tartaruga", "crocodilo", "cobra"
};
typedef struct Animal *animalptr;
typedef struct Animal {
char *str;
animalptr left, right;
} Animal;
typedef int (*compare)(const char*, const char*);
void insert (char* key, Animal** leaf, compare cmp) {
int res;
if (*leaf == NULL) {
*leaf = (Animal*) malloc(sizeof(Animal));
(*leaf)->str = malloc(strlen(key) + 1);
strcpy ((*leaf)->str, key);
(*leaf)->left = NULL;
(*leaf)->right = NULL;
// printf("\nnew node for %s", key);
}
else {
// printf("%d\n", res);
res = cmp (key, (*leaf)->str);
if (res < 0) insert (key, &(*leaf)->left, cmp);
else if (res > 0) insert (key, &(*leaf)->right, cmp);
else printf("key '%s' already in tree\n", key);
}
}
int cmpStr (const char* a, const char* b) {
// printf("a = %d\n b = %d", strlen(a), strlen(b));
return (strcmp (a,b));
}
fill it in as follows:
int main () {
Animal *parent = NULL;
char q;
// printf("%ld\n", sizeof(dict));
// insert(dict[0], &parent, (compare)cmpStr);
// printTree(parent);
for (int i = 0; i < NUM_NODES; i++) {
insert(dict[i], &parent, (compare)cmpStr);
}
printf ("%s", search(parent, "", (compare)cmpStr)->str);
// printTree(parent);
// do {
// // scanf("%c", &q);
// // printf("%s?", dict[rand() % 32]);
// // }while (q != 'q');
return 0;
}
so now my saga would be how to apply some weight to each word, to direct it to one side or the other, the exercise I'm trying to solve in this way is this:
Build a program that is able to conclude which of the following animals
was chosen through questions and answers. Possible animals: lion, horse, man,
monkey, whale, ostrich, penguin, duck, eagle, turtle, crocodile and snake.
teste

Each word can be marked with a parent-child relationship, weight = parent's weight + own serial number under the same parent, like
Mamifero => "1"
aves => "2"
repteis => "3"
bipede => "12" (second child under Mamifero)
afrutifero => "122" (second child under bipede)
If the prefixes are the same, it means that there is a parent-child relationship, insert it on the right side of the tree, otherwise insert it on the left side of the tree
Please see the modified code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct words {
char *weight;
char *name;
} Words;
Words dict[] = {
{"1", "Mamifero"},
{"2", "aves"},
{"3", "repteis"},
{"11", "quadrupede"},
{"12", "bipede"},
{"13", "voadores"},
{"14", "aquaticos"},
{"21", "nao-voadoras"},
{"22", "nadadoras"},
{"23", "de rapina"},
{"31", "com casco"},
{"32", "carnivoros"},
{"33", "sem patas"},
{"111", "carnivoro"},
{"112", "herbivoro"},
{"121", "onivoro"},
{"122", "afrutifero"},
{"211", "tropical"},
{"212", "polar"},
{"1111", "leao"},
{"1121", "cavalo"},
{"1211", "homem"},
{"1221", "macaco"},
{"131", "morcego"},
{"141", "baleia"},
{"2111", "avestruz"},
{"2121", "pinguim"},
{"221", "pato"},
{"231", "aguia"},
{"311", "tartaruga"},
{"321", "crocodilo"},
{"331", "cobra"}
};
#define NUM_NODES (sizeof(dict)/sizeof(*dict))
typedef struct Animal *animalptr;
typedef struct Animal {
char *weight;
char *str;
animalptr left, right;
} Animal;
typedef int (*compare)(const char *, const char *);
void insert(Words *key, Animal **leaf, compare cmp)
{
int res;
if (*leaf == NULL) {
*leaf = (Animal *) malloc(sizeof(Animal));
(*leaf)->str = strdup(key->name);
(*leaf)->weight = strdup(key->weight);
(*leaf)->left = NULL;
(*leaf)->right = NULL;
} else {
res = cmp(key->weight, (*leaf)->weight);
if (res < 0) insert(key, &(*leaf)->left, cmp);
else if (res > 0) insert(key, &(*leaf)->right, cmp);
else printf("key '%s' already in tree\n", key->name);
}
}
int cmpStr(const char *a, const char *b)
{
if (strcmp(a, b) == 0)
return 0;
// If the prefixes are the same, it means a is a child of b, insert on the right
if (strncmp(a, b, strlen(b)) == 0)
return 1;
// Otherwise insert left
return -1;
}
char *search(Animal *leaf)
{
char *ret = "";
char buf[16];
while (leaf) {
if (!leaf->right && !leaf->left) {
ret = leaf->str;
break;
}
// guess
printf("É %s (yes,no): ", leaf->str);
fgets(buf, sizeof(buf), stdin);
if ((*buf == 'q') || (*buf == 'Q'))
break;
// If yes, go to the right
if ((*buf == 'y') || (*buf == 'Y'))
leaf = leaf->right;
// Otherwise, left
else if ((*buf == 'n') || (*buf == 'N'))
leaf = leaf->left;
}
return ret;
}
int main()
{
Animal *parent = NULL;
for (int i = 0; i < NUM_NODES; i++) {
insert(&dict[i], &parent, (compare)cmpStr);
}
printf("%s\n", search(parent));
return 0;
}

Related

Convert sha256 to smaller uint type

I'm implementing a hash table in c and I chose as key the sha256 hash of the file that I need to store. The problem is that I need to convert the key to a reusable index to insert in the hash table. I tought to rehash the key, but this way I would increase the possibility of overlapping values. Is there a way to use this hash as the key to the table?
The sha256 is store as a BYTE[32] or can be converted as a sting
void* ht_get(ht* table, const char *key) {
size_t index = magicfunction(key);
while (table->entries[index].key != NULL) {
if (strcmp(key, table->entries[index].key) == 0) {
// Found key, return value.
return table->entries[index].value;
}
// Key wasn't in this slot, move to next (linear probing).
index++;
if (index >= table->capacity) {
index = 0;
}
}
return NULL;
}
When I need an absolute minimalist hashmap in C, I usually end up doing something like that:
#include <stddef.h>
#include <string.h>
#include <stdio.h>
struct hmap_entry {
const char *key;
/* Or in your case: */
/* char key[SIZE_OF_SHA256]; */
void *payload;
};
#define HMAP_ENTRY_CNT 256
struct hmap {
struct hmap_entry entries[HMAP_ENTRY_CNT];
};
static size_t str_hash(const char *s) {
size_t hash = 0;
while(*s) {
hash = (hash << 7) ^ (size_t)*s++ ^ hash;
}
return hash;
}
static struct hmap_entry *hmap_search_entry(struct hmap *map
, const char *key)
{
size_t hash = str_hash(key);
/* Or in your case: */
/* size_t hash = 0; memcpy(&hash, key, sizeof(key)) */
hash = hash % HMAP_ENTRY_CNT;
struct hmap_entry *e = NULL;
while(1) {
e = map->entries + hash;
if(e->key == NULL
|| strcmp(key, e->key) == 0) {
break;
}
/* Or in your case: */
/* if(e->key == NULL
|| memcmp(key, e->key, SIZE_OF_SHA256) == 0) {
break;
}
*/
hash = (hash + 1) % HMAP_ENTRY_CNT;
}
return e;
}
And this is how I use it:
int main()
{
struct hmap_entry *e;
struct hmap map = {};
/* insert foo */
e = hmap_search_entry(&map, "foo");
e->key = "foo";
e->payload = "hello world";
/* insert bar */
e = hmap_search_entry(&map, "bar");
e->key = "bar";
e->payload = "Something else";
/* get foo */
e = hmap_search_entry(&map, "foo");
if(e->key) {
printf("Value of \"%s\" is \"%s\"\n", e->key, (const char *)e->payload);
}
else {
printf("Not found!\n");
}
/* get bar */
e = hmap_search_entry(&map, "bar");
if(e->key) {
printf("Value of \"%s\" is \"%s\"\n", e->key, (const char *)e->payload);
}
else {
printf("Not found!\n");
}
/* get test */
e = hmap_search_entry(&map, "test");
if(e->key) {
printf("Value of \"%s\" is \"%s\"\n", e->key, (const char *)e->payload);
}
else {
printf("Not found!\n");
}
return 0;
}

CS50 "Speller": Problem with dictionary file

I'm working on CS59 Week 5's "Speller" assignment. Currently, I'm trying to write the unload function with a test dictionary file on my desktop. But for some reason, line 91 (while(!feof(dictionary))) is returning an error:
"request for member '_flag' in something not a structure or a union"
I'm working from a new computer. My previous computer did not return this error. Can someone please explain what's going on here and how I can fix it? Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
unsigned int HASH_MAX = 50;
unsigned int LENGTH = 20;
unsigned int hash (const char *word);
bool load(char *dictionary);
bool check (char *word);
bool unload (void);
void print (void);
typedef struct _node
{
char *word[20];
struct _node *next;
} node;
node *HASH_TABLE[50];
int main(int argc, char *argv[])
{
FILE *dictionary = fopen("C:/Users/aaron/Desktop/Dictionary.txt", "r");
if(!dictionary)
{
printf("FILE NOT FOUND\n");
return 1;
}
if (load(dictionary))
{
// print "LIST (number): {(name, address), ...}\n
print();
}
int lois = hash("Lois");
printf("\n%s\n", HASH_TABLE[lois]->word);
if (check("StEwIe"))
{
printf("STEWIE found\n");
}
else if (!check("StEwIe"))
{
printf("STEWIE not found\n");
}
if (check("Tron"))
{
printf("TRON found\n");
}
else if (!check("Tron"))
{
printf("TRON not found\n");
}
if (unload())
{
print();
}
}
unsigned int hash(const char *word) // hash code function (FIND A BETTER ALGORITHM)
{
char word_conv[LENGTH + 1]; // store converted word for uniform key
unsigned int code = 0; // hash code
for (int i = 0; i < strlen(word); i++) // set all letters in the word to lower case
{
word_conv[i] = tolower(word[i]);
}
for (int j = 0; j < strlen(word_conv); j++) // for all letters in converted word, add ascii value to code and multiply by 3
{
code += word_conv[j];
code *= 3;
}
code = code % HASH_MAX; // set code to remainder of current code divided by maximum hash table size
return code;
}
bool load (char *dictionary)
{
char word[LENGTH+1];
while(!feof(dictionary))
{
fscanf(dictionary, "%s", word);
node *new_n = malloc(sizeof(node));
strcpy(new_n->word, word);
new_n->next = NULL;
unsigned int code = hash(word);
if (HASH_TABLE[code] == NULL)
{
HASH_TABLE[code] = new_n;
}
else if (HASH_TABLE[code] != NULL)
{
node *trav = HASH_TABLE[code];
while (trav->next != NULL)
{
trav = trav->next;
}
if (trav->next == NULL)
{
trav->next = new_n;
}
}
}
return true;
}
bool check (char *word)
{
unsigned int code = hash(word);
node *check = malloc(sizeof(node));
check = HASH_TABLE[code];
while (check != NULL)
{
if (strcasecmp(check->word, word) == 0)
return true;
}
if (check == NULL)
return false;
}
bool unload (void)
{
for (int i = 0; i < HASH_MAX; i++)
{
while (strcmp(HASH_TABLE[i]->word, "FREE" != 0)
{
node *curr = HASH_TABLE[i];
node *prev = NULL;
while (curr != NULL)
{
prev = curr;
curr = curr->next;
}
strcpy(prev->word, "FREE");
}
}
return true; // freed successfully
}
void print (void)
{
for (int i = 0; i < HASH_MAX; i++)
{
node *check = HASH_TABLE[i];
printf("LIST %02d: {", i);
while (check != NULL)
{
printf("%s, ", check->word);
check = check->next;
}
printf("}\n");
}
}

When passing through void pointer values changing and getting segmentation fault?

Background: Creating a hash table library which allows 3 different implementations 2 which solve collisions with open addressing (one being of fixed size and the other dynamic) and a 3rd which uses separate chaining with linked lists.
Background to the Problem: Started by creating the fixed size open addressing. At first this worked perfectly, however, had to make some changes to to adapt the library to cater for the other two implementations. For this I needed to record the size of the list (used structures) and void pointers as structures for the open addressing and separate chaining are different.
The Actual Problem: For some reason my save function is no longer working while debugging I've noticed two issues which I cannot discern the cause of:
passing a structure through a void pointer to this function the
values change (please note that this only happens in this functions
and while I uses the same method in the rest of the functions I do
not get the same problem)
I get a segmentation fault on an fprintf statement (that previous to the above mentioned amendments was working.
The Save Function in Question:
void hashtbl_savetofile(void* hashtbl, const char *path){
Hashtbl *temp;
temp = hashtbl;
int i;
FILE *f = fopen(path, "w");
if (f == NULL){
printf("FILE NOT FOUND!");
}else {
for(i = 0; i<SIZE; i++) {
fprintf(f, "%s", temp->data[i].subscript); // segmentation fault occurs
fprintf(f, "%s", temp->data[i].value);
}
fclose(f);
free(temp);
}
}
The Header File:
#ifndef TASK2_HASHTABLE_H
#define TASK2_HASHTABLE_H
#define SIZE 20
#define MAX_LENGTH 100
#define INIT_SIZE 64000
typedef struct Data_struct{
char *subscript;
char *value;
} Data;
typedef struct Hashtbls{
int numberOfEntries;
int allocatedEntries;
Data *data;
}Hashtbl;
int hashtbl_hashKey(void* hashtbl, const char *subscript ); // generates hash key for hash table
Data hashtbl_lookup(void* hashtbl, const char *subscript); //looks up and retrieves a given subscript and its value hash table
void* hashtbl_insert(void* hashtbl, const char *subscript, const char *value); //adds a given subscript and its value to hash table
void* hashtbl_delete(void* hashtbl, const char *subscript); //deletes a given subscript and its value from hash table
void hashtbl_savetofile(void* hashtbl, const char *path); //saves hash table to file
void* hashtbl_loadfromfile(void* hashtbl, const char *path); //loads hash table from file
void* hashtbl_init();
#endif //TASK2_HASHTABLE_H
The Test Driver:
#include <stdio.h>
#include "hashtable.h"
int main() {
Hashtbl *nums;
Data d;
nums = hashtbl_init();
hashtbl_insert(nums, "tea", "40c");
hashtbl_insert(nums, "coffee", "50c");
hashtbl_insert(nums, "biscuit", "4c");
hashtbl_insert(nums, "tart", "30c");
d = hashtbl_lookup(nums, "tart");
printf("%s", d.subscript);
printf("%s", d.value);
hashtbl_savetofile(nums, "test.txt");
}
The Implementation (for reference):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hashtable.h"
void* hashtbl_init(){ //initiates static 2D hash table when called
Hashtbl* hashtbl;
hashtbl = malloc(INIT_SIZE);
hashtbl->numberOfEntries = SIZE;
hashtbl->allocatedEntries = 0;
hashtbl->data = calloc(SIZE, sizeof(Data));
for(int i = 0; i<SIZE; i++){
hashtbl->data[i].subscript = malloc(MAX_LENGTH + 1);
strcpy(hashtbl->data[i].subscript, "ZERO\n");
hashtbl->data[i].value = malloc(MAX_LENGTH + 1);
strcpy(hashtbl->data[i].value, "ZERO\n");
}
return hashtbl;
}
Data hashtbl_lookup(void* hashtbl, const char *subscript){
Data item;
Hashtbl *temp;
temp = hashtbl;
int i = 0;
char *c;
c = malloc(MAX_LENGTH);
strcpy(c, subscript);
strct(c, "\n");
while (strcmp(temp->data[(hashtbl_hashKey(hashtbl, subscript)+i)].subscript, c) != 0){
i++;
if(hashtbl_hashKey(hashtbl, subscript)+i == temp->numberOfEntries){
break;
}
}
if(hashtbl_hashKey(hashtbl, subscript)+i != temp->numberOfEntries) {
if(strcmp(temp->data[(hashtbl_hashKey(hashtbl, subscript)+i)].subscript, c) == 0) {
item.subscript = temp->data[(hashtbl_hashKey(hashtbl, subscript) + i)].subscript;
item.value = temp->data[(hashtbl_hashKey(hashtbl, subscript) + i)].value;
}
}else{
for(i = 0; i<hashtbl_hashKey(hashtbl, subscript); i++){
if(strcmp(temp->data[i].subscript, c) == 0){
item.subscript = temp->data[i].subscript;
item.value = temp->data[i].value;
}
}
}
free(hashtbl);
free(c);
free(temp);
return item;//what if not found
}
void* hashtbl_insert(void* hashtbl, const char *subscript, const char *value){
Hashtbl *temp;
temp = hashtbl;
int i = 0;
char *c1;
c1 = malloc(MAX_LENGTH);
strcpy(c1, subscript);
strcat(c1, "\n");
char *c2;
c2 = malloc(MAX_LENGTH);
strcpy(c2, value);
strcat(c2, "\n");
if(temp->allocatedEntries == temp->numberOfEntries){
free(c1);
free(c2);
free(temp);
return hashtbl;
}else {
if (strcmp(temp->data[hashtbl_hashKey(hashtbl, subscript) + i].subscript, "ZERO\n") == 0) {
temp->data[hashtbl_hashKey(hashtbl, subscript)].subscript = c1;
temp->data[hashtbl_hashKey(hashtbl, subscript)].value = c2;
temp->allocatedEntries++;
} else {
while ((strcmp(temp->data[hashtbl_hashKey(hashtbl, subscript) + i].subscript, "ZERO\n") != 0)) {
i++;
if (hashtbl_hashKey(hashtbl, subscript) + i == temp->numberOfEntries) {
break;
}
}
if (hashtbl_hashKey(hashtbl, subscript) + i != temp->numberOfEntries) {
if ((strcmp(temp->data[hashtbl_hashKey(hashtbl, subscript) + i].subscript, "ZERO\n") == 0)) {
temp->data[(hashtbl_hashKey(hashtbl, subscript) + i)].subscript = c1;
temp->data[(hashtbl_hashKey(hashtbl, subscript) + i)].value = c2;
temp->allocatedEntries++;
}
} else {
for (i = 0; i < hashtbl_hashKey(hashtbl, subscript); i++) {
if (strcmp(temp->data[i].subscript, "ZERO\n") == 0) {
temp->data[i].subscript = c1;
temp->data[i].value = c2;
temp->allocatedEntries++;
}
}
}
}
free(c1);
free(c2);
return temp;
}
}
void* hashtbl_delete(void* hashtbl, const char *subscript){
Hashtbl *temp;
temp = hashtbl;
int i = 0;
int j = 0;
char *c;
c = malloc(MAX_LENGTH);
strcpy(c, subscript);
strcat(c, "\n");
while ((strcmp(temp->data[(hashtbl_hashKey(hashtbl, subscript)+i)].subscript, c) != 0)){
i++;
if((hashtbl_hashKey(hashtbl, subscript)+i) == temp->numberOfEntries) {
break;
}
}
if(hashtbl_hashKey(hashtbl, subscript)+i != temp->numberOfEntries) {
if(strcmp(temp->data[(hashtbl_hashKey(hashtbl, subscript)+i)].subscript, c) == 0) {
temp->data[(hashtbl_hashKey(hashtbl, subscript) + i)].subscript = "ZERO\n";
temp->data[(hashtbl_hashKey(hashtbl, subscript) + i)].value = "ZERO\n";
temp->allocatedEntries--;
}
}else{
for(j = 0; j<hashtbl_hashKey(hashtbl, subscript); j++){
if(strcmp(temp->data[j].subscript, c) == 0){
temp->data[j].subscript = "ZERO\n";
temp->data[j].value = "ZERO\n";
temp->allocatedEntries--;
}
}
}
hashtbl=temp;
free(c);
free(temp);
return hashtbl;
}
void hashtbl_savetofile(void* hashtbl, const char *path){
//provided above
}
void* hashtbl_loadfromfile(void* hashtbl, const char *path){
Hashtbl *temp;
temp = hashtbl;
temp->allocatedEntries=0;
int i = 0;
int j =0;
int c = 0;
char line[100];
char* string[temp->numberOfEntries];
FILE *f = fopen(path, "r");
if(f == NULL){
free(temp);
return NULL;
}else {
while(fgets(line, sizeof line, f)){
j++;
}
if((j/2)>temp->numberOfEntries){
free(temp);
return NULL;
}else {
while (fgets(line, sizeof line, f)) {
string[i] = malloc(strlen(line) + 1);
if (string[i] == NULL) {
break;
return NULL;
} else {
strcpy(string[i], line);
i++;
if (i == sizeof string / sizeof *string) {
break;
}
}
}
fclose(f);
for (i = 0; i < (SIZE * 2); i++) {
if (strlen(string[i]) >= MAX_LENGTH) {
char *k = realloc(temp->data[c].subscript, strlen(string[i]));
if (k == NULL) {
break;
return NULL;
} else {
temp->data[c].subscript = k;
strcpy(temp->data[c].subscript, string[i]);
if(temp->data[c].subscript != "ZERO\n"){
temp->allocatedEntries++;
}
i++;
c++;
}
} else {
temp->data[c].subscript = malloc(strlen(string[i]) + 1);
strcpy(temp->data[c].subscript, string[i]);
if(temp->data[c].subscript != "ZERO\n"){
temp->allocatedEntries++;
}
i++;
c++;
}
}
c = 0;
for (i = 1; i < (SIZE * 2); i++) {
if (strlen(string[i]) >= MAX_LENGTH) {
char *k = realloc(temp->data[c].value, strlen(string[i]));
if (k == NULL) {
break;
return NULL;
} else {
temp->data[c].value = k;
strcpy(temp->data[c].value, string[i]);
i++;
c++;
}
} else {
temp->data[c].value = malloc(strlen(string[i]) + 1);
strcpy(temp->data[c].value, string[i]);
i++;
c++;
}
}
hashtbl=temp;
free(temp);
free(string);
return hashtbl;
}
}
}
int hashtbl_hashKey(void* hashtbl, const char *subscript){
int i;
int h = 0;
for(i = 0; subscript[i]; i++){
h += subscript[i];
}
h = h%SIZE;
return h;
}

Array of singly linked list won't delete node

I have an array[4] where each index of the array has a singly linked list that holds the following information: name, size. The switch statement controls what index the information will go into according to the size of the party.
Problem: When trying to delete a node according to the size (user inputs) the node will not delete.
I know that all of the cases of deletion have the proper syntax but I cannot figure out why my node will not delete. Appreciate any help.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
char name[20];
int size;
struct node *next;
}node;
node* head[4]={NULL,NULL,NULL,NULL};
node* tail[4]={NULL,NULL,NULL,NULL};
//
// proto's
//
void add_party(int, char name[], int size);
void delete_party(char name[], int size);
void list_parties(void);
void change_p_size(char name[], int size);
//
// main function
//
int main()
{
int x;
while (1)
{
fflush(stdin);
printf("\n\nEnter 1 to add a party\nEnter 2 to remove a party\nEnter 3 for the list of the party\nEnter 4 to change party size.\nEnter 5 to quit.\n\n");
// user interface
scanf("%d",&x);
switch(x)
{
char name[20]; //local variables
int size;
case 1:
printf("\nParty Name: ");
scanf("%s", name);
printf("\nParty Size: ");
scanf("%d", &size);
if(size == 0)
{
printf("\nThat is not a valid command. Party not added!\n");
}
if(size >= 1 && size <= 2)
{
add_party(0, name, size);
}
else if(size >= 3 && size <= 4)
{
add_party(1, name, size);
}
else if(size >= 5 && size <= 6)
{
add_party(2, name, size);
}
else if(size >= 7)
{
add_party(3, name, size);
}
break;
case 2:
printf("\nSize of party to delete: ");
scanf("%i", &size);
delete_party(NULL, size);
break;
case 3:
list_parties();
break;
case 4:
change_partysize();
break;
case 5:
exit(0);
default:
continue;
}
}
}
//
//add function
//
void add_party(int h, char *name, int size)
{
//create a new node
int i=0;
int breaker = 0;
node *p;
node *new_item;
new_item = (node *)malloc(sizeof(node)); // allocate memory the size of the struct
strcpy(new_item->name,name);
new_item->size = size;
if(head[h] == NULL && tail[h] == NULL) // if an empty list, create a head and tail
{
head[h] = new_item;
tail[h] = head[h];
new_item->next = NULL;
return;
}
//traversal
for(i=0; i<4; i++)
{
p = head[i];
while(p != NULL)
{
//check that no repeating names. delete nodes that do have repeating names
if(strcmp(p->name,name) == 0)
{
printf("\nSorry, that name is already taken\n");
free(new_item);
return;
}
p = p->next; //go to the next node in the list
}
}
tail[h]->next = new_item;
new_item->next = NULL;
tail[h] = new_item;
}
//
//delete function
//
void delete_party(char *name, int size)
{
int i=0;
int breaker = 0;
node *p;
node *previous;
if(name != NULL)
{
for(i=0; i<4; i++)
{
p = previous = head[i]; //make sure previous trails behind head
while(p != NULL)
{
int c = (strcmp(p->name,name)==0);
if(c==0)
{
breaker = 1;
break;
}
else
previous = p;
p = p -> next;
}
if(breaker==1)
break;
}
}
else
{
int group = -1;
if(size == 1 || size == 2)
{
group = 0;
}
if(size == 3 || size == 4)
{
group = 1;
}
if(size == 5 || size == 6)
{
group = 2;
}
if(size >= 7)
{
group = 3;
}
for(i = group; i > -1; i--)
{
node *p = head[i];
node *previous = head[i];
while(p != NULL)
{
if(p <= size)
{
breaker = 1;
break;
}
else
{
previous = p;
p = p-> next;
}
}
if(breaker)
break;
}
}
if(p == NULL)
return;
if(head[i] == tail[i] && head[i] != NULL) // case 1, empty list
{
printf("\nList is empty!\n");
return;
}
else if(p == tail[i] && p == head[i]) // case 2, one element
{
head[i] = NULL;
tail[i] = NULL;
free(p);
}
else if(p == head[i]) // case 3, delete from the head
{
head[i] = head[i] -> next;
tail[i] = NULL;
free(p);
}
else if(p == tail[i]) // case 4, delete from tail
{
tail[i] = previous;
tail[i] -> next = NULL;
free(p);
}
else // case 5, delete from middle
{
previous -> next = p -> next;
free(p);
}
}
//
// list function
//
void list_parties(void)
{
int i = 0;
node *p=head;
for(i=0; i<4; i++)
{
p=head[i];
while(p != NULL)
{
printf("\n\n%s, %d\n\n", p->name, p->size);
p=p->next;
}
}
}
//
// change function
//
void change_partysize(char *name, int size)
{
int absolute_value = 0;
int comparison = 0;
int current_size = 0;
printf("\nWhat name is your party under?\n");
scanf("%s", name);
//check if the name
printf("\nWhat would you like to change the size to?\n");
scanf("%d", &size);
node *p;
while(p != NULL)
{
if(p->name == name) //new size falls into same range as the size coorelating to the name
{
current_size = p->size;
absolute_value = abs(size - current_size);
comparison = size - current_size;
if(current_size > 7 && size > 7)
{
current_size = size;
return;
}
else if(absolute_value >= 1)
{
//delete the node's value but not the name
delete_party(NULL, size);
//insert node with new name & dif size
add_party(NULL, name, size);
}
else
{
printf("\nYou did not enter a different party size\n");
return;
}
}
}
}
You're declaring new variables p and previous inside the for loop when you delete by size. So when the code after the loop uses these variables, it's using the uninitialized variables declared at the top of the function. Get rid of the declarations and just assign the variables.
Also, if (p <= size) appears to be a typo for if (p->size <= size). I'm surprised you didn't get a compiler warning for that.
You can also replace the if(breaker) check with a test in the for header.
for(i = group; !breaker && i > -1; i--)
{
p = head[i];
previous = head[i];
while(p != NULL)
{
if(p->size <= size)
{
breaker = 1;
break;
}
else
{
previous = p;
p = p-> next;
}
}
}

AddNumber function in program not working

I am having trouble understanding what I should do in the AddNumber function of my program. When the AddNumber function is called in main a pointer variable previous is created, and it takes the user's input and points it at the address of the variable newNum. I created an if statement for it to do that, but I was informed it doesn't do anything.
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
typedef struct A_NewNumber{
struct A_NewNumber *next;
double newNum;
} NewNumber;
NewNumber *AddNumber(NewNumber *previous, char *input){
//char input[16];
//double numEntered = 0;
NewNumber *newNum = malloc(sizeof(NewNumber));
sscanf(input, "%lf", &newNum->newNum);
//sscanf(input, "%s", newNum->enterNumber);
//numEntered = atof(input);
/*if (previous != NULL){
previous->newNum;
}*/
newNum->next = NULL;
newNum->newNum = 0;
return newNum;
}
void PrintList(NewNumber *start){
NewNumber *currentNumber = start;
int count = 0;
while(currentNumber != NULL){
count++;
printf("Numbers:%lf\n",
currentNumber->newNum);
currentNumber = currentNumber->next;
}
printf("Total Numbers Entered%d\n", count);
}
void CleanUp(NewNumber *start){
NewNumber *freeMe = start;
NewNumber *holdMe = NULL;
while(freeMe != NULL){
holdMe = freeMe->next;
free(freeMe);
freeMe = holdMe;
}
}
int main(){
//indexNum = 0;
char command[16];
char input[16];
//float userInput;
NewNumber *userEnter = NULL;
NewNumber *start = NULL;
NewNumber *newest = NULL;
while(fgets(input, sizeof input, stdin)){
printf("Please enter a number->");
printf("Enter 'quit' to stop or 'print' to print/calculate");
sscanf(input, "%s", command);
if(newest == NULL){
start = AddNumber(NULL, input);
newest = start;
}else{
newest = AddNumber(newest, input);
}if(strncmp(command, "print", 5) == 0){
PrintList(start);
}else if(strncmp(command, "quit", 4)== 0){
printf("\n\nQuitting....\n");
break;
//userInput = enterNumber;
}
}
CleanUp(start);
return 0;
}
}
It was not that bad, was just in need of a bit of clean-up.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// ALL CHECKS OMMITTED!
typedef struct A_NewNumber {
struct A_NewNumber *next;
double newNum;
} NewNumber;
NewNumber *AddNumber(NewNumber * previous, char *input)
{
int res;
// allocate new node
NewNumber *newNum = malloc(sizeof(NewNumber));
if (newNum == NULL) {
fprintf(stderr, "Malloc failed in AddNUmber()\n");
return previous;
}
// convert input string to float
res = sscanf(input, "%lf", &newNum->newNum);
if (res != 1) {
fprintf(stderr, "Something bad happend in AddNUmber()\n");
return previous;
}
// terminate that node
newNum->next = NULL;
// if this is NOT the first node
// put new node to the end of the list
if (previous != NULL) {
previous->next = newNum;
}
// return pointer to new node at end of the list
return newNum;
}
void PrintList(NewNumber * start)
{
NewNumber *currentNumber = start;
int count = 0;
while (currentNumber != NULL) {
count++;
printf("Numbers:%lf\n", currentNumber->newNum);
currentNumber = currentNumber->next;
}
printf("Total Numbers Entered %d\n", count);
}
void CleanUp(NewNumber * start)
{
NewNumber *freeMe = start;
NewNumber *holdMe = NULL;
while (freeMe != NULL) {
holdMe = freeMe->next;
free(freeMe);
freeMe = holdMe;
}
}
int main()
{
char input[16];
NewNumber *start = NULL;
NewNumber *newest = NULL;
int res;
// infinite loop
while (1) {
// give advise
printf("Please enter a number or\n");
printf("'quit' to stop or 'print' to print/calculate\n");
// get input from user
res = scanf("%s", input);
if (res != 1) {
if (res == EOF) {
fprintf(stderr, "Got EOF, bailing out\n");
break;
} else {
fprintf(stderr, "something bad happend, bailing out\n");
break;
}
}
// check if a command was given
if (strncmp(input, "print", 5) == 0) {
PrintList(start);
continue;
} else if (strncmp(input, "quit", 4) == 0) {
printf("\n\nQuitting....\n");
break;
}
// otherwise gather numbers
if (newest == NULL) {
start = AddNumber(NULL, input);
if (start == NULL) {
fprintf(stderr, "AddNumber returned NULL\n");
break;
}
newest = start;
} else {
newest = AddNumber(newest, input);
if (newest == NULL) {
fprintf(stderr, "AddNumber returned NULL\n");
break;
}
}
}
CleanUp(start);
return 0;
}
You should really make a habit of checking all returns and if you don't: be able to give a good reason why you didn't.
Don't forget to switch on all warnings your compiler offers. Even if you don't understand them now, Google might have an answer and if not some people here do (in that order, thank you).

Resources