Basic functions of link list - Add and print

`// Function to add new nodes to the linked list
void add(node * head , int value)
{
   temp = (mynode *) malloc(sizeof(struct node));
   temp->next=(mynode *)0;
   temp->value=value;

   if(head==(mynode *)0)
   {
      head=temp;
      tail=temp;
   }
   else
   {
     tail->next=temp;
     tail=temp;
   }
}`

`// Function to print the linked list…
*void print_list(struct node head)
{
  mynode *temp;

  printf("\n[%s] -> “, listName);
  for(temp=head;temp!=NULL;temp=temp->next)
  {
    printf("[%d]->”,temp->value);
  }

  printf(“NULL\n”);

}`

Similar code in java:

public class ListNode<T>{  
    public T data;  
    public ListNode<T\> next;      
    public ListNode(T value){  
        data \= value;  
    }  
      
    public String toString(){  
        return data.toString();  
    }  
}  

public class LinkedList<T> {  
    ListNode<T\> head;  
  
    // Function to add new nodes to the linked list   
    public void add(T value)   
    {   
        ListNode<T\> temp \= new ListNode<T\>(value) ;  
        temp.next\=null;  
  
        if(head\=\=null) {  
            head\=temp;   
            return ;  
        }   
  
        ListNode<T\> current \= head;  
        while(current.next!\=null)  
        {  
            current\=current.next;  
        }  
        current.next\=temp;  
    }  
//---Other functions  
}  

See the code for linked list here.


See also