In: Computer Science
Define empty methods in Stack class using LinkedList class in Java
-------------------------------------------------------------------------------
//Stack class
public class Stack{
public Stack(){
// use LinkedList class
}
public void push(int item){
// push item to stack
}
public int pop(){
// remove & return top item in Stack
}
public int peek(){
// return top item in Stack without removing it
}
public boolean isEmpty(){
// return true if the Stack is empty, otherwise false
}
public int getElementCount(){
// return current number of elements in Stack
}
public static void main(String[] args){
Stack s = new Stack();
int[] inputArr = {1, 2, 3, 4, 5, 6};
System.out.println("\nTrying to pop and peek on empty Stack:");
int test = s.pop();
test = s.peek();
System.out.println("\nPopulating the Stack:");
for (int v : inputArr){
System.out.println("Pushing: " + v);
s.push(v);
}
System.out.println("\nRemoving from the Stack:");
while (!s.isEmpty()){
System.out.println("Popping: " + s.pop());
}
System.out.println("\nTrying to pop and peek on empty Stack:");
test = s.pop();
test = s.peek();
}
}
------------------------------------
//Node class
public class Node{
private int value; // Stores the actual value
private Node nextNode; // Stores the link to the next Node object
public Node(int value){
setValue(value);
setNextNode(null);
}
public Node(int value, Node nextNode){
setValue(value);
setNextNode(nextNode);
}
// getters
public int getValue(){
return value;
}
public Node getNextNode(){
return nextNode;
}
// setters
public void setValue(int value){
this.value = value;
}
public void setNextNode(Node nextNode){
this.nextNode = nextNode;
}
@Override
public String toString(){
return String.valueOf(value);
}
}
-------------------------------------------------------------------------------
//LinkedList class
public class LinkedList implements LinkedListBehavior{
private int length;
private Node head;
private Node tail;
public LinkedList(){
length = 0;
head = null;
tail = null;
}
public boolean isEmpty(){
return head == null;
}
public int getLength(){
return length;
}
public Node getHead(){
return head;
}
public Node getTail(){
return tail;
}
public void append(Node newNode){
Node oldTail = getTail();
tail = newNode;
if (isEmpty()){
head = newNode;
}
else{
oldTail.setNextNode(tail);
}
length++; // update the current number of Node's in the LinkedList
}
public Node removeHead(){
if (isEmpty()){
System.out.println("\nLinkedList is empty. Can not remove head Node.");
return null;
}
Node oldHead = getHead();
head = oldHead.getNextNode();
oldHead.setNextNode(null);
if (isEmpty()){
tail = null;
}
length--;
return oldHead;
}
public Node removeTail(){
if (isEmpty()){
System.out.println("\nLinkedList is empty. Can not remove tail Node.");
return null;
}
Node oldTail = tail;
if (length == 1){
head = null;
tail = null;
length = 0;
return oldTail;
}
Node curNode = head;
while (curNode.getNextNode() != oldTail){
curNode = curNode.getNextNode();
}
curNode.setNextNode(null);
tail = curNode;
length--;
return oldTail;
}
@Override
public String toString(){
String output = "";
Node curNode = head;
while (curNode != null){
output += curNode + " ---> ";
curNode = curNode.getNextNode();
}
return output;
}
}
-------------------------------------------------------------------------------
public interface LinkedListBehavior{
public void append(Node newNode);
public Node getHead();
public Node getTail();
public Node removeHead();
public Node removeTail();
public boolean isEmpty();
public int getLength();
}
note: You've already created node, so I won't redefine the node.
Node top;
int count;
public class Stack{
public Stack(){
this.top = null;
}
public void push(int item){
Node temp = new Node();
if (temp == null) {
System.out.print("Stack overflow");
return;
}
temp = item;
temp.nextNode = top;
top = temp;
}
public int pop(){
if (top == null) {
System.out.print("\Stack underflow");
return;
}
top = (top).nextNode;
}
public int peek(){
if (!isEmpty(()) {
return top.value;
}
else {
System.out.print("Stack is empty");
}
}
public boolean isEmpty(){
return top == null;
}
public int getElementCount(){
count = 0;++;
Node temp = top;
while (temp != NULL) {
temp = temp.nextNode;
count++;
}
return count;
}
I've defined all the functions which were not defined.
If you have any doubts, feel free to ask
And if you like my answer, kindly upvote as an appreciation.
Happy learning.