/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package DataStructures; import ADTs.StackADT; import Exceptions.EmptyCollectionException; /** * * @author Qiong * @param */ 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 { return null; //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() { return count == 0; } @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(); } } }
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package DataStructures;
import ADTs.StackADT;
import Exceptions.EmptyCollectionException;
/**
*
* @author Qiong
* @param <T>
*/
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 {
return null;
//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() {
return count == 0;
}
@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();
}
}
}
![Your job is to write code for the push and peek methods. The pop0, 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
Push
Push
1. Create a new linear
node, passing in the
eiement to te pushed.
1. Create a re Ineor
2 Assign the re
node's nest to be
node, pessing in the
ement to be pushed
the same as top.
lop
nul
nul
Ref to
ineor node
Ref to
linear node
Count
Count
3
3
int
Int
Turte
object
Turte
Tutie
otjet
Turte
Tute
object
Ture
Ture
otject
Turtie
otject
ohject
object
object
3
4
Push
Push
2. Assign the new
node's rext to be
the same n top.
1. Create a new inear
1. Create anew neer
node, passing in the
element to be pushed
2. Assign the new
node's nest to be
he same as top
3. Assign top to pont
to the rew node
node, pasing in the
elemant to be pushed
3. Aasign top to point
o the new node
4. Inerement count
lop
nul
Ref to
Ref to
Inear nede
linear node"
Count
count
4
int
int
Turtle
Turtie
Tute
Turte
Turtle
Turte
Turtie
Turtie
otjact
object
object
otject
object
otjact
abjact
obet](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Ff1bf2399-f913-4a71-a1ed-43d29c065340%2F829a78c2-6edc-4b6e-bc1f-ba39f86c0e0e%2F0tjfna_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)