Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all you need to define the proper structure.The first times it will be easier for you to create a list that terminates when the pointer to the next node is NULL.Afterwards you will discover lists with sentinel, bidirectional lists and things that now may seem too complicated.<br> For example that's a structure: </p> <pre><code>typedef struct __node { int info; struct __node* next; }node; typedef node* list; </code></pre> <p>This time let's assume that list and node are the same thing, you will find more precise to separate the concept of list than the concept of node, and for example you may store in the list it's length (avoiding to count everytime all the nodes), but for now let's do it that way.<br> You initialize the list: </p> <pre><code>list l=NULL; </code></pre> <p>So the list contains zero nodes, to test if it's empty you just see if the pointer is NULL.<br> Add a new element: </p> <pre><code>if(NULL==l) { l=(node*)malloc(sizeof(node)); l-&gt;next=NULL; l-&gt;info=0; } </code></pre> <p>Now the list contains zero nodes, create a function to add a new node: </p> <pre><code>void pushBack(list* listPointer, int info) { if(NULL==*listPointer) { *listPointer=(node*)malloc(sizeof(node)); (*listPointer)-&gt;info=info; } else { node* ptr=l; while(ptr-&gt;next!=NULL) ptr=ptr-&gt;next; ptr-&gt;next=(node*)malloc(sizeof(node)); ptr-&gt;info=info; } } </code></pre> <p>You could also gain efficiency adding the elements in front.Or optimize the code by returning the added element, so that you don't have to find the last element everytime.I leave this to you.Now let's call the pushBack function for every element of the array: </p> <pre><code>for(int i=0; i&lt;N; i++) { pushBack(l,arr[i]); } </code></pre> <p>That's all, learn your way to implement linked lists.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload