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 linear (single) linked list that hold 10 int numbers (1, 2, 3...10) in C

3 Answers

0 votes
#include <stdio.h>
#include <stdlib.h> 
 
struct int_linked_list
{
   int n;
   struct int_linked_list *next;
};

typedef struct int_linked_list item;
 
int main(void)
{
    item *curr, *head;
    int i = 1;

    head = curr = (item *)malloc(sizeof(item));
    curr->n = i;
    
    for (i = 2; i <= 10; i++)
    {
      curr->next = (item *)malloc(sizeof(item));
      curr->next->n = i;
      curr = curr->next;
    }
    curr->next = NULL;

    curr = head;
    while (curr) 
    {
      printf("%d\n", curr->n);
      curr = curr->next;
    }
        
    // free the linked list
    curr = head;
    for (i = 1; i <= 10; i++)
    {
        curr = curr->next;
        free(head);
        head = curr;
    }
        
    return 0;
}

  
/*
 
run:
 
1
2
3
4
5
6
7
8
9
10
 
*/


answered Feb 7, 2015 by avibootz
edited Feb 7, 2015 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h> 
  
struct int_linked_list
{
   int n;
   struct int_linked_list *next;
};
 
typedef struct int_linked_list item;

void print_linked_list(item *head);
void free_linked_list(item *head);
  
int main(void)
{
    item *curr, *head;
    int i = 1;
 
    head = curr = (item *)malloc(sizeof(item));
    curr->n = i;
     
    for (i = 2; i <= 10; i++)
    {
      curr->next = (item *)malloc(sizeof(item));
      curr->next->n = i;
      curr = curr->next;
    }
    curr->next = NULL;
 
    print_linked_list(head);
         
    free_linked_list(head);
         
    return 0;
}

void print_linked_list(item *head)
{
    item *curr = head;
    
    curr = head;
    while (curr) 
    {
        printf("%d\n", curr->n);
        curr = curr->next;
    } 
}
void free_linked_list(item *head)
{
    item *curr = head;
    
    while (curr) 
    {
        curr = curr->next;
        free(head);
        head = curr;
    }
}

/*
  
run:
  
1
2
3
4
5
6
7
8
9
10
  
*/

 



answered Dec 16, 2015 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h> 
  
struct int_linked_list
{
   int n;
   struct int_linked_list *next;
};
 
typedef struct int_linked_list item;

void print_linked_list(item *head);
void free_linked_list(item *head);
  
int main(void)
{
    item *curr, *head;
    int i = 1;
 
    head = curr = (item *)malloc(sizeof(item));
    if (head == NULL) exit (1);
    curr->n = i;
     
    for (i = 2; i <= 10; i++)
    {
      curr->next = (item *)malloc(sizeof(item));
      if (curr->next == NULL) exit (1);
      curr->next->n = i;
      curr = curr->next;
    }
    curr->next = NULL;
 
    print_linked_list(head);
         
    free_linked_list(head);
         
    return 0;
}

void print_linked_list(item *head)
{
    item *curr = head;
    
    curr = head;
    while (curr) 
    {
        printf("%d\n", curr->n);
        curr = curr->next;
    } 
}
void free_linked_list(item *head)
{
    item *curr = head;
    
    while (curr) 
    {
        curr = curr->next;
        free(head);
        head = curr;
    }
}

/*
  
run:
  
1
2
3
4
5
6
7
8
9
10
  
*/

 



answered Dec 16, 2015 by avibootz
...