package DataStructures; import ADTs.StackADT; import Exceptions.EmptyCollectionException; /** * * @author Qiong */ public class LinkedStack implements StackADT { int count; SinglyLinkedNode top; public LinkedStack(){ top = null; count = 0; } public LinkedStack(T data){ top = new SinglyLinkedNode(data); count = 1; } @Override public void push(T element) { // TODO implete the push method // The push method will insert a node with holds the given input into the top of the stack } @Override public T pop() throws EmptyCollectionException { if (this.isEmpty()) throw new EmptyCollectionException(); SinglyLinkedNode node = top; top = top.getNext(); count--; node.setNext(null); return node.getElement(); } @Override public T peek() throws EmptyCollectionException { //TODO: Implement this method //This should look like pop, except we aren�t changing the stack at all. Just returning the element. } @Override public boolean isEmpty() { if (count == 0) { return true; } return false; } @Override public int size() { return count; } @Override public String toString() { if (top != null) { return "LinkedListStack{" + "count=" + count + ", top=" + top.getElement() + '}'; } else { return "LinkedListStack{" + "count=" + count + '}'; } } public static void main(String argv[]){ StackADT cities = new LinkedStack(); try{ cities.push("Tokyo"); cities.push("Atlanta"); cities.pop(); cities.pop(); cities.push("Miami"); cities.pop(); cities.push("Charlotte"); System.out.println("Charlotte".equalsIgnoreCase(cities.peek())); System.out.println(1 == cities.size()); }catch (Exception ex){ ex.printStackTrace(); } } }

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter17: Linked Lists
Section: Chapter Questions
Problem 3SA
icon
Related questions
Question
100%

package DataStructures;

import ADTs.StackADT;
import Exceptions.EmptyCollectionException;

/**
*
* @author Qiong
*/
public class LinkedStack<T> implements StackADT<T> {

int count;
SinglyLinkedNode<T> top;

public LinkedStack(){
top = null;
count = 0;
}

public LinkedStack(T data){
top = new SinglyLinkedNode(data);
count = 1;
}

@Override
public void push(T element) {
// TODO implete the push method
// The push method will insert a node with holds the given input into the top of the stack


}

@Override
public T pop() throws EmptyCollectionException {
if (this.isEmpty())
throw new EmptyCollectionException();

SinglyLinkedNode<T> node = top;
top = top.getNext();
count--;
node.setNext(null);
return node.getElement();
}

@Override
public T peek() throws EmptyCollectionException {
//TODO: Implement this method
//This should look like pop, except we aren�t changing the stack at all. Just returning the element.

}

@Override
public boolean isEmpty() {
if (count == 0) {
return true;
}
return false;
}

@Override
public int size() {
return count;
}

@Override
public String toString() {
if (top != null) {
return "LinkedListStack{" + "count=" + count + ", top=" + top.getElement() + '}';
} else {
return "LinkedListStack{" + "count=" + count + '}';
}
}

public static void main(String argv[]){
StackADT<String> cities = new LinkedStack<String>();
try{
cities.push("Tokyo");
cities.push("Atlanta");
cities.pop();
cities.pop();
cities.push("Miami");
cities.pop();
cities.push("Charlotte");
System.out.println("Charlotte".equalsIgnoreCase(cities.peek()));
System.out.println(1 == cities.size());
}catch (Exception ex){
ex.printStackTrace();
}
}
}

ry_Stacks - Apache NetBeans IDE
<default conf...
821.8/1045MB
ts x
Files
Services
DS_Library_Stat...
.age LinkedStack.java x
8 LinkedStackTest.java x
SinglyLinkedList.java
DS_Library_Stacks
Source Packages
Source
History
1 E
/*
ADTS
* To change this license header, choose License Head
* To change this template file, choose Tools | Templ
* and open the template in the editor.
2
DataStructures
ArrayListStack.java
ArrayStack.java
O CArrayList.java
DoublyLinked List.java
DoublyLinkedNode.java
LinkedStack.java
SinglyLinked List.java
SinglyLinkedNode.java
4
*/
6.
package Datastructures;
7
8 - import ADTS. StackADT;
9
import Exceptions. EmptyCollectionException;
10
11
**
Exceptions
12
Test Packages
* @author Qiong
13
DataStructures
14
*/
public class LinkedStack<T> implements StackADT<T> {
DoublyLinked ListTest.java
LinkedStackTest.java
SinglyLinked ListTest.java
16
int count;
SinglyLinkedNode<T> top;
17
18
Libraries
19
hamcrest-core-1.3.jar
public LinkedStack (){
20
junit-4.12.jar
JDK 15 (Default)
Test Libraries
ITCS2214
Test Results x
Output
DataStructures.LinkedStack Test x
DataStructures.
Tests passed: 45.45 %
LinkedListStack{
e 5 tests passed, 6 tests failed. (0.219 s)
A DataStructures.LinkedStackTest Faile
>>
>>
图萄图图图
Transcribed Image Text:ry_Stacks - Apache NetBeans IDE <default conf... 821.8/1045MB ts x Files Services DS_Library_Stat... .age LinkedStack.java x 8 LinkedStackTest.java x SinglyLinkedList.java DS_Library_Stacks Source Packages Source History 1 E /* ADTS * To change this license header, choose License Head * To change this template file, choose Tools | Templ * and open the template in the editor. 2 DataStructures ArrayListStack.java ArrayStack.java O CArrayList.java DoublyLinked List.java DoublyLinkedNode.java LinkedStack.java SinglyLinked List.java SinglyLinkedNode.java 4 */ 6. package Datastructures; 7 8 - import ADTS. StackADT; 9 import Exceptions. EmptyCollectionException; 10 11 ** Exceptions 12 Test Packages * @author Qiong 13 DataStructures 14 */ public class LinkedStack<T> implements StackADT<T> { DoublyLinked ListTest.java LinkedStackTest.java SinglyLinked ListTest.java 16 int count; SinglyLinkedNode<T> top; 17 18 Libraries 19 hamcrest-core-1.3.jar public LinkedStack (){ 20 junit-4.12.jar JDK 15 (Default) Test Libraries ITCS2214 Test Results x Output DataStructures.LinkedStack Test x DataStructures. Tests passed: 45.45 % LinkedListStack{ e 5 tests passed, 6 tests failed. (0.219 s) A DataStructures.LinkedStackTest Faile >> >> 图萄图图图
Part 2: Complete the LinkedStack implementation
Your job is to write code for the push and peek methods. The pop(), size() and isEmpty() methods are already done.
1. Examine the code for the LinkedStack implementation, in particular the pop() method.
2. Write a peek() method. This should look like pop, except we aren't changing the stack at all. Just returning the
element.
3. Write a push() method. Remember that the push algorithm will look like this:
1
2
Transcribed Image Text:Part 2: Complete the LinkedStack implementation Your job is to write code for the push and peek methods. The pop(), size() and isEmpty() methods are already done. 1. Examine the code for the LinkedStack implementation, in particular the pop() method. 2. Write a peek() method. This should look like pop, except we aren't changing the stack at all. Just returning the element. 3. Write a push() method. Remember that the push algorithm will look like this: 1 2
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning