Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package myUtil;
- public class LStack<T> extends java.lang.Object implements Stack<T>
- {
- private Node<T> top = null;
- public LStack(){
- }
- public T push(T item){
- top = new Node(item, top);
- return item;
- }
- public T pop(){
- T removed = top.getData();
- top.setNext(top.getNext());
- return removed;
- }
- public T peek(){
- return this.top.getData();
- }
- public boolean empty(){
- if (this.size() == 0){
- return true;
- }else{
- return false;
- }
- }
- public int size(){
- if (top == null){
- return 0;
- }
- int count = 1;
- Node<T> next = top;
- while (next.hasNext()){
- count++;
- next = next.getNext();
- }
- return count;
- }
- }
- class Node<T> {
- private T data;
- private Node<T> next;
- public Node(T data, Node<T> next) {
- this.data = data;
- this.next = next;
- }
- public boolean hasNext() {
- return this.next != null;
- }
- public Node<T> getNext() {
- return this.next;
- }
- public void setNext(Node<T> next) {
- this.next = next;
- }
- public T getData() {
- return this.data;
- }
- public void setData(T data) {
- this.data = data;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement