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
    }
  }
示例#2
0
 public void deleteNode(Node node) {
   if (node.next != null) {
     node.data = node.next.data;
     node.next = node.next.next;
   } else {
     throw new NoSuchElementException();
   }
 }
  // Helper method to add new nodes to the end of a linked list.
  private void addToEnd(Node toAdd) {

    if (poly == null) {
      poly = toAdd;
    } else {
      Node tmp;
      for (tmp = poly; tmp.next != null; tmp = tmp.next) {}
      tmp.next = toAdd;
    }
  }
  // Helper method to combine all like terms in a list. MUST BE SORTED FIRST.
  private void compress() {

    Node tmp = poly;

    while (tmp.next != null) {
      if (tmp.term.degree == tmp.next.term.degree) {
        tmp.term.coeff += tmp.next.term.coeff;
        tmp.next = tmp.next.next;
      } else if (tmp.term.degree < tmp.next.term.degree) tmp = tmp.next;
    }
  }
  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;
        }
      }
    }
  }
  // Helper method to sort an unsorted linked list in ascending order. All like degrees will be next
  // to each other.
  private void sort() {

    Polynomial newList = new Polynomial();

    for (Node tmp1 = poly; tmp1 != null; tmp1 = tmp1.next) {
      Node n = new Node(tmp1.term.coeff, tmp1.term.degree, null);

      if (newList.poly == null) newList.poly = n;
      else {
        for (Node tmp2 = newList.poly; tmp2 != null; tmp2 = tmp2.next) {
          if (tmp2.next == null) {
            tmp2.next = n;
            break;
          } else if (tmp2.next.term.degree > n.term.degree) {
            n.next = tmp2.next;
            tmp2.next = n;
            break;
          }
        }
      }
    }
    poly = newList.poly;
  }
  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;
    }
  }
示例#8
0
 public static void main(String args[]) {
   System.out.println("Partition based on a number x");
   System.out.println("Enter no of elements in a linked list");
   try {
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     int n = Integer.parseInt(br.readLine());
     Node head = new Node();
     Node temp = head;
     System.out.println("Enter elements");
     for (int i = 0; i < n; i++) {
       temp.next = new Node();
       temp.next.data = Integer.parseInt(br.readLine());
       temp = temp.next;
     }
     temp = head.next;
     System.out.println("Enter x:");
     int x = Integer.parseInt(br.readLine());
     System.out.println("Current Linked List");
     for (int i = 0; i < n - 1; i++) {
       System.out.print(temp.data + " -> ");
       temp = temp.next;
     }
     System.out.println(temp.data);
     System.out.println();
     temp = head.next;
     Node before = null, after = null;
     Node beforerun = new Node();
     Node afterrun = new Node();
     int a = 0, b = 0;
     while (temp != null) {
       if (temp.data <= x) {
         if (b == 0) {
           before = temp;
           b = 1;
         }
         beforerun.next = temp;
         temp = temp.next;
         beforerun = beforerun.next;
       } else if (temp.data > x) {
         if (a == 0) {
           after = temp;
           a = 1;
         }
         afterrun.next = temp;
         temp = temp.next;
         afterrun = afterrun.next;
       }
     }
     if (before != null) head.next = before;
     else head.next = after;
     if (beforerun != null) beforerun.next = after;
     if (afterrun != null) afterrun.next = null;
     temp = head.next;
     System.out.println("Linked List after partition");
     if (temp != null) {
       System.out.print(temp.data);
     }
     temp = temp.next;
     while (temp != null) {
       System.out.print(" -> " + temp.data);
       temp = temp.next;
     }
     System.out.println();
   } catch (IOException e) {
     System.err.println("Error:" + e);
   }
 }