Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A far better solution could be using list instead of stack to implement linked list. The code given is stack implementation of linked list, which I believe is a norm in python but in C/C++ you can use list for efficient implementation.</p> <p>A sample code in C would be as follows :-</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; struct node{ int data; struct node *next; }; struct node* add(struct node *head, int data){ struct node *tmp; if(head == NULL){ head=(struct node *)malloc(sizeof(struct node)); if(head == NULL){ printf("Error! memory is not available\n"); exit(0); } head-&gt; data = data; head-&gt; next = head; }else{ tmp = head; while (tmp-&gt; next != head) tmp = tmp-&gt; next; tmp-&gt; next = (struct node *)malloc(sizeof(struct node)); if(tmp -&gt; next == NULL) { printf("Error! memory is not available\n"); exit(0); } tmp = tmp-&gt; next; tmp-&gt; data = data; tmp-&gt; next = head; } return head; } void printlist(struct node *head) { struct node *current; current = head; if(current!= NULL) { do { printf("%d\t",current-&gt;data); current = current-&gt;next; } while (current!= head); printf("\n"); } else printf("The list is empty\n"); } void destroy(struct node *head) { struct node *current, *tmp; current = head-&gt;next; head-&gt;next = NULL; while(current != NULL) { tmp = current-&gt;next; free(current); current = tmp; } } void main() { struct node *head = NULL; head = add(head,1); /* 1 */ printlist(head); head = add(head,20);/* 20 */ printlist(head); head = add(head,10);/* 1 20 10 */ printlist(head); head = add(head,5); /* 1 20 10 5*/ printlist(head); destroy(head); getchar(); } </code></pre> <p>In the above example if you create an array of pointers with size 3, each of the pointer pointing to head, you can create three linked lists. This would handle the space with maximum efficiency and there is no need to check for free nodes too.</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.
    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