Java LinkedList push() Method with Examples

In the previous article, we have discussed about Java LinkedList toArray() Method with Examples

In this article we are going to see the use of Java LinkedList push() method in Java along with suitable examples.

Java LinkedList push() Method with Examples

This java.util.LinkedList.push() method is used to push an element into the stack means here it simply inserts the element at the first position(head/top) of the linked list.

It returns no value i.e void.

Syntax:

LinkedListName.push()

Where,

  • LinkedListName refers to the name of your LinkedList.

Let’s see different examples to understand it more clearly.

Method-1: Java LinkedList push() Method – Example with String Type LinkedList

Approach:

  • Create a new LinkedList of type String.
  • Add string elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Now push the new element in the LinkedList using push( ) method.
  • Print the element.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of string datatype
        LinkedList<String> l = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l.add("windows");
        l.add("linux");
        l.add("android");
        l.add("ios");
        l.add("symbian");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // push the element into LinkedList
        l.push("solaris");
        l.push("ubuntu");
        // Prints the LinkedList elements after push() method
        System.out.println("The new elements of LinkedList are: "+l);
    }
}

Output:

The elements of LinkedList are: [windows, linux, android, ios, symbian]
The new elements of LinkedList are: [ubuntu, solaris, windows, linux, android, ios, symbian]

Method-2: Java LinkedList push() Method – Example with Integer Type LinkedList

Approach:

  • Create a new LinkedList of type Integer.
  • Add integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Now push the new element in the LinkedList using push( ) method.
  • Print the element.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of Integer datatype
        LinkedList<Integer> l = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l.add(20);
        l.add(521);
        l.add(132);
        l.add(173);
        l.add(14);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // push the element into LinkedList
        l.push(706);
        l.push(87);
        // Prints the LinkedList elements after push() method
        System.out.println("The new elements of LinkedList are: "+l);
    }
}

Output:

The elements of LinkedList are: [20, 521, 132, 173, 14]
The new elements of LinkedList are: [87, 706, 20, 521, 132, 173, 14]

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Related Java Programs: