Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1. import java.util.Iterator;
  2.  
  3. public class Linked_List implements Iterable<Integer> {
  4.    
  5.     private class ListNode {
  6.         //ListNode Class
  7.         public int val;
  8.         public ListNode prev, next;
  9.         public ListNode(int x) {x = val;}  
  10.     }
  11.    
  12.     //data fields
  13.     private ListNode head, tail;
  14.     private int numOfNodes;
  15.    
  16.     //Constructors
  17.     public Linked_List() {}
  18.    
  19.     //methods
  20.     public boolean isEmpty() {return numOfNodes == 0; }
  21.     public int size() {return numOfNodes; }
  22.     public void addLast (int integer) {
  23.         if (numOfNodes++ == 0) {head = tail = new ListNode(integer); }
  24.         else {
  25.             tail.next = new ListNode(integer);
  26.             tail.next.prev = tail;
  27.             tail = tail.next; }
  28.         }
  29.    
  30.     public void addFirst (int integer) {
  31.         if (numOfNodes++ ==0) {head = tail = new ListNode(integer); }
  32.         else {
  33.             head.prev = new ListNode(integer);
  34.             head.prev.next = head;
  35.             head = head.prev;
  36.         }
  37.     }
  38.        
  39.     public int getFirst() throws Exception {
  40.         if (numOfNodes == 0) { throw new Exception("Attempt to access element in an empty list."); }
  41.         return head.val;
  42.     }
  43.    
  44.     public int getLast() throws Exception {
  45.         if (numOfNodes == 0) { throw new Exception("Attempt to access element in an empty list"); }
  46.         return tail.val;
  47.     }
  48.    
  49.     public int removeFirst() throws Exception {
  50.         if (numOfNodes == 0) {throw new Exception("List empty yo"); }
  51.         int returnValue = head.val;
  52.         if (numOfNodes-- == -1) {head = tail = null; }
  53.         else {
  54.             head = head.next;
  55.             head.prev = null;
  56.         }
  57.         return returnValue;
  58.     }
  59.    
  60.     public int removeLast() throws Exception {
  61.         if (numOfNodes == 0) {throw new Exception("List empty yo"); }
  62.         int returnValue = tail.val;
  63.         if (numOfNodes-- == 1) {head = tail = null;}
  64.         else {
  65.             tail = tail.prev;
  66.             tail.next = null;
  67.         }
  68.         return returnValue;
  69.     }
  70.    
  71.     public String toString() {
  72.         String output = "[";
  73.         ListNode p = head;
  74.         while (p != null) {
  75.             output += p.val;
  76.             if (p.next != null) { output += " -> "; }
  77.             p = p.next;
  78.         }
  79.         return output + ']';
  80.     }
  81.  
  82.     @Override
  83.     public Iterator<Integer> iterator() {
  84.         // TODO Auto-generated method stub
  85.         return null;
  86.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement