LinkedList push() Method in Java
The java.util.LinkedList.push() method is used to push an element at the starting(top) of the stack represented by LinkedList. This is similar to the addFirst() method of LinkedList and simply inserts the element at the first position or top of the linked list.
Syntax:
LinkedListObject.push(Object element)
Parameters: The method accepts one parameter element of object type and represents the element to be inserted. The type ‘Object’ should be of same Stack represented by the LinkedList.
Return Type: The return type of the method is void i.e. it doesn’t returns any value.
Below programs illustrate the java.util.LinkedList.push() method:
Program 1:
import java.util.LinkedList;
public class GfG {
public static void main(String[] args)
{
LinkedList<String> stack = new LinkedList<>();
stack.push( "I" );
stack.push( "Like" );
stack.push( "GeeksforGeeks" );
System.out.println(stack);
}
}
|
Output:
[GeeksforGeeks, Like, I]
Program 2 :
import java.util.LinkedList;
public class GfG {
public static void main(String[] args)
{
LinkedList<Integer> stack = new LinkedList<>();
stack.push( 30 );
stack.push( 20 );
stack.push( 10 );
System.out.println(stack);
}
}
|
Last Updated :
10 Dec, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...