View the step-by-step solution to:

Question

public class LinkedIntList {


private class Node {

int data;

Node

next;

Node(int item, Node n) {

data = item;

next = n;

}

}


private Node front;

private Node back;

private int numItems;


public LinkedIntList() {

front = null;

back = null;

numItems = 0;

}


public void addFront(int item) {

front = new Node(item, front);

if (back == null)

back = front;

numItems += 1;

}


public void addBack(int item) {

if (back == null) {

back = front = new Node(item, null);

} else {

Node t = new Node(item, null);

back.next = t;

back = t;

}

numItems += 1;

}


public int removeFront() {

int ret = front.data;

front = front.next;

if (front == null)

back = null;

numItems -= 1;

return ret;

}

public int size() {

return numItems;

}


public boolean isEmpty() {

return numItems == 0;

}


  // Your implementations of peek() and exchange() go here.


public static void main(String[] args) {

int i = 0;

assert(i++ == 0);

if (i != 1) {

System.out.println("Not running with assertions turned on!");

System.out.println("Use the -ea option to enable assertions.");

}


// how do I Create a new list make sure it's empty.

LinkedIntList ll = new LinkedIntList();

assert(ll.isEmpty());

assert(ll.size() == 0);

// how do I Add an item, and make sure that

// (1) the list is not empty

// (2) the list has one item

ll.addFront(1);

assert(!ll.isEmpty());

assert(ll.size() == 1);

// how do I Remove an item and make sure that

// (1) the list is empty

// (2) the list has zero items

// (3) we got back the value we pushed

int val1 = ll.removeFront();

assert(ll.isEmpty());

assert(ll.size() == 0);

assert(val1 == 1);

// How do I Add and remove two items.

ll.addFront(2);

assert(!ll.isEmpty());

assert(ll.size() == 1);

ll.addFront(3);

assert(!ll.isEmpty());

assert(ll.size() == 2);

int val3 = ll.removeFront();

int val2 = ll.removeFront();

assert(ll.isEmpty());

assert(ll.size() == 0);

assert(val3 == 3);

assert(val2 == 2);

// How do IAdd to the back.

ll.addBack(4);

assert(!ll.isEmpty());

assert(ll.size() == 1);

int val4 = ll.removeFront();

assert(ll.isEmpty());

assert(ll.size() == 0);

assert(val4 == 4);

// Test addBack then addFront

ll.addBack(5);

ll.addFront(6);

int val6 = ll.removeFront();

int val5 = ll.removeFront();

assert(ll.isEmpty());

assert(ll.size() == 0);

assert(val5 == 5);

assert(val6 == 6);

// Test addFront then addBack

ll.addFront(7);

ll.addBack(8);

int val7 = ll.removeFront();

int val8 = ll.removeFront();

assert(ll.isEmpty());

assert(ll.size() == 0);

assert(val7 == 7);

assert(val8 == 8);

// Test peek

ll.addFront(9);

int val9 = ll.peek();

assert(val9 == 9);

assert(!ll.isEmpty());

assert(ll.size() == 1);

ll.addBack(10);

val9 = ll.peek();

assert(val9 == 9);

assert(!ll.isEmpty());

assert(ll.size() == 2);

ll.removeFront();

int val10 = ll.peek();

assert(val10 == 10);

val10 = ll.removeFront();

assert(val10 == 10);

assert(ll.isEmpty());

assert(ll.size() == 0);

// Test exchange

ll.addFront(11);

ll.addFront(12);

assert(ll.peek() == 12);

ll.exchange();

assert(ll.peek() == 11);

ll.addFront(13);

assert(ll.peek() == 13);

ll.exchange();

assert(ll.peek() == 11);

int val11 = ll.removeFront();

assert(val11 == 11);

assert(ll.peek() == 13);

ll.exchange();

assert(ll.peek() == 12);

int val12 = ll.removeFront();

assert(val12 == 12);

int val13 = ll.removeFront();

assert(val13 == 13);

assert(ll.isEmpty());

assert(ll.size() == 0);

// Test minElement and maxElement

LinkedIntList ll2 = new LinkedIntList();

ll2.addFront(50);

assert(ll2.minElement() == 50);

assert(ll2.maxElement() == 50);

ll2.addFront(100);

assert(ll2.minElement() == 50);

assert(ll2.maxElement() == 100);

ll2.addBack(ll2.removeFront());

assert(ll2.minElement() == 50);

assert(ll2.maxElement() == 100);

}

}

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes