Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. /**
  2.  * COMP 410
  3.  *See inline comment descriptions for methods not described in interface.
  4.  *
  5. */
  6. package LinkedList_A1;
  7.  
  8. public class LinkedListImpl implements LIST_Interface {
  9.   Node sentinel; //this will be the entry point to your linked list (the head)
  10.  
  11.   public LinkedListImpl(){//this constructor is needed for testing purposes. Please don't modify!
  12.     sentinel=new Node(0); //Note that the root's data is not a true part of your data set!
  13.   }
  14.  
  15.   //implement all methods in interface, and include the getRoot method we made for testing purposes. Feel free to implement private helper methods!
  16.   public void clear() {
  17.       sentinel.next=null;
  18.       sentinel.prev=null;
  19.   }
  20.  
  21.   public int size() {
  22.       int size = 0;
  23.       if (sentinel.next==null) {return size;}
  24.       else {
  25.           Node curr = sentinel.next;
  26.           while (curr != sentinel) {
  27.             size++;
  28.             curr = curr.next;
  29.           }
  30.           return size;
  31.       }
  32.   }
  33.  
  34.   public boolean isEmpty() {
  35.       if (this.size() == 0) {return true;}
  36.       else {return false;}
  37.   }
  38.  
  39.  
  40.   public boolean insert(double elt, int index) {
  41.       if ((sentinel.next == null) && (index==0)) {
  42.           Node c = new Node(elt);
  43.           sentinel.next = c;
  44.           c.prev = sentinel;
  45.           c.next = sentinel;
  46.           sentinel.prev = c;
  47.           return true;
  48.           }
  49.       else if ((this.size() < index) || (index < 0)) {return false;}
  50.       else {
  51.           Node c = new Node(elt);
  52.           Node curr = sentinel.next; //location of new cell
  53.           int ct = 0;
  54.           while (curr != sentinel) {
  55.               if (ct < index) {curr=curr.next; ct++;}
  56.               else {break;}
  57.           }
  58.           c.prev = curr.prev;
  59.           curr.prev = c;
  60.           c.prev.next = c;
  61.           c.next = curr;  
  62.           return true;
  63.       }
  64.   }
  65.  
  66.   public boolean remove(int index) {
  67.       if ((this.size() <= index) || (index < 0)) {return false;}
  68.       else {
  69.       Node curr = sentinel.next;
  70.       int ct = 0;
  71.       while (curr != sentinel) {
  72.           if (ct<index) {curr=curr.next; ct++;}
  73.           else {break;}
  74.       }
  75.       curr.next.prev = curr.prev;
  76.       curr.prev.next = curr.next;
  77.       return true;
  78.       }
  79.   }
  80.  
  81.   public double get(int index) {
  82.       if ((this.size() <= index) || (index < 0)) {return Double.NaN;}
  83.       else {
  84.           Node curr = sentinel.next;
  85.           int ct = 0;
  86.           while (curr != sentinel) {
  87.               if (ct<index) {curr=curr.next; ct++;}
  88.               else {break;}
  89.           }
  90.           return curr.data;
  91.       }
  92.   }
  93.  
  94.  
  95.   public Node getRoot(){ //leave this method as is, used by the grader to grab your linkedList easily.
  96.     return sentinel;
  97.   }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement