Most of the OPs are able to paste their code on the forum and use code tags. It can't be that hard if everyone can do it. Try again.
type: [code]
then paste the code
then type: [/code]
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Most of the OPs are able to paste their code on the forum and use code tags. It can't be that hard if everyone can do it. Try again.
type: [code]
then paste the code
then type: [/code]
If you don't understand my answer, don't ignore it, ask a question.
import java.util.Random; public class DLinkedList { private class Node { int item; Node next; Node previous; } private Node head = null; public void Insert(int item) { if( head==null){ head = new Node(); head.item = item; head.next = head; head.previous = head; }else{ Node newNode = new Node(); newNode.item = item; Node tail = head.previous; tail.next = newNode; newNode.previous = tail; newNode.next = head; head.previous = newNode; } } public boolean insertAfter(int key, int item) { Node first=null; Node last=null; Node current = first; // System.out.println( key); while (current.item !=key) { current = current.next; if (current == null){ return false; } } Node newNode = new Node(); if (current == last) { newNode.next = null; last = newNode; } else { newNode.next = current.next; current.next.previous = newNode; } newNode.previous = current; current.next = newNode; return true; } public int count() { if( head==null){ return 0; } Node node = head; int count = 0; while( true){ node = node.next; if( node!=head){ count++; }else{ break; } } if( head==null){ System.out.println( " List is empty"); }else{ //Node node = head.previous.next; int x=1; while(x <= count/2){ node = node.next; x++; if( node!=head){ System.out.print( node.item + " "); }else{ break; } } //System.out.println(); } return count; } //public int getLargest() { //number below is the bottom negative range of integer //int largest = -2147483648; //if( head==null){ //return largest; //} //Node node = head; //largest = head.item; //while( true){ //node = node.next; //if( node!=head){ //if( node.item > largest){ //largest = node.item; //} //}else{ //break; //} //} //return largest; //} public void print(){ if( head==null){ System.out.println( " List is empty"); }else{ Node node = head.previous.next; while( true){ node = node.next; if( node!=head){ System.out.print( node.item + " "); }else{ break; } } System.out.println(); } } public static void main( String[] args){ DLinkedList list = new DLinkedList(); //Random random = new Random(); //randomly add between 5 to 14 numbers to list for(int i=1; i<10; i++){ //random insert numbers between 0 and 100, not including 100 list.Insert(i); } list.print(); System.out.println( "the middle of list after item no. " + list.count()/2 ); //System.out.println( "The larget number in the list is " + list.getLargest()); list.insertAfter(5, 77); list.print(); } }
--- Update ---
i post it again.i did like this every time but i do not know where the problem
I have no idea what your problem is.
Why is the formatting for the insertAfter() method good and the rest of the code is not formatted?
If you don't understand my answer, don't ignore it, ask a question.
can you copy it and paste in your IDE??
I will copy it when it is properly formatted.
If you don't understand my answer, don't ignore it, ask a question.
look!! this is the capture of the code
I'm waiting for properly formatted code to be posted.
If you don't understand my answer, don't ignore it, ask a question.
it is formatted!!
--- Update ---
ok thank you..i think you are annoying from me
where? I can't see where any of the posted code is properly formatted.
What is the post# where it's formatted? except for the insertAfter() method.
If you don't want to format the source code, let me know and I will leave you for someone else that doesn't care if the code they work with is formatted or not.ok thank you..i think you are annoying from me
Let me know.
If you don't understand my answer, don't ignore it, ask a question.
aegr (March 24th, 2013)
i type,formate the sizeand paste the code and type
this is not the first time i post code in forums!!
--- Update ---
thank you..for your patiance..you see the captures?
--- Update ---
import java.util.Random; public class DLinkedList { private class Node { int item; Node next; Node previous; } private Node head = null; public void Insert(int item) { if( head==null){ head = new Node(); head.item = item; head.next = head; head.previous = head; }else{ Node newNode = new Node(); newNode.item = item; Node tail = head.previous; tail.next = newNode; newNode.previous = tail; newNode.next = head; head.previous = newNode; } } public boolean insertAfter(int key, int item) { Node first=null; Node last=null; Node current = first; // System.out.println( key); while (current.item !=key) { current = current.next; if (current == null){ return false; } } Node newNode = new Node(); if (current == last) { newNode.next = null; last = newNode; } else { newNode.next = current.next; current.next.previous = newNode; } newNode.previous = current; current.next = newNode; return true; } public int count() { if( head==null){ return 0; } Node node = head; int count = 0; while( true){ node = node.next; if( node!=head){ count++; }else{ break; } } if( head==null){ System.out.println( " List is empty"); }else{ //Node node = head.previous.next; int x=1; while(x <= count/2){ node = node.next; x++; if( node!=head){ System.out.print( node.item + " "); }else{ break; } } //System.out.println(); } return count; } //public int getLargest() { //number below is the bottom negative range of integer //int largest = -2147483648; //if( head==null){ //return largest; //} //Node node = head; //largest = head.item; //while( true){ //node = node.next; //if( node!=head){ //if( node.item > largest){ //largest = node.item; //} //}else{ //break; //} //} //return largest; //} public void print(){ if( head==null){ System.out.println( " List is empty"); }else{ Node node = head.previous.next; while( true){ node = node.next; if( node!=head){ System.out.print( node.item + " "); }else{ break; } } System.out.println(); } } public static void main( String[] args){ DLinkedList list = new DLinkedList(); //Random random = new Random(); //randomly add between 5 to 14 numbers to list for(int i=1; i<10; i++){ //random insert numbers between 0 and 100, not including 100 list.Insert(i); } list.print(); System.out.println( "the middle of list after item no. " + list.count()/2 ); //System.out.println( "The larget number in the list is " + list.getLargest()); list.insertAfter(5, 77); list.print(); } }
--- Update ---
i will be crazy!!!what is the problem
We see your posted code, and while it has *some* formatting, it is not *well formatted* since you are not using indentation in a uniform and standard way.
<blunt>Look. You are asking for free help from volunteers. Is it really asking too much of you to put in just a little effort to post *well formatted* code so that we can more easily read your code and understand it? </blunt>
aegr (March 24th, 2013)
thank you for all