// no concurrent read/write is assumed
 public V put(double key, V value) {
   if (value == null) throw new NullPointerException();
   // not incrementing b's refcnt, so need not release it.
   Node b = skipListMap.findGrandPredecessor(key);
   if (b == null) b = head;
   Node n = b.next;
   if (n == null) {
     n = Node.alloc();
     n.put(key, value, this);
     n.next = null;
     b.next = n;
     return null;
   }
   Node f = n.next;
   if (f == null) { // put (key,value) into node n
     Object val = n.put(key, value, this);
     return (V) val;
   } else {
     assert f.len() > 0 && f.first() >= key;
     if (f.first() == key) {
       Object val = f.put(key, value, this);
       return (V) val;
     } else {
       Object val = n.put(key, value, this);
       return (V) val;
     }
   }
 }
  private static Node partition(Node head, int val) {
    Node less = null;
    Node p1 = less;
    Node great = null;
    Node p2 = great;
    while (head != null) {
      Node temp = head.next;
      head.next = null;
      if (head.getData() < val) {
        if (less == null) {
          less = head;
          p1 = less;
        } else {
          p1.next = head;
          p1 = p1.next;
        }
      } else {
        if (great == null) {
          great = head;
          p2 = great;
        } else {
          p2.next = head;
          p2 = p2.next;
        }
      }
      head = temp;
    }

    p1.next = great;
    return less;
  }
  public void deleteitem(int element) {
    Node temp; // ref:http://web.cse.ohio-state.edu/~reeves/CSE2421sp13/lab3linklistCOD
    temp = head;
    while (temp != null) // while LL is not empty
    {
      if (element == temp.data) {
        if (temp == head) // deleting the first node
        {
          head = head.next;
          temp.next.prev = null;
          temp.next = null;

        } else if (end == temp) // is it end
        {
          temp.prev.next = null;
          end = temp.prev;
          temp.prev = null;

        } else // if not first then
        {
          // while(head.next != temp)
          // {
          temp.prev.next = temp.next;
          temp.next.prev = temp.prev;
          temp.next = null;
          temp.prev = null;
          // }
        }
      }
      temp = temp.next; // traversing through the LL by pointing to next ref
    }
  }
Example #4
0
File: Merge.java Project: nawns/epi
  public static void main(String[] args) {
    Node a = new Node(2);
    Node b = new Node(5);
    Node c = new Node(7);
    a.next = b;
    b.next = c;
    System.out.println(a);

    Node d = new Node(3);
    Node e = new Node(11);
    d.next = e;
    System.out.println(d);

    System.out.println(merge(a, d));
  }
 private static void deleteFromMiddle(Node del) {
   if (del.next == null) {
     return;
   }
   del.setData(del.next.getData());
   del.next = del.next.next;
 }
Example #6
0
 public void deleteNode(Node node) {
   if (node.next != null) {
     node.data = node.next.data;
     node.next = node.next.next;
   } else {
     throw new NoSuchElementException();
   }
 }
	/*To reverse a singly list, we recurse till the bottom
	and then come back out linking everything back*/
	public Node reverseList(Node current){
		if(current==null){			
			return null;	
		}			
		else{
			Node nextNode = reverseList(current.next);
			if(nextNode!=null){
				nextNode.next = current;
				current.next = null;
			}
			else
			{
				this.head = current;
			}			
			return current;
		}
	}
Example #8
0
 public static Node partition(Node head) {
   Node mnode = median(head);
   if (mnode != null) {
     Node ret = mnode.next;
     mnode.next = null;
     return ret;
   } else return mnode;
 }
 public void clear() {
   for (Node n = head.next; n != null; n = n.next) {
     Node.release(n);
     assert n.getRefCnt() == 0;
     n.clearEntry();
     Node.free(n);
   }
   head.next = null;
 }
Example #10
0
 public void moveToTail(Node curt) {
   curt.prev = tail.prev;
   curt.next = tail;
   curt.prev.next = curt;
   curt.next.prev = curt;
   /*curt.prev = tail.prev;
   tail.prev = curt;
   curt.prev.next = curt;
   curt.next = tail;*/
 }
  public void final_add(int element) {
    // tried using bubble sort after adding the new node  but didnot worked so checked manually
    Node n;
    Node temp_node;
    temp_node = head;

    if (head == null) {
      n = new Node(element, null, null);
      head = n;
      end = n;
    } else if (head == end) {
      if (element <= head.data) {
        n = new Node(element, temp_node, temp_node.prev);
        temp_node.prev = n;
        head = n;
      } else {
        n = new Node(element, temp_node.next, temp_node);
        temp_node.next = n;
        end = n;
      }
    } else if (head != end) {
      if (element <= head.data) {
        n = new Node(element, temp_node, temp_node.prev);
        temp_node.prev = n;
        head = n;
      } else if (element > end.data) {
        n = new Node(element, end.next, end);
        end.next = n;
        end = n;
      } else {
        while (temp_node != null) {
          if (element <= temp_node.data) {
            n = new Node(element, temp_node, temp_node.prev);
            temp_node.prev.next = n;
            temp_node.prev = n;
          }
          temp_node = temp_node.next;
        }
      }
    }
  }
	public void addElement(int data, Node next){
		Node current = head;
		Node newNode = new Node(data,next);
		if(head==null)
			head = newNode;
		else
		{
			while(current.next!=null){
				current = current.next;
			}
			current.next = newNode;
		}
	}
Example #13
0
 public static Node mergeList(Node s1, Node s2) {
   Node curr = null;
   Node head = curr;
   while (s1 != null || s2 != null) {
     if (s1 == null) {
       if (curr == null) head = curr = s2;
       else {
         curr.next = s2;
         curr = curr.next;
       }
       s2 = s2.next;
     } else if (s2 == null) {
       if (curr == null) head = curr = s1;
       else {
         curr.next = s1;
         curr = curr.next;
       }
       s1 = s1.next;
     } else {
       if (s1.data < s2.data) {
         if (curr == null) head = curr = s1;
         else {
           curr.next = s1;
           curr = curr.next;
         }
         s1 = s1.next;
       } else {
         if (curr == null) head = curr = s2;
         else {
           curr.next = s2;
           curr = curr.next;
         }
         s2 = s2.next;
       }
     }
   }
   return head;
 }
    // concurrent read/write access is allowed
    boolean appendNewAtomic(double key, Object value, ConcurrentDoubleOrderedListMap orderedMap) {
      synchronized (this) {
        if (isMarked()) return false;
        if (next != null) return false;

        Node n = Node.alloc();
        n.put(key, value, orderedMap);
        // assert n.len()==1;
        n.next = null;
        boolean success = casNext(null, n);
        assert success;
        return true;
      }
    }
  public void deletingfirst() {
    Node temp;
    temp = head;
    if (head == end) {
      end = null;
      head = null;

    } else if (head != end) {
      head = temp.next;
      temp.next.prev = null;
      temp.next = null;
      temp.prev = null;
    }
  }
Example #16
0
  public static Node partition(Node node, int partition) {
    Node prev = null;
    Node temp = node;
    Node next = node.next;

    Node less = null;
    Node more = null;
    Node lessHead = null;
    Node moreHead = null;

    while (temp != null) {
      next = temp.next;
      temp.next = null;
      if (temp.val < partition) {
        if (less == null) {
          less = temp;
          lessHead = less;
        } else {
          less.next = temp;
        }
        if (less.next != null) less = less.next;
      } else {
        if (more == null) {
          more = temp;
          moreHead = more;
        } else {
          more.next = temp;
        }
        if (more.next != null) more = more.next;
      }
      prev = temp;
      temp = next;
    }

    less.next = moreHead;
    return lessHead;
  }
Example #17
0
 public void set(int key, int value) {
   if (get(key) != -1) {
     map.get(key).val = value;
     return;
   } else {
     Node newNode = new Node(key, value);
     if (map.size() == capacity) {
       Node lruNode = head.next;
       map.remove(lruNode.key);
       head.next = head.next.next;
       head.next.prev = head;
     }
     map.put(key, newNode);
     moveToTail(newNode);
   }
 }
	public void deleteElement(int data){
		Node current = head;
		if(head==null)
			System.out.println("Empty list");
		else
		{
			while(current.next!=null){
				if(current.data==data){
					System.out.println("Element found and deleted");
					current.data = current.next.data;
					current.next = current.next.next;
					return;
				}
					
				current = current.next;
			}			
		}
	}
 private static Node deleteNode(Node head, int n) {
   Node h = head;
   if (h == null) {
     return head;
   } else {
     if (h.getData() == n) {
       return head.next;
     } else {
       while (h.next != null) {
         if (h.next.getData() == n) {
           h.next = h.next.next;
           // return head;
         }
         h = h.next;
         if (h == null) {
           return head;
         }
       }
       return head;
     }
   }
 }
Example #20
0
 public static void main(String[] args) {
   Node a = new Node(3);
   Node b = new Node(5);
   Node c = new Node(8);
   Node d = new Node(5);
   Node e = new Node(10);
   Node f = new Node(2);
   Node g = new Node(1);
   a.next = b;
   b.next = c;
   c.next = d;
   d.next = e;
   e.next = f;
   f.next = g;
   System.out.println(a);
   Node x = partition(a, 5);
   System.out.println(x);
 }
  private static Node addLinkedList(Node p1, Node p2) {
    Node head = null;
    Node temp = null;
    int digit = 0;
    int carry = 0;
    if (p1 == null && p2 == null) {
      return null;
    }
    while (p1 != null && p2 != null) {
      digit = p1.getData() + p2.getData() + carry;
      if (digit >= 10) {
        digit -= 10;
        carry = 1;
      } else {
        carry = 0;
      }
      if (head == null) {
        head = new Node(digit);
        temp = head;
      } else {
        temp.next = new Node(digit);
        temp = temp.next;
      }
      p1 = p1.next;
      p2 = p2.next;
    }

    if (p1 == null && p2 == null) {
      if (carry == 1) {
        temp.next = new Node(1);
      }
    } else if (p1 != null) {
      while (p1 != null) {
        digit = p1.getData() + carry;
        if (digit > 10) {
          digit -= 10;
          carry = 1;
        } else {
          carry = 0;
        }
        temp.next = new Node(digit);
        temp = temp.next;
        p1 = p1.next;
      }
      if (carry == 1) {
        temp.next = new Node(1);
      }

    } else {
      while (p2 != null) {
        digit = p2.getData() + carry;
        if (digit > 10) {
          digit -= 10;
          carry = 1;
        } else {
          carry = 0;
        }

        temp.next = new Node(digit);
        temp = temp.next;
        p2 = p2.next;
      }
      if (carry == 1) {
        temp.next = new Node(1);
      }
    }

    return head;
  }
Example #22
0
 public LRUCache2(int capacity) {
   this.capacity = capacity;
   head.next = tail;
   tail.prev = head;
 }
Example #23
0
 // Push/add an item to the stack.
 public void push(T item) {
   Node temp = new Node();
   temp.value = item;
   temp.next = first;
   first = temp;
 }
    // only used by putAtomic and PutAtomicIfAbsent. Inside synchronized(b) and synchronized(this).
    private void putAtomicReally(
        Node b, int pos, double key, Object value, ConcurrentDoubleOrderedListMap orderedMap) {
      int len = len();
      if (len + 1 <= keys.length) {
        if (pos == len) { // in-place append in the current node
          keys[pos] = key;
          vals[pos] = value;
          length = len + 1;
          if (pos == 0) {
            orderedMap.skipListMap.put(keys[0], this);
          }
        } else { // copied to a new node, replacing the current node
          mark();
          Node n = Node.alloc();
          n.next = this.next;
          if (next != null) next.incRefCnt();
          double[] nkeys = n.keys;
          Object[] nvals = n.vals;
          n.length = len + 1;
          nodeCopy(keys, vals, 0, nkeys, nvals, 0, pos);
          nkeys[pos] = key;
          nvals[pos] = value;
          nodeCopy(keys, vals, pos, nkeys, nvals, pos + 1, len - pos);

          orderedMap.skipListMap.put(nkeys[0], n);
          b.casNext(this, n); // should always succeed.
          if (pos == 0) {
            orderedMap.skipListMap.remove(keys[0], this);
          }
          release(this);
          free(this);
        }
      } else { // requires 2 new nodes, to replace the current node
        mark();
        Node n1 = Node.alloc();
        double[] n1keys = n1.keys;
        Object[] n1vals = n1.vals;
        Node n2 = Node.alloc();
        double[] n2keys = n2.keys;
        Object[] n2vals = n2.vals;
        int l1 = len / 2, l2 = len - l1;
        if (pos < l1) { // key, value stored in n1
          n1.length = l1 + 1;
          n2.length = l2;

          nodeCopy(keys, vals, 0, n1keys, n1vals, 0, pos);
          n1keys[pos] = key;
          n1vals[pos] = value;
          nodeCopy(keys, vals, pos, n1keys, n1vals, pos + 1, l1 - pos);
          nodeCopy(keys, vals, l1, n2keys, n2vals, 0, l2);

          n1.next = n2;
          n2.next = this.next;
          if (next != null) next.incRefCnt();

          orderedMap.skipListMap.put(n1keys[0], n1);
          orderedMap.skipListMap.put(n2keys[0], n2);
          b.casNext(this, n1); // should always succeed.
          if (pos == 0) {
            orderedMap.skipListMap.remove(keys[0], this);
          }
          release(this);
          free(this);
        } else { // key,value is stored in n2
          n1.length = l1;
          n2.length = l2 + 1;
          int newpos = pos - l1;

          nodeCopy(keys, vals, 0, n1keys, n1vals, 0, l1);

          nodeCopy(keys, vals, l1, n2keys, n2vals, 0, newpos);
          n2keys[newpos] = key;
          n2vals[newpos] = value;
          nodeCopy(keys, vals, pos, n2keys, n2vals, newpos + 1, l2 - newpos);

          n1.next = n2;
          n2.next = this.next;
          if (next != null) next.incRefCnt();

          orderedMap.skipListMap.put(n1keys[0], n1);
          orderedMap.skipListMap.put(n2keys[0], n2);
          b.casNext(this, n1); // should always succeed.
          release(this);
          free(this);
        }
      }
    }
    // no concurrent read/write access is assumed
    private void putReally(
        int pos, double key, Object value, ConcurrentDoubleOrderedListMap orderedMap) {
      int len = len();
      if (len + 1 <= keys.length) { // inserted in the current node
        nodeCopy(keys, vals, pos, keys, vals, pos + 1, len - pos);
        keys[pos] = key;
        vals[pos] = value;
        length = len + 1;
        if (pos == 0) {
          orderedMap.skipListMap.put(keys[0], this);
          if (len != 0) orderedMap.skipListMap.remove(keys[1], this);
        }
      } else if (emptySlotInNextTwo()) {
        if (pos == len) {
          next.put(key, value, orderedMap);
          return;
        }
        next.put(keys[len - 1], vals[len - 1], orderedMap);
        nodeCopy(keys, vals, pos, keys, vals, pos + 1, len - pos - 1);
        keys[pos] = key;
        vals[pos] = value;
        if (pos == 0) {
          orderedMap.skipListMap.remove(keys[1], this);
          orderedMap.skipListMap.put(keys[0], this);
        }
      } else { // current node is full, so requires a new node
        Node n = Node.alloc();
        double[] nkeys = n.keys;
        Object[] nvals = n.vals;
        int l1 = len / 2, l2 = len - l1;
        if (next == null && pos == len) { // this is the last node, simply add to the new node.
          nkeys[0] = key;
          nvals[0] = value;
          n.length = 1;
          orderedMap.skipListMap.put(nkeys[0], n);
        } else if (pos < l1) { // key,value is stored in the current node
          length = l1 + 1;
          n.length = l2;
          nodeCopy(keys, vals, l1, nkeys, nvals, 0, l2);

          nodeCopy(keys, vals, pos, keys, vals, pos + 1, l1 - pos);
          keys[pos] = key;
          vals[pos] = value;
          if (pos == 0) {
            orderedMap.skipListMap.remove(keys[1]);
            orderedMap.skipListMap.put(keys[0], this);
          }
          orderedMap.skipListMap.put(nkeys[0], n);
        } else { // key,value is stored in the new node
          length = l1;
          n.length = l2 + 1;
          int newpos = pos - l1;

          nodeCopy(keys, vals, l1, nkeys, nvals, 0, newpos);
          nkeys[newpos] = key;
          nvals[newpos] = value;
          nodeCopy(keys, vals, pos, nkeys, nvals, newpos + 1, l2 - newpos);

          orderedMap.skipListMap.put(nkeys[0], n);
        }
        n.next = this.next;
        this.next = n;
      }
    }
Example #26
0
File: Merge.java Project: nawns/epi
 public static Node append(Node node, Node tail) {
   tail.next = node;
   tail = node;
   return tail;
 }
Example #27
0
 public void push(int n) {
   Node p = new Node(n);
   p.next = top;
   top = p;
 } // end push