Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,844 questions

51,765 answers

573 users

How to build a simple double linked list that hold 10 int numbers (1, 2, 3...10) in C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>
  
struct int_linked_list
{
   int n;
   struct int_linked_list *next;
   struct int_linked_list *prev;
};
 
typedef struct int_linked_list item;
  
int main(void)
{
    item *curr, *head, *tail;
    int i = 1;
 
    head = curr = (item *)malloc(sizeof(item));
    curr->n = i;
    curr->prev = NULL;
     
    for (i = 2; i <= 10; i++)
    {
      curr->next = (item *)malloc(sizeof(item));
      curr->next->n = i;
      curr->next->prev = curr;
      curr = curr->next;
    }
    curr->next = NULL;
    tail = curr;
 
    printf("Print with next (left to right):\n");
    curr = head;
    while (curr)
    {
      printf("%d ", curr->n);
      curr = curr->next;
    }
    
    printf("\n\nPrint with prev (right to left):\n");
    curr = tail;
    while (curr)
    {
      printf("%d ", curr->n);
      curr = curr->prev;
    }
         
    // free the linked list
    curr = head;
    while (curr)
    {
        curr = curr->next;
        free(head);
        head = curr;
    }
         
    return 0;
}

  
/*
 
run:
 
Print with next (left to right):
1 2 3 4 5 6 7 8 9 10

Print with prev (right to left):
10 9 8 7 6 5 4 3 2 1
 
*/


answered Feb 11, 2015 by avibootz
edited Dec 22, 2015 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
   
struct int_linked_list
{
   int n;
   struct int_linked_list *next;
   struct int_linked_list *prev;
};

typedef struct int_linked_list item;

void print_linked_list_ltr(item *head);
void print_linked_list_rtl(item *tail); 
void free_linked_list(item *head);
   
int main(void)
{
    item *curr, *head, *tail;
    int i = 1;
  
    head = curr = (item *)malloc(sizeof(item));
    curr->n = i;
    curr->prev = NULL;
      
    for (i = 2; i <= 10; i++)
    {
      curr->next = (item *)malloc(sizeof(item));
      curr->next->n = i;
      curr->next->prev = curr;
      curr = curr->next;
    }
    curr->next = NULL;
    tail = curr;
  
    printf("Print with next (left to right):\n");
    print_linked_list_ltr(head);
     
    printf("\n\nPrint with prev (right to left):\n");
    print_linked_list_rtl(tail);
          
    free_linked_list(head);
          
    return 0;
}
void print_linked_list_ltr(item *head) 
{
    item *curr = head;
    
    while (curr) {
        printf("%d ", curr->n);
        curr = curr->next;
    }
} 
void print_linked_list_rtl(item *tail) 
{
    item *curr = tail;
    
    while (curr) {
        printf("%d ", curr->n);
        curr = curr->prev;
    }
}
void free_linked_list(item *head)
{
    item *curr = head;
    
    while (curr) {
        curr = curr->next;
        free(head);
        head = curr;
    }
}


/*
  
run:
  
Print with next (left to right):
1 2 3 4 5 6 7 8 9 10
 
Print with prev (right to left):
10 9 8 7 6 5 4 3 2 1
  
*/

 



answered Dec 22, 2015 by avibootz
...