[英]Sorting Elements Linked List Insertion Sort in Java
Hi I am getting Null Pointer Exception from this code in sort method can't figure out why. 嗨,我从排序方法中的此代码中获取Null Pointer Exception无法弄清原因。 I would appreciate any response.
我将不胜感激。 Its on line number 93 in Sort method under the while loop next= first.next .
它在while循环next = first.next下的Sort方法中的第93行 。 Thanks in Advance
提前致谢
public class LinkedList {
Student first,last,match,pointer;
Course start,end;
int linkCount = 1;
public LinkedList(){
first = null;
last = null;
}
public boolean isEmpty(){
return first==null;
}
public void deleteLink(int id){
Student firstnext = first;
if(id==first.id){
Student temp = first.next;
first = temp;
}
else if(last.id==id){
while(first!=null){
if(first.next.id == id){
first.next=null;
}
first = first.next;
}
first = firstnext;
}
else{
while(first.next!=null){
if(first.next.id == id){
first.next = first.next.next;
}
first = first.next;
}
first = firstnext;
}
}
public void traverseStudent(){
Student currentStudent = pointer;
while(currentStudent != null) {
currentStudent.printLink();
currentStudent.traverseCourse();
currentStudent = currentStudent.next;
}
System.out.println("");
}
public void insert(String fname, String lname, int id, String courseId, int credits,char grade){
if(isExist(id)){
match.insertCourse(courseId,credits,grade);
}
else{
Student link = new Student(fname, lname, id);
if(first==null){
link.next = null;
first = link;
last = link;
}
else{
last.next=link;
link.next=null;
last=link;
}
linkCount++;
link.insertCourse(courseId,credits,grade);
}
}
public void sort(){
Student current,next,firstLink = first,temp=null;
int flag = 0,flag2 =0;
pointer = null;
if(first!=null){
if(first.next==null){
current = first;
}
else{
while(linkCount>0){
current = first;
next = first.next;
while(next!=null){
if(current.lName.compareToIgnoreCase(next.lName)>0){
current = next;
if(flag2 == 0)
flag = 1;
}
next = next.next;
}
first = firstLink;
if(flag == 1){
deleteLink(current.id);
current.next = null;
pointer = current;
temp = pointer;
flag =0;
flag2 =1;
}
else if(flag2 ==1){
deleteLink(current.id);
current.next = null;
pointer.next = current;
pointer = pointer.next;
}
linkCount--;
}
}
pointer = temp;
}
}
public boolean isExist(int id){
Student currentStudent = first;
while(currentStudent != null) {
if(currentStudent.id==id){
match = currentStudent;
return true;
}
currentStudent = currentStudent.next;
}
return false;
}
}
This error occurs when you call a method with a null value. 当您调用具有空值的方法时,会发生此错误。 The method cannot run as the parameter given has no value.
该方法无法运行,因为给定的参数没有值。
As you did not provide a specific line of code that is causing the issue, I can only say to check all of the variables that you are using in sort()
and make sure that they are initialized before you call it. 由于您没有提供引起问题的特定代码行,因此我只能说要检查您在
sort()
中使用的所有变量,并确保在调用它们之前对其进行了初始化。
Selection/Insertion Sort should have two loops (Outer and Inner) => O(n^2). 选择/插入排序应具有两个循环(外部和内部)=> O(n ^ 2)。 When the pointer to the current value is greater than the value currently being evaluated - the nodes should be swapped.
当指向当前值的指针大于当前正在评估的值时,应交换节点。
Pseudocode: 伪代码:
Sort = function(first) {
var current = first;
while(current!= null) {
var innerCurrent = current.next;
while(innerCurrent != null) {
if(innerCurrent.Value < current.Value) {
Swap(current, innerCurrent);
}
innerCurrent = innerCurrent.next;
}
current = current.next;
}
}
Swap = function(current, innerCurrent) {
var temp;
temp.Value = current.Value;
temp.Next = current.Next;
temp.Prev = current.Prev;
current.Value = innerCurrent.Value;
current.Next = innerCurrent.Next;
current.Prev = innerCurrent.Prev;
innerCurrent.Value = temp.Value;
innerCurrent.Next = temp.Next;
innerCurrent.Prev = temp.Prev;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.