• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Need c programming help. Linked Lists

aceO07

Diamond Member
Can someone provide basic code for linked lists? Here is the outline of what I need it for. I understand what a linked list is, unfortunately I haven't done it in C before or have forgotten.

#include <stdio.h>

struct nlist
{
struct nlist *next;
char *name;
};

static struct nlist *usersList;

main ()
{
add("user1");
remove("user2");
}


int add (char *s)
{
//can't figure out code

}


int remove
{
//can't figure out code
}
 
for the add fun,

the *usersList is just a pointer to an item in your linked list, you use this to manipulate the individual items. to add an item, you will:

1. use a temp variable to save what the current usersList->next is pointing to.
2. set usersList->next to "new nlist"
3. set usersList to usersList->next //now your pointer is pointing to the new item which you just created
4. set usersList->name to the passed variable, s
5. set usersList->next to the temp variable from step 1

to remove an item,
(you'll have to pass the name like you did in the add fn.)

1. iterate through the LL until you find the item before the item w/ name == searchName
(ie, check usersList->next->name == searchName, if false, usersList = usersList->next and check the condition again. other wise, we've found it and you can stop searching)
2. save what usersList->next->next points to to a temp variable
3. delete usersList->next and remove from memory
4. set usersList->next to the temp variable from 2

hope that's right, i'm a bit rusty
 
Back
Top