passing linked list to function by value - c

EDIT: my struct is of the form:
struct node{
int key;
struct node* next;
}
typedef struct node* LIST;
I'm writing a function, called print_list_iteratively, which takes a singly linked list and prints its elements in reverse order iteratively. Let me put my 3 functions below and then explain what's my problem with them?
LIST reverse_list(LIST L) {
LIST current = L, prev = NULL, subsequent = NULL;
while (current != NULL) {
// first we need to get the next element to not lose the link.
subsequent = current->next;
// now reverse the pointer
current->next = prev;
// we need to update our prev to create link between the currrent and next element.
// in short, we need to get the address of current node
prev = current;
// finally update the current
current = subsequent;
}
return (L = prev);
}
The above code reverses the list L, and returns the new head of the list L.
void print_list(LIST L) {
while (L) {
printf("%d ", L->key);
L = L->next;
}
}
print_list prints the elements of the list in the order they've been connected.
I've all needed to write the print_list_iteratively function I mentioned at the beginning.
void print_list_iteratively(LIST l) {
// first reverse the list, then call print_list. that's all.
printf("address of function parameter is %p\n", l);
l = reverse_list(l);
print_list(l);
}
I've no problem with these functions; they work as expected, but the notion of pass by value and pass by reference confused me, when I tried to understand how the function takes list as a parameter and returns it. Let me simply write a main for testing these and we'll see how things get mixed.
int main(void) {
l = insert(l, 5);
l = insert(l, 10);
l = insert(l, 15);
l = insert(l, 45);
print_list(l);
print_list_iteratively(l);
print_list(l);
return 0;
}
when first print_list function is called, it prints 5->10->15->45. Next print_list_iteratively is called on list l, which in its turn calls reverse_list and print_list in the body of the function. The print_list inside print_list_iteratively give reversed list 45->15->10->5. Eventually, print_list inside main prints 5.
I want to know why has second print_list just printed 5? I've send list to print_list_iteratively pass by value, not pass by referce, despite this it's changed my original list insinde main function. Can somebody explain me how the lists in this example are passed to functions and how they're returned from them? I appreciate your answers.

The problem is that you pass the head pointer by value. This lets your program to modify your linked list, but the head pointer remains the same.
You should pass the head pointer to the function by reference or a pointer which points to your pointer. (node*& or node**)
Keep in mind, if you pass it by pointer, you need to dereference it, when it is needed.

Related

Function to remove entry from linked list in c

I'm struggling with exercise 4 in chapter 10 of the Programming in C book by Kochan.
The task is the following:
Write a function called removeEntry() to remove an entry from a
linked list. The sole argument to the procedure should be a pointer to
the list. Have the function remove the entry after the one pointed to
by the argument.
For some reason the function doesn't work. If I do a printf() inside it, it seems to work, but it doesn't work in main(). I thought that given I am passing a pointer, it will change the original (as opposed to passing by value). But clearly I'm not doing it right. Could anyone please help me see where I'm going wrong?
// Function to remove an entry from a linked list
#include <stdio.h>
struct entry
{
int value;
struct entry *next;
};
int main(void)
{
struct entry n1, n2, n3; // Original list
struct entry *listPointer = &n1; // List pointer set up for iterating list
void removeEntry(struct entry *pList);
//listHead.next = &n1; set list head to point to original beginning of list
n1.value = 100;
n1.next = &n2;
n2.value = 200;
n2.next = &n3;
n3.value = 300;
n3.next = (struct entry *) 0; // marks end of list
// Print out values before removing entry
printf("Before removing entry:\n");
while (listPointer != (struct entry *) 0)
{
printf("%i\n", listPointer->value);
listPointer = listPointer->next;
}
// reset listPointer to point to n1
listPointer = &n1;
// run function (DOES NOT WORK)
removeEntry(listPointer);
// print out values after (supposedly) removing entry
printf("\n\nAfter removing entry:\n");
while (listPointer != (struct entry *) 0)
{
printf("%i\n", listPointer->value);
listPointer = listPointer->next;
}
return 0;
}
/********************* REMOVE ENTRY ********************************/
void removeEntry(struct entry *pList)
{
// <-------&n2
pList = pList->next;
}
There are three misunderstanding in your code:
First as mentioned by kaylum in his comment, function arguments are passed by value in C. It means that whenever you pass an argument to a function, a copy of the argument is used within the function. And when the function returns, this copy is lost. So you won't be able to change pList's value within removeEntry() as you inteded.
The second issue (which is somehow linked) is that your function removeEntry() is supposed to remove the entry just after the one pointed to by the function argument. So if pList points to an entry, you should remove the entry pointed by pList->next. If you do so, you will solve your first issue at the same time...
The third issue, is that you should check whether the element you want to remove does exists before removing it. Eventually, free the memory corresponding to the element you remove, but in your case this does not apply as you do not allocate entries dynamically.
Yay! Thanks to kaylum and Heyji I finally managed this! Thank you both!
Always funny with C how a problem seems so difficult, and when you figure it out you wonder how you could ever have struggled with something so simple....
void removeEntry(struct entry *pList)
{
struct entry *pTemp = pList->next;
// <-------&n3
pList->next = pTemp->next;
}

