Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Chapter24LinkedListCodeEtc;
- //Nate Wheeler
- //hw linklist
- //nrwheel@student.stcc.edu
- import Chapter24LinkedListCodeEtc.MyLinkedList.Node;
- public class MyLinkedListEnhanced<E> extends MyLinkedList<E> {
- @Override
- /** Return true if this list contains the element e */
- public boolean contains(E e) {
- Node<E> curr = head;
- if (head == null)
- return false;
- for (int i = 0; i < size; i++) {
- if (curr.element.equals(e))
- return true;
- curr = curr.next;
- }
- return false;
- }
- @Override
- /** Return the element at the specified index */
- public E get(int index) {
- if (head == null)
- return null;
- Node<E> temp = head;
- for (int i = 0; i < index - 1; i++) {
- temp = temp.next;
- }
- return temp.element;
- }
- @Override
- /**
- * Return the index of the head matching element in this list. Return -1 if
- * no match.
- */
- public int indexOf(E e) {
- Node<E> curr = head;
- int index = 0;
- if (head == null)
- return -1;
- for (int i = 0; i < size; i++) {
- index++;
- if (curr.element.equals(e))
- return index;
- curr = curr.next;
- }
- return -1;
- }
- @Override
- /**
- * Return the index of the last matching element in this list. Return -1 if
- * no match.
- */
- public int lastIndexOf(E e) {
- Node<E> curr = head;
- Node<E> last;
- int index = 0;
- if (head == null)
- return -1;
- for (int i = 0; i < size; i++) {
- curr = curr.next;
- index++;
- }
- return index;
- }
- @Override
- /**
- * Replace the element at the specified position in this list with the
- * specified element.
- */
- public E set(int index, E e) {
- if (head == null)
- return null;
- Node<E> temp = head;
- for (int i = 0; i < index - 1; i++) {
- temp = temp.next;
- }
- return temp.element;
- }
- @Override
- /**
- * Remove Node pointed to by n
- */
- protected void removeNode(Node<E> n) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement