|
Node(){count=0;}//constructor
void display() {System.out.printf("%3d%2d\n",key,count);}
}
class LinkedList
{
Node head=null;
void display()
{Node p=head;
System.out.println("Key Count\n--- --");
while (p!=null)
{
p.display();
p=p.next;
}
}// end method display
void add(int number)
{
Node newnode,p;
if (head==null) // nothing in the list yet
{head=new Node();
head.key=number;
head.count=1;
return;
}
// see if this number is already in the list,
//if so just increment its count
//Put your code here , include a return if found(填补的代码全放在这些注释之间,要求检察输入数是否与之前所有数重复,如果重复,只增加count数,不重复打印,如果不重复,把不重复的新数移到list的最上放。求教各位高手!!!谢谢!!)
//it was not found,so add it to the beginning of the list
newnode= new Node();
newnode.key=number;
newnode.count=1;
newnode.next=head;
head=newnode;
}// end method add
}//end class LinkedList
public class lab11 {
public static void main(String args[])
{
LinkedList data= new LinkedList();
Scanner input;
int next;
input= new Scanner(System.in);
while (input.hasNextInt())
{
next=input.nextInt();
data.add(next);
}
data.display();
}
}//end class lab11
在是我在爱问知识人上得到的答案,请楼主指教。我也看不懂,帮我解释。 |
|