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.
Hi all. I am trying to create a program that uses a linked list of structures. I have the structure and the linked-list main pointer defined as follows:
I already have the linked-list created. I have read the items from a file and loaded the list. Now I am trying to recursively delete the last entry in the list. Here is my "remove" function:
Code:
void remove_an_ssid(ssidlist *ss,int i) /* The i variable is for testing */
{
printf ("Removal: %i\n",++i); /* Only for testing purposes 8?
if (ss->next == NULL)
{
printf("Removing SSID: %s\n",ss->ssid);
free(ss->ssid);
free(ss->next);
ss = NULL;
}
else
{
remove_an_ssid(ss->next,i);
}
}
I am using the following call to start the removal of the last entry in the linked list.
Code:
remove_an_ssid(&ssids,0);
What happens is that the last entry in the list is getting "freed", but the linked list is not updating properly. So, when I write the entries in the linked list to a file, the last thing written to the file is bad characters. Here is my write function:
I just tried that. When I compiled it, this is the result I got:
Code:
mdhkarma.c: In function ‘in_ssids_list’:
mdhkarma.c:62: warning: assignment from incompatible pointer type
mdhkarma.c: In function ‘write_ssids’:
mdhkarma.c:79: warning: assignment from incompatible pointer type
mdhkarma.c: In function ‘remove_an_ssid’:
mdhkarma.c:92: error: dereferencing pointer to incomplete type
mdhkarma.c:94: error: dereferencing pointer to incomplete type
mdhkarma.c:95: error: dereferencing pointer to incomplete type
mdhkarma.c:101: warning: passing argument 1 of ‘remove_an_ssid’ from incompatible pointer type
mdhkarma.c: In function ‘on_probe_req’:
mdhkarma.c:154: warning: passing argument 1 of ‘remove_an_ssid’ from incompatible pointer type
mdhkarma.c:198: warning: assignment from incompatible pointer type
mdhkarma.c: In function ‘load_ssids’:
mdhkarma.c:230: warning: assignment from incompatible pointer type
make: *** [mdhkarma.o] Error 1
The errors are pointing to the ss->next->next and the ss->next->ssid references.
int remove_an_ssid (ssidlist **head) {
int r;
if ((r = (*head) ? (1 + remove_an_ssid(&((*head)->next))) : 0) == 1) {
free((*head)->ssid);
free(*head);
*head = NULL;
}
return r;
}
That one even removes the final element in a list with one element (at least... it should); I don't think any of the previously posted code handled that right. But you use it differently:
Code:
ssidlist *thelist = generate_an_ssidlist();
/* this would remove every element */
while (thelist)
remove_an_ssid(&thelist);
... maybe.
If you don't need to do it recursively, for any particular reason, though, you may want to consider using a doubly-linked list, and storing a tail pointer as well as a head pointer. Then you can remove the last element in constant time, and not recursively.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.