Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * COMP 410
- *See inline comment descriptions for methods not described in interface.
- *
- */
- package LinkedList_A1;
- public class LinkedListImpl implements LIST_Interface {
- Node sentinel; //this will be the entry point to your linked list (the head)
- public LinkedListImpl(){//this constructor is needed for testing purposes. Please don't modify!
- sentinel=new Node(0); //Note that the root's data is not a true part of your data set!
- }
- //implement all methods in interface, and include the getRoot method we made for testing purposes. Feel free to implement private helper methods!
- public void clear() {
- sentinel.next=null;
- sentinel.prev=null;
- }
- public int size() {
- int size = 0;
- if (sentinel.next==null) {return size;}
- else {
- Node curr = sentinel.next;
- while (curr != sentinel) {
- size++;
- curr = curr.next;
- }
- return size;
- }
- }
- public boolean isEmpty() {
- if (this.size() == 0) {return true;}
- else {return false;}
- }
- public boolean insert(double elt, int index) {
- if ((sentinel.next == null) && (index==0)) {
- Node c = new Node(elt);
- sentinel.next = c;
- c.prev = sentinel;
- c.next = sentinel;
- sentinel.prev = c;
- return true;
- }
- else if ((this.size() < index) || (index < 0)) {return false;}
- else {
- Node c = new Node(elt);
- Node curr = sentinel.next; //location of new cell
- int ct = 0;
- while (curr != sentinel) {
- if (ct < index) {curr=curr.next; ct++;}
- else {break;}
- }
- c.prev = curr.prev;
- curr.prev = c;
- c.prev.next = c;
- c.next = curr;
- return true;
- }
- }
- public boolean remove(int index) {
- if ((this.size() <= index) || (index < 0)) {return false;}
- else {
- Node curr = sentinel.next;
- int ct = 0;
- while (curr != sentinel) {
- if (ct<index) {curr=curr.next; ct++;}
- else {break;}
- }
- curr.next.prev = curr.prev;
- curr.prev.next = curr.next;
- return true;
- }
- }
- public double get(int index) {
- if ((this.size() <= index) || (index < 0)) {return Double.NaN;}
- else {
- Node curr = sentinel.next;
- int ct = 0;
- while (curr != sentinel) {
- if (ct<index) {curr=curr.next; ct++;}
- else {break;}
- }
- return curr.data;
- }
- }
- public Node getRoot(){ //leave this method as is, used by the grader to grab your linkedList easily.
- return sentinel;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement