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 a double 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;
   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);
item *insert_at_front(item *head, int val);
item *insert_at_end(item *tail, int val);
void insert_in_middle(item *head, int item_number, int val);
    
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);
	
	
	head = insert_at_front(head, 0); 
    tail = insert_at_end(tail, 11);
    insert_in_middle(head, 3, 3000);
    insert_in_middle(head, 7, 7000);
	
	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;
    }
}
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;
	head->prev = new_item;
    new_item->next = head;
	new_item->prev = NULL;
     
    return new_item;
}
item *insert_at_end(item *tail, int val)
{
  
    item *new_item = (item *)malloc(sizeof(item));
    if (new_item == NULL) exit(1);
    new_item->n = val;
    new_item->next = NULL; 
	new_item->prev = tail;	
    tail->next = new_item;
	
	return 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->prev = new_item;
	curr->next = new_item;
	new_item->prev = 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 Print with next (left to right):
0 1 3000 2 3 4 7000 5 6 7 8 9 10 11

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

 



answered Jan 1, 2016 by avibootz
...