Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Inserting Element In Linkedlist In Alphabetical Order.

linked list element

  • Please log in to reply
4 replies to this topic

#1 An Alien

An Alien

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 323 posts
  • Programming Language:Java
  • Learning:C, Java, PHP, Python, JavaScript, Lisp, Transact-SQL, Others

Posted 30 June 2012 - 03:13 PM

Hi guys, I need to insert a customer object in a linkedList, but it has to be added in the correct spot so the list is in alphabetical order by customer name. Not sure how to do it. Wondering if you guys could help me out with the logic.

LinkedListClass:
// Linked list used to keep track of Customer objects
public class CustomerList
{
static CustomerList head = null;
static CustomerList tail = null;
CustomerList  next;
Customer  data;
public CustomerList() { next = null; }
public CustomerList(Customer <img src='http://forum.codecall.net/public/style_emoticons/<#EMO_DIR#>/cool.png' class='bbc_emoticon' alt='B)' /> { data = b; next = null; }
public Customer getData() { return data; }
public CustomerList getNext() { return next; }
public void setData(Customer d) { data = d; }
public void setNext(CustomerList n) { next = n; }
public static CustomerList getHead() { return head; }
public static void setHead(CustomerList h) { head = h; }
// HELP ME OUT WITH THIS METHOD PLEASE<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
public void  add(Customer newNode, int dummy)
   {
    if (head == null) { //first node
	   head = tail = this;
	   head.setData(newNode);
	   System.out.println(head.getData().getName());
	   return;
    }
    else{
	 CustomerList temp = new CustomerList();
	 CustomerList temp2 = new CustomerList();
	 CustomerList before = null;
	 CustomerList newNod = new CustomerList();
	 for(CustomerList curr = head; curr != null; curr = curr.getNext()){
	  if(curr.getData().getName().compareToIgnoreCase(newNode.getName()) > 0){ //if newNode comes before curr
	   if(before != null){
	    newNod.setData(newNode);
	    before.setNext(newNod);
	    System.out.println(newNode.getName());
	   // break;
	   }
	   else{
	    newNod.setData(newNode);
	    newNod.setNext(head);
	    head = newNod;
	    System.out.println(newNode.getName());
	   // break;
	   }
	  }
	 
	  if(curr.getData().getName().compareToIgnoreCase(newNode.getName()) < 0){
	  
	  }
	  before = new CustomerList();
	  before = curr; //before curr.next() in for loop.
	 }
    }
   } // add
// Append the new node to the end of list
public void  add(Customer newNode)
   {
   if (head == null) // The first node
	  {
	  head = tail = this;
	  head.setData(newNode);
	  return;
	  }
   CustomerList temp = new CustomerList(newNode);
   tail.setNext(temp);
   tail = temp;
   } // add
public void show(CustomerList h)  // display all nodes
   {
   if (h == null)  return;
   System.out.println(h.getData());
   h = h.getNext();
   if (h != null)	   show(h);
   } // show
} // CustomerList

  • 0

#2 An Alien

An Alien

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 323 posts
  • Programming Language:Java
  • Learning:C, Java, PHP, Python, JavaScript, Lisp, Transact-SQL, Others

Posted 01 July 2012 - 11:35 AM

Bump, please help me out with the add method. I can't get the logic right. My brain can't handle it. What should I do in situations were the logic is tough? I tried drawing it out, but I still couldn't do it. Does this mean that I don't think logical enough to be a programmer?
  • 0

#3 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 01 July 2012 - 06:37 PM

Well, to think about what you need to do to insert a node in a linked list, you can image about this problem: A railway train is a connected series of vehicles (not same as linked list???). As example, there are 10 vehicles in a train including engine.So what you need to do if you want to put another vehicle in that train in between the vehicle 4 and 5?

So what you need to do is to break the link in between 4 & 5 vehicle. Then make the link between 4 and the new vehicle, and make another link in between the new vehicle and 5. I don't even think you made the right things for creating a linked list. You should create a class like Customer which will have the the customer information only. Then another class like LinkedList (a generic class) that can be used as linked list for any type of data.
  • 1

#4 An Alien

An Alien

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 323 posts
  • Programming Language:Java
  • Learning:C, Java, PHP, Python, JavaScript, Lisp, Transact-SQL, Others

Posted 01 July 2012 - 07:06 PM

Thanks, I do have a Customer cl**. I just didn't post it. The getName() is a method for the customer objects. And I didn't think of the train analogy. That could have actually really been helpful.

Thanks to Lethal again who personally helped me solve this problem. If anyone would like the solution, please PM me.
  • 0

#5 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 01 July 2012 - 07:58 PM

Here is the solution (in terms of code) of your add method.
public void  add(Customer newNode)

{
if (head == null) { //first node
  head = tail = this;
  head.setData(newNode);
  System.out.println(head.getData().getName());
  return;
}

CustomerList newNod = new CustomerList();
    newNod.setData(newNode);


if (head.getData().getName().compareToIgnoreCase(newNode.getName()) >= 0) {
// new item is less than head.
CustomerList preHead = head;
setHead(newNod);
head.setNext(preHead);
return;
}

if (head.getNext() == null) {
// inserting second node (if the above if is not executed).
head.setNext(newNod);
tail = newNod;
return;
}

     CustomerList before = head;
     CustomerList current = head.getNext();
     for(; current != null; current = current.getNext()){
      if(current.getData().getName().compareToIgnoreCase(newNode.getName()) >= 0){ 
      // newNode should be inserted here.
      before.setNext(newNod);
      newNod.setNext(current);
      break;
      }     
      before = current;
     }

     if (current == null) {
      // new item is greater than after tail.
      before.setNext(newNod);
      tail = newNod;
     }    
}
Note that, I have not tested this code.

Now you have to tell us what you have learned from this code.
  • 0





Also tagged with one or more of these keywords: linked list, element

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download