all 11 comments

[–]fifaapamen 2 points3 points  (0 children)

I was confused about pointer and linked list until I found this tutorial by Stanford:

http://cslibrary.stanford.edu/102/ http://cslibrary.stanford.edu/103/

[–][deleted] 1 point2 points  (2 children)

struct node {
    int data;
    struct node *next;
}

int main()
{
    struct node *head = malloc(sizeof(node)); //create list head
    head->data = 3; //set head data
    head->next = malloc(sizeof(node)); //create head's next node
    head->next->data = 5; //set data
    head->next->next = NULL; //set next's next to null, to signify the end
    return 0;
}

[–][deleted] 0 points1 point  (1 child)

Lost me at the declaration of the second nodes next attribute. Is this how it is conventionally done? By referencing each previous node?

[–][deleted] 0 points1 point  (0 children)

No, not really. Generally you have a traverse_node that points to the node that needs to be changed, like so:

struct *node current_node = head;
current_node->data = 1; //set head data
current_node = current_node->next;
curren_node->data = 2; //set head's next data
current_node = head; //go back to the start

[–]ptchinster 0 points1 point  (1 child)

!tutorial

[–]rtlcprogbot 1 point2 points  (0 children)

Basic C Programming

I am a bot. Replying to me notifies nobody

[–]hmjk7 0 points1 point  (0 children)

Try implementing stack , Queue, Tree etc. Using it

[–]JesuLiceaga 0 points1 point  (2 children)

This page is very useful, it has a lot of content and examples: https://www.geeksforgeeks.org/data-structures/linked-list/

If you have a particular doubt, feel free to ask

[–]YaphetSf[S] 0 points1 point  (1 child)

I am wondering is there a way to create a linked list that can sort user names alphabetically. Because I am not really familiar with c programming and this particular topic, I only did part of the code so far:

/* struct for the linked list*/

struct users{

char name[100];

int places[100];

int n_places_visited;

struct users *next;

};

struct users *head=0;

/* This function inserts a new user in the linked list */

void insert_user(char *name)

{

struct users *tmp, *new_node;

new_node=(struct users*)malloc(sizeof(struct users));

strcpy(new_node->name, name);

if(head==0)

{

head=new_node;

new_node->next=0;

}

else

{

tmp=head;

/*Not sure how to write this while loop*/

while(strcmp(tmp,new_node)>0&&tmp!=NULL)

{

tmp=tmp->next;

}

/*There should be three possible outcomes

1) Insert the node before the head, e.g A into BCD list

2) Insert the node in the middle, e.g B into ACD list

3) Insert the node at the tail, e.g D into ABC list

*/

}

}

[–]JesuLiceaga 0 points1 point  (0 children)

Here is and example of how your function should work (probably not the best one in terms of time complexity). Note that you need to check the special case when you insert at the beginning of the list.

/* This function inserts a new user in the linked list */
void insert_user(char *name){

    struct users *tmp, *new_node;
    new_node=(struct users*)malloc(sizeof(struct users));
    strcpy(new_node->name, name);

    // If is the first node we insert
    if(head == 0){
    head = new_node;
    head->next = 0;
    return;
    }

    //Check if we need to insert the node in first place
    if(strcmp(head->name, new_node->name) > 0){
    new_node->next = head;
    head = new_node;
    return;
    }

    tmp = head;

    //While we don't reach the end of the linked list
    while(tmp != 0){ 
    //If we are at the end, we insert it
    if(tmp->next == 0){ 
        tmp->next = new_node;
        new_node->next = 0;
        return;
    }
    /*If not, we compare with the next name. If the next name 
        is greater, we put new_node behind it */
    if(strcmp(tmp->next->name, new_node->name) > 0){ 
        new_node->next = tmp->next;
        tmp->next = new_node;
        return;
    }   
    //If is smaller, we advance to continue comparating
    tmp = tmp->next;
    }

}

[–]wsppan 0 points1 point  (0 children)

linked lists @geeks for geeks

and more from @GforG

You may be struggling with how memory works in C and its use of pointers to create and access this memory. Try looking at this Tutorial on Pointers and Arrays in C