public int search(Object p) {
    int index = 0;
    boolean flag = false;
    temp = back;
    ListNode temp2 = new ListNode(p, null);

    if (back == null) {
      return -1;
    }
    // else if case

    else {
      while (temp.getNext() != null && flag == false) {
        if (temp.getValue() == temp2.getValue()) {
          flag = true;
        } else {
          temp = temp.getNext();
          index++;
        }
      }

      if (flag) {
        return index;
      } else {
        return -1;
      }
    }
  }
Beispiel #2
0
  /**
   * doR : implement the operation for R Pre-condition : valid x and there must be a ball labelled x
   * in the list Post-condition : remove the ball labelled x
   */
  public void doR(int x) {
    for (ListNode n = head; n != null; n = n.getNext()) {
      if (n.getElement() == x) {
        // when found, remove that node
        if (n == head && n == tail) { // only one node in the list
          head = null;
          tail = null;
        }

        // fix prev pointer
        if (n != tail) {
          n.getNext().setPrev(n.getPrev());
        } else {
          tail = n.getPrev();
          tail.setNext(null);
        }

        // fix next pointer
        if (n != head) {
          n.getPrev().setNext(n.getNext());
        } else {
          head = n.getNext();
          head.setPrev(null);
        }
      }
      num_nodes--;
    }
  }
  public void remove(Object inputData) {
    ListNode currentNode = (ListNode) this.firstNode;

    if (this.size == 0) {
      return;
    }

    boolean wasDeleted = false;

    /* Are we deleting the first node? */
    if (inputData.equals(currentNode.getData())) {

      /* Only one node in list, be careful! */
      if (currentNode.getNext() == null) {
        ((ListNode) this.firstNode).setData(null);
        this.firstNode = new ListNode();
        this.lastNode = (ListNode) this.firstNode;
        this.size--;
        return;
      }

      currentNode.setData(null);
      currentNode = currentNode.getNext();
      this.firstNode = currentNode;
      this.size--;
      return;
    }

    while (true) {
      /* If end of list, stop */
      if (currentNode == null) {
        wasDeleted = false;
        break;
      }

      /* Check if the data of the next is what we're looking for */
      ListNode nextNode = currentNode.getNext();
      if (nextNode != null) {
        if (inputData.equals(nextNode.getData())) {

          /* Found the right one, loop around the node */
          ListNode nextNextNode = nextNode.getNext();
          currentNode.setNext(nextNextNode);

          nextNode = null;
          wasDeleted = true;
          break;
        }
      }

      currentNode = currentNode.getNext();
    }

    if (wasDeleted) {
      this.size--;
    }
  }
  /**
   * Removes <strong>ListNode</strong> in <strong>List</strong>
   *
   * @param node
   */
  public void remove(ListNode node) {
    if (head.equals(node))
      head = head.getNext(); // previous head is now unreachable -- collected by jvm
    else {
      node.getPrev().setNext(node.getNext());
      if (node.getNext() != null) node.getNext().setPrev(node.getPrev());
    }

    size--;
  }
Beispiel #5
0
  /**
   * doA : implement the operation for A Pre-condition : x and y must exist the in the ListNode
   * Post-condition : if x is to the right of y, move to left of y
   */
  public void doA(int x, int y) {
    ListNode lnY = null;
    ListNode lnX = null;

    // traverse the node until it reaches x, if it hits y along the way, continue
    for (ListNode n = head; n != null; n = n.getNext()) {

      if (n.getElement() == y) {
        lnY = n;
      }

      if (n.getElement() == x) {
        lnX = n;

        // settle the chains for x
        if (lnX == head && lnX == tail) { // only one node in the list
          head = null;
          tail = null;
        }
      }

      if (lnX != null && lnY != null) {
        // check whether x is on the left of Y
        if (lnY.getPrev() == lnX) break;

        // fix prev pointer
        if (lnX != tail) {
          lnX.getNext().setPrev(lnX.getPrev());
        } else {
          tail = lnX.getPrev();
          tail.setNext(null);
        }

        // fix next pointer
        if (lnX != head) {
          lnX.getPrev().setNext(lnX.getNext());
        } else {
          head = lnX.getNext();
          head.setPrev(null);
        }

        // insert it before y
        if (lnY != head) {
          lnX.setPrev(lnY.getPrev());
          lnY.getPrev().setNext(lnX);
        } else {
          head = lnX;
          head.setPrev(null);
        }

        lnY.setPrev(lnX);
        lnX.setNext(lnY);
      }
    }
  }
  /**
   * Inserts <strong>ListNode</strong> into the end of <strong>List</strong>.
   *
   * @param newE
   */
  public void insert(Entity newE) {
    if (head == null) {
      head = new ListNode(newE);
    } else {
      ListNode temp = head;
      while (temp.getNext() != null) {
        temp = temp.getNext();
      }
      temp.setNext(new ListNode(newE));
      temp.getNext().setPrev(temp);
    }

    size++;
  }
  public int[] List(String keyword) {
    int counter = 0; // this counts the number of occurrences of the given keyword
    int[] arr; // keeps page numbers of the given keyword
    ListNode result;
    int page; // holds the current page number

    result =
        ht2.chainedHashSearch(
            ht2,
            keyword); // check whether there's at least a single page with the given keyword,if the
                      // node prsent we get the first one,there can be more !!!

    if (result != null) {
      page = result.getPage();
      counter = 1; // found one occurence of the keyword

      // now let's find all the other occurrences of the same keyword
      while (result.getNext() != null) { // iterate through the whole list
        result = result.getNext();
        if (result.getKeyword().equals(keyword) && result.getPage() != page) {
          counter++;
          page = result.getPage();
        }
      }
      // now we know how many pages contain the given key word
      // let's create an array to hold all the page numbers
      arr = new int[counter];

      // let's iterate through the same linked list again to feed the array with page numbers
      counter = 0; // reset to zero again
      result = ht2.chainedHashSearch(ht2, keyword);
      arr[0] = page = result.getPage();
      counter = 1;
      while (result.getNext() != null) {

        result = result.getNext();

        if (result.getKeyword().equals(keyword) && result.getPage() != page) {
          arr[counter] = result.getPage();
          counter++;
        }
      }

      this.sort(arr);
      return arr;
    } else {
      return null;
    }
  }
  public void Keywords(int page) {

    /**
     * HashTable1 class is responsible for this action. It uses the page number as a keyword and
     * stores all the keywords with that page number Observe the implementation of HashTable1 for
     * further information
     */
    int counter; // number of keywords in the given page

    ListNode result =
        ht1.chainedHashSearch(ht1, page); // find the first occurence of the page number

    if (result != null) { // if not null
      counter = 1;

      while (result.getNext() != null) {

        result = result.getNext();
        if (result.getPage()
            == page) { // if equal to the given page number and not equal to the previous result's
                       // page number
          counter++;
        }
      }

      String kwordArr[] = new String[counter];
      counter = 1;
      result =
          ht1.chainedHashSearch(ht1, page); // again go to the first node with the given keyword
      kwordArr[0] = result.getKeyword();

      while (result.getNext() != null) {

        result = result.getNext();
        if (page
            == result
                .getPage()) { // if equal to the given page number and not equal to the previous
                              // result's page number
          kwordArr[counter] = result.getKeyword();
          counter++;
        }
      }

      this.stringSort(kwordArr); // send the array for sorting

    } else {
      System.out.println("Invalid Page Number");
    }
  }
 public ListNode removeLast() {
   if (front == null) {
     return null;
   } else {
     ListNode temp2 = back;
     while (temp2.getNext() != front && temp2.getNext() != null) // second condition test
     {
       temp2 = temp2.getNext();
     }
     temp = front;
     front = temp2;
     size--;
     return temp;
   }
 }
 public static ListNode reverseKNodes(ListNode head, int k) {
   // Start with head
   ListNode current = head;
   // last node after reverse
   ListNode prevTail = null;
   // first node before reverse
   ListNode prevCurrent = head;
   while (current != null) {
     // loop for reversing K nodes
     int count = k;
     ListNode tail = null;
     while (current != null && count > 0) {
       ListNode next = current.getNext();
       current.setNext(tail);
       tail = current;
       current = next;
       count--;
     }
     // reversed K Nodes
     if (prevTail != null) {
       // Link this set and previous set
       prevTail.setNext(tail);
     } else {
       // We just reversed first set of K nodes, update head point to the Kth Node
       head = tail;
     }
     // save the last node after reverse since we need to connect to the next set.
     prevTail = prevCurrent;
     // Save the current node, which will become the last node after reverse
     prevCurrent = current;
   }
   return head;
 }
 /**
  * 打印链表
  *
  * @param head 头指针
  */
 public static void printList(ListNode head) {
   ListNode current = head;
   while (current != null) {
     System.out.print(current.getValue() + "、");
     current = current.getNext();
   }
   System.out.println();
 }
 public static void printList(ListNode head) {
   ListNode current = head;
   while (current != null) {
     System.out.printf("[%d]", current.getData());
     current = current.getNext();
   }
   System.out.println("");
 }
 /*
 	Returns the object in the next ListNode of the Linked List
 	@return The object in the next ListNode of the Linked List
 **/
 public E next() {
   if (!hasNext()) {
     throw new NoSuchElementException("End of Linked List");
   }
   ListNode<E> holder = curr;
   curr = curr.getNext();
   return holder.getItem();
 }
Beispiel #14
0
 // add objec to queue, at the end of it. increment item count as wel.
 public boolean enqueue(Object o) {
   itemcount++;
   if (isEmpty()) {
     head = tail = new ListNode(o);
     return true;
   }
   tail.setNext(new ListNode(o));
   tail = tail.getNext();
   return true;
 }
  public ListNode removeFirst() {

    if (back == null) {
      return null;
    } else {
      temp = back;
      back = back.getNext();
      size--;
      return temp;
    }
  }
  /**
   * 合并两个链表
   *
   * @param head1 链表1
   * @param head2 链表2
   * @return
   */
  public static ListNode mergeList(ListNode head1, ListNode head2) {
    ListNode head = null; // 合并后的头指针

    // 如果有一个为空,则为另一个链表
    if (head1 == null) head = head2;
    if (head2 == null) head = head1;

    // 两个都不为空
    if (head1 != null && head2 != null) {
      // node_1和node_2是用于遍历
      ListNode node_1 = head1;
      ListNode node_2 = head2;
      if (node_1.getValue() < node_2.getValue()) {
        head = node_1;
        head.setNext(mergeList(node_1.getNext(), node_2));
      } else {
        head = node_2;
        head.setNext(mergeList(node_1, node_2.getNext()));
      }
    }
    return head;
  }
  public Object elementAt(int inputPosition) {

    if (inputPosition >= this.size || inputPosition < 0) {
      return null;
    }

    ListNode currentNode = (ListNode) this.firstNode;

    for (int position = 0; position < inputPosition; position++) {
      currentNode = currentNode.getNext();
    }

    return currentNode.getData();
  }
  public String toString() {
    ListNode currentNode = (ListNode) this.firstNode;
    StringBuffer buffer = new StringBuffer();

    buffer.append("{");
    for (int i = 0; currentNode != null; i++) {
      if (i > 0) {
        buffer.append(",");
      }
      Object dataObject = currentNode.getData();

      buffer.append(dataObject == null ? "" : dataObject);
      currentNode = currentNode.getNext();
    }
    buffer.append("}");
    return buffer.toString();
  }
  public static ListNode reverseKNodesRecursive(ListNode head, int k) {
    ListNode current = head;
    ListNode next = null;
    ListNode prev = null;
    int count = k;

    // Reverse K nodes
    while (current != null && count > 0) {
      next = current.getNext();
      current.setNext(prev);
      prev = current;
      current = next;
      count--;
    }

    // Now next points to K+1 th node, returns the pointer to the head node
    if (next != null) {
      head.setNext(reverseKNodesRecursive(next, k));
    }
    // return head node
    return prev;
  }
  public int indexOf(Object inputData) {
    ListNode currentNode = (ListNode) this.firstNode;
    int position = 0;
    boolean found = false;

    for (; ; position++) {
      if (currentNode == null) {
        break;
      }

      if (inputData.equals(currentNode.getData())) {
        found = true;
        break;
      }

      currentNode = currentNode.getNext();
    }

    if (!found) {
      position = -1;
    }

    return position;
  }
Beispiel #21
0
 public void print() {
   for (ListNode n = head; n != null; n = n.getNext()) {
     System.out.print(n.getElement() + " ");
   }
 }