Thread: inserting nodes

  1. #1
    Registered User
    Join Date
    Jul 2022
    Posts
    17

    inserting nodes

    I am trying to insert a node into list of ordered values let's say 1, 2,3

    I can add the node when list is empty but I can't figured what to do add more nodes in list

    Code:
     #include <stdio.h>
    
    #include <stdlib.h>
    
    
    struct node
    {
    	int data;
    	struct node* next;
    };	
    
    
    void ADD ( struct node **Head, int value)
    {
    	
    	
    	if( *Head == NULL)
    	{
    		struct node *temp = malloc(sizeof(struct node));
    	
    		if(temp != NULL )       
    		{ 
    			temp -> data = value;
    			temp -> next = NULL;
    			*Head = temp;
    		}	
    	}
    	
    	else
    	{
    		
    	}
    }
    
    
    int main ()
    {
    	struct node *head = NULL;
    	
    	ADD ( &head, 1);
    	
    	ADD ( &head, 2);
    		
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    Quote Originally Posted by king12 View Post
    ...
    I can add the node when list is empty but I can't figured what to do add more nodes in list
    ...
    Well first you have to find a way to walk down a list that contains nodes.
    Second, you have to find a way to compare the values of the nodes with the value to insert.

  3. #3
    Registered User
    Join Date
    Jul 2022
    Posts
    17
    Quote Originally Posted by G4143 View Post
    Well first you have to find a way to walk down a list that contains nodes.
    .
    head should be set to null when list is empty
    Pointer of first node should be point to null when first node of list is created
    Pointer of second node should be point to first node when second node is createdPointer of third node should be point to second node when third node is created

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List - inserting nodes
    By johngoodman in forum C Programming
    Replies: 7
    Last Post: 02-19-2013, 12:13 AM
  2. Inserting Nodes at Binary Trees
    By blondeamon in forum C++ Programming
    Replies: 3
    Last Post: 12-25-2007, 08:27 AM
  3. Binary tree not inserting nodes correctly
    By jk1998 in forum C++ Programming
    Replies: 7
    Last Post: 09-22-2007, 12:37 PM
  4. inserting strings into linked list nodes
    By occams razor in forum C Programming
    Replies: 8
    Last Post: 03-31-2007, 12:17 AM
  5. Inserting new nodes in a sorted double linked list
    By carrja99 in forum C++ Programming
    Replies: 2
    Last Post: 03-07-2003, 08:34 AM

Tags for this Thread