Author - StudySection Post Views - 64 views
Stacks in Java

Available methods for Stacks using Linked List in Java

  1. Constructor: Initializes the Data member as a requirement.
  2. Stack.push(data) (public function): It takes one argument as Integer and pushes it into the stack.
  3. Stack.pop() (public function): It removes the last element from the stack and if the size of the stack is 0 , returns -1
  4. (public function): It gives the top element in the stack and if the stack size is 0, it returns -1;
  5. Stack.size() (public function): Returns the size of the stacks.
  6. Stack. is_Empty() (public function): Returns boolean value true if the stack is Empty false if not

Implementation of Stacks

package Recursion;
Following is the structure of the node class for a Singly Linked List
class Node {
int data;
Node next;
public Node(int data) { = data; = null;
public class Stack {
//Define the data members
Node head;
int size;
public Stack() {
//Implement the Constructor
this.head = null;
this.size = 0;
/*----------------- Public Functions of Stack -----------------*/
public int getSize() {
//Implement the getSize() function
return this.size;
public boolean is_Empty() {
//Implement the isEmpty() function
return this.size == 0;
public void push(int element) {
//Implement the push(element) function
if(this.head == null){
this.head = new Node(element);
Node newNode = new Node(element); = this.head;
this.head = newNode;
public int pop() {
//Implement the pop() function
if(this.size != 0){
int element =;
this.head =;
return element;
return -1;
public int top() {
//Implement the top() function
if(this.size == 0){
return -1;

Knowledge of .NET is quite rewarding in the IT industry. If you have got some skills in the .NET framework then a .NET certification from StudySection can prove to be a good attachment with your resume. You can go for a foundation level certificate as well as an advanced level certificate in the .NET framework.

Leave a Reply

Your email address will not be published. Required fields are marked *