Array of pointer to the linked list - c

I'm working of my assignment, in C, which to store 500 string into strings of 5 char by the mean of hash table with chaining method to fix the collision.
Hashing algorithm : to add up the ASCII value and apply the modulus operator to the result.
The hash table store the hash key generated and a pointer which points to a linked list. Each linked list has more than one element if there are more than one 5-char string that gives the same hash key.
So far this is my code. I compiled it (Codeblock) and it appears that there is no error. However the program crashed.
Please give some inputs on where did i do wrong.
#include <stdio.h>
#include <string.h>
#define SLEN 500
#define WLEN 5
#define MPRIME 73
struct Node {
char s[WLEN+1]; // array to hold the 5-letter word
int sindex; // starting index of the word
struct Node * next; // a pointer to the next word in the list
};
int searchword(char *);
int hashfunc(char *);
void build_hashtbl();
struct Node * hashtable[MPRIME] = {NULL};
char string[SLEN+1] = "thenamewasfamiliartomeonseverallevelslookingbackitwasfatethatifoundhimihadcometopeppervillebeachtocloseonasmallhousethathadbeeninourfamilyforyearsonmywaybacktotheairportistoppedforcoffeetherewasafieldacrossthestreetwherekidsinpurpletshirtswerepitchingandhittingihadtimeiwanderedoverasistoodatthebackstopmyfingercurledinthechainlinkfenceanoldmanmaneuveredalawnmoweroverthegrasshewastannedandwrinkledwithahalfcigarinhismouthheshutthemowerwhenhesawmeandaskedifihadakidoutthereisaidnoheaskedwhatiwasdoing";
int main(void) {
int index;
char query[WLEN+1];
build_hashtbl(); // prepare the hash table
printf("Enter a 5-letter word to search: ");
scanf("%s", query);
index = searchword(query);
if (index != -1)
printf("The word %s starts at index %d.\n", query, index);
else
printf("The word %s is not found.\n", query);
return 0;
}
int searchword(char * word) {
int hashval;
struct Node * lhead;
hashval = hashfunc(word);
lhead = hashtable[hashval];
while (lhead) {
if (strcmp(lhead->s,word) == 0)
return lhead->sindex;
lhead = lhead->next;
}
return -1;
}
int hashfunc(char *){
int hashval = 0;
int i = 0;
for (i = 0; i < WLEN; i++){
hashval += (int) string[i];
}
return (int) (hashval % MPRIME);
}
void build_hashtbl(){
struct Node *hashtable[MPRIME]; //already declared. put here for ease
struct Node * head = NULL;
struct Node * last = NULL;
int i = 0;
int k = 0;
int key = 0;
char sElement[WLEN+1] = {0};
for (i = 0; i <SLEN; i = i+WLEN){ //for every 5 char, find they hashtable index key
key = hashfunc(*string[i]);
for (k = 0; k <WLEN; k++){ //create a new string, sElement from the 5 letter word
sElement[k] = string[i+k];
}
if (hashtable[key] != (NULL)){ //if the hashtable element at that index is empty, STORE it in a node
hashtable[key] = head;
struct Node *new_node;
new_node = (struct Node *) malloc ( sizeof (struct Node) );
strcpy(new_node->s, sElement); //put the new 5 letter word string into the node
new_node->sindex = i; //put the starting index of this word
new_node->next = NULL; //the next pointer is set to NULL
head->next = new_node; //finally set the head node to point to this new node
last = new_node; //set the new node as the last node
}
else { //if there is already a node in the array
struct Node *new_node;
new_node = (struct Node *) malloc ( sizeof (struct Node) );
strcpy(new_node->s, sElement); //put the new 5 letter word string into the node
new_node->sindex = i; //put the starting index of this word
new_node->next = NULL; //the next pointer is set to NULL
head->next = new_node; //finally set the head node to point to this new node
last->next = new_node; //set the last node to point to thew new created node
last = new_node; //set the new node as the last node
}
}
}

