-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedStack.java
More file actions
98 lines (77 loc) · 1.96 KB
/
LinkedStack.java
File metadata and controls
98 lines (77 loc) · 1.96 KB
1
2
3
package Graphs;/** A class of stacks whose entries are stored in a chain of nodes. @author Frank M. Carrano @author Timothy M. Henry @version 4.0
UPDATED by C. Lee-Klawender*/public class LinkedStack<T> implements StackInterface<T>{ private Node topNode; // References the first node in the chain
// ADD A PRIVATE INT FOR COUNTER THAT INDICATES HOW MANY NODES ARE IN THE STACK private int count=0; // ADDED FOR EXERCISE 2.2***************** public LinkedStack() { topNode = null; } // end default constructor public boolean push(T newEntry) { topNode = new Node(newEntry, topNode); ++count; // ADDED FOR EXERCISE 2.2***************** return true; } // end push public T peek() { if (isEmpty()) return null; else return topNode.getData(); } // end peek public T pop() { T top = peek(); if(topNode != null) { topNode = topNode.getNextNode(); --count; // ADDED FOR EXERCISE 2.2***************** } return top; } // end pop public boolean isEmpty() { return topNode == null; // FINISH FOR EXERCISE 2.2 } // end isEmpty// WRITE THE "MISSING" METHOD, REQUIRED FOR THIS CLASS SO IT'S NOT ABSTRACT (also Ex. 2.2): public int size(){ return count; } private class Node { private T data; // Entry in stack private Node next; // Link to next node private Node(T dataPortion) { this(dataPortion, null); } // end constructor private Node(T dataPortion, Node linkPortion) { data = dataPortion; next = linkPortion; } // end constructor private T getData() { return data; } // end getData private void setData(T newData) { data = newData; } // end setData private Node getNextNode() { return next; } // end getNextNode private void setNextNode(Node nextNode) { next = nextNode; } // end setNextNode } // end Node} // end LinkedStack