So I wanted to write a function in C which converts a generic array into a single linked list.
The code I wrote:
typedef struct Node {
struct Node* next;
void *value;
} Node;
void insert(Node** root, void* value) {
Node* new_node = (Node *) malloc(sizeof(Node));
Node* ptr;
new_node->value = value;
new_node->next = NULL;
if (*root == NULL)
*root = new_node;
else {
ptr = *root;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = new_node;
}
}
Node* arr2list(void* array, size_t length) {
Node *root = NULL;
for(int i = 0; i < length; i++) {
insert(&root,&array[i]);
}
return root;
}
I wrote a small test for it:
int main() {
int arr[] = { 1, 2, 3, 4, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
Node* root = arr2list(arr, n);
while (root != NULL)
{
printf("%d,",*(int*) root->value);
root = root->next;
}
return 0;
}
But I get garbage values: -13308,-2145276560,-2145276560,-2145276560,-2145276560,.
I can't seem to find the mistake that leads to those results.
What could be the issue?
Your program contains &array[i] where array has type void* at line 28.
This is not standard C. GCC accepts it and treats the pointer arithmetic as char* arithmetic (arguably a bad idea, in particular because it feeds the confusion in an example such as this one).
Since your function arr2list consumes the array through some misaligned pointers, the results are apparently arbitrary values (that contain some of the bytes of the first array element and some of the bytes of the second array element, for instance).
I would be happy to say that the function arr2list must simply take as argument the length of one element, but this small change alone is not going to be enough in itself to make things work. Your linked list type stores pointers as data, so the function will also need to allocate a block for each of the elements, and store a pointer to this element inside the Node.
If you are content with making the list point to the elements of the array, then forget the above paragraph, you almost have a working solution, just make arr2list take an extra argument size_t elt_size and use (char*)array + elt_size*i instead of &array[i].
Related
I'm working on implementing a binary search tree data structure in C, but I got stuck at the part where you point to the left or right child. I understand that if the value you're inserting is smaller than the root, it goes to the left and to the right if it's larger. I'm just struggling with the double pointers part as shown in the code below. Let's take bs_tree_insert_left for example, I want pos->left_child to point to the left_child in order to place the value given there, but I'm not sure how I would write this.
For context regarding the main function, the numbers in arr[] will be randomly shuffled but I removed that part of the code to keep the post short and compact.
struct node
{
int value;
struct node *left_child;
struct node *right_child;
};
typedef struct node BSTree;
typedef struct node* BSTreePos;
BSTree *bs_tree_make(int value){
// Allocate memory for new node
struct node* origin = (struct node*)malloc(sizeof(struct node));
// Assign data to this node
origin->value = value;
// Initialize left and
// right children as NULL
origin->left_child = NULL;
origin->right_child = NULL;
return (origin);
}
BSTreePos bs_tree_insert_left(int value, BSTreePos pos){
pos->left_child = bs_tree_make(value);
return pos->left_child;
}
void insert_value(int value, BSTreePos pos)
{
if (pos == NULL) return bs_tree_make(value);
if (value < pos->value)
{
pos->left_child = bs_tree_insert_left(value, pos->left_child);
}
else if (value > pos->value)
{
pos->right_child = bs_tree_insert_right(value, pos->right_child);
}
}
int main(void)
{
// Create an array with the values 1, 2, ..., 10 and print out the content.
int n = 10;
int arr[n];
for (int i = 0 ; i < n ; i++) {
arr[i] = i + 1;
}
print_array(n, arr);
BSTree *tree = bs_tree_make(arr[0]);
for (int i = 1 ; i < n ; i++) {
BSTreePos pos = bs_tree_root(tree);
insert_value(arr[i], pos);
}
return 0;
}
You know what, I'm just going to write the correct algorithm that uses the double pointer to maximum effect.
void insert_value(int value, struct node **node)
{
if (*node == NULL) {
*node = malloc(sizeof(struct node));
node[0]->value = value;
node[0]->left_child = NULL;
node[0]->right_child = NULL;
} else if (node[0]->value < value)
insert_value(value, &node[0]->left_child);
else if (node[0]-> value > value)
insert_value(value, &node[0]->right_child);
/* else duplicate value found -- don't insert (from OP's code) */
}
BSTree *tree = NULL;
for (int i = 0 ; i < n ; i++) {
insert_value(arr[i], &tree);
}
node[0]->value is the idiomatic way of accessing a struct through a double pointer.
But let's look at how this works and how much value this gets out of the double pointer. The empty tree is stored as the NULL pointer. This makes initializing the tree and adding a node to the tree the same code. Notice how insert_value takes a double pointer to the tree; this allows it to fill out the root node when adding the first node, or any child node thereof. Thus double pointer which is pointer to pointer is used to update a pointer to a node when we make a new one.
With this algorithm, having a bs_tree_insert_left literally makes no sense. The whole idea is the code that does the insertion doesn't know where it is inserting the node.
Fun fact: the compiler will transform away the recursion for us when compiling with optimizations.
I try to create a library program with a linked list in C. I get an "assignment makes integer from pointer without a cast" error and when I try to print screen the list print_list function not working; only printf "H->" doesn't print in the while loop.
#include<stdio.h>
#include<stdlib.h>
struct node
{
char bookname[40];
char writer[50];
int available;
int memberid;
struct node *next;
};
void AddBook(struct node **head, char bookname[50], char writer[50], int available, int memberid)
{
struct node * new_node = NULL;
struct node * last = NULL;
new_node = (struct node *)malloc(sizeof(struct node));
if (new_node == NULL)
{
printf("Failed to insert element. Out of memory");
return;
}
new_node->bookname[50]= bookname;
new_node->writer[50]= writer;
new_node->available= available;
new_node->memberid= memberid;
new_node->next = NULL;
At this point, I get an "assignment makes integer from pointer without a cast" problem on these two;
new_node->bookname[50]= bookname;
new_node->writer[50]= writer;
if( *head == NULL)
{
*head = new_node;
return;
}
last = *head;
while(last->next) last = last->next;
last->next = new_node;
}
void print_list(struct node *head)
{
printf("H->");
while(head)
{
printf("%s %s %d %d ->", head->bookname[50],head->writer[50],head->available,head->memberid);
head = head->next;
}
printf("|||\n\n");
}
int main()
{
struct node * head = NULL;
AddBook(&head,"Hamlet","William_Shakespeare",1,1);
AddBook(&head,"The Odyssey","Homer",1,1);
AddBook(&head,"The Great Gatsby","F. Scott Fitzgerald",1,1);
print_list(head);
return 0;
}
What can I do to get book name and writer get with scanf? I tried to do it this way but it didn't work;
int main()
{
struct node * head = NULL;
struct node book;
prinft("please enter the bookname:");
scanf("%s", book.bookname);
prinft("\n please enter the writer name:"); scanf("%s",book.bookname);
book.available=1;
book.memberid=1;
AddBook(&head,*book.bookname,*book.writer,book.available,book.memberid);
print_list(head);
return 0;
}
On these lines:
new_node->bookname[50]= bookname;
new_node->writer[50]= writer;
You think you're copying the contents of one array to another. What you're actually doing is attempting to copy a pointer (since arrays as function arguments decay to pointers) to a single element of the array (i.e. a char), and to an element past the end of the array at that.
Even if you removed the index from the destination, this wouldn't work because arrays are not assignable.
To copy one string to another, use strcpy:
strcpy(new_node->bookname, bookname);
strcpy(new_node->writer, writer);
Although arrays and pointers to arrays can be treated as interchangeable in some cases, this is not one of them. Since bookname and writer are declared as char arrays and not char pointers, you won't be able to accomplish what you're trying to do by assigning a char * to either of them. Instead, you should use strncpy() (assuming these are null-terminated strings) to copy them over.
You should also double-check the array indices--you're trying to copy to bookname[50] (which would be the 51st element, because of zero-based numbering) while bookname[]'s declaration is only for 40 elements (for the same reason of zero-based index numbering, there is no writer[50] even though it is declared as char writer[50] -- the declaration creates a 50-element array, whose first element is at index 0 and fiftieth at 49).
I am trying to do a function that receives the root of a supposed BST and I want to know if the tree in question is a BST.
Problem is that, I am traveling the tree with recursion and what I'm trying to do is, put inside an array all the values of the tree. I searched for how to put a BST into an array (AddtoArray), but the answers I've found on stackoverflow and other websites didn't solve my problem. (I got seg fault).
Here's what I got so far:
#include<stdio.h>
#include<stdlib.h>
struct node{
int key;
struct node *left, *right;
};
struct node *newNode(int item){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
struct node* insert(struct node* node, int key){
if(node == NULL) return newNode(key);
if(key < node->key)
node->left = insert(node->left, key);
else if(key >= node->key)
node->right = insert(node->right, key);
return node;
}
void check(struct node *root, int *array, int i){
if(root != NULL){
check(root->left, array, i);
array[i++] = root->key;
//I tried to put i++, ++i in every place of this function (trying table test) and I realized it was too difficult to realize what to do here, I've found some functions returning an integer, the "i" in question, but they didn't work out for me.
check(root->right, array, i);
}
}
int main(){
int *array;
int array_length = sizeof(array)/sizeof(array[0]);
int i = 0;
array = (int*)malloc(array_length*sizeof(int));
struct node *root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 20);
check(root, array, i);
printf("PRINTING ARRAY TO SEE IF THE BST IS IN ARRAY:\n");
for(i = 0; i < array_length; i++){
printf("VALUE: %d ", array[i]);
}
free(array);
return 0;
}
I'd like to solve this problem WITHOUT using global variables. How can I do it?
I see two issues in your check-code and its usage (did not analyse the other functions):
First, your malloc does not work as intended, because sizeof(array)/sizeof(array[0]) will always give the same (small) value, probably 1 or 2, regardless of the size of your BST.
So you'd rather introduce a function getting the number of nodes and use this as the length for your array:
int array_length = getNrOfNodes(root); // function to be coded
int *array = malloc(array_length*sizeof(int));
Second, you pass an integral value i by value to a recursive function. As it is passed by value, any i++ will take effect only for the respective function call instance, but will not influence the other ones on the call stack. So it is very likely that the keys of the complete left part of your tree (the one before i is altered), will be written to array[0].
I'd suggest to share the array index among the function instances on the call stack. This can be achieved by passing a pointer to some i rather than passing i-s value around. And I'd introduce an internal version that does the work, because the user of the function needs not to be aware of the helper variable i:
void writeToArray_internal(struct node *root, int *array, int *i){
if(root != NULL){
writeToArray_internal(root->left, array, i);
array[*i] = root->key;
(*i)++;
writeToArray_internal(root->right, array, i);
}
}
void writeToArray(struct node *root, int *array) {
int i=0;
writeToArray_internal(root, array, &i);
}
This line here
int array_length = sizeof(array)/sizeof(array[0]);
is wrong. This only works with pure arrays, because sizeof returns the amount
of bytes of the expression/variable. Your variable array is a pointer, so
sizeof array returns you the size of a pointer and it doesn't matter where the
pointer is pointer, you always will get the same size.
Besides trying to get the number of elements of the "array" before knowing how
many nodes you have, makes no sense, because the number of elements that you need in array depend on the number of nodes, so
you've got write a function that returns you the number of nodes and then
you can allocate space for array.
Also note that your check function is not correct either. The variable i is
local to every call of check so you are going to overwrite values of the
array, because the i++ of the n-th iteration only affects the i of the
n-the iteration, the n-1-th iteration does not see that change and thus
overwrites the value of the n-th iteration.
You'll need to pass i as a pointer as well:
void check(struct node *root, int *array, size_t *i, size_t maxlen) {
if(root != NULL){
check(root->left, array, i, maxlen);
if(*i < maxlen) // checking that you don't step out of bounds
array[(*i)++] = root->key;
check(root->right, array, i, maxlen);
}
}
and the you call it like this:
size_t i = 0;
size_t nodes_number = get_number_of_nodes(root); // you have to write this function
int *array = malloc(nodes_number * sizeof *array);
if(array == NULL)
{
// error handling
// do not continue
}
check(root, array, &i, nodes_number);
I'm writing a simple C program to manage a linked list defined as follow:
typedef struct node {
int value;
struct node *next;
} *List;
I reviewed the code and it seems okay but when printing results something is not working well.
My main, with problems on comments:
int main(void) {
List n = list_create(1);
insert(n, 2);
insert(n, 3);
insert(n, 5);
insert(n, 4);
//something here does not work properly. It produces the following output:
//Value: 1
//Value: 2
//Value: 3
//Value: 4
//where is value 5?
print_list(n);
delete(n, 3);
print_list(n);
return 0;
}
I don't know where am I destroying list structure. These are my functions, to debug, if you are too kind.
List list_create(int value) {
List new = malloc(sizeof(struct node));
new->value = value;
new->next = NULL;
return new;
}
List new_node(int value, List next_node) {
List new = malloc(sizeof(struct node));
new->value = value;
new->next = next_node;
return new;
}
void print_list(List l) {
List *aux;
for (aux = &l; (*aux) != NULL; aux = &((*aux)->next))
printf("Valor: %d\n", (*aux)->value);
}
void insert(List l, int value) {
List *p;
for (p = &l; (*p) != NULL; p = &((*p)->next))
if ((*p)->value > value) {
List tmp = *p;
List new = new_node(value, tmp);
*p = new;
break;
}
*p = new_node(value, NULL);
}
void delete(List l, int value) {
List *p;
for (p = &l; (*p) != NULL; p = &((*p)->next))
if ((*p)->value == value) {
List del = (*p);
(*p) = ((*p)->next);
free(del);
break;
}
}
This code has (at least) two bugs:
The line
if ((*p)->value > value){
means that if you start the list with 1 as the first value and then try to insert 2,3,4..., the body of the 'if' statement never runs, so nothing ever gets inserted.
If you insert a value below the starting value, you have to modify the list pointer itself. However, as #EOF alluded, you are trying to modify a value passed to a function by taking its address. This won't work. &l does not give you the address of the List you passed, it gives you the address of the local copy on insert()'s stack. You are better off modifying the values of first element of the list 'in place'. If you really want to make the List parameter mutable, you'll need to pass it as a List *, and call the function with the address of the list (e.g. insert(&n,2); ) Your delete() function suffers from the same problem - try deleting the first element of the list.
Try this for your insert function:
void insert(List l, int value)
{
List p;
// Find end of list or highest item less than value
for(p = l; p->next != NULL && p->next->value < value; p = p->next);
if (p->value >= value) {
// Over-write p with new value, and insert p as a new one after.
// This saves having to modify l itself.
int tmpval = p->value;
p->value = value;
p->next = new_node(tmpval, p->next);
} else {
// Insert new item after p
p->next = new_node(value, p->next);
}
}
A comment: it is possible the way you are using pointers is not helping the debugging process.
For example, your print_list() could be re-written like this:
void print_list(List l){
List aux;
for(aux = l; aux != NULL; aux = aux->next)
printf("Valor: %d\n", aux->value);
}
and still behave the same. It is generally good practice not to 'hide' the pointer-like nature of a pointer by including a '*' in the typedef.
For example, if you define your list like this:
typedef struct node{
int value;
struct node *next;
} List
And pass it to functions like this:
my_func(List *l, ...)
then it'll make some of these issues more apparent. Hope this helps.
There are many problems in your code:
Hiding pointers behind typedefs is a bad idea, it leads to confusion for both the programmer and the reader.
You must decide whether the initial node is a dummy node or if the empty list is simply a NULL pointer. The latter is much simpler to handle but you must pass the address of the head node to insert and delete so they can change the head node.
printlist does not need an indirect pointer, especially starting from the address of the pointer passed as an argument. Simplify by using the Node pointer directly.
in insert you correctly insert the new node before the next higher node but you should then return from the function. Instead, you break out of the switch and the code for appending is executed, replacing the inserted node with a new node with the same value and a NULL next pointer. This is the reason 5 gets removed and lost when you insert 4. Furthermore, you should pass the address of the head node so a node can be inserted before the first.
delete starts from the address of the argument. It cannot delete the head node because the pointer in the caller space does not get updated. You should pass the address of the head node.
You should avoid using C++ keywords such as new and delete in C code: while not illegal, it confuses readers used to C++, confuses the syntax highlighter and prevents compilation by C++ compilers.
Here is a simplified and corrected version:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int value;
struct Node *next;
} Node;
Node *new_node(int value, Node *next_node) {
Node *node = malloc(sizeof(*node));
if (node != NULL) {
node->value = value;
node->next = next_node;
}
return node;
}
void print_list(Node *list) {
for (; list != NULL; list = list->next)
printf("Valor: %d\n", list->value);
}
void insert_node(Node **p, int value) {
while ((*p) != NULL && (*p)->value < value)
p = &(*p)->next;
*p = new_node(value, *p);
}
void delete_node(Node **p, int value) {
while (*p != NULL) {
if ((*p)->value == value) {
Node *found = *p;
*p = (*p)->next;
free(found);
// return unless delete() is supposed to remove all occurrences
return;
} else {
p = &(*p)->next;
}
}
}
int main(void) {
Node *n = NULL;
insert_node(&n, 2);
insert_node(&n, 3);
insert_node(&n, 5);
insert_node(&n, 4);
insert_node(&n, 1);
print_list(n);
delete_node(&n, 3);
print_list(n);
delete_node(&n, 1);
print_list(n);
return 0;
}
Below is my code that provides a simple interface to link lists in C. So that it can behave simular to ArrayLists in java.
My question is this:
My code will only work for people who want to have a link list of ints and nothing else.
I understand that they can use the int to hold the address of their data type via pointers.
However, I want something more versatile.
Can I use void* instead of the int in the node struct. Then users can provide int, double, char etc...?
Code:
#include <stdio.h>
#include <stdlib.h>
int AL_appened(int val);
struct Tuple AL_find(int val);
int AL_remove(int val);
int AL_setup();
int AL_len();
typedef struct Node Node;
typedef struct Tuple Tuple;
struct Node{
int val;
Node *next;
};
struct Tuple{
int index;
int val;
};
Node *root, *curr;
int AL_appened(int val) {
Node *tmp;
tmp = (Node *)malloc(sizeof (Node));
curr->next = tmp;
tmp->val = val;
curr = tmp;
root->val++;
return 0;
}
struct Tuple AL_find(int val){
if(root->next) {
curr = root->next;
int count = 0;
while (curr->next){
if (curr->val == val){
Tuple r = {count+=1, val};
return r;
}
count++;
curr = curr->next;
}
if (curr->val == val){
Tuple r = {count+=1, val};
return r;
}
}
Tuple r = {-1, -1};
return r;
}
int AL_remove(int val){
Node *prev;
prev = (Node *)malloc(sizeof (Node));
curr = root;
while (curr->next->val != val){
prev = curr;
curr = curr->next;
}
if (curr->next->val != val) return -1;
curr->next = curr->next->next;
root->val--;
free(curr->next);
return 1;
}
int AL_setup(){
root = (Node *)malloc(sizeof(Node));
root->val = 0;
root->next = 0;
curr = root;
return 0;
}
int AL_len(){
return root->val;
}
void printAll(){
curr=root->next;
while (curr->next != NULL) {
printf("%d\n",curr->val);
curr=curr->next;
}
printf("%d\n",curr->val);
}
int main(){
AL_setup(); //setup the root, we will use root to keep track of the number of links
AL_appened(1); // append 1 so it should look like root>1
AL_appened(2); // append 2 so it should look like root>1>2
AL_appened(3);
AL_appened(4);
printf("%d\n", AL_len()); // print len of list
Tuple results = AL_find(4); // find 4 in list
printf("%d %d\n", results.index, results.val); // return the index and the number found
AL_remove(3);
Tuple results2 = AL_find(4);
printf("%d %d\n", results2.index, results2.val);
results2 = AL_find(4);
printf("%d %d\n", results2.index, results2.val);
printAll(); // print entire list
return 0;
}
Well, you could have void* as the value type, but that would require an extra memory allocation per-node. Another approach I've seen is to put only the link in the struct, and have the user declare their own node type (with the link first), and cast to the generic node type. I've even seen linked list libraries put in preprocessor macros, such that DEFINE_LINKED_LIST(Foo) makes a FooList, a FooNode, an appendFoo function, etc. Finally, you could put all the types you think you might want into a union for the value.
Ultimately, there's no especially clean, pretty way of doing this; C is not well-suited to polymorphism. You'll need to decide which imperfect option you like best.
The linux programmers had a similar problem, and they came up with a general solution to it. In short, their approach is to define a list_head structure, that is inserted into whatever user structure that needs to be part of a linked list. Since the inclusion is the other way around as in your code, the linked list implementation 1. does not constrict the user structure in any way, and 2. allows a user structure to be a member of more than one linked lists. This is really a very flexible design, and as it's GPL'd, you can use it in any GPL'd code.
(This answer to another question on SO points to a user space adaption of the kernel lists. I have not tested it myself, so use it at your own risk. It looks sane, though.)