Results 1 to 2 of 2
 05152012, 03:15 PM #1Member
 Join Date
 May 2012
 Posts
 1
 Rep Power
 0
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:
<pre>
[ 1][ 3][ 4]
[ 2][ 5]
[ 6]
</pre>
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){ System.out.println(temp); 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) { size++; } //size++; } 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) { s.append(j); } s.append('\n'); } 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); } } }
Java Code:import java.util.Scanner; public class App { public static void main (String[] args){ Scanner input = new Scanner (System.in); 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 = tokens.next().charAt(0); switch (command){ case 'i': while (tokens.hasNextInt()){ matrix.insert(tokens.nextInt()); } break; case 's': while (tokens.hasNextInt()){ matrix.insert(tokens.nextInt()); } break; case 'r': matrix.delete(); break; case 'p': System.out.println(matrix.toString()); break; } } } }
 05152012, 03:22 PM #2
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

DoublyLinked list
By swp in forum New To JavaReplies: 3Last Post: 10112011, 03:27 PM 
Sorted Doubly linked List
By student2889 in forum New To JavaReplies: 1Last Post: 10142010, 10:05 AM 
Doubly Linked List
By matin1234 in forum New To JavaReplies: 0Last Post: 06022010, 05:58 AM 
doubly linked list insert
By ineedhelpwithjava in forum Advanced JavaReplies: 1Last Post: 03202009, 03:05 PM 
Help with Doubly linked list
By Dr Gonzo in forum New To JavaReplies: 5Last Post: 12062008, 08:45 AM
Bookmarks