Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- GENERAL TASK DESCRIPTION
- Implement barebones (meaning - no getters/setters, or anything extraneous
- In task descriptions after data structure name the required interface (list of public methods) is in parentheses
- Use proper formatting. Implement each data structure in a separate class. For Node classes use inner classes.
- For brevity use primitive integer as value type (no generics).
- */
- // Task 1 - singly-linked list (add, get, remove) add(), get(i), remove(i), remove(O)
- // Task 2 - stack (push, pop)
- // Task 3 - queue (offer, poll)
- // Task 4 - flexible-size list (using arrays - think of ArrayList) - (add, get, remove, insert)
- class Stack {
- private Node last;
- public void push(Integer value) {
- if (last == null)
- last = new Node(value, null);
- else {
- Node node = new Node(value, last);
- last = node;
- }
- }
- public Integer pop() { // get should return the value, not the node
- if (last == null)
- throw new IllegalStateException();
- Node node = last;
- last = last.next;
- return node.value;
- }
- public Integer top() {
- if (last == null)
- throw new IllegalStateException();
- return last.value;
- }
- public boolean empty() {
- return last == null;
- }
- static class Node {
- public Integer value;
- public Node next;
- Node(Integer value, Node next) {
- this.value = value;
- this.next = next;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement