@Override
 public void remove(int index) {
   Node<T> tmp = getNode(index - 1);
   Node<T> removido = tmp.getNext();
   removido.getNext().setPreview(tmp);
   tmp.setNext(removido.getNext());
 }
Exemplo n.º 2
0
  public void remove(int index) {
    if (index >= numItems || index < 0) {
      return;
    }
    // delete first item
    if (index == 0) {
      head = head.getNext();
      // deleting end item
    } else if (index == numItems - 1) {
      Node n = head;
      for (int i = 0; i < index - 1; i++) {
        n = n.getNext();
      }
      // make it point nowhere
      n.setNext(null);

      // remove from the middle
    } else {
      Node n = head;
      for (int i = 0; i < index - 1; i++) {
        n = n.getNext();
      }
      // ask the node for its next next node
      n.setNext(n.getNext().getNext());
    }
    numItems--;
  }
Exemplo n.º 3
0
  private JsExpression mapObjectLit(Node objLitNode) throws JsParserException {
    JsObjectLiteral toLit = new JsObjectLiteral();
    Node fromPropInit = objLitNode.getFirstChild();
    while (fromPropInit != null) {

      Node fromLabelExpr = fromPropInit;
      JsExpression toLabelExpr = mapExpression(fromLabelExpr);

      // Advance to the initializer expression.
      //
      fromPropInit = fromPropInit.getNext();
      Node fromValueExpr = fromPropInit;
      if (fromValueExpr == null) {
        throw createParserException("Expected an init expression for: " + toLabelExpr, objLitNode);
      }
      JsExpression toValueExpr = mapExpression(fromValueExpr);

      JsPropertyInitializer toPropInit = new JsPropertyInitializer(toLabelExpr, toValueExpr);
      toLit.getPropertyInitializers().add(toPropInit);

      // Begin the next property initializer, if there is one.
      //
      fromPropInit = fromPropInit.getNext();
    }

    return toLit;
  }
  public static void main(String args[]) {
    myList1.insertAtBegin(11);
    myList1.insertAtEnd(12);
    myList1.insertAtEnd(13);
    myList1.insertAtEnd(14);
    myList1.insertAtEnd(15);
    myList1.insertAtEnd(16);
    myList1.insertAtEnd(17);
    System.out.println("Original List1: " + myList1.toString());

    myList2.insertAtEnd(14);
    myList2.insertAtEnd(113);
    myList2.insertAtEnd(124);
    myList2.insertAtEnd(134);
    myList2.insertAtEnd(154);
    myList2.insertAtEnd(164);
    myList2.insertAtEnd(174);
    System.out.println("Original List2: " + myList2.toString());
    Node head = mergeLists(myList1.head, myList2.head);
    while (head.getNext() != null) {
      System.out.print(head.getData() + "-");
      head = head.getNext();
    }

    head = mergeListRecursion(myList1.head, myList2.head);
    while (head.getNext() != null) {
      System.out.print(head.getData() + " - ");
      head = head.getNext();
    }
  }
  // Rotate LL by K
  public static void rotator(Node head, int k) {

    Node current = head;
    int count = 1;
    Node tempHead = null;
    Node next = null;

    while (current != null & count < k) {
      count++;
      current = current.getNext();
    }

    if (count == k & current != null) { // We have enough nodes
      if (current.getNext() != null) {
        next = current.getNext();
        tempHead = head;
        head = next;
        current.setNext(null);
        while (next.getNext() != null) {
          next = next.getNext();
        }
        next.setNext(tempHead);
      }
    } else {
      return; // No enough elements to rotate
    }

    ll.setHead(head);
  }
Exemplo n.º 6
0
  /*
      the type of an expression is relatively unknown. Cases we can be sure
      about are -
          Literals,
          Arithmetic operations - always return a Number
  */
  private static int findExpressionType(OptFunctionNode fn, Node n, int[] varTypes) {
    switch (n.getType()) {
      case Token.NUMBER:
        return Optimizer.NumberType;

      case Token.CALL:
      case Token.NEW:
      case Token.REF_CALL:
        return Optimizer.AnyType;

      case Token.GETELEM:
        return Optimizer.AnyType;

      case Token.GETVAR:
        return varTypes[fn.getVarIndex(n)];

      case Token.INC:
      case Token.DEC:
      case Token.MUL:
      case Token.DIV:
      case Token.MOD:
      case Token.BITOR:
      case Token.BITXOR:
      case Token.BITAND:
      case Token.LSH:
      case Token.RSH:
      case Token.URSH:
      case Token.SUB:
      case Token.POS:
      case Token.NEG:
        return Optimizer.NumberType;

      case Token.ARRAYLIT:
      case Token.OBJECTLIT:
        return Optimizer.AnyType; // XXX: actually, we know it's not
        // number, but no type yet for that

      case Token.ADD:
        {
          // if the lhs & rhs are known to be numbers, we can be sure that's
          // the result, otherwise it could be a string.
          Node child = n.getFirstChild();
          int lType = findExpressionType(fn, child, varTypes);
          int rType = findExpressionType(fn, child.getNext(), varTypes);
          return lType | rType; // we're not distinguishing strings yet
        }
    }

    Node child = n.getFirstChild();
    if (child == null) {
      return Optimizer.AnyType;
    } else {
      int result = Optimizer.NoType;
      while (child != null) {
        result |= findExpressionType(fn, child, varTypes);
        child = child.getNext();
      }
      return result;
    }
  }
Exemplo n.º 7
0
 public void add(int n) {
   Node node = new Node(n);
   Node temp = head;
   if (numItems == 0 || node.getNum() < temp.getNum()) {
     node.setNext(temp);
     temp.setPrev(node);
     head = node;
     numItems++;
   } else {
     for (int i = 0; i <= numItems; i++) {
       if (node.getNum() == temp.getNum()) {
         temp.getPrev().setNext(node);
         node.setPrev(temp.getPrev());
         node.setNext(temp);
         temp.setPrev(node);
         numItems++;
         break;
       } else if (node.getNum() <= temp.getNum() && temp.getNext() == null) {
         temp.setNext(node);
         node.setPrev(temp);
         node.setNext(null);
         numItems++;
         break;
       } else {
         temp = temp.getNext();
       }
     }
   }
 }
Exemplo n.º 8
0
 /**
  * Falls es ein aktuelles Objekt gibt (hasAccess() == true), wird das aktuelle Objekt geloescht
  * und das Objekt hinter dem gelaeschten Objekt wird zum aktuellen Objekt. Wird das Objekt, das am
  * Ende der Liste steht, geloescht, gibt es kein aktuelles Objekt mehr (hasAccess() == false).
  * Wenn die Liste leer ist oder es kein aktuelles Objekt gibt (hasAccess() == false), bleibt die
  * Liste unveraendert.
  */
 public void remove() {
   Node lPos, lFront;
   if (this.hasAccess()) {
     if (current == first) {
       first = current.getNext();
       if (current.getNext() == tail) {
         tail.setNext(first);
       }
       current = first;
     } else {
       lPos = current;
       this.toFirst();
       lFront = current;
       while (this.hasAccess() && !(current == lPos)) {
         lFront = current;
         this.next();
       }
       lFront.setNext(lPos.getNext());
       current = lFront.getNext();
       if (current == tail) {
         tail.setNext(lFront);
       }
     }
   }
 }
 public Node add(Node node) {
   if (node.getNext() == null) {
     node.setNext(head.getNext());
   }
   head.setNext(node);
   return node;
 }
Exemplo n.º 10
0
 // insert an item at current position in the linked list
 public void insert(T item) {
   curr.setNext(new Node<T>(item, curr.getNext()));
   if (tail == curr) {
     tail = curr.getNext();
   }
   len++;
 }
  @Override
  public boolean insert(int index, Object data) {
    boolean flag = false;

    isValidIndx(index);
    if (head == null) return false; // 链表为空

    if (index == 0) { // 头结点
      Node newNod = new Node(data, head);
      head = newNod;
      length++;
      return true;
    }
    Node temp = head.getNext();
    Node Prvtemp = head;
    int count = 1;

    while (temp != null) { // 中间结点
      if (count == index) {
        Node newNode = new Node(data, temp);
        Prvtemp.setNex(newNode);
        flag = true;
      }
      temp = temp.getNext();
      Prvtemp = Prvtemp.getNext();
      count++;
    }
    if (index == length) { // 尾结点
      Node newNod = new Node(data, null);
      Prvtemp.setNex(newNod);
      flag = true;
    }
    return flag;
  }
Exemplo n.º 12
0
  private JsTry mapTryStatement(Node tryNode) throws JsParserException {
    JsTry toTry = new JsTry();

    // Map the "try" body.
    //
    Node fromTryBody = tryNode.getFirstChild();
    toTry.setTryBlock(mapBlock(fromTryBody));

    // Map zero or more catch blocks.
    //
    Node fromCatchNodes = fromTryBody.getNext();
    Node fromCatchNode = fromCatchNodes.getFirstChild();
    while (fromCatchNode != null) {
      assert (fromCatchNode.getType() == TokenStream.CATCH);
      // Map the catch variable.
      //
      Node fromCatchVarName = fromCatchNode.getFirstChild();
      JsCatch catchBlock = scopeContext.enterCatch(fromCatchVarName.getString());

      // Pre-advance to the next catch block, if any.
      // We do this here to decide whether or not this is the last one.
      //
      fromCatchNode = fromCatchNode.getNext();

      // Map the condition, with a little fixup based on whether or not
      // this is the last catch block.
      //
      Node fromCondition = fromCatchVarName.getNext();
      JsExpression toCondition = mapExpression(fromCondition);
      catchBlock.setCondition(toCondition);
      if (fromCatchNode == null) {
        if (toCondition instanceof JsBooleanLiteral) {
          if (((JsBooleanLiteral) toCondition).getValue()) {
            // Actually, this is an unconditional catch block.
            // Indicate that by nulling the condition.
            //
            catchBlock.setCondition(null);
          }
        }
      }

      // Map the catch body.
      //
      Node fromCatchBody = fromCondition.getNext();
      catchBlock.setBody(mapBlock(fromCatchBody));

      // Attach it.
      //
      toTry.getCatches().add(catchBlock);
      scopeContext.exitCatch();
    }

    Node fromFinallyNode = fromCatchNodes.getNext();
    if (fromFinallyNode != null) {
      toTry.setFinallyBlock(mapBlock(fromFinallyNode));
    }

    return toTry;
  }
Exemplo n.º 13
0
 public String toString() {
   String str = "";
   Node<T> temp = head;
   while (temp.getNext() != null) {
     str += temp.getData() + " ,";
     temp = temp.getNext();
   }
   return str + temp.getData();
 }
Exemplo n.º 14
0
  private void removeNext(Node<T> prev) {
    Node<T> node = prev.getNext();

    prev.setNext(node.getNext());

    node.setItem(null);
    node.setNext(null);
    size.decrementAndGet();
  }
Exemplo n.º 15
0
 // move the current to one step left
 // no change if at head
 public void prev() {
   if (curr == head) {
     return;
   }
   Node<T> tmp = head;
   while (tmp.getNext() != curr) {
     tmp = tmp.getNext();
   }
   curr = tmp;
 }
 public int remove() {
   int val = -1;
   if (head.getNext() == null) {
     throw new IllegalArgumentException("No more free nodes left");
   } else {
     val = head.getNext().getValue();
     head.setNext(head.getNext().getNext());
   }
   return val;
 }
Exemplo n.º 17
0
 public static void printNodes(Node val) throws Exception {
   Node temp = val;
   System.out.println("Node" + " " + "Next" + " " + "Random");
   while (temp.getNext() != null) {
     System.out.println(
         temp.getData() + " " + temp.getNext().getData() + " " + temp.getRandom().getData());
     temp = temp.getNext();
   }
   System.out.println(temp.getData() + " ");
 }
Exemplo n.º 18
0
 public int find(E d) {
   Node<E> tmp = current;
   int c = 0;
   while (tmp.getNext() != current) {
     if (tmp.getData() == d) return c;
     tmp = tmp.getNext();
     c++;
   }
   return -1;
 }
Exemplo n.º 19
0
  public void add(Task data) {
    Node temp = new Node(data);
    Node current = head;

    while (current.getNext() != null) {
      current = current.getNext();
    }

    current.setNext(temp);
    listCount++;
  }
Exemplo n.º 20
0
 public String toString() {
   if (current == null) return "";
   Node<E> tmp = current;
   String s = tmp.getData() + " ";
   ;
   while (tmp.getNext() != null && tmp.getNext() != current) {
     tmp = tmp.getNext();
     s = s + tmp.getData() + " ";
   }
   return s;
 }
Exemplo n.º 21
0
 // remove and return current item from the list
 public T remove() {
   if (curr.getNext() == null) {
     return null;
   }
   T item = curr.getNext().getData();
   if (tail == curr.getNext()) {
     tail = curr;
   }
   curr.setNext(curr.getNext().getNext());
   len--;
   return item;
 }
Exemplo n.º 22
0
  public void add(Task data, int index) {

    Node temp = new Node(data);
    Node current = head;

    for (int i = 1; i < index && current.getNext() != null; i++) {
      current = current.getNext();
    }
    temp.setNext(current.getNext());
    current.setNext(temp);
    listCount++;
  }
Exemplo n.º 23
0
  public String toString() {

    Node current = head.getNext();
    String output = "";

    while (current != null) {
      output += "[" + current.getData().getDescription() + "]";
      current = current.getNext();
    }

    return output;
  }
  /**
   * Moves the iterator one step forwards in the list. When it hits the end it will not move past
   * the end of the list.
   *
   * @return true if the iterator did not hit the end of the list, false otherwise
   */
  public boolean moveToNext() {
    boolean result = false;

    if (current != null) {
      if (current.getNext() != null) {
        current = current.getNext();
        result = true;
      }
    }

    return result;
  }
Exemplo n.º 25
0
 private Node getLastNode() {
   if (getHead() == null) {
     return null;
   } else if (getHead().getNext() == null) {
     return getHead();
   } else {
     Node next = getHead().getNext();
     while (next.getNext() != null) {
       next = next.getNext();
     }
     return next;
   }
 }
Exemplo n.º 26
0
  public Task get(int index) {

    if (index < 0) return null;

    Node current = head.getNext();

    for (int i = 1; i <= index; i++) {
      if (current.getNext() == null) return null;

      current = current.getNext();
    }

    return current.getData();
  }
Exemplo n.º 27
0
 // Add support for Integer.MAX_VALUE
 @Override
 public int size() {
   if (getHead() == null) {
     return 0;
   } else {
     int count = 1;
     Node next = getHead();
     while (next.getNext() != null) {
       count++;
       next = next.getNext();
     }
     return count;
   }
 }
Exemplo n.º 28
0
  private JsExpression mapConditional(Node condNode) throws JsParserException {
    JsConditional toCond = new JsConditional();

    Node fromTest = condNode.getFirstChild();
    toCond.setTestExpression(mapExpression(fromTest));

    Node fromThen = fromTest.getNext();
    toCond.setThenExpression(mapExpression(fromThen));

    Node fromElse = fromThen.getNext();
    toCond.setElseExpression(mapExpression(fromElse));

    return toCond;
  }
Exemplo n.º 29
0
 public String remove(int i) {
   int count = -1;
   String ans = "";
   Node tmp = head;
   while (tmp != null) {
     count++;
     if (count == (i - 1)) {
       ans = tmp.getNext().getData(); // to return this value
       tmp.setNext(tmp.getNext().getNext());
     }
     tmp = tmp.getNext();
   }
   return ans;
 }
Exemplo n.º 30
0
  private JsSwitch mapSwitchStatement(Node switchNode) throws JsParserException {
    JsSwitch toSwitch = new JsSwitch();

    // The switch expression.
    //
    Node fromSwitchExpr = switchNode.getFirstChild();
    toSwitch.setExpression(mapExpression(fromSwitchExpr));

    // The members.
    //
    Node fromMember = fromSwitchExpr.getNext();
    while (fromMember != null) {
      if (fromMember.getType() == TokenStream.CASE) {
        JsCase toCase = new JsCase();

        // Set the case expression. In JS, this can be any expression.
        //
        Node fromCaseExpr = fromMember.getFirstChild();
        toCase.setCaseExpression(mapExpression(fromCaseExpr));

        // Set the case statements.
        //
        Node fromCaseBlock = fromCaseExpr.getNext();
        mapStatements(toCase.getStatements(), fromCaseBlock);

        // Attach the case to the switch.
        //
        toSwitch.getCases().add(toCase);
      } else {
        // This should be the only default statement.
        // If more than one is present, we keep the last one.
        //
        assert (fromMember.getType() == TokenStream.DEFAULT);
        JsDefault toDefault = new JsDefault();

        // Set the default statements.
        //
        Node fromDefaultBlock = fromMember.getFirstChild();
        mapStatements(toDefault.getStatements(), fromDefaultBlock);

        // Attach the default to the switch.
        //
        toSwitch.getCases().add(toDefault);
      }
      fromMember = fromMember.getNext();
    }

    return toSwitch;
  }