需要帮助在已排序的链接列表中添加节点

时间:2013-03-15 03:15:24

标签: java data-structures linked-list

我尝试按字母顺序按客户对象的名称按排序顺序添加新的Customer对象(应该是node.data,而不是节点)。但它不起作用。它以未排序的顺序打印出列表(与原始顺序不变)。

public void  add(Customer newNode, int dummy){
   if (head == null){ // The first node
      head = tail = this;
      head.setData(newNode);
      size=1;
      return;

   }else{
       CustomerList last = null;
       for(CustomerList node = head; 
               node != null && node.getData().toString().compareTo(newNode.name) < 0; 
                    node = node.next){
          last = node; 

       }
       CustomerList newList = new CustomerList(newNode);
       newList.setNext(last.next);
       last.next = newList;
   }

} // add

从txt文件输入Customer对象。应该按字母顺序(客户名称)再次打印出来。

10121,Airgo Fresh ods,OH,870023
10125,Bird Out fittered ,MI,870023
10134,Kit river ,IL,870023
10167,Mouin Gontaods,OR,870021
10178,Theiasu El senter,CA,870022

从txt文件读取数据并创建对象并添加到列表的代码:

public void byCustomerName()
 {
 records = null;
 System.gc();
 CustomerList.setHead(null);
 records = new CustomerList();
 try
  {
  String line;
  StringTokenizer st;
  String id, name, state, salesrep;
  BufferedReader infile = new BufferedReader(new FileReader("Customer.txt"));
  while ((line = infile.readLine()) != null)
      {
      st = new StringTokenizer(line, ",");
      id = st.nextToken(",");
      name = st.nextToken(",");
      state = st.nextToken(",");
      salesrep = st.nextToken(",");
      records.add(new Customer(id, name, state, salesrep), 99);
      }
  infile.close();
  } catch (IOException x) { System.err.println(x); } 
 } // byCustomerName

3 个答案:

答案 0 :(得分:1)

我认为你的代码中存在各种各样的问题。首先,如果您替换第一个或最后一个元素,我将永远不会看到您更新headtail。此外,我们不会检查last可能是null。如果不知道某些基础CustomerList项目如何运作,就很难说更多。

答案 1 :(得分:0)

我实际上在两个学期之前完成了同样的任务,但决定再次上课,因为我无法参加决赛。但由于两个学期没有编程,我现在非常生疏。我现在就要放弃,并使用我第一次上课时出现的旧解决方案。

解决方案:

public void add(Customer newNode, int dummy) {  
    CustomerList before = null;
    boolean inserted = false;
    if (head == null) {  //first node   
        head = tail = this;
        head.setData(newNode);
        return;
    } else {
        CustomerList curr = head;
        while(curr != null) {
            String currentName = curr.getData().getName();
            String newNodeName = newNode.name;
            if (currentName.compareToIgnoreCase(newNodeName) > 0) {

                CustomerList cList = new CustomerList(newNode);
                cList.setNext(curr);//curr is greater than clist, therefore cList's next element is curr
                if(before!=null)
                    before.setNext(cList);
                else {  //this tests the case when 
                    //the newNode goes at the BEGINNING of the list
                    head = cList;
                }
                curr = cList;
                inserted = true;
                return;
            }
            before = curr;
            curr = curr.next;               
        }
    }
    if(!inserted) {
        add(newNode);
    }

} // add

答案 2 :(得分:0)

正如Sudhanshu已经告诉过你可以在你的Collections.sort(-)对象上使用List方法,或者作为替代方法,只需使用TreeSet,以防你想要唯一的对象。最好使用java API的内置方法,因为这些方法不易出错且更可靠,同时您可以减少代码和时间来填充它。