Changing value of Linked List without flexiblility in parameters

I have an assignment. I was provided with a function declaration that I cannot modify.
The function declaration is void Insert (Item x, int p, List *L); where I am supposed to change the values of the linked list struct, L.
Now, the code that invokes that method in my main function is
struct List *L = malloc(sizeof(List));
Insert(x,p,L);
How would I change my code so I can pass the address of the struct List instead of making another copy of it?
Like I said, I cannot change the function declaration at all.
/*********************************************************************
* FUNCTION NAME: Insert
* PURPOSE: Inserts an Item in a List.
* ARGUMENTS: . The Item to be inserted (Item)
* . The position in the List
* where the Item should be inserted in (int)
* . The address of the List (List *L)
* REQUIRES (preconditions):
* . The position should be a nonnegative integer
* not greater than the size of the List.
* . The List should not be full.
* ENSURES: . Empty will return false (0).
* . Size will return the first integer greater
* than the size of the List before the call.
* . Peek in the same position will find
* the Item that was inserted.
*********************************************************************/
extern void Insert (Item X, int position, List *L);
What I tried that didn't work was
head->next = L; //changing the next item in list to L
L = head; //changing the address of L so it remains the head of the list
I think this will work:
void Insert (Item x, int p, List *L) {
struct List newnode, last = *L;
newnode = (struct List)malloc(sizeof(struct List));
newnode->item = x;
newnode->next = NULL;
if (last == NULL){
if (p == 0) { *L = newnode; }//first node
else { printf("List is empty and index %d does not exist", p); }
} else if (p == 0) {
newnode->next = *L;
*L = newnode;
}
else{
int counter = 0;
while (1) {
if (counter == p) {
newnode->next = last->next;
last->next = newnode;
break;
}
last = last->next;
counter++;
if (last->next == NULL && counter != p){ break; }
}
}
}
You might want to review the differences (or similarities) between addresses, pointers in C because the answer to your question is that you are already passing the address of your struct List.
I will try and explain a bit. malloc() returns a memory address, AKA a pointer, which is denoted by the * symbol in struct List *L. When I read this line in my head, I would say "L contains a pointer to a struct List object" or "L contains the memory address of a struct List object".
So in that case, when you write Insert(L, x) you are already passing a pointer to your struct List object. And no other copy is being made. So any actions you perform inside of your Insert() function will be acting on the original list.
The only exception to this rule is if you try to reassign L in your insert method, like this:
void Insert(struct List* L, int x) {
L = NULL; // or L = malloc(sizeof(struct List));
}
This will not do what you might expect, but the reason for that is more complicated and something you probably don't need to know for the time being.
There are irregularities with your function declarations.
Your code suggests that you have a function with the following signature :
void Insert (struct List *L, int x);
The header file that you have linked uses a function insert with this signature :
extern void Insert (Item X, int position, List *L);
Note the additional parameter Item X.
There isn't much to go on since you haven't showed us much of your code, but I would suggest taking a look at your missing parameter.
First of all:
How would I change my code so I can pass the address of the struct List instead of making another copy of it?
You are already doing it: void Insert (Item x, int p, List *L); takes pointer to List structure, not structure itself, so you are not copying it.
Secondly:
What I tried that didn't work was head->next = L; //changing the next item in list to L
L = head; //changing the address of L so it remains the head of the list
It won't work. Inside Insert function L is value of pointer to List. So if you change this value inside the function it won't be change outside, as value of L is passed by copy. Only changes to *L will be seen, as they will not change value of L itself, but value of what L is pointing to.
I can assure you, that no L = is needed inside Insert function to complete this task.
Moreover take good look at prerequisites - they should discribe situations, which you do not need to trouble yourself with (probably, I don't know person which created this task, but I would understand it this way) - meaning, if user doesn't comply to them, it's users own fault, anything bad can happen.

linked list insert function - passing a list by pointer

Im trying to create linked-list insert function that takes a list (or more correctly a pointer to it) then inserts the value to the end of the list.
void ll_insert(struct ll **l, int n){
struct ll *temp=NULL;
while ( (*l) != NULL){
temp= (*l);
(*l) = (*l)->next;
}
(*l)= (struct ll*)malloc(sizeof(struct ll));
(*l)->n=n;
(*l)->next=NULL;
if (temp) temp->next= (*l);
}
int main(void){
struct ll *l=NULL;
ll_insert(&l, 1);
printf("%d ", l->n);
ll_insert(&l, 1);
ll_insert(&l, 2);
ll_insert(&l, 3);
printf("%d ", l->n);
}
The output after running the code above is 1 3. This is no surprise, since
(*l) = (*l)->next;
updates the list to point to to the end node, and every time I run insert(...) the list's head is updated to point to the end (if im not wrong). What's the way around this?
You are not using the pointer to pointer correctly: this line in the while loop
(*l) = (*l)->next;
should be
l = &((*l)->next);
If you use it that way, you wouldn't need your temp variable at all.
Since this is C, not C++, it is OK to not cast malloc.
Your function should only change *l if it is inserting into an empty list, as this is the only case where the first element of the list changes. This can be accomplished by using a local variable instead of *l inside of your function for those cases (initialized to *l).
if you don't move the pointer l, then it remains at the head of the list. First assign l to temp, then move temp along the list but leave pointer l alone.

Pointer isn't chaning after function calls in C

I have this popFront function for a linked list:
Node* popFront(Node* list)
{
Node* returnMe = (Node*)malloc(sizeof(Node));
Node* oldHead = list;
returnMe->mData = list->mData;
if (list->mNext != NULL)
list = list->mNext; // This line is definitely called
else
list = blankNode; // And not this one
free(oldHead);
return returnMe;
}
But whenever I call it like this:
Node* list = buildList(10);
Node* oldHead = popFront(list);
printInfo(list);
'oldHead' is correct but 'list' still has its first Node.
You need to read up on the difference between pass by value and pass by reference. The pointer list is passed to the function popFront() by value, meaning that its value is copied into the local variable in the function. When you change that copy, the original variable outside of popFront() is unchanged. If you want to be able to change the value of list, then you need to pass a pointer to list (i.e., pass it by reference) and modify popFront() accordingly to work with a pointer to pointer to Node.
Here is a simpler example to understand:
void foo(int x)
{
x = 2;
}
int main()
{
int x = 3;
foo(x);
// if you understand why x is still 3 here,
// then you'll understand your problem as well.
return 0;
}
If you want the variable list to change, you need to pass it by pointer. Since the value of list is Node*, you would need to pass to have the signature of popFront be Node* popFront(Node **list).

C Linked list only contains first element...not sure what happens to rest

I posted a question a few days ago about a linked list in C. I thought everything was ok then the prof emails us saying that instead of this signature:
int insert_intlist( INTLIST* lst, int n); /* Inserts an int (n) into an intlist from the beginning*/
He accidentally meant:
int insert_intlist( INTLIST** lst, int n); /* Inserts an int (n) into an intlist from the beginning*/
I thought to myself cool now that I have a pointer to a pointer I can move the pointer outside of main and when I return to main I will still have my complete linked list.
He starts off with giving us this:
INTLIST* init_intlist( int n )
{
INTLIST *lst; //pointer to store node
lst = (INTLIST *)malloc(sizeof(INTLIST)); //create enough memory for the node
lst->datum = n; //set the value
lst->next = NULL; //set the pointer
return lst; //return the new list
}
Which is simply to initialize the list like so in main:
if (lst==NULL)
lst = init_intlist(i);
else
insert_intlist(lst, i);
lst is of type INTLIST* so its defined as INTLIST* lst. So I read in some numbers from a text file like 1 3 4 9.
It is supposed to create a linked list from this...so the first number would go to init_intlist(1); And that was defined above. Then it grabs the next number 3 in this case and calls insert_intlist(lst, 3). Well here is my insert_intlist and all I want to do is insert at the beginning of the list:
int insert_intlist(INTLIST** lst, int n )
{
INTLIST* lstTemp; //pointer to store temporary node to be added to linked list
lstTemp = (INTLIST *)malloc(sizeof(INTLIST)); //create enough memory for the node
lstTemp->datum = n; //assign the value
//check if there is anything in the list,
//there should be, but just in case
if(*lst == NULL)
{
*lst=lstTemp;
lstTemp->next=NULL;
}
else
{
lstTemp->next = *lst; //attach new node to the front
*lst = lstTemp; //incoming new node becomes the head of the list
}
return 0;
}
So if the list contained 1 initially this function would simply create a new node and then make this temp node->next point to the head of the list (which I thought was lst) and then reassign the head of the list to this new temp node.
It all looks like it is running right but when I try to print my list to the screen it only prints the number 1.
Anyone have any clues as to what I am doing wrong?
You're being passed a pointer to a pointer. You want to alter the pointer that is being pointed to, not the pointer to a pointer itself. Does that make sense?
if(lst == NULL)
Here you're checking to see if you were passed a NULL pointer. Good practice for error checking, but not for what you were doing right there. If lst is NULL, then you don't even have a pointer to a pointer, can't do anything, and should return a nonzero error code without doing anything else.
Once you're sure your pointer is not NULL, then you look at the pointer it's pointing to (*lst). The pointed-to pointer is the pointer to the first list item. If that pointer is NULL, then you change it to the new item's pointer. Basically, where you use lst you should be using *lst or (*lst). (REMEMBER: the * operator runs after the -> operator! So to get a field in the object pointed to by the pointer that is pointed to by lst [pant, pant], you use (*lst)->whatever.)
P.S. This kind of pointer work is critical to learn to be a good programmer, especially with C.
P.P.S. The other thing you got wrong is that instead of
insert_intlist(lst, i);
you're supposed to call it like
insert_intlist(&lst, i);
... and, for brownie points, check the return code for errors.
The first problem that pops into my mind is that in insert_intlist() you're doing lst = lstTemp;. This should be *lst = lstTemp;. This way you assign to the list pointer that you were supplied rather than to the list pointer pointer (which does not update anything outside of the function).
Since you are using a pointer to another pointer in the insert function, you can change at which memory location the latter actually points to. I changed the insert code a bit and it works fine:
int insert_intlist(INTLIST** lst, int n )
{
INTLIST* lstTemp; //pointer to store temporary node to be added to linked list
lstTemp = (INTLIST *)malloc(sizeof(INTLIST)); //create enough memory for the node
lstTemp->datum = n; //assign the value
//check if there is anything in the list,
//there should be, but just in case
if(*lst == NULL)
{
lstTemp->next=NULL;
*lst = lstTemp;
}
else
{
lstTemp->next = *lst; //attach new node to the front
*lst = lstTemp; //incoming new node becomes the head of the list
}
return 0;
}
EDIT: I see you edited your question. In that case, maybe you are calling the insert function in the wrong way in main(). Try this
int main()
{
INTLIST *head;
head = init_intlist(42);
insert_intlist(&head, 41);
display(head);
return 0;
}
Check to see if lstTemp->next == NULL after lstTemp->next=lst;

Resources