按字母顺序排列的多个链接列表

时间:2014-04-05 15:10:03

标签: java linked-list

我想要多个索引链表的帮助。我试图按字母顺序排列一组名字,我甚至不知道如何开始。我在谷歌搜索时尝试了一些代码,但案例是我知道代码是如何工作的,但我不知道在哪里放置代码。所以,如果你能帮助我,我会非常感激。提前谢谢....

这是我的节点类......

    public class node {
    String data;long id;
    node next;



    public node(String data){
        this.data = data;
      this.id = getId(data);
    }



    public long getId(String line)
       {
          int i;
          long id=0;
          String s= null;
      char a;
      int j=2;
      for(i = 0;i <3;i++)
      {

         a = line.charAt(i);
         id += Character.getNumericValue(a) * Math.pow(26, j);
         j--;
      }
      return id;
   }

   public long getId()
   {
      return id;
   } 

   public String getData()
   {
      return data;
   }

   public void setData(String data)
   {
      this.data = data;
   }

   public node getNext()
   {
      return next;
   }

   public void setNext(node next)
   {
      this.next = next;
   }
}

这是我的链表类........

public class LinkedList {
    private node front;

    public void init(){
        front = null;
    }
    public node makeNode(String data){
        node newNode = new node(data);      
        return newNode;
    }

    public node findTail(node head){
        node current = front;
        while(current.next != null){
            current = current.next;
        }
        return current;
    }

   public node findSpot(String num) {
       node current = front;
       boolean searching = true, found = false;

       while((searching)&&(!found)) {
           if(current == null) {
               searching = false;
           }
           else if(current.data == num) {
               found = true;
           }
           else {
               current = current.next;
           }
       }
       return current;
   }

   public void deleteAfter(node spot) 
   { 
       node nextNode; 

       nextNode = spot.next; 
       spot.next = nextNode.next; 
   }

    public void addNodeAtEndOfList(String data){
        if(front == null){
            front = makeNode(data);
        }else{
            node tail;
            tail = findTail(front);
            tail.next = makeNode(data);
        }
    }

   public void addAfter(node spot, String data)
   {
    node newNode;

    newNode = makeNode(data);

    newNode.next = spot.next;
    spot.next = newNode;
   }

   public void addBefore(node spot, String data)
   {
      node newNode;
      if(front == null || spot.data == front.data){
            front = makeNode(data);
         front.next= spot;
        }
      else{
         newNode = makeNode(data);
         node tail = front;
         if(tail.next == spot)
         {
            tail.next = newNode;
            newNode.next = spot;
         }
         else{
            while((tail.next).next!= spot)
               tail= tail.next;

            tail.next = newNode;
            newNode.next = spot;
         }
      }
   }


   public void makeIndexList()
   {
    node spot;
    int currAlpha;

    indexNode AlphaIndex[26];


    /* Fill in the index array. */
    for (int j=0 ;j<26; j++) {
        currAlpha = j;
        AlphaIndex[j].weight = currAlpha;
        spot = findSpot(currAlpha);

        if (spot is equal to the front of the list) and
           (spot.weight > (currWeight + 10)) {
            weightIndex[j].firstOne = null;
        }
        else {
            weightIndex[j].firstOne = spot;
        }
    }
   }    


    public void showList(){
        node current = front;
      int i = 0;
        while(current != null){
            System.out.println("a["+i+"] = " + current.data);
            current = current.next;
         i++;
        }
    }
}

public class indexNode {
    int alpha;
    node firstOne;
}

这是我的文件类..

import java.util.Scanner;
import java.io.*;

public class fileIn {
    String fname;
   LinkedList mylist = new LinkedList();
   node front;
   public fileIn() {
        //System.out.println("Constructor");
        getFileName();
        readFileContents();
      System.out.print(mylist.showList());
    }

    public void readFileContents()
    {
        boolean looping;
        DataInputStream in;
        String line;
        int j, len;
        char ch;

        /* Read input from file and process. */
        try {
            in = new DataInputStream(new FileInputStream(fname));
            looping = true;
            while(looping) {
                /* Get a line of input from the file. */
                if (null == (line = in.readLine())) {
                    looping = false;
                    /* Close and free up system resource. */
                    in.close();
                }
                else {
                    //System.out.println("line = "+ line);
                  mylist.addNodeAtEndOfList(line);
                    j = 0;
                    len = line.length();

                  /*for(j=0;j<len;j++){
                       //System.out.println("line["+j+"] = "+line.charAt(j));
                    }*/
                }
            } /* End while. */
             mylist.showList();

        } /* End try. */

        catch(IOException e) {
            System.out.println("Error " + e);
        } /* End catch. */
    }

    public void getFileName()
    {
        Scanner in = new Scanner(System.in);

        System.out.println("Enter file name please.");
        fname = in.nextLine();
        System.out.println("You entered "+fname);

    }
}

1 个答案:

答案 0 :(得分:0)

完成这项任务的方法看起来像这样。

package org.jproggy;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class Sorter {

  public static void main(String[] args) throws Exception {
    BufferedReader in = new BufferedReader(
        new InputStreamReader(new FileInputStream(args[0])));
    String line;
    List<String> lines = new LinkedList<>();
    while ((line = in.readLine()) != null) {
      lines.add(line);
    }
    in.close();

    Collections.sort(lines, String.CASE_INSENSITIVE_ORDER);

    for (String val: lines) {
      System.out.println(val);
    }
  }
}

编译完课程后,您可以使用

调用它
java org.jproggy.Sorter <path-to-file>

如果您希望了解构建数据结构或排序算法,则需要更具体地了解所需内容。但是JDK的源代码是可用的,你可以在那里学到很多东西。 顺便说一句。在Java 8中,它甚至可能如下所示:

    Path path = FileSystems.getDefault().getPath(args[0]);
    BufferedReader data = Files.newBufferedReader(path);
    data.lines().sorted(String.CASE_INSENSITIVE_ORDER).forEach(System.out::println);

尼斯。

相关问题