title: Creating and Understanding linked lists of structs in C
body: <p>I am having trouble grasping the concepts of <code>struct</code> and the linked list data structure together. For example lets say we have have this code: a <code>struct</code> that has a worker's content and a linked list of these structs that contains nodes of each worker and a pointer to the next node(?). </p>
  
  <pre><code>    typedef struct Schedule {
          char name[10];
          char description[10];
          int hours;
          int workordernum;
      } Work;
  
      typedef struct linkedlist {
          struct Schedule work;
          struct linkedlist *next;
      } Node;
  </code></pre>
  
  <p>The problem is how do you make a method that always adds a node in the beginning of the list, a method that adds it anywhere(middle) in the list using user defined <code>workordernum</code>, and a method that always puts it in the end.</p>
  
  <p>I am not quite understanding <code>-&gt;</code> and <code>*</code> usages properly. I did read online about creating head and tail nodes but I didn't quite get its usage properly, since they had a <code>struct</code> for a list and a <code>struct</code> for a node.</p>
  
  <p>Another thing which I didn't get is, lets say adding a node to the beginning of the list works, how do you go about then changing every single <code>workordernum</code> value for all the nodes that were previously there?</p>
  
  <p>I understand one must keep track of every time a node gets added, removed, or moved, meaning every time these methods are called we must have a variable that keeps track of the number. So if we have a node in the list all ready, its order would be one, then we add another one to the beginning, how would we change the order number 1 to 2 and the one being added to 1?</p>
  
  <p>Or how does node->next->next->next work if we only have one pointer? Then how would we print all of them? Since we cannot use a <code>for</code> loop.</p>
  
  <p>So these are the concepts I cant grasp code wise. <strong>I would KINDLY appreciate it if you take your time to explain it rather than just give the code, if possible.</strong> Because I would have to apply what I learn to moving around and deleting nodes as well. I want to learn it on my own. If something must be given as a code example then that's fine but please just don't post all the answer codes for me.</p>
  
  <p>-Thank You</p>
  
  <p>*Please forgive any format errors since I am new to this site.</p>
  
  <p>Edit: I do understand a pointer is an address and that <code>-&gt;</code> pertains to "pointing to" a member. I mean I understand all the basics, but my understanding isn't firm enough else I could be doing what I need help with.</p>
  
  <p>Edit2: I will try and make a head node with linked list from what I have learned so far. I will be using the structs above and it will be loose code, not perfect. This is just to make sure I'm on the right track so far.</p>
  
  <pre><code>int main() {
  
     // creates a work struct to hold user content
     Work *workstruct = (Work*)malloc((sizeof(struct Schedule));
  
     // creates first node to hold a work struct for linkedlist
     Node *initialNode = (Node*)malloc((sizeof(struct linkedlist));
  
     // Method call to add work nodes to list in main
     addWork(initialNode, workstruct);
  
  }
  
  void addWork(Node *initialNode, Work *workstruct) {
  
     // Suppose user already initialized workstruct
  
     // create double-pointer to make initialNode as head
     Node **head = (Node **)malloc(sizeof(struct linkedlist));
  
     // assigns user's workstruct to the workstruct of initialNode
     initialNode-&gt;work = *workstruct;
  
     // If im understanding what you have taught me so far,
     // this is how you always add a node on the head
  
     initialNode-&gt;next = *head;
     *head = initialNode;
  }
  </code></pre>
  
  <p>The only issue I seem to have so far is that every time I try and add a new node to the list, it makes the new node the head but loses the previous node that was in the list.</p>