• Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Have any question?

(+91) 98222 16647
info@simplycoding.in
RegisterLogin
Simply Coding
  • Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Java

Single Linked List Programs in Java

  • Categories Java, Data Structures

To understand more about Linked List watch our both videos. To watch videos on Single Linked List click on Single Linked List Programs in Java and single Linked List Programs Part 2 

Q. What is linked list?
Ans. A linked list is a collection of elements, but the elements are not stored in a consecutive location. 

Q. What is the difference between array and link list.
Ans.

arraylink list
Arrays are stored at contiguous memory
location.
Linked lists are stored at different locations in the memory.
fixed sizedynamic size
insert and delete an element is difficultinsert and delete an element is easy
Array elements can be accessed randomly using the array index.Random accessing is not possible in linked lists. The elements will have to be accessed sequentially.

Q. What are the different types of Linked list?
Ans. Linked list are of three types:
               – Single Linked List
               – Double Linked List
               – Circular Linked list

 java program print, count Node, count if Node, sum of Node, sum If Node, product of Node, product If Node, min Node, max Node, search any Node operation on link list.

import java.util.Scanner;
class Node {
int data;
Node next;
Node(int d){
data = d;
next = null;
}
}

public class LinkedList
{
Node insertNode(Node start, int data, int pos)
{
Node n1 = new Node(data);
if (pos == 0 || start == null)
{
n1.next=start; return n1;
}
else if (pos < 0)
{
Node copy = start;
while (copy.next != null)
{
copy = copy.next;
}
copy.next = n1;
return start;
}
else
{
int step = 1;
Node copy = start;
while (copy.next != null)
{
if (step == pos) {
n1.next = copy.next;
copy.next = n1;
return start;
}
step++;
copy = copy.next;
}
System.out.println("Reached end of list");
return start;
}
}

void printNode(Node start) {
while (start != null) {
System.out.println(start.data);
start = start.next;
}
}

void countNode(Node start) {
int count = 0;
while (start != null) {
count++;
start = start.next;
}
System.out.println ("Count = " + count);
}

void countIfNode(Node start) {
int count = 0;
while (start != null) {
if(start.data >= 18)
count++;
start = start.next;
}
System.out.println ("Count = " + count);
}

void sumNode(Node start) {
int sum = 0;
while (start != null) {
sum = sum + start.data;
start = start.next;
}
System.out.println ("Sum = " + sum);
}

void sumIfNode(Node start) {
int sum = 0;
while (start != null) {
if (start.data%2 == 0)
sum = sum + start.data;
start = start.next;
}
System.out.println ("Sum = " + sum);
}

void productNode(Node start) {
int prod = 1;
while (start != null) {
prod = prod * start.data;
start = start.next;
}
System.out.println ("Product = " + prod);
}

void productIfNode(Node start) {
int prod = 1;
while (start != null) {
if(start.data%2 == 1)
prod = prod * start.data;
start = start.next;
}
System.out.println ("Product of odd = " + prod);
}

void minNode(Node start) {
int min = start.data;
while (start != null) {
if(start.data < min)
min = start.data;
start = start.next;
}
System.out.println ("Min Value = " + min);
}

void maxNode(Node start) {
int max = start.data;
while (start != null) {
if(start.data > max)
max = start.data;
start = start.next;
}
System.out.println ("Min Value = " + max);
}
void searchNode(Node start, int val) {
while (start != null) {
if (start.data == val){
System.out.println("found");
return;
}
start = start.next;
}
System.out.println("not found");
}

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int opt = 0;
Node start = null;
LinkedList llist = new LinkedList();
do {
System.out.println("Enter the option: 1 insert 2 print 3 countNode 4 countIfNode 5 sumNode 6 sumIfNode 7 productNode 8 productIfNode 9 minNode 10 maxNode 11 searchNode 12 exit ");
opt = input.nextInt();
switch (opt ) {
case 1:
System.out.print("Enter data");
int data = input.nextInt();
System.out.print("Enter position");
int pos = input.nextInt();
start = llist.insertNode(start,data,pos);
break;
case 2:
llist.printNode(start);
break;
case 3:
llist.countNode(start);
break;
case 4:
llist.countIfNode(start);
break;
case 5:
llist.sumNode(start);
break;
case 6:
llist.sumIfNode(start);
break;
case 7:
llist.productNode(start);
break;
case 8:
llist.productIfNode(start);
break;
case 9:
llist.minNode(start);
break;
case 10:
llist.maxNode(start);
break;
case 11:
System.out.print("Enter search Node");
int p = input.nextInt();
llist.searchNode(start,p );
break;
}
} while (opt != 12);
}
}

Link list insertion, deletion, reverse and rotate operation.

import java.util.Scanner;
class Node {
    int data;
    Node next;
    Node(int d){
       data = d;
       next = null;
    }
}
public class LinkedList
{
Node insertNode(Node start, int data, int pos)
{
     Node n1 = new Node(data);
     if (pos == 0 || start == null) 
     {
        n1.next=start;
        return n1;
     }
     else if (pos < 0)
     {
        Node copy = start;
        while (copy.next != null) {
          copy = copy.next;
        }
        copy.next = n1;
        return start;
     }
     else
     {
       int step = 1;
       Node copy = start;
       while (copy.next != null) {
         if (step == pos) 
         {
            n1.next = copy.next;
            copy.next = n1;
            return start;
         }
         step++;
         copy = copy.next;
       }
       System.out.println("Reached end of list");
       return start;
    }
  }
void printNode(Node start)
{
while (start != null)
{
System.out.println(start.data);
start = start.next;
}
} Node rotateList(Node start, int n) { int pos = 0; Node newStart = start, lastNode=start; Node copy = start; while (copy.next != null) { if (++pos == n) { lastNode = copy; newStart = copy.next; } copy = copy.next; } copy.next = start; lastNode.next = null; return newStart; }
Node reverseList(Node start)
{
Node newStart = null;
while (start!= null)
{
Node n1 = new Node(start.data);
n1.next = newStart;
newStart = n1;
start = start.next;
}
return newStart;
} Node deleteNode(Node start, int pos) { if (pos == 0 ) { return start.next; } else if (pos < 0) { Node copy = start; while (copy.next.next != null) { copy = copy.next; } copy.next = null; } else { Node copy = start; int step = 1; while (copy.next != null) { if (step == pos) { copy.next = copy.next.next; return start; } copy = copy.next; step++; } System.out.println("Reached end of list"); } return start; } public static void main(String[] args) { Scanner input = new Scanner(System.in); int opt = 0; Node start = null; LinkedList llist = new LinkedList(); do { System.out.print("Enter the option: 1 insert 2 print 3 rotate 4 reverse 5 delete 6 quit"); opt = input.nextInt(); switch (opt ) { case 1: System.out.print("Enter data"); int data = input.nextInt(); System.out.print("Enter position"); int pos = input.nextInt(); start = llist.insertNode(start,data,pos); break; case 2: llist.printNode(start); break; case 3: System.out.print("Enter no of items to rotate"); int i = input.nextInt(); start = llist.rotateList(start,i); llist.printNode(start); break; case 4: Node n1 = llist.reverseList(start); llist.printNode(n1); break; case 5: System.out.print("Enter position"); pos = input.nextInt(); start = llist.deleteNode(start,pos); break; } } while (opt != 6); } }

 

 

  • Share:
author avatar
Simply Coding

Previous post

Implementing Stack using Array in Java
August 28, 2021

Next post

ICSE 10 Computer Applications Specimen Paper (2020)
August 28, 2021

You may also like

Implementing Stack using Array in Java
Implementing Stack using Array in Java
28 August, 2021
Constructor Programs
Constructor Programs
3 July, 2021
factorialProg
Solve any Factor Program in 10 minutes
3 July, 2021

Leave A Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String
Simply Coding Computer Courses for School                Privacy Policy     Terms of Use     Contact Us

© 2021 Simply Coding

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now