Question

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.

Homework Answers

Answer #1

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) + " ");
}
       }
   }
}

Below is the Main Class for testing:

public class Main{

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

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
IN JAVA Language- Singly Linked List Implementation Implement a Linked List in your language. Use your...
IN JAVA Language- Singly Linked List Implementation Implement a Linked List in your language. Use your Can class. You need to create a driver that makes several Can objects and places them in alphabetical order in a list. Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods. NOT USE your language's Library List . You will receive zero points. Write a LinkedList class. Include...
In five sentences describe the following: a) how you would implement a stack using an array,...
In five sentences describe the following: a) how you would implement a stack using an array, including the push and pop operation b) how you could implement a queue using a linked list, including what type of linked list would be best, the enqueue and dequeue operations
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h" template StackLinked::StackLinked (int maxNumber) { } template StackLinked::StackLinked(const StackLinked& other) { } template StackLinked& StackLinked::operator=(const StackLinked& other) { } template StackLinked::~StackLinked() {    clear(); } template void StackLinked::push(const DataType& newDataItem) throw (logic_error) {    } template DataType StackLinked::pop() throw (logic_error) { } template void StackLinked::clear() {    StackNode* t;    while ( top != NULL)    {        t = top;       ...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following Without using LinkedList from the JAVA LIBRARY. and please include methods for each function. Create a menu that contains the following operations : 1. Add new node to DLL. ( as a METHOD ) 2. Delete a node from DLL. ( as a METHOD ) 3. Show how many nodes in DLL. ( as a METHOD ) 4. Print all data in the DLL....
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists....
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists. Recall that double-ended means keeping first and last references and doubly linked feature allows us to go backwards, using a prev reference at each Link. Also, note that the greater the number, the lower the priority. For instance, 2 is of higher priority compared to 5. Specifically, write a class LinkedListPriorityQ which implements the priority queue methods: boolean isEmpty() void enqueue(int item) int dequeue()...
Stack Queue Program Implement a Stack class using the List Class you developed earlier. Test your...
Stack Queue Program Implement a Stack class using the List Class you developed earlier. Test your Stack class by determining if the following strings have matching open and closing ( ) and/or [ ] and/or { }. To test matches, push open (, [, { onto the stack. Input a close char, pop off the stack and see if input matches symbol form stack.   Use the following data to test your code:                               ( )                               [ ] ( )...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(),...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(), and isEmpty(). For this assignment, enqueue() will be implemented in an unusual manner. That is, in the version of enqueue() we will use, if the element being processed is already in the queue then the element will not be enqueued and the equivalent element already in the queue will be placed at the end of the queue. Additionally, you must implement a circular queue....
Using Java, in the most simple algorithm please Implement a static stack class of char. Your...
Using Java, in the most simple algorithm please Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...
Instructions: SLLStack (12 pts) ● Using the two properties below, implement the stack interface using the...
Instructions: SLLStack (12 pts) ● Using the two properties below, implement the stack interface using the SLLNode class from Lab 2: ○ top_node → SLLNode object or None ○ size → int, keep track of stack size ● Implement the push( ), pop( ), top( ) methods ● Use SLLNode methods: get_item( ), set_item( ), get_next( ), set_next( ) ● (5 pts) In push(item): ○ Create new SLLNode with item ○ Add new node before top node ○ Update top_node...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT