how to draw circular doubly linked list?

Doubly Linked List Java Programming?

  • I need to add a count and GetLargest method to this DoublyLinkedList class. 1) Count method - takes no parameters, it counts and returns the number of elements in the linked list. 2) GetLargest method - finds and returns the largest element in the linked list. Assume all values are greater than 0 and return 0 on an empty list. --------------------------------------… public class DoublyLinkedList { private class node { int Item; node Next; node Previous; }; private node Head; public DoublyLinkedList() { // Set up the dummy node. Head = new node(); Head.Next = Head; Head.Previous = Head; }; // Add at the front public void Insert(int Item) { // Insert at the front of the doubly linked list. node afterMe = Head; node newNode = new node(); newNode.Item = Item; // Set up the pointers in the new node // The new node points back to the dummy node newNode.Previous = afterMe; // The new node points forward to the former first node. newNode.Next = afterMe.Next; // This changes the dummy node to pointer to the new node. newNode.Previous.Next = newNode; // Change the former first node to indicate we are in front of it. newNode.Next.Previous = newNode; }; // Add body to the Count Method public int Count() { ... } // Add body to the GetLargest Method public int GetLargest() { ... } };

  • Answer:

    i added the two methods u left. also i simplified your insert method and removed the constructor because there was no need for it. also method and field names all must start with a small letter, so i changed those for you as well. there is also a simple test cycle in your main. import java.util.Random; public class DoublyLinkedList { 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 int count() { if( head==null){ return 0; } Node node = head; int count = 0; while( true){ node = node.next; if( node!=head){ count++; }else{ break; } } 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){ DoublyLinkedList list = new DoublyLinkedList(); Random random = new Random(); //randomly add between 5 to 14 numbers to list for( int i=0; i< random.nextInt( 10)+5; i++){ //random insert numbers between 0 and 100, not including 100 list.Insert( random.nextInt( 100)); } list.print(); System.out.println( "There are " + list.count() + " items in the list"); System.out.println( "The larget number in the list is " + list.getLargest()); } } good luck don't forget to choose best answer

ben at Yahoo! Answers Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.