CodesJava

Easy learning with example program codes

linked list push and pop in java


LinkedList:

The LinkedList class extends AbstractSequentialList and implements the List and Deque interface. It uses linked list data structure to store elements. It can contain duplicate elements. It is not synchronized.

Note: It not provides the random access facility.

Following methods are used for linked list push and pop operations:

  • push(): Pushes an element onto the stack represented by this list.
  • pop(): Pops an element from the stack represented by this list.

Example:

package com.codesjava;
 
import java.util.LinkedList;
 
public class Test {
  public static void main(String args[]){
	LinkedList<String> linkedList = new LinkedList<String>();
	linkedList.add("Jai");
	linkedList.add("Mahesh");
	linkedList.add("Naren");
	linkedList.add("Vivek");
	linkedList.add("Vishal");
	linkedList.add("Hemant");
	System.out.println("Actual LinkedList:"+linkedList);
	linkedList.push("Ajay");
        System.out.println("After push operation:");
        System.out.println(linkedList);
        linkedList.pop();
        System.out.println("After pop operation:");
        System.out.println(linkedList);
  }
}

Output

Actual LinkedList:[Jai, Mahesh, Naren, Vivek, Vishal, Hemant]
After push operation:
[Ajay, Jai, Mahesh, Naren, Vivek, Vishal, Hemant]
After pop operation:
[Jai, Mahesh, Naren, Vivek, Vishal, Hemant]
Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki