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.

Results 1 to 2 of 2

Thread: addInOrder LinkedList

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default addInOrder LinkedList

    I need to create a LinkedList out of the given strings and as I am adding them sort the entries alphabetically and get rid of duplicates. I got it to not add duplicate strings but I can't figure out add them in order as I go.

    Here is my code for the Node.

    package nodes;
     
    public class Node {
     
     
     
    	 private String data; // The data stored in this node private LinkedNode next; // 
     
    	 private Node next; //The next node in the data structure
     
     
     
    	 public Node () {
     
    	 this ( null, null ) ;
     
    	 }
     
     
     
    	 public Node ( String newData, Node newNext ) {
     
    	 data = newData;
     
    	 next = newNext;
     
    	 }
     
     
     
    	 public String getData () {
     
    	 return data;
     
    	 }
     
     
     
    	 public Node getNext () {
     
    	 return next;
     
    	 }
     
     
     
    	 public void setData ( String newData ) {
     
    	 data = newData;
     
    	 }
     
     
     
    	 public void setNext (Node newNext) {
     
    	 next = newNext;
     
    	 }
     
     
     
     
     
    	} // LinkedNode

    And the LinkedList

    package nodes;
     
     
    public class ListLL {
     
    	protected Node top; // The first node in the list
     
    	protected Node tail; // The last node in the list
     
    	public ListLL() {
     
    		top = null;
     
    		tail = null;
     
    	}
     
    	public void addInorder(String s) {
    		Node temp = new Node(s, null);
    		Node current = top;
     
    		if (top == null) {
    			top = temp;
    			return;
    		}
    		if (current.getData().equals(s)) {
    			return;
    		}
    			while (current.getNext() != null) {
     
    				current = current.getNext();
     
    				if (current.getData().equals(s)) {
    					return;
    				}
    			}
     
    			temp.setNext(current.getNext());
    			current.setNext(temp);
    		}
     
     
     
     
    	public void display() {
     
    		Node temp = top;
     
    		while (temp != null) {
     
    			System.out.println(temp.getData());
     
    			temp = temp.getNext();
     
    		}
     
    	}
     
    	public void displayUseTail() {
     
    		Node temp = top;
     
    		while (temp != tail.getNext()) {
     
    			System.out.println(temp.getData());
     
    			temp = temp.getNext();
     
    		}
     
    	}
     
    	public static void main(String[] args) {
     
    		ListLL list = new ListLL();
     
    		list.addInorder("banana");
     
    		list.addInorder("banana");
     
    		list.addInorder("orange");
     
    		list.addInorder("pear");
     
    		list.addInorder("apple");
     
    		list.addInorder("grape");
     
    		list.addInorder("grape");
     
    		list.addInorder("pear");
     
    		list.addInorder("grape");
     
    		list.addInorder("banana");
     
    		list.addInorder("berry");
     
    		list.addInorder("strawberry");
     
    		list.addInorder("melon");
     
    		list.display();
     
    		//list.displayUseTail();
     
    	}
     
     
    }


    Thank you for your help!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: addInOrder LinkedList

    To add them in order you will need to compare them and find the position in the list where they fit.
    The String class has a method to compare two Strings and tell you which one comes first in the natural ordering.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. LinkedList of String
    By oxnume in forum Collections and Generics
    Replies: 3
    Last Post: April 6th, 2012, 01:12 AM
  2. Weird problem with my linkedlist
    By clydefrog in forum Collections and Generics
    Replies: 19
    Last Post: February 22nd, 2012, 06:47 PM
  3. Can you rewrite this method without using LinkedList?
    By wholegrain in forum Java Theory & Questions
    Replies: 1
    Last Post: February 13th, 2012, 12:37 AM
  4. LinkedList Iterator
    By cpguy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2011, 09:51 PM
  5. LinkedList Objects
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 13th, 2010, 04:14 PM