




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Data structures, Queue, FIFO, Implementation using link list
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





public class SimpleQueue { private Object[] queue; private int size; // size of the queue private int top; // for next insertion private int base; // for next removal public SimpleQueue(int capacity) { queue = new Object[capacity]; size = top = base = 0; } public boolean isEmpty() { return (size == 0); } public boolean isFull() { return (size==queue.length); } public void enqueue(Object item) { if (isFull()) { throw new RuntimeException(“queue overflow”); } else { queue[top++] = item; if (top == queue.length)top = 0; ++size; } } public Object dequeue() { if (isEmpty()) { throw new RuntimeException(“queue underflow”); } else { Object item = queue[base++]; if (base == queue.length)base = 0; --size; return item; } } }
import java.util.NoSuchElementException; public class StackList implements Stack { private ListNode top; public StackList() { top = null; } public boolean isEmpty() { return (top == null); } public void push(Object item) { top = new ListNode(item, top); } public Object pop() { if (top == null) { throw new NoSuchElementException(); } else { Object item = top.data; top = top.next; return item; } } }