Advertisement
jig487

Untitled

Apr 22nd, 2024
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.50 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.NoSuchElementException;
  4. import java.util.Iterator;
  5.  
  6. public class LinkedList1<E> implements Iterable<E>{
  7.  
  8.    private listNode firstNode;
  9.    private int size;
  10.  
  11.    public LinkedList1() {
  12.       firstNode = new listNode(null);
  13.       size = 0;
  14.    }
  15.  
  16.    public Iterator<E> iterator() {
  17.       return new ListIterator();
  18.    }
  19.    
  20.    private class ListIterator implements Iterator<E> {
  21.          
  22.       private listNode curNode;
  23.      
  24.       public ListIterator() {
  25.          curNode = firstNode;
  26.       }
  27.      
  28.       public boolean hasNext() {
  29.          return curNode.nextNode != null;
  30.       }
  31.  
  32.       public E next() {
  33.          if (!hasNext()) {
  34.             throw new NoSuchElementException();
  35.          }
  36.          curNode = curNode.nextNode;
  37.          return curNode.value;
  38.       }
  39.    }
  40.  
  41.    public int getSize() {
  42.       return size;
  43.    }
  44.  
  45.    public boolean isEmpty() {
  46.       if (size == 0) {
  47.          return true;
  48.       }
  49.       listNode curNode = firstNode;
  50.       while(curNode.nextNode != null) {
  51.          if (!curNode.value.equals(null)) {
  52.             return false;
  53.          }
  54.          curNode = curNode.nextNode;
  55.       }
  56.       return true;
  57.    }
  58.  
  59.    public void clear() {
  60.       size = 0;
  61.       firstNode = null;
  62.    }
  63.  
  64.    public E get(int index) {
  65.       listNode returnNode = firstNode;
  66.       for(int i = 0; i < index; i++) {
  67.          returnNode = returnNode.nextNode;
  68.       }
  69.       return returnNode.value;
  70.    }
  71.  
  72.    public E set(int index, E e) {
  73.       E toReturn = remove(index);
  74.       add(index, e);
  75.       return toReturn;
  76.    }
  77.  
  78.    public int indexOf(E e) {
  79.       int count = 0;
  80.       listNode curNode = firstNode;
  81.       for(int i = 0; i < size; i++) {
  82.          if (curNode.value.equals(e)) {
  83.             return count;
  84.          }
  85.          curNode = curNode.nextNode;
  86.          count++;
  87.       }
  88.       return -1;
  89.    }
  90.  
  91.    public boolean contains(E e) {
  92.       listNode curNode = firstNode;
  93.       for(int i = 0; i < size; i++) {
  94.          if (curNode.value.equals(e)) {
  95.             return true;
  96.          }
  97.          curNode = curNode.nextNode;
  98.       }
  99.       return false;
  100.    }
  101.  
  102.    public int lastIndexOf(E e) {
  103.       int count = 0;
  104.       int lastFound = -1;
  105.       listNode curNode = firstNode;
  106.       for(int i = 0; i < size; i++) {
  107.          if (curNode.value.equals(e)) {
  108.             lastFound = count;
  109.          }
  110.          curNode = curNode.nextNode;
  111.          count++;
  112.       }
  113.       return lastFound;
  114.    }
  115.  
  116.    public E getFirst() {
  117.       return firstNode.value;
  118.    }
  119.  
  120.    public E getLast() {
  121.       listNode curNode = firstNode;
  122.       while(curNode.nextNode != null) {
  123.          curNode = curNode.nextNode;
  124.       }
  125.       return curNode.value;
  126.    }
  127.  
  128.    public void addFirst(E e) {
  129.       add(0, e);
  130.    }
  131.  
  132.    public void addLast(E e) {
  133.       add(e);
  134.    }
  135.  
  136.    public boolean add(E e) {
  137.       listNode newNode = new listNode(e);
  138.        
  139.         listNode curNode = firstNode;
  140.        
  141.         // while we're not at end of list, iterate over list
  142.         while(curNode.nextNode != null) {
  143.             curNode = curNode.nextNode;
  144.         }
  145.        
  146.         // curNode = last node in list
  147.         curNode.nextNode = newNode;
  148.        
  149.         size++;
  150.        
  151.         return true;
  152.    }
  153.  
  154.    public void add(int index, E e) {
  155.       if(index < 0 || index > size) {
  156.             throw new IndexOutOfBoundsException();
  157.         }
  158.        
  159.         listNode newNode = new listNode(e);
  160.        
  161.         listNode curNode = firstNode;
  162.        
  163.         for(int i = 0; i < index; i++) {
  164.             curNode = curNode.nextNode;
  165.         }
  166.        
  167.         // curNode is at index-1
  168.         newNode.nextNode = curNode.nextNode;
  169.         curNode.nextNode = newNode;
  170.        
  171.         size++;
  172.    }
  173.  
  174.    public E remove() {
  175.       //if list is empty
  176.       if (size == 0) {
  177.          throw new NoSuchElementException();
  178.       }
  179.       return remove(0);
  180.    }
  181.  
  182.    public E remove(int index) {
  183.       //error checking
  184.       if(index < 0 || index >= size) {
  185.             throw new IndexOutOfBoundsException();
  186.         }
  187.        
  188.         listNode curNode = firstNode;
  189.        
  190.         for(int i = 0; i < index; i++) {
  191.             curNode = curNode.nextNode;
  192.         }
  193.        
  194.         // curNode is at index-1
  195.         E toReturn = curNode.nextNode.value;
  196.         curNode.nextNode = curNode.nextNode.nextNode;
  197.        
  198.         size--;
  199.        
  200.         return toReturn;
  201.    }
  202.  
  203.    public boolean remove(E e) {
  204.       if(size == 0) {
  205.          return false;
  206.       //} else if( firstNode.value.equals(e) ) {
  207.       //   firstNode = firstNode.nextNode;
  208.       //   size--;
  209.       //   return true;
  210.       } else {
  211.          listNode curNode = firstNode;
  212.          for (int i = 0; i < size-1; i++) {
  213.             curNode = curNode.nextNode;
  214.             if (curNode.value.equals(e)) {
  215.                curNode.nextNode = curNode.nextNode.nextNode;
  216.                size--;
  217.                return true;
  218.             }
  219.          }
  220.          return false;
  221.       }
  222.    }
  223.  
  224.    //test function to print out list
  225.    public void printLinkedList() {
  226.       listNode curNode = firstNode;
  227.       System.out.print("Printing list: Size: " + size + ", ");
  228.       for(int i = 0; i < size; i++) {
  229.          System.out.print(curNode.value + " >> ");
  230.          curNode = curNode.nextNode;
  231.       }
  232.       System.out.println("");
  233.    }
  234.  
  235.    private class listNode {
  236.       private E value;
  237.       private listNode nextNode;
  238.  
  239.       public listNode(E e) {
  240.          value = e;
  241.          nextNode = null;
  242.       }
  243.  
  244.       public void setNext(listNode next) {
  245.          nextNode = next;
  246.       }
  247.  
  248.       public listNode getNext() {
  249.          return nextNode;
  250.       }
  251.  
  252.       public E getValue() {
  253.          return value;
  254.       }
  255.    }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement