Example #1
0
 public T getData(int index) {
   LLNode<T> node = head;
   if (index < 0) return null;
   for (int i = 0; i < index; i++) {
     node = node.getPointer();
   }
   return node.getData();
 }
Example #2
0
 public void removeFront() throws StackEmptyException {
   if (empty()) {
     throw new StackEmptyException("Stack is Empty");
   } else {
     head.setNext(head.getNext());
     size--;
   }
 }
Example #3
0
 public String toString() {
   String line = "";
   LLNode<T> current = head;
   while (current != null) {
     line += current.getData().toString() + ",";
     current = current.getPointer();
   }
   return line;
 }
Example #4
0
 public void pop_last() {
   LLNode temp;
   temp = head;
   for (int i = 0; i < n - 2; i++) {
     temp = temp.next;
   }
   temp.next = null;
   n -= 1;
 }
Example #5
0
 // generates a list of the elements in the collection
 // Postcondion: list is independant (deep copy) of original collection
 public List<T> createList() {
   List<T> list = new ArrayList<T>();
   LLNode<T> temp = node;
   while (temp.getLink() != null) {
     list.add(temp.getInfo());
     temp = temp.getLink();
   }
   list.add(temp.getInfo());
   return list;
 }
Example #6
0
 public String toString() {
   String retStr = "HEAD->";
   LLNode tmp = _head; // init tr
   while (tmp != null) {
     retStr += tmp.getCargo() + "->";
     tmp = tmp.getNext();
   }
   retStr += "NULL";
   return retStr;
 }
Example #7
0
  public String get(int index) {

    if (index < 0 || index >= size()) throw new IndexOutOfBoundsException();

    String retVal;
    LLNode tmp = _head; // create alias to head

    for (int i = 0; i < index; i++) tmp = tmp.getNext();

    retVal = tmp.getCargo();
    return retVal;
  }
Example #8
0
 public void insertFront(T data) {
   LLNode<T> nn = new LLNode<T>(data);
   if (empty()) {
     head = nn;
     tail = nn;
     size++;
   } else {
     nn.setNext(tail);
     tail = nn;
     size++;
   }
 }
Example #9
0
 // Adds element to this collection.
 // Precondition: element is not already in the collection
 public void add(T element) {
   if (node == null) {
     node = new LLNode<T>(element);
     size++;
   } else {
     LLNode<T> temp = node;
     while (temp.getLink() != null) {
       temp = temp.getLink();
     }
     temp.setLink(new LLNode<T>(element));
     size++;
   }
 }
Example #10
0
  public void print() {
    if (n == 0) System.out.println("Empty List");
    else {
      LLNode temp;
      temp = head;

      for (int i = 0; i < n; i++) {
        System.out.print(temp.getData());
        System.out.print(" ");
        temp = temp.next;
      }
    }
    System.out.print("\n");
  }
Example #11
0
 public void remove(int pos) {
   LLNode temp;
   temp = head;
   if (pos == 0) {
     if (n == 0) return;
     head = head.next;
     n -= 1;
   } else {
     for (int i = 0; i < pos - 1; i++) {
       temp = temp.next;
     }
     temp.next = temp.next.next;
     n -= 1;
   }
 }
Example #12
0
 private void enqueue(T data) {
   LLNode<T> newNode = new LLNode<>(data);
   if (tail == null) head = newNode;
   else tail.setPointer(newNode);
   tail = newNode;
   count++;
 }
Example #13
0
  public String toString(T data) {
    String s = "[ ";
    while (head.getNext() != null) {
      s += (data + ", ");
    }

    return s;
  }
Example #14
0
 public void insertBack(T data) {
   LLNode<T> nn = new LLNode<T>(data);
   if (empty()) {
     tail.setNext(nn);
     tail = nn;
     size++;
   }
 }
Example #15
0
 public void removeSecondLast() {
   LLNode<T> tmp = new LLNode();
   LLNode<T> prev = new LLNode();
   tmp.setNext(head.getNext());
   while (tmp.getNext() != tail) {
     prev.getNext();
   }
   prev.setNext(prev.getNext());
   tail = prev;
 }
Example #16
0
  public String toString() {
    String response;
    response = new String("[");
    if (n == 0) return response;
    else {
      LLNode temp;
      temp = head;

      for (int i = 0; i < n; i++) {
        // System.out.print(temp.getData());
        // System.out.print(" ");
        if (i != n - 1) response += temp.getData() + ", ";
        else response += temp.getData();
        temp = temp.next;
      }
    }
    response += "]";
    return response;
  }
Example #17
0
 // Returns the element if this collection contains an element e such that
 // e.compareTo(element) == 0; otherwise, returns null.
 public T fetch(T element) {
   if (size == 0) {
     return null;
   } else {
     LLNode<T> temp = node;
     while (temp.getLink() != null) {
       if (element.compareTo(temp.getInfo()) == 0) {
         return temp.getInfo();
       }
       temp = temp.getLink();
     }
     if (temp.getInfo().compareTo(element) == 0) {
       return temp.getInfo();
     }
   }
   return null;
 }
Example #18
0
 public void append(Object data) {
   LLNode insert_node;
   insert_node = new LLNode(data);
   /*LLNode insert_node, temp;
   temp = head;
   insert_node = new LLNode(data);
   for(int i=0;i<n-1;i++){
   	temp = temp.next;
   }
   temp.next = insert_node;*/
   if (n == 0) {
     head = insert_node;
     end = head;
   } else {
     end.next = insert_node;
   }
   n += 1;
   end = insert_node;
 }
Example #19
0
 public void removeBack() throws StackEmptyException {
   if (empty()) {
     throw new StackEmptyException("Stack is Empty");
   } else {
     LLNode<T> tmp = new LLNode();
     LLNode<T> prev = new LLNode();
     tmp.setNext(head);
     while (tmp.getNext() != tail) {
       prev.getNext();
     }
     prev.setNext(prev);
     tail = prev;
   }
 }
Example #20
0
  public void add(int index, String newVal) {

    if (index < 0 || index >= size()) throw new IndexOutOfBoundsException();

    LLNode newNode = new LLNode(newVal, null);

    if (index == 0) add(newVal);
    else {
      LLNode tmp = _head; // create alias to head

      for (int i = 0; i < index - 1; i++) tmp = tmp.getNext();

      newNode.setNext(tmp.getNext());
      tmp.setNext(newNode);

      _size++;
    }
  }
Example #21
0
  public String remove(int index) {

    if (index < 0 || index >= size()) throw new IndexOutOfBoundsException();

    String retVal;
    LLNode tmp = _head; // create alias to head

    if (index == 0) {
      retVal = _head.getCargo();

      _head = _head.getNext();
    } else {
      for (int i = 0; i < index - 1; i++) tmp = tmp.getNext();

      retVal = tmp.getNext().getCargo();

      tmp.setNext(tmp.getNext().getNext());
    }

    _size--;

    return retVal;
  }
Example #22
0
 public void insert(int pos, Object data) {
   if (pos > n) System.out.println("Error, Can't insert at postion : " + pos);
   else if (pos == 0) {
     LLNode insert_node;
     insert_node = new LLNode(data);
     insert_node.next = head;
     head = insert_node;
     end = insert_node;
     n += 1;
   } else {
     LLNode insert_node, next, prev, temp;
     temp = head;
     insert_node = new LLNode(data);
     for (int i = 0; i < pos - 1; i++) {
       temp = temp.next;
     }
     prev = temp;
     next = temp.next;
     prev.next = insert_node;
     insert_node.next = next;
     if (pos == n) end = insert_node;
     n += 1;
   }
 }