Sem II (Program II) - Practice Flashcards
1
Q
Create Java single node in a linked list.
A
public class Node {
int data;
Node next;
public Node(int d) { data = d; next = null; } }
2
Q
Create Java simple linked list with method size.
A
public class Liste { private Node first = null; private Node last = null; public int size() { Node p = first; int length = 0; while (p != null) { length++; p = p.next; } return length; } }
3
Q
Create Java simple linked list with method valueExists.
A
public class Liste { private Node first = null; private Node last = null; public boolean valueExists(int value) { Node p = first; while (p != null) { if (p.data == value) { return true; } p = p.next; } return false; } }
4
Q
Create Java simple linked list with method addOn.
A
public class Liste { private Node first = null; private Node last = null; public void addOn(int value, int index) { if (index < 0 || index > size()) { throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size()); } Node p = first; Node q = new Node(value); if (index == 0) { q.next = first; first = q; } else { for (int i = 0; i < index - 1; i++) { p = p.next; } q.next = p.next; p.next = q; } if (index == size()) { last = q; } } }
5
Q
A