Your used last and head uninitialized, so head->next and friends would segfault. In fact you don't need them at all and you don't need your if branches - just replace hashtable[key] by new_node after setting new_node->next to hashtable[key]
void build_hashtbl(){
int i = 0;
int k = 0;
int key = 0;
char sElement[WLEN+1] = {0};
for (i = 0; i <SLEN; i = i+WLEN){ //for every 5 char, find they hashtable index key
key = hashfunc(string+i);
for (k = 0; k <WLEN; k++){ //create a new string, sElement from the 5 letter word
sElement[k] = string[i+k];
}
struct Node *new_node;
new_node = (struct Node *) malloc ( sizeof (struct Node) );
strcpy(new_node->s, sElement); //put the new 5 letter word string into the node
new_node->next=hashtable[key];
new_node->sindex=i;
hashtable[key]=new_node;
}
}
Works for me.
Edit: Also needs #include <stdlib.h> (at least here)

Related

How to Copy Characters in a Linked List to an Array in C?

I am having an issue with copying the contents of the character array in a linked list to a regular array of characters. I have an issue with a segmentation fault that I am not sure why.
The program that I have created works when the character array in the linked list is only one character, but it does not work when it is greater than 1. The main issue occurs on line 62 ("array[index] = p -> word[count]"). I have tried using strcpy to copy each index of it into the character array but that as well produced an error that reads: "passing argument 2 of ‘strcpy’ makes pointer from integer without a cast".
However, when I use an assignment statement, I just get a segmentation fault. I am not sure why because I feel I've created enough memory that should be able to hold the contents of the linked list for the array.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
char word[100];
struct node *next;
} ListNode;
int main ()
{
ListNode * head = NULL;
ListNode * tail = NULL;
for (int count = 0; count < 5; count++)
{
ListNode * temp = malloc (sizeof (*temp));
strcpy(temp -> word, "Hi");
temp -> next = NULL;
if (tail == NULL)
{
head = temp;
tail = temp;
}
else
{
tail->next = temp;
tail = temp;
}
}
char array[999]; // array that will hold the characters in the linked list
ListNode * p = head; //position of the current node
int count;
int index = 0;
// while p is still a node in the list
while(p != NULL)
{
if((int) strlen(p -> word) > 1) // checks if the string is longer than one character
{
count = 0; // initializes count as 0
while(count < (int) strlen(p -> word)) // counts how many characters are in the string
{
array[index] = p -> word[count]; // assings the words into charater array
count++; // increments the count
index++; // changes the index
}
}
else
{
array[index] = p -> word[0]; // copies p-word to array
index++; // changes the index in the array
p = p -> next;
}
}
return 0;
}
As mentioned before, the program works whenever the character array in the linked list is only 1, but a segmentation fault is produced when the number is greater than 1. Please let me know what I need to correct in this program. Thank you.
simplify your loops; for-loops allow you to keep the loop-machinery on one line
avoid special cases; there is nothing special about a one-char string
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
char word[100];
struct node *next;
} ListNode;
int main ()
{
ListNode * head = NULL;
ListNode * tail = NULL;
ListNode * p ;
int count;
int index ;
char array[999]; // array that will hold the characters in the linked list
for (count = 0; count < 5; count++)
{
ListNode * temp = malloc (sizeof *temp);
strcpy(temp->word, "Hi");
temp->next = NULL;
if (!tail) { head = temp; tail = temp; }
else { tail->next = temp; tail = temp; }
}
count=0;
for(p=head;p; p=p->next) { // walk the linked list
for(index=0; p->word[index]; index++) { // walk the string
array[count++] = p->word[index];
}
}
array[count++] = 0; // terminate
printf("%s\n", array);
return 0;
}

Random i character being printed into my string hash table despite appropriate datatypes

Can anybody explain why my hash table program for strings is just printing the letter i in the first array instead of Ben? I do not specify i anywhere so am extremely puzzled as to why this result is showing:
I have correctly set my datatypes to char with the appropriate array lengths specified, so why is it that the string is not recognised?
Code:
#include<stdio.h>
#include<stdlib.h>
#define size 7
struct node
{
char data;
struct node *next;
};
struct node *chain[size];
void init()
{
int i;
for(i = 0; i < size; i++)
chain[i] = NULL;
}
void add(char name[])
{
//create a newnode with value
struct node *newNode = malloc(sizeof(struct node));
newNode->data = name[10];
newNode->next = NULL;
//calculate hash key
char key = name[10] % size;
//check if chain[key] is empty
if(chain[key] == NULL)
chain[key] = newNode;
//collision
else
{
//add the node at the end of chain[key].
struct node *temp = chain[key];
while(temp->next)
{
temp = temp->next;
}
temp->next = newNode;
}
}
/*
* return 1, search found
* return 0, Otherwise
*/
int search(int name)
{
char key = name % size;
struct node *temp = chain[key];
while(temp)
{
if(temp->data == name)
return 1;
temp = temp->next;
}
return 0;
}
void print()
{
int i;
for(i = 0; i < size; i++)
{
struct node *temp = chain[i];
printf("chain[%d]-->",i);
while(temp)
{
printf("%c -->",temp->data);
temp = temp->next;
}
printf("NULL\n");
}
}
int main()
{
//init array of list to NULL
init();
add("Ben");
print();
printf("Searching element 10\n");
if(search(10))
printf("Search Found\n");
else
printf("Search Not Found\n");
return 0;
}
Result:
chain[0]-->i -->NULL
chain[1]-->NULL
chain[2]-->NULL
chain[3]-->NULL
chain[4]-->NULL
chain[5]-->NULL
chain[6]-->NULL
Searching element 10
Search Not Found
Syntactically your code is good.
The error is, that you call add() like this:
add("Ben");
This means that the address of a 4 character array (exactly: the address of its first member) is given to add(). The 4 characters are:
'B'
'e'
'n'
'\0'
Now in add() you read from the 11th character of the address given, at the offset of 10:
newNode->data = name[10];
This is called "out of bounds" and in Java (because you seem to know that) will throw an IndexOutOfBoundsException. But C doesn't have such checks and so the code reads whatever is there. In your example it is an 'i' by accident.

copying a string in an array element of a struct

I have the following problem: I made this node structure
typedef struct NODE{
struct NODE *sons[1024]; //this array will be used to store children pointers
char name[255];
int leaf;
}NODE;
and this function to create a new node with a given name. The problem is that the first printf shows the right name, the second one doesn't. It seems like the for loop erases the name and I can't explain myself why...
NODE *AllocateNewNode( char *inputname) {
NODE *newnode;
newnode = (NODE *)malloc(sizeof(NODE));
memset(newnode->name, '\0', sizeof(newnode->name));
strcpy(newnode->name, inputname);
printf("node %s created\n", newnode->name); //right name in the output
int i = 0;
for (i = 0; i <= 1024; i++) {
newnode->sons[i] = NULL;
}
newnode->leaf = 1;
printf("node %s created\n", newnode->name); //no name in the output
return newnode;
}
You're writing past the end of your sons array;
Should be for (i = 0; i < 1024; i++) { since there are only 1024 elements in the array 0...1023.

Swapping linked list items issue C

i'm trying to invert the "info" field of the in a list like the one below
struct nodo {
int info;
struct nodo *next;
struct nodo *prev;
} ;
typedef struct nodo nodo;
Here is the main, the two output should be the original list of n mebmers, and the inverted list (First value go n , second n-1 and so on)
int main(int argc, const char * argv[]) {
struct nodo *p;
p = CreateList();
PrintList(p);
IvertList(p);
Printlist(p);
return 0;
}
Here is InvertList(): (Count() function just returns dimension of the list, i know it is a messy way but i'm focused on result for now)
void InvertList (struct nodo *p) {
int tmp = 0, num = 0, i = 0;
num = (Count(p));
tmp = num;
for (i=1; i!=tmp; i++) {
Swap(p,num);
num--;
}
}
And here is Swap(), this should bring a value (int info) to the first place of the list, to the last swapping with each:
void Swap (struct nodo *p, int n) {
int *tmp1 = NULL, *tmp2 = NULL;
int i;
for ( i = 1; i != n && p != NULL; i++) {
tmp1 = &p->info;
p = p->succ;
tmp2 = &p->info;
p->info = *tmp1;
p->prec->info = *tmp2;
}
}
Now the output i got printed is:
Value: 1
Value: 2
Value: 3
Value: 4
Value: 5
Value: 1
Value: 1
Value: 1
Value: 1
Value: 1
Where the last 5 values should be 5-4-3-2-1.
The bug(s) in your code not withstanding, you're not reversing your physical list at all, which I can all-but-guarantee is the point of the exercise in the first place.
Inversion of a linked list means all the pointers switch directions and the old tail becomes the new head. You seem to be avoiding that and trying to swap node info values instead.
To invert your list using simple pointer swapping:
// note head pointer passed by address
void InvertList(node **pp)
{
node *cur = *pp;
while (cur)
{
node *tmp = cur->prev;
cur->prev = cur->next;
cur->next = tmp;
*pp = cur;
cur = cur->prev;
}
}
And invoke from main() as:
InvertList(&p);
Note that no info values need be swapped, copied, etc. The node pointers simply switch direction and their enumeration will start at the other end. A full working example appears below:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
struct node *prev;
};
typedef struct node node;
static void PrintList(const node *head)
{
while (head)
{
printf("%d: this=%p, prev=%p, next=%p\n",
head->info, head, head->prev, head->next);
head = head->next;
}
}
static void InvertList(node **pp)
{
node *cur = *pp;
while (cur)
{
node *tmp = cur->prev;
cur->prev = cur->next;
cur->next = tmp;
*pp = cur;
cur = cur->prev;
}
}
int main()
{
node *prev = NULL, *head = NULL, **pp = &head;
for (int i=1; i<=5; ++i)
{
*pp = malloc(sizeof **pp);
(*pp)->info = i;
(*pp)->prev = prev;
prev = *pp;
pp = &(*pp)->next;
}
*pp = NULL;
PrintList(head); // prints 1,2,3,4,5
InvertList(&head);
PrintList(head); // prints 5,4,3,2,1
}
Output (addresses vary, obviously)
1: this=0x1001054b0, prev=0x0, next=0x1001054d0
2: this=0x1001054d0, prev=0x1001054b0, next=0x1001054f0
3: this=0x1001054f0, prev=0x1001054d0, next=0x100105510
4: this=0x100105510, prev=0x1001054f0, next=0x100105530
5: this=0x100105530, prev=0x100105510, next=0x0
5: this=0x100105530, prev=0x0, next=0x100105510
4: this=0x100105510, prev=0x100105530, next=0x1001054f0
3: this=0x1001054f0, prev=0x100105510, next=0x1001054d0
2: this=0x1001054d0, prev=0x1001054f0, next=0x1001054b0
1: this=0x1001054b0, prev=0x1001054d0, next=0x0
There is a bug in your Swap fcn:
p->info = *tmp1;
p->prev->info = *tmp2;
But what are tmp1 and tmp2 at that time? Well, after we advanced p, tmp1 points at p->prev->info, while tmp2 = &p->info; So we could rewrite these assignments in effect as:
p->info = p->prev->info;
p->prev->info = p->info;
So, we could rewrite them again, in effect, as:
p->info = p->prev->info;
p->prev->info = p->prev->info;
So, the second assignment doesn't change anything in effect. Therefore, the first call to Swap in InvertList takes the value of the first element (1) and sets all the values in the list equal to it. The subsequent calls to Swap act similarly but have no effect as the list already contains all 1's.
Here's a simple way to rewrite Swap:
void Swap(struct nodo *p, int n)
{
if (n <= 1)
return;
int tmp = p->info;
for (int i = 1; i != n; ++i, p = p->next)
p->info = p->next->info;
p->info = tmp;
}
Note, however, that the way you've written InvertList is doing theta(n^2) work. The first loop iteration shifts the first element n-1 spots, the second iteration shifts the first element n-2 spots, the third iteration shifts the first element n-3 spots, and so on, down to shifting the first element 1 spot. So, you end up doing something like n * (n - 1) / 2 total shifts to reverse the list.
A linked list can be reversed / inverted in theta(n) work. See if you can figure out a better way to do this. If you had a pointer to both the beginning and the end of the list (which you often want anyway), then you could do something similar to reversing the characters in a string, for example.

Description is only printing out for the last one entered

I'm quite new to C and I'm trying to implement a binary tree in C which will store a number and a string and then print them off e.g.
1 : Bread
2 : WashingUpLiquid
etc.
The code I have so far is:
#include <stdio.h>
#include <stdlib.h>
#define LENGTH 300
struct node {
int data;
char * definition;
struct node *left;
struct node *right;
};
struct node *node_insert(struct node *p, int value, char * word);
void print_preorder(struct node *p);
int main(void) {
int i = 0;
int d = 0;
char def[LENGTH];
struct node *root = NULL;
for(i = 0; i < 2; i++)
{
printf("Please enter a number: \n");
scanf("%d", &d);
printf("Please enter a definition for this word:\n");
scanf("%s", def);
root = node_insert(root, d, def);
printf("%s\n", def);
}
printf("preorder : ");
print_preorder(root);
printf("\n");
return 0;
}
struct node *node_insert(struct node *p, int value, char * word) {
struct node *tmp_one = NULL;
struct node *tmp_two = NULL;
if(p == NULL) {
p = (struct node *)malloc(sizeof(struct node));
p->data = value;
p->definition = word;
p->left = p->right = NULL;
}
else {
tmp_one = p;
while(tmp_one != NULL) {
tmp_two = tmp_one;
if(tmp_one->data > value)
tmp_one = tmp_one->left;
else
tmp_one = tmp_one->right;
}
if(tmp_two->data > value) {
tmp_two->left = (struct node *)malloc(sizeof(struct node));
tmp_two = tmp_two->left;
tmp_two->data = value;
tmp_two->definition = word;
tmp_two->left = tmp_two->right = NULL;
}
else {
tmp_two->right = (struct node *)malloc(sizeof(struct node));
tmp_two = tmp_two->right;
tmp_two->data = value;
tmp_two->definition = word;
tmp_two->left = tmp_two->right = NULL;
}
}
return(p);
}
void print_preorder(struct node *p) {
if(p != NULL) {
printf("%d : %s\n", p->data, p->definition);
print_preorder(p->left);
print_preorder(p->right);
}
}
At the moment it seems to work for the ints but the description part only prints out for the last one entered. I assume it has something to do with pointers on the char array but I had no luck getting it to work. Any ideas or advice?
You're always doing a scanf into def and then passing that to your insert routine which just saves the pointer to def. So, since all of your entries point to the def buffer, they all point to whatever was the last string you stored in that buffer.
You need to copy your string and place a pointer to the copy into the binary tree node.
The problem is that you're using the same buffer for the string. Notice your struct is holding a pointer to a char, and you are passing the same char array as that pointer each time.
When you call scanf on the buffer, you are changing the data it points to, not the pointer itself.
To fix this, before assigning it over to a struct, you can use strdup. So the lines of code would become
tmp_*->definition = strdup(word);
Keep in mind that the char array returned by strdup must be freed once you are done with it, otherwise you'll have a leak.

Resources