The document defines two classes that implement a stack data structure: ArrayStack uses an array to store elements, while LinkedStack uses a linked list with Nodes. Both classes track the size of the stack and throw exceptions if empty or overflow. Key methods for both include push to add, pop to remove, top to access the top, size to get count, and isEmpty to check if empty.
The document defines two classes that implement a stack data structure: ArrayStack uses an array to store elements, while LinkedStack uses a linked list with Nodes. Both classes track the size of the stack and throw exceptions if empty or overflow. Key methods for both include push to add, pop to remove, top to access the top, size to get count, and isEmpty to check if empty.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online from Scribd
The document defines two classes that implement a stack data structure: ArrayStack uses an array to store elements, while LinkedStack uses a linked list with Nodes. Both classes track the size of the stack and throw exceptions if empty or overflow. Key methods for both include push to add, pop to remove, top to access the top, size to get count, and isEmpty to check if empty.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online from Scribd
this.capacity = capacity; data = new Object[capacity]; } } public int size() { return (top + 1); }
public boolean isEmpty() {
return (top < 0); }
public Object top() throws EmptyStackException {
if (top == -1) throw new EmptyStackException("empty"); return data[top]; }
public void push(Object o) throws StackOverflowException {
if (top == capacity - 1) throw new StackOverflowException(); data[++top] = o; }
public Object pop() throws EmptyStackException {
Object o; if (top == -1) throw new EmptyStackException(); o = data[top]; data[top--] = null; return o; } class Node { private Object info; private Node next; public Node(Object info, Node next) { this.info = info; this.next = next; } void setInfo(Object info) {this.info = info;} void setNext(Node next) {this.next = next;} Object getInfo() {return info;} Node getNext() {return next;} } public class LinkedStack implements Stack { private Node top; private int size;
public LinkedStack() { top = null; size = 0; }
public int size() {
return size; }
public boolean isEmpty() {
return (top == null); }
public void push(Object info) {
Node n = new Node(info, top); top = n; size++; }
public Object top() throws EmptyStackException {
if (top == null) throw new EmptyStackException(); return top.getInfo(); }
public Object pop() throws EmptyStackException {
Object info; if (top == null) throw new EmptyStackException(); info = top.getInfo(); top = top.getNext(); size--; return info; } public interface Queue { public int size(); public boolean isEmpty(); public void enqueue(Object o) throws QueueOverflowException; public Object dequeue() throws EmptyQueueException; public Object front() throws EmptyQueueException; }