i created a function to detect if a word does exist in a linked list with recursion.
i wanted to know if its correct or not .
bool does_exist_in_list(index *head,char word[25]){
while( head != NULL ){
//to detect the first element
if(strcmp(word,head->word) == 0)
return true;
else{
//to go to the next element
return does_exist_in_list(head->next,word);
}
}
}
because im using it in a long code so i dont know if there is a problem in it
The while loop is misleading, as the function always returns during the first iteration.
Additionally, if head is NULL, the function fails to return a value at all, invoking Undefined Behaviour.
bool does_exist_in_list(index *head, char word[25])
{
if (!head)
return false;
if (0 == strcmp(word, head->word))
return true;
return does_exist_in_list(head->next, word);
}
Related
I have trouble understanding what happens to a list when I use a function to insert some values in it. The exercise is to duplicate all the even element of a list. As you can see I wrote some comments (in the function) to better understand what's the problem of my code.
The function works without return and I can't understand why. To make it work I have to use it like a void function on the second call.
The integral code can be found at
https://replit.com/#Vash221/evenDub?v=1
Here you can see just the main function.
/*
Complete the listDupEven function which, given a list, duplicates all even numbers;
The function must use recursion
*/
#include <stdio.h>
#include "TList.h"
//RECURSIVE FUNCTION
TList listDupEven(TList list)
{
if(list==NULL)
return list;
if(list->info%2==0)
{
list=listInsert(list, list->info);
list=list->link;
}
//If I delete the return and use the function like a void it works:
//listDupEven(list->link);
return listDupEven(list->link);
//If I put a return it doesn't work
}
int main(void) {
TList list=listCreate();
list=listInsert(list, 1);
listInsert(list, 2);
listInsert(list, 3);
listInsert(list, 4);
listInsert(list, 5);
listInsert(list, 6);
list=listDupEven(list);
printf("The new list is:\n");
listPrint(list);
return 0;
}
the culprit here is this :
if(list==NULL)
return list;
You are returning a NULL pointer .
You can fix this in many ways , for example you can do this :
TList listDupEven(TList list , TList orig)
{
if(list==NULL)
return orig;
if(list->info%2==0)
{
list=listInsert(list, list->info);
list=list->link;
}
return listDupEven(list->link , orig);
}
orig is just the original pointer you pass to your function :
list = listDupEven(list , list) ;
The reason it did work when removing the return statement (and by that , I imagine you are talking about a void function , rather than a function without return statement , which is undefined behavior by the way) , is because you didn't overwrite your list pointer by NULL .
Edit :
This can work also :
TList listDupEven(TList list)
{
if(list==NULL)
return NULL;
if(list->info%2==0)
{
list=listInsert(list, list->info);
list=list->link;
}
listDupEven(list->link);
return list;
}
I want to return the last in a stack. Something like the following:
Item* get_last_item()
{
if (item_stack_size == 0) {
// return <nothing> ?
} else {
return ItemStack[item_stack_size-1];
}
}
What is the suggested practice when returning the equivalent of a null value if the stack is empty? Should this usually issue a hard error? Something like a value of (Item*) 0, or what's the suggested practice for doing something like this? My first thought was to do something like this, but I'm not sure if there's a better way:
Item* get_last_item()
{
return (item_stack_size != 0) ? ItemStack[item_stack_size-1] : (void*) 0;
}
For functions returning a pointer, a NULL pointer can be used as an out of band value( the caller of course should check the pointer for NULL, before dereferencing it):
Item *pop_last_item()
{
if (!item_stack_size) {
return NULL;
} else {
return ItemStack[--item_stack_size];
}
}
I am making a binary search tree program and I want to traverse to the node whose left or right child is to be selected for inserting the element. Whereas in the while condition I am getting warning of comparison of pointer with integer. Help me
while(temp1->data != NULL)
{
temp2=temp1;
if(value<=temp1->data)
{
temp1=temp1->lchild;
}
else
{
temp1=temp1->rchild;
}
}
Your while loop test is wrong. It should be testing for a null pointer to a node, like this:
while(temp1 != NULL) // <-- this line was wrong
{
temp2=temp1;
if(value<=temp1->data)
{
temp1=temp1->lchild;
}
else
{
temp1=temp1->rchild;
}
}
I recently started programming in C, and I've been working in a linked list program for a while. Now, the program is about having a profile in which you will register movies you watch and then save them in a .txt file. the trouble comes with the movie getting into the list. when I try to print it, the fields will show empty, as if I weren't assigning the pointers properly, but the fact is that the program KNOWS that I stacked a movie in my profile. I know it's a hard question to ask, any help would be appreciated. I'll show here the Insertmovie function, where I think there might be the problem, and the moviecopy function(I tested that function and does not work itself, although I doubt I did something wrong there):
int stacknewmovie (movie* p, list* l){
if(!p || !l){
return 0;
}
node* n;
n=newnode();
if(!n){
return 0;
}
insertnodeinfo(n, p);
n->next=NULL;
if(l->first==NULL){
l->first=n;
return 1;
}else{
n->next=l->first;
l->first=n;
return 1;
}
}
Here the moviecopy:
int moviecopy(movie* pel2,movie* pel1){
if(!pel1 || !pel2){
return NULL;
}else{
pel2=pel1;
return 1;
}
}
Again, thanks for taking your time. I didn't know how to show my problem better, as the compiler doesn't even warns me about anything.
node* insertnodeinfo(node* n, movie* p){
if(!p || !n){
return NULL;
}else{
moviecopy(n->info, p);
return n;
}
}
int stacknewmovie (movie* p, list* l){
if(!p || !l){
return 0;
}
node* n;
n=newnode();
if(!n){
return 0;
}
insertnodeinfo(n, p);
n->next=NULL;
if(l->first==NULL){
l->first=node; //problem here replace "node" by "n"??
return 1;
}else{
n->next=l->first;
l->first=n;
return 1;
}
as I did in the comment replace "node" by "n" first (I suppose that you want to write "n" not "node")
I think it is not a linked list because:
n->next=l->first; //this made the list a circular linked list
l->first=n; //Here is the problem I think
Because always the new node becomes the first node in the list, Is this that you want?
So i am trying to prevent the segfault that is occuring in the following code. It is occuring because i am trying to access the next element in the list that does not exist yet. How do i prevent this?
while (vm->item_list.head->data !=NULL) {
printf("%-10s",vm->item_list.head->data->id);
printf("%-20s",vm->item_list.head->data->name);
printf("%-70s",vm->item_list.head->data->description);
printf("%-15d",vm->item_list.head->data->on_hand);
printf("%s","$");
printf("%d",vm->item_list.head->data->price.dollars);
printf("%s",".");
printf("%02d",vm->item_list.head->data->price.cents);
printf("\n");
vm->item_list.head = vm->item_list.head->next;
}
printf("\n");
}
use something like while ((vm->item_list.head != NULL ) && (vm->item_list.head->data !=NULL))
In the initialization phase of the list, you have to declare the end of the list, in this case your next pointer, with NULL. If you do this, than you ensure that this pointer doesn't point in a undefined region.
So than you can check like this:
while (vm->item_list.head->data !=NULL) {
printf("%-10s",vm->item_list.head->data->id);
printf("%-20s",vm->item_list.head->data->name);
printf("%-70s",vm->item_list.head->data->description);
printf("%-15d",vm->item_list.head->data->on_hand);
printf("%s","$");
printf("%d",vm->item_list.head->data->price.dollars);
printf("%s",".");
printf("%02d",vm->item_list.head->data->price.cents);
printf("\n");
if(vm->item_list.head->next != NULL){
vm->item_list.head = vm->item_list.head->next;
}else{
break;
}
}
Or you can do it like this:
ItemNode* node = vm->item_list.head;
while (node->data !=NULL || node->next != NULL) {
printf("%-10s",node->data->id);
printf("%-20s",node->data->name);
printf("%-70s",node->data->description);
printf("%-15d",node->data->on_hand);
printf("%s","$");
printf("%d",node->data->price.dollars);
printf("%s",".");
printf("%02d",node->data->price.cents);
printf("\n");
node = node->next;
}