Linked Lists: Pointers sent to functions as arguments do not change their valur
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Linked Lists: Pointers sent to functions as arguments do not change their valur
After long C vacations, I decided to create a linked list to see if I remembered things right. I see I don't. My linked list is supposed to be a queue.
In function mk_item, if tail equals NULL, then it means there is no linked list created. Hence, a list is created and both head and tail are forced to point into that direction. However, when this function exits, the value of head returns to the original NULL. I don't understand why, ,considering it is pointers we are talking about. What I am doing wrong? And how can I solve it? Thank you very much for your help
Code:
#include <stdio.h>
#include <stdlib.h>
/* List of structures */
struct list
{
int ID;
int value;
struct list* pointer;
};
typedef struct list list;
/* Function Prototypes */
void show_menu(void);
list* mk_item(list*, list*);
list* rm_item(list*, list*);
void print_list(list*);
/* Global variables */
int ID_assigner = 0;
/* main() */
int main()
{
char option;
list* tail = NULL;
list* head = NULL;
while (option != 'd')
{
show_menu();
scanf("%s", &option);
switch (option)
{
case 'a': case 'A':
tail = mk_item(tail, head);
printf("The value of head is %p\n" ,head);
break;
case 'b': case 'B':
head = rm_item(tail, head);
break;
case 'c': case 'C':
printf("%s \t\t %s \n", "ID", "Value");
print_list(tail);
break;
case 'd': case 'D':
break;
default :
printf("\aOoops! Invalid option!\n");
break;
}
}
printf("You have made a small program very happy :D Thank you for using me. \n");
return 0;
}
/* show_menu() */
void show_menu(void)
{
printf("Choose the correct option \n"
" a) Create a new item \n"
" b) Delete last item \n"
" c) Print items \n"
" d) Exit \n"
" $ ");
}
list* mk_item(list* tail, list* head)
{
if (tail == NULL)
{
tail = (list*) malloc(sizeof(list));
head = tail;
tail -> ID = ++ID_assigner;
tail -> pointer = NULL;
printf("Please provide the value of item ID %d: ", ID_assigner);
scanf("%d", &tail -> value);
printf("Value of tail is %p and value of head is %p\n", tail, head);
}
else
{
list* temp = NULL;
temp = (list*) malloc(sizeof(list));
temp -> ID = ++ID_assigner;
temp -> pointer = NULL;
printf("Please provide the value of item ID %d: ", ID_assigner);
scanf("%d", &temp -> value);
head -> pointer = temp;
head = temp;
}
return tail;
}
list* rm_item(list* tail, list* head)
{
list* temp = tail;
if (temp -> pointer == NULL || temp == NULL)
tail = NULL;
else
{
while ((temp -> pointer)-> pointer != NULL)
{
temp = temp -> pointer;
}
head = temp;
}
return head;
}
void print_list(list* tail)
{
if (tail == NULL)
{
printf("All printed\n");
}
else
{
printf("%d \t\t %d \n", tail -> ID, tail -> value);
print_list(tail -> pointer);
}
}
I am slightly confused by your logic here. But I did go out with the guys after work today so that might be why.....
The way I do link lists is to declare a structure (I will call it NODE) like the one you have created with some kind of data inside it and a next pointer. I start the program with head and tail (both NODE's) pointing to null. My routine that adds data would first check to see if head is NULL. If it is it creates a new NODE (mallocs data, sets the data value, and sets the next value to NULL) and points head to this node and points tail to head->next. If head isn't null it checks to see if tail is NULL, if it is it creates a new node where tail is currently pointing. If both head and tail aren't null then tail = tail->next and we create a new node where tail is currently pointing. To pull data off the queue we simply save the data from head, and point head to head->next. This of course assumes a FIFO queue.
I think maybe what you need to do is to pass the pointers to mk_item by reference, rather than by value. The two parameters to mk_item (list * tail, list * head) are local copies that are lost once the function returns, so if you set "tail = " something, it's only for the scope of that function.
wapcaplet is right but references are C++ if your doing it in C(seems like you are) you'll have to do it with a double pointer, redeclare mk_item like this
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.