Note that there are some explanatory texts on larger screens.

plurals
  1. PODouble linked list implementation in c
    primarykey
    data
    text
    <p>I am trying to improve my c programming skills and so started with trying to program a double linked list.</p> <p>Here is what i have come up with so far.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;malloc.h&gt; //forward definition typedef struct Node node_t; //Define the structures needed for double linked list //Each node typedef struct Node { int data; node_t *next; node_t *prev; }node_t; void initList(node_t** first , node_t** last) { //Allocate memory for the first and the last node *first = (node_t*) malloc(sizeof(node_t)); *last = (node_t*) malloc(sizeof(node_t)); (*first)-&gt;data = 1; (*last)-&gt;data = 2; (*first)-&gt;prev = NULL; (*last)-&gt;next = NULL; (*first)-&gt;next = (*last)-&gt;prev; return; } void destroyList(node_t** first) { node_t* temp; temp = *first; free((*first)-&gt;next); free((*first)); temp = NULL; return; } int main() { node_t *first =NULL, *last = NULL; printf("Initalizing the List\n"); initList(&amp;first,&amp;last); printf(" 1st element is %d\n",first-&gt;data); printf(" 2nd element is %d\n",last-&gt;data); printf("Destroying the List\n"); destroyList(&amp;first) ; return 0; } </code></pre> <p>I actually looked up for some code online and i see that most implementations have</p> <p>1) 1 structure for Node and 1 structure for List itself (with head and tail). My question is, is this mandatory? Can i not implement it with just 1 structure?</p> <p>2) My idea is to make this file as a library and call it from an application. Like<br> InitList(), DestroyList(), AddNode, DeleteNode etc.</p> <p>And that is why i am using double pointers for the INit and destroy. I am having some trouble destroying the list. I know i am doing it wrong, i will continue to correct it.</p> <p>3) I found that node pointer</p> <pre><code> temp = first </code></pre> <p>was pointing to some address. If i do temp++. Why doesn't it point to next node?</p> <p>4)We can either pass the first or the last node pointer to delete the entire list correct?. ( i.e traverse and dleete sequentialluy?)</p> <p>Thanks!</p>
    singulars
    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.
 

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