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,851 questions

51,772 answers

573 users

How to insert items in linear (single) linked list: insert at front, insert at end, insert in middle in C

1 Answer

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);
item *insert_at_front(item *head, int val);
void insert_at_end(item *head, int val);
void insert_in_middle(item *head, int item_number, int val);
  
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; // // you can use scanf()
      curr = curr->next;
    }
    curr->next = NULL;

    head = insert_at_front(head, 0); 
    insert_at_end(head, 11);
    insert_in_middle(head, 3, 3000);
    insert_in_middle(head, 7, 7000);

    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;
    }
}
item *insert_at_front(item *head, int val)
{
    item *new_item = (item *)malloc(sizeof(item));
    if (new_item == NULL) exit(1);
    new_item->n = val;
    new_item->next = head;
    
    return new_item;
}
void insert_at_end(item *head, int val)
{
    item *end = head;
    
    while (end->next)
        end = end->next;
    
    item *new_item = (item *)malloc(sizeof(item));
    if (new_item == NULL) exit(1);
    new_item->n = val;
    new_item->next = NULL;       
    end->next = new_item;
}

void insert_in_middle(item *head, int item_number, int val)
{
    item *curr = head;
    int i = 1;
    
    while (i < item_number - 1) 
    {
        curr = curr->next;
        i++;
    }
    
    item *new_item = (item *)malloc(sizeof(item));
    if (new_item == NULL) exit(1);
    new_item->n = val;
    new_item->next = curr->next;;       
    curr->next = new_item;
}

/*
  
run:
  
0
1
3000
2
3
4
7000
5
6
7
8
9
10
11
 
*/

 



answered Dec 16, 2015 by avibootz
edited Dec 16, 2015 by avibootz
...