我不知道如何按降序显示具有相同 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 New York
- 222 Smith Tom 5807404822 CS 3.6 1979 3 1 Dallas
- 213 Smith Boris 5807404822 ENG 3.5 1993 5 13 Austin
- 433 Keller Frankin 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
- Boston 111 Broke Tim 5807404822 CS 3.8 1969 11 12 New York
- 111 Broke Tim 5807404822 CS 3.8 1969 11 12 New York
- 222 Smith Tom 5807404822 CS 3.6 1979 3 1 Dallas
- 433 Keller Frankin 5807404822 ECON 1.3 1932 6 22 LA
最佳答案
您的代码有很多不必要的复杂性,我也无法捕获代码,因为它具有非常不可读的变量名称等。我强烈建议您尽可能地使其可读。
我认为您问题的答案在 ByGPA
方法中。
当它找到第一个 gpa 时,它会打印它,但由于 return
语句,它无法迭代到其他元素。
您可以删除此返回语句或重新考虑 ByGPA 方法的实现。
关于java - 双向链表排序: One of the data display twice and the other one is missing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59240577/