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
- 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;       ...
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....
#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 <<...
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
Create a student record management system With JAVA using linked list and queue using Java language...
Create a student record management system With JAVA using linked list and queue using Java language and (oracle or any) database to save files and GUI Java swing to create background The program will have the following properties: A. Register students ( assume each students has ID, first name, last name and middle name) B. Register students with courses ( course no ,course title chr ) C. Able to maintain grade on which course they are registered D. Searches students...
Specify and implement an ADT character string by using a linked chain of characters. Include typical...
Specify and implement an ADT character string by using a linked chain of characters. Include typical operations such as fi nding its length, appending one string to another, fi nding the index of the leftmost occurrence of a character in a string, and testing whether one string is a substring of another. Do not #include<string>...you are making something like a STL string. Also do not use an array or vector...the point is to practice linked lists. Remember the last character...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty queue,...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists In this lab, you will create simple single linked structures consisting of Node objects. Each node will have a pointer to the next node. You will use a head pointer to keep track of the first node in the linked list, and a tail pointer to keep track of the last node in the linked list. Set both head and tail to NULL when...
Create another list to store your Song objects using a linked list. Show that you understand...
Create another list to store your Song objects using a linked list. Show that you understand how to use pointers and linked list by demonstrating the use of them with the Song objects. 8) Additional criteria for your code - Usability – your code provides users with meaningful messages/prompts for inputs and display of outputs. [3 points] - Accuracy - your code must run and terminate normally. [3 points] - Readability – your code should be well structured and easy...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT