Advertisement
jig487

Untitled

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