Results 1 to 2 of 2
  1. #1
    cuteAli is offline Member
    Join Date
    May 2012
    Rep Power

    Default insert problem with my doubly linked list.

    please help me with insert method.... i am stuck on one of the conditions... not sure how to solve it.

    Thnx for the answer, i understand doubly linked list. but i want more than one row of doubly linked list. for example:

    i 2 1 6 3 5 4 (if i give this to my app class) which works fine.
    then the toString (which works fine as well) should print like below format:

    [ 1][ 3][ 4]
    [ 2][ 5]
    [ 6]

    this is how we got the above result.

    1.if matrix is empty, add newNode as first (in my case its smallest)
    2.if newNode is greater than smallest or anynode in that row, then add it to right;
    3.if newNode is smaller than smallest, then newNode becomes smallest and pushes the old smallest down the row. (in our case, 2 was smallest, now 1)
    4.if newNode have greater value, replace the greater node to the newNode: for example we had [ 1][ 6] then add [ 3], which will go between 1 and 6, hence replace 6 with 3 and put it (6) in the below row where appropriate. if [ 6] in below row have greater value then replace it and put that greater value in more below row untill all is sorted.** (this is were all the problem is)
    5.Please see nested CEll class for information on nodes i am using. (left. right. below. above, smallest)

    Java Code:
    public class Matrix {
      private Cell smallest = null;
      private Cell temp = null;
       public void insert(int newInt) {
       Cell newCell = new Cell (newInt);
        if (this.size() == 0){
      smallest = newCell;
      for (Cell i = smallest; i != null; i = i.right){
      // if newCell is the larger than all in row, then add it to the end. 
      if (newCell.value > i.value && i.right == null){
          i.right = newCell; 
      // if newCell is smaller than smallest, then add it to smallest
      // and old smallest will go down the list
      //  (compare and put it where appropriate). 
      if (newCell.value < smallest.value){
          newCell.below = smallest; 
          newCell.right = smallest.right; 
          smallest.right = null; 
          smallest = newCell;             
      // if newCell is smallest than any element, 
      // then replace that cell with newCell, and send that cell to below row
      // do again untill all below rows are sorted. 
      // hence this could be a while loop. 
      if (newCell.value > i.value && newCell.value < i.right.value){
          System.out.println("newCell is " + newCell);
          Cell largerRight = i.right; 
          newCell.right = largerRight.right;
          largerRight.right = null; 
          i.right = newCell;                      
          temp = smallest.below; 
          while (temp != null){
              for (Cell k = temp; k != null; k = k.right){
                  if (largerRight.value > temp.value && temp.right == null){
                      temp.right = largerRight;
                      temp = null; 
              temp =temp.below; 
        }//end insert
       public int size() {
       int size = 0;
       for (Cell i = smallest; i != null; i = i.below) {
        for (Cell j = i; j != null; j = j.right) {
        return size;
        // check if matrix is empty. we can use size as well here 
        public boolean isEmpty() { 
         return smallest == null; 
        public String toString() {
        StringBuilder s = new StringBuilder();
         for (Cell i = smallest; i != null; i = i.below) {
        for (Cell j = i; j != null; j = j.right) {
         return s.toString();
         // nested class. 
        private static class Cell {
         public int value;
         public Cell above, below, left, right;
           public Cell(int value) {
          this.value = value;
           public String toString() {
            return String.format("[%2d]", value);
    and my application class is as below...

    Java Code:
    import java.util.Scanner;
          public class App {
          public static void main (String[] args){
          Scanner input = new Scanner (;
          Matrix matrix = new Matrix();
          while (input.hasNextLine()){
             handleLine (input.nextLine(), matrix);
           public static void handleLine (String line, Matrix matrix){
          Scanner tokens = new Scanner (line);
          if (tokens.hasNext("[ispr]")){
             char command =;
             switch (command){
             case 'i':
                while (tokens.hasNextInt()){
             case 's':
                while (tokens.hasNextInt()){
             case 'r':
             case 'p':

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Washington, DC
    Rep Power

    Default Re: insert problem with my doubly linked list.

    Sorry, but what's your actual technical question? You've listed some requirements and a bunch of code, but that doesn't really tell us much about what you tried or where you're stuck. Much better to post an SSCCE that demonstrates the problem.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

  1. Doubly-Linked list
    By swp in forum New To Java
    Replies: 3
    Last Post: 10-11-2011, 03:27 PM
  2. Sorted Doubly linked List
    By student2889 in forum New To Java
    Replies: 1
    Last Post: 10-14-2010, 10:05 AM
  3. Doubly Linked List
    By matin1234 in forum New To Java
    Replies: 0
    Last Post: 06-02-2010, 05:58 AM
  4. doubly linked list insert
    By ineedhelpwithjava in forum Advanced Java
    Replies: 1
    Last Post: 03-20-2009, 02:05 PM
  5. Help with Doubly linked list
    By Dr Gonzo in forum New To Java
    Replies: 5
    Last Post: 12-06-2008, 07:45 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts