Question

In: Computer Science

IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...

IN JAVA LANGUAGE

Linked List-Based Stack Implementation

Implement Stack using a Linked List

  • Use the language library LinkedList
  • Stack methods will call the LinkedList methods
  • You can use string as the object

Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array implementation, you wrote code to manipulate the array. For this linked list implementation, methods already exist.

Before the underlying implementation of stack was array, now the underlying implementation of stack will be Linked list.

Solutions

Expert Solution

Below is the stack implementation using the LinkedList Class.


import java.util.*;

//StackUsingLinkedList is the class which uses LinkedList for stack implementation

public class StackUsingLinkedList {
//LinkedList of String Object
   LinkedList<String> ll;
   public StackUsingLinkedList() {
       ll = new LinkedList<>();
   }
//add the string object to top of the stack
   public void push(String nodeValue) {
       ll.addFirst(nodeValue);
   }
//checks if the stack is empty
   public boolean isEmpty() {
       if (ll.size() == 0) {
           return true;
       }
       return false;
   }
//prints the top of the stack and also removes it
   public void pop() {
       if (ll.size()==0) {
           System.out.println("stack is empty");
       }
       else {
           System.out.println(ll.getFirst());
           ll.removeFirst();
       }
   }
//returns the top of the stack value
   public String peek() {
       if (ll.size()==0) {
           System.out.println("stack is empty");
           return "";
       }
       else {
           return ll.getFirst();
       }
   }
//deletes the stack
   public void deleteStack() {
       ll.clear();
   }
//prints the size of stack
   public int size() {
       return ll.size();
   }

//displays the stack from top to bottom
   public void printStackDown()
   {
       // check for stack underflow
       if (ll.size() == 0) {
           System.out.println("No data in the stack");
       }
       else {
           for (int i = 0; i < ll.size(); i++) {
          
   System.out.println(ll.get(i) + " ");
   }
       }
   }
//main method for testing
   public static void main(String[] args) {
          
           StackUsingLinkedList m = new StackUsingLinkedList();
           m.push("Hi");
           m.push("Hello");
           System.out.println("size of stack: " + m.size());
           System.out.println("printing the stack: " );
           m.printStackDown();
           System.out.println("pop the stack" );
           m.pop();
           m.push("How r u");
           System.out.println("printing the stack: " );
           m.printStackDown();
           System.out.println("pop the stack" );
           m.pop();
           System.out.println("pop the stack" );
           m.pop();
           System.out.println("printing the stack: " );
           m.printStackDown();
           System.out.println("pop the stack" );
           m.pop();
   }
}


Related Solutions

IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language library LinkedList Queue methods will call the LinkedList methods You can use string as the object Instead of using an array, as the QueueLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : enqueue(), dequeue(), size(), printQueue(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate that it can solve the Tower of Hanoi problem. Write implementation body of method “infixToPrefix(String[] e)” of class ArithmeticExpression to convert infix expressions into prefix expressions.
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.
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
Data structure program Implement (your own) the Radix Sort using single linked list java language
Data structure program Implement (your own) the Radix Sort using single linked list java language
using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
How do you implement stack by using linked list? No code just explain it.
How do you implement stack by using linked list? No code just explain it.
Implement a non-recursive reverse print of linked list using stack and the main function to test:...
Implement a non-recursive reverse print of linked list using stack and the main function to test: You will need to finish the printReversed_nonrecursive method in ch04.LinkedStack2 class, and the ch04.UseStack2 is the main function to test. public class LinkedStack2<T> extends LinkedStack<T> { private void revPrint(LLNode<T> listRef) { if (listRef != null) { revPrint(listRef.getLink()); System.out.println(" " + listRef.getInfo()); } } public void printReversed() { revPrint(top); } /* use stack to implement non-recursive reverse print */ public void printReversed_nonrecursive() { } public...
Description( IN C++)!! The purpose of this challenge is to implement a stack using a Linked...
Description( IN C++)!! The purpose of this challenge is to implement a stack using a Linked List as a backing data structure Requirements Write the following structs struct Location { string name; string address; }; struct VisitNode { Location loc; VisitNode * next; }; Create a class called Stack. In this class, create a private variable VisitNode * head. This will keep track of the location of the head node. Add the following private function. This function will be used...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT