Q:

singly Linked List

belongs to collection: Singly Linked List Programs

0

Program to create and display singly Linked List.

Input:

Head Node = 100  

Second Node = 200  

Third Node = 300  

Output:

100
200
300

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C Program

#include<stdio.h>  
#include<stdlib.h>  
struct Node {  
int data;  
struct Node *next;  
};  
void display(struct Node *n)  
{  
while (n != NULL)  
  {  
printf(" %d \n", n->data);  
     n = n->next;  
  }  
}  
int main()  
{  
struct Node* head = NULL;  
struct Node* second = NULL;  
struct Node* third = NULL;  
  // allocate 3 nodes in the heap    
head = (struct Node*)malloc(sizeof(struct Node));   
second = (struct Node*)malloc(sizeof(struct Node));  
third = (struct Node*)malloc(sizeof(struct Node));  
head->data = 100; //assign data in first node  
head->next = second; // Link first node with the second node  
  // assign data to second node   
second->data = 200;   
  // Link second node with the third node  
second->next = third;  
third->data = 300; //assign data to third node  
third->next = NULL;  
display(head);  
return 0;  
}  

 

Output:

100 
 200 
 300 

 

Java Program

public class LinkedList  
{  
    Node head;  // head of list  
static class Node {  
int data;  
        Node next;  
Node(int d)  { data = d;  next=null; }  
    }  
  
    /* This function prints contents of the linked list starting from head */  
public void display()  
    {  
        Node n = head;  
while (n != null)  
        {  
System.out.print(n.data+" \n");  
            n = n.next;  
        }  
    }  
  
    /* method to create a simple linked list with 3 nodes*/  
public static void main(String[] args)  
    {  
        /* Start with the empty list. */  
LinkedList list = new LinkedList();  
  
list.head       = new Node(100);  
        Node second      = new Node(200);  
        Node third       = new Node(300);  
  
list.head.next = second; // Link first node with the second node  
second.next = third; // Link first node with the second node  
  
list.display();  
    }  
}  

 

Output:

100 
 200 
 300 

 

Python Program

# Node class  
class Node:  
  
    # Function to initialise the node object  
    def __init__(self, data):  
        self.data = data  # Assign data  
        self.next = None  # Initialize next as null  
  
# Linked List class contains a Node object  
class LinkedList:  
  
    # Function to initialize head  
    def __init__(self):  
        self.head = None  
  
    # This function prints contents of linked list  
    # starting from head  
    def display(self):  
        temp = self.head  
        while (temp):  
            print temp.data,  
            temp = temp.next  
  
# Code execution starts here  
if __name__=='__main__':  
  
    # Start with the empty list  
    list = LinkedList()  
  
    list.head  = Node(100)  
    second = Node(200)  
    third  = Node(300)  
  
    list.head.next = second; # Link first node with second  
    second.next = third; # Link second node with the third node  
  
    list.display()  

 

Output:

100 
 200 
 300 

 

PHP Program

<?php  
class Node  
{  
public $data;  
public $next;  
  
public function __construct($item)  
    {  
        $this->data = $item;  
        $this->next = null;  
    }  
}  
  
class LinkList  
{  
public $head = null;  
public $second=null;  
public $third=null;  
  
private static $count = 0;  
public function Insert() {  
            $this->head = new Node(100);  
           $this->second=new Node(200);  
           $this->third=new Node(300);  
  
            $this->head->next=$this->second;  
            $this->second->next=$this->third;  
  
        }  
  
  
public function PrintAsList()  
    {  
        $items = [];  
        $current = $this->head;  
while($current != null) {  
array_push($items, $current->data);  
            $current = $current->next;  
        }  
  
        $str = '';  
foreach($items as $item)  
        {  
            $str .= $item . ' ';  
        }  
  
echo $str;  
  
echo PHP_EOL;  
    }  
}  
  
$ll = new LinkList();  
  
$ll->Insert();  
$ll->PrintAsList();  
echo PHP_EOL;  
?>  

 

Output:

100 
 200 
 300 

 

Explanation

Linked List is a type of data structure in which information is stored in the form of a node which consists of data and address of next node. In a linked list, the node represents a single element.

Pictorial representation of a node:-

Representation of a node programmatically:-

C

struct Node  
{  
int data;  
struct Node *next;  
};  

 

Java

class Node  
    {  int data;  
        Node next;            
        // Constructor to create a new node  
        // Next is by default initialized as null  
        Node(int d) {data = d;}  
    }  

 

Python

classNode:  
    
    # Function to initialize the node object  
    def__init__(self, data):  
        self.data =data  # Assign data  
        self.next=None  # Initialize   
                          # next as null  

 

Php

class Node  
{  public $data;  
    public $next;  
    public function __construct($item)    {  
        $this->data = $item;  
        $this->next = null;    }  
}  

By using the above syntax of creating a node in each language. A linked list is created by creating each node object and giving some value to the data section of the node and linking to the next node by using the next pointer.

Steps to create a linked list by using node:-

1. Firstly we will create an object of the node and initialize them. Each object represents the single node.

C

struct Node* head = NULL;  

 

struct Node* second = NULL;  

  head = (struct Node*)malloc(sizeof(struct Node));   

  second = (struct Node*)malloc(sizeof(struct Node));  

  third = (struct Node*)malloc(sizeof(struct Node));  

 

Java

Node second      = new Node(200);  

 

        Node third       = new Node(300);  

 

Python

second = Node(200)  

    third  = Node(300)  

 

Php

public $head = null;  

    public $second=null;  

    public $third=null;  

$this->head = new Node(100);  

           $this->second=new Node(200);  

           $this->third=new Node(300);  

 

2. Finally, link nodes to each other. If there is no any next node to link, Next variable of node class will be initialized as null;

C

head->data = 100; //assign data in first node  

 

  head->next = second; // Link first node with the second node  

 

Java

 

list.head.next = second; // Link first node with the second node  

second.next = third; // Link first node with the second node  

 

Python

 

list.head.next = second; # Link first node with second  

second.next = third; # Link second node with the third node  

 

Php

 

$this->head->next=$this->second;  

            $this->second->next=$this->third;  

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Program to create and display a singly linked list... >>