双向链表排序:数据显示两次,而另一项缺失

问题描述 投票:0回答:2

我不确定如何以降序显示具有相同GPA的人。如果有两个相同的GPA,它将显示第一个人两次,而第二个人具有相同的GPA将不会显示。

这是主班

  public class N12 { 

public static void main(String[] args) 
{
    N1 myList = new N1();

myList.InsertAt(111,"Broke","Tim","5807404822","CS",3.8,1969,11,12,"New York");
myList.InsertAt(222,"Smith","Tom","5807404822","CS",3.6,1979,3,1,"Dallas");
myList.InsertAt(322,"Cook","John","5807404822","ENG",4.0,1993,4,4,"Boston");   
myList.InsertAt(433,"Keller","Frankin","5807404822","ECON",1.3,1932,6,22,"LA");
myList.InsertAt(213,"Smith","Boris","5807404822","ENG",3.5,1993,5,13,"Austin");

myList.GPADS();
    }
}

这是方法

public class N1 {
    private class Node
    {  
        Node next;

        int idNum;
        String lname;
        String fname;
        String tel;
        String major;
        double gpa;
        int year;
        int month;
        int date;
        String home;  

        public Node(int id,String ln,String fn,String t,String m,double g,int y, int mon, int d,String h)
        {   idNum=id;
            lname=ln;
            fname=fn;
            tel= t;
            major = m;
            gpa=g;
            year=y;
            month= mon;
            date= d;
            home=h;
            next = null;

        }
        public Node(int id,String ln,String fn,String t,String m,double g,int y, int mon, int d,String h,Node n)
        {   
            idNum=id;
            lname=ln;
            fname= fn;
            tel=t;
            major = m;
            gpa=g;
            year=y;
            month= mon;
            date= d;
            home=h;
            next = n;

        }
    }

    private Node first;
    private Node last;
    private int length;
    private Node currentPos;
    private double gpalist[]=new double [10000];
    private int idd[]=new int [10000];

    private int yearc[]=new int[10000];
    private int monthc[]=new int[10000];
    private int dayc[]=new int[10000];


    public N1()
    {
        length = 0;
        first = last = currentPos = null;
    }


    public void AddToFirst(int id,String ln,String fn,String t,String m,double g,int y, int mon, int d,String h)
    { 
        Node newNode = new Node(id,ln,fn,t,m,g,y,mon,d,h,first);

        first = newNode;
        length++;
        if(length==1) last = newNode;
    }

    public void AddToLast(int id,String ln,String fn,String t,String m,double g,int y, int mon, int d,String h)
    {
        Node newNode = new Node(id,ln,fn,t,m,g,y,mon,d,h);
        if(length==0)
        {
            first=last=newNode;
            length++;
            return;
        }
        last.next = newNode;
        last = newNode;
        length++;
    }


    public void InsertAt(int id,String ln,String fn,String t,String m,double g,int y, int mon, int d,String h)
    {



          gpalist[length]=g;
          idd[length]=id;
           yearc[length]=y;
            monthc[length]=mon;
             dayc[length]=d;
  //System.out.println(length);
        if(id<=0) 
        {
            AddToFirst(id,ln,fn,t,m,g,y,mon,d,h);
            return;
        }
        if(id>=length)
        {
            AddToLast(id,ln,fn,t,m,g,y,mon,d,h);
            return;
        }
        Node newNode = new Node(id,ln,fn,t,m,g,y,mon,d,h);
        Node current = first;
        for(int i=0;i<id-1;i++) current = current.next;
        newNode.next = current.next;
        current.next = newNode;
        length++;



    }




    public  void GPADS()
    { 
            String output="";

        int i;
                int j;
                double value;
        for(i=1;i<gpalist.length;i++)
        {
            value = gpalist[i];
            j = i-1;
            while(j>=0&&gpalist[j]<value) 
            {
                gpalist[j+1] = gpalist[j];
                j--;
            }
            gpalist[j+1] = value;
        }

  System.out.println("This is Descending by Gpa: ");
                  for(i=0;i<gpalist.length;i++)
        {
                    if(gpalist[i]!=0)
                    {
                   ByGPA(gpalist[i]); 

                    }

    }
        }


    public String ByGPA(double gpa)//second method
{      String output="";
        Node current = first;

while(current!=null)
       {

    if(current.gpa==gpa)
    {

            output = output+" "+current.idNum+" "+current.lname+" "+current.fname+" "+current.tel
                    +" "+current.major+" "+current.gpa+" "+current.year+" "+current.month
                            +" "+current.date+" "+current.home;  
          System.out.println(output);
         return null;
        }
    current = current.next;

    }
              return "";
}



}

输出(具有不同的GPA):

这是GPA降序的:

  • 322 Cook John 5807404822 ENG 4.0 1993 4 4 Boston
  • 111 Broke Tim 5807404822 CS 3.8 1969 11 12纽约
  • 222史密斯汤姆5807404822 CS 3.6 1979 3 1达拉斯
  • 213 Smith Boris 5807404822 ENG 3.5 1993 5 13 Austin
  • [433凯勒·弗兰金5807404822 ECON 1.3 1932 6 22 LA

如果我将Boris GPA更改为3.8,它将不会显示,而Broke将显示两次。

输出(具有相同的GPA):这是按GPA降序:

  • 322 Cook John 5807404822 ENG 4.0 1993 4 4
  • 波士顿111打破蒂姆5807404822 CS 3.8 1969 11 12纽约
  • 111 Broke Tim 5807404822 CS 3.8 1969 11 12纽约
  • 222史密斯汤姆5807404822 CS 3.6 1979 3 1达拉斯
  • [433凯勒·弗兰金5807404822 ECON 1.3 1932 6 22 LA
java sorting data-structures doubly-linked-list
2个回答
0
投票

您的代码有太多不必要的复杂性,我也无法捕获代码,因为它具有非常不可读的变量名,等等。我强烈建议您尽可能提高可读性。

我认为您问题的答案在ByGPA方法中。

当找到第一个gpa时,将打印它,但是由于return语句,它无法迭代到其他元素。您可以删除此return语句或重新考虑ByGPA方法的实现。


0
投票

我建议您使用arrayList <>,这样可以节省时间和代码。

Solutin:


    public void GPADS() {


        int i;
        int j;
        double value;
        for (i = 1; i < gpalist.length; i++) {
            value = gpalist[i];
            j = i - 1;
            while (j >= 0 && gpalist[j] < value) {
                gpalist[j + 1] = gpalist[j];
                j--;
            }
            gpalist[j + 1] = value;
        }

        double tmpGpa = 0;
        System.out.println("This is Descending by Gpa: ");
        for (i = 0; i < gpalist.length; i++) {
                                   // If gpalist[i] already been used.
            if (gpalist[i] != 0 && gpalist[i]  != tmpGpa) {
                // tmpGpa
                tmpGpa = ByGPA(gpalist[i]);

            }
        }
    }

    //public String ByGPA(double gpa)
    public double ByGPA(double gpa)//second method
    {
        String output = "";
        Node current = first;

        while (current != null) {

            if (current.gpa == gpa) {

                output =  " " + current.idNum + " " + current.lname + " " + current.fname + " " + current.tel
                        + " " + current.major + " " + current.gpa + " " + current.year + " " + current.month
                        + " " + current.date + " " + current.home;
                System.out.println(output);
                // return null;

            }
            current = current.next;
        }

        return gpa;
    }


© www.soinside.com 2019 - 2024. All rights reserved.