Waste less time on Facebook — follow Brilliant.
×

Abstract Data Types

ADTs classify data structures based on usage and behavior, providing an understanding of the interface and responses.

Queues - Intermediate

The java code below is a simple implementation of a linked list based queue data structure. The three methods one(1),two(2) and three(3) need to be renamed. How should they be renamed so that they describe their functions?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    private int N;              
    private Node<object> first;    
    private Node<object> last;     

    private static class Node<object> {
        private object object;
        private Node<object> next;
    }

    /**
     * Initializes an empty queue.
     */
    public queue() {
        first = null;
        last  = null;
        N = 0;
    }

    public boolean isEmpty() {
        return first == null;
    }
    public int size() {
        return N;     
    }
    public object three() {
        return first.object;
    }

    public void one(object item) {
        Node<object> oldlast = last;
        last = new Node<object>();
        last.object = item;
        last.next = null;
        if (isEmpty())
            first = last;
        else     
            oldlast.next = last;
        N++;
    }
    public object two() {
        object item = first.object;
        first = first.next;
        N--;
        if (isEmpty())
            last = null;  
        return item;
    }
}

A polish expression or postfix expression is a notational form for logic, arithmetic, and algebra which places operators to the left of their operands. What is the value of the polish expression below when evaluated?

- 7 + * - 9 40 17 35

\(100\) children numbered \(1\) to \(100\) are gathered in a circle. Child \(100\) stands up and starts eliminating every fourth child starting from 4 she encounters (\(4,8,12,...\)). When a child is eliminated, he/she leaves the circle and child 100 continues to eliminate every fourth child looping around the circle. The last child to be eliminated wins the game. Which child wins this game?

A polish expression or postfix expression is a notational form for logic, arithmetic, and algebra which places operators to the left of their operands. What is the value of the polish expression below when evaluated?

- 7 + * - 9 40 17 35

The java code below is a simple implementation of a linked list based queue data structure. The three methods one(1),two(2) and three(3) need to be renamed. How should they be renamed so that they describe their functions?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    private int N;              
    private Node<object> first;    
    private Node<object> last;     

    private static class Node<object> {
        private object object;
        private Node<object> next;
    }

    /**
     * Initializes an empty queue.
     */
    public queue() {
        first = null;
        last  = null;
        N = 0;
    }

    public boolean isEmpty() {
        return first == null;
    }
    public int size() {
        return N;     
    }
    public object three() {
        return first.object;
    }

    public void one(object item) {
        Node<object> oldlast = last;
        last = new Node<object>();
        last.object = item;
        last.next = null;
        if (isEmpty())
            first = last;
        else     
            oldlast.next = last;
        N++;
    }
    public object two() {
        object item = first.object;
        first = first.next;
        N--;
        if (isEmpty())
            last = null;  
        return item;
    }
}
×

Problem Loading...

Note Loading...

Set Loading...