Question

In: Computer Science

Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...

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();

}

Solutions

Expert Solution

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.


Related Solutions

Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
1. (10 pts) Define the nodes in the LinkedList. Create the LinkedList using the ListNode class....
1. (10 pts) Define the nodes in the LinkedList. Create the LinkedList using the ListNode class. Create a method to find a node with given value in a LinkedList. Return the value is this value exists in the LinkedList. Return null if not exists. Use these two examples to test your method. Example 1: Input: 1 -> 2 -> 3, and target value = 3 Output: 3 Example 2: Input: 1 -> 2 -> 3, and target value = 4...
In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may...
In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may not use any standard Java or C++ libraries. Assume your data structure only allows Strings. Implement the following operations for the data structure: Queue: enqueue, dequeue, create, isEmpty (10 points) Stack: push, pop, create, isEmpty (10 points) Here is a link to get started on transferring from Java to C++ http://www.horstmann.com/ccj2/ccjapp3.html (Links to an external site.) Upload a zip file with one implementation for...
Remove the minimum element from the linked list in Java public class LinkedList {      ...
Remove the minimum element from the linked list in Java public class LinkedList {       // The LinkedList Node class    private class Node{               int data;        Node next;               Node(int gdata)        {            this.data = gdata;            this.next = null;        }           }       // The LinkedList fields    Node head;       // Constructor    LinkedList(int gdata)   ...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
Code in Java Create a stack class to store integers and implement following methods: 1) void...
Code in Java Create a stack class to store integers and implement following methods: 1) void push(int num): This method will push an integer to the top of the stack. 2) int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3) void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a stack class to store integers and implement following methods: 1- void...
Program in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Create in Java Create a stack class to store integers and implement following methods: 1- void...
Create in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
JAVA Write a class for a Stack of characters using a linked list implementation. Write a...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a class for a Queue of characters using a linked list implementation. Write a class for a Queue of integers using a circular array implementation.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT