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. Stack.top (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) {
this.data = data;
this.next = 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);
this.size++;
}else{
Node newNode = new Node(element);
newNode.next = this.head;
this.head = newNode;
this.size++;
}
}
public int pop() {
//Implement the pop() function
if(this.size != 0){
int element = this.head.data;
this.head = this.head.next;
this.size--;
return element;
}else{
return -1;
}
}
public int top() {
//Implement the top() function
if(this.size == 0){
return -1;
}
return this.head.data;
}
}

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 *