Program To Create And Display Singly Linked List

Description

What is linked list	
	*) Linked List is linear collection of data structure.
	*) A record called node contain data and address of next node.
	*) Randomly stored in memory.
	
Advantage of linked list
	*) No need to declare the size in advance.
	*) Any number of nodes can be added.
	*) The elements of lists are not not present continuously. The elements are
	linked with each other by address. This provide complete optimization of space.
	*) Insertion and deletion can be done at any point in a constant time.

Disadvantage of linked list
	*) Elements can not be accessed randomly. Can be accessed in sequential manner

C/C++

/* C program of Program To Create And Display Singly Linked List */
//Save it as create_display_singly_linked_list.c

#include<stdio.h>
#include<malloc.h>
struct node *createLinkedList(int);
void dispalyLinkedList(struct node *);

//Declaring node
struct node{
    int data;
    struct node *next;
};

int main(){
    int n;
    struct node *head;
    printf("Enter the size of linked list : ");
    scanf("%d",&n);
    
    //Calling function to create node
    head=createLinkedList(n);
    
    //Calling function to display list
    dispalyLinkedList(head);

    return 0;
}

struct node *createLinkedList(int n){
    int i;
    struct node *head=NULL;
    struct node *newNode, *ptr;
    for(i=0;i<n;i++){
        
        //Creating a node
        newNode=(struct node *)malloc(sizeof(struct node));
        
        //Assigning data to newly created node
        printf("Enter the %d node data : ",(i+1));
        scanf("%d",&newNode->data);
        newNode->next=NULL;
        
        /*If list is empty assign the address of newly created node
        to head*/
        if(head==NULL){
            head=newNode;
        }else{
            
            /* If list already have few elements then loop through 
            list and add newly created node at the end of list*/
            ptr=head;
            while(ptr->next!=NULL){
                ptr=ptr->next;
            }
            ptr->next=newNode;
        }
    }
    return head;
}

void dispalyLinkedList(struct node *head){
    struct node *ptr;
    
    //If list is empty
    if(head==NULL){
        printf("The Linked List is empty");
    }else{
        
        /*If list has elements then loop through the loop and 
        print elements one by one in sequential manner*/
        ptr=head;
        while(ptr!=NULL){
            printf("The value of node is %d and their address is %u\n",ptr->data,ptr);
            ptr=ptr->next;
        }
    }
}
Input:
Enter the size of linked list : 5
Enter the 1 node data : 6
Enter the 2 node data : 7
Enter the 3 node data : 2
Enter the 4 node data : 5
Enter the 5 node data : 1

Output:
The value of node is 6 and their address is 4787536
The value of node is 7 and their address is 4787568
The value of node is 2 and their address is 4787584
The value of node is 5 and their address is 4787600
The value of node is 1 and their address is 4787616

Java

/* Java program of Program To Create And Display Singly Linked List */
//Save it as CreateDisplaySinglyLinkedList.java

import java.io.*;
import java.util.Scanner;

public class CreateDisplaySinglyLinkedList{
    
    
        //Declaring node
    class Node{
        int data;
        Node next;
        public Node(int data) {
            this.data=data;
            
            //Assigning null value to the next field
            this.next=null;
        }
    }
    
    public Node head=null;
    
    //Function to add node
    public void addNode(int data) {
        
        //Creating a new node
        Node newNode = new Node(data);
        
                /*If list is empty assign the address of newly created node
        to head*/
        if(head==null) {
            head=newNode;
        }else {
            
                    /* If list already have few elements then loop through 
            list and add newly created node at the end of list*/
            Node tail=head;
            while(tail.next!=null) {
                tail=tail.next;
            }
            tail.next=newNode;
        }
    }
    
    public void display() {
        Node ptr=head;
        
                //If list is empty
        if(ptr==null) {
            System.out.println("The list is empty");
            return;
        }
        System.out.println("The nodes of lists are : ");
        
             /*If list has elements then loop through the loop and 
               print elements one by one in sequential manner*/
        while(ptr!=null) {
            System.out.print(ptr.data+" ");
            ptr=ptr.next;
        }
    }

    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);
        
        CreateDisplaySinglyLinkedList list = new CreateDisplaySinglyLinkedList();
        
        System.out.println("Enter number of nodes : ");
        int n = scanner.nextInt();
        
        for(int i=0;i<n;i++) {
            System.out.println("Enter value of "+(i+1)+" node : ");
            int val = scanner.nextInt();
            list.addNode(val);
        }
        
                //Calling function to display list
        list.display();
    }
}
Input:
Enter number of nodes : 
5
Enter value of 1 node : 
4
Enter value of 2 node : 
2
Enter value of 3 node : 
3
Enter value of 4 node : 
7
Enter value of 5 node : 
5

Output:
The nodes of lists are : 
4 2 3 7 5

Related Programs

1) Program To Create And Display Circular Singly Linked List
2) Program To Add A Node In Beginning Of Singly Linked List
3) Program To Add A Node In The End Of Singly Linked List
4) Program To Add A Node After A Given Node Singly Linked List
5) Program To Delete Node From Beginning Singly Linked List
6) Program To Delete Given Node Singly Linked List
7) Program To Delete Node After Given Node Singly Linked List
8) Program To Delete All Nodes Of Singly Linked List
9) Program To Sort Elements Of Singly Linked List
10) Program To Reverse Elements Singly Linked List
11) Program to Search an Element In Singly Linked List
Share Me

Leave a Reply