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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

/*
* 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
Transcribed Image Text: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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Generic Type
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education