2017-11-15 2 views
0

이것은 매우 구체적인 질문이며 특히 도움이되는 어떤 것도 찾을 수없는 것 같습니다. 각 노드에 Student 객체를 저장하는 단일 링크 된 목록 (구현 된 연결된 목록이 아니라 내가 찾은 모든 것)이 있습니다. 각 변수에 액세스하는 데 문제가 있지만 각 Student 객체에는 변수가 있습니다.단일 링크리스트 (반복)의 노드에 저장된 객체의 특정 변수에 액세스하는 방법

개체 배열을 반복하는 방법과 비슷하다고 생각합니다. 그러나 for 루프를 사용합니다. 링크 된 목록을 탐색하려면 노드와 그 다음 노드의 & 데이터 값을 사용해야합니다.

나는 2 가지 아이디어를 결합하는 방법에 대해 잘 모릅니다. 나는 바보처럼 단순한 것 같지 않습니다. 아니면 완전히 다른 접근 방식을 취하고 있습니다.

import java.util.scanner; 

public class StudentNode extends Student 
{ 
private Student data; 
private StudentNode next; 


class SinglyLinkedList 
{ 

private StudentNode first; 

//constructor 
public SinglyLinkedList() 
{ 
    first=null; 
} 


public addToList(Student newData) 
{ 
    StudentNode newNode= new StudentNode(); 
    newNode.data=newData; 
    newNode.next=first; //refs to the element first is currently pointing to 
    first=newNode;//first now refs to added element 
}   

public courseMark(Student data) 
{ 
    double cm=courseMark(StudentNode.data); 
    return "Student number : "+stuNum +"Course Mark: "+cm; 

} 



public double classAverage(Student data) 
{ 
//traverses linked list, not enirely sure about how to access the course mark 
    double classAvg=0; 
    double sum = 0; 
    int i=0; 
    StudentNode current = first; 
    StudentNode previous = null; 
    while (current != null) 
    { 
     i++; 
     StudentNode current= Student.courseMark(); 
     sum += current.data;//not sure bout course mark access 
     previous = current; 
     current = current.next; 
    } 
    return classAvg=sum/i; 
    } 

다음은 데이터 구성 요소가 사용하는 학생 클래스입니다. 답변이 필요한지 확실하지 않습니다.

public class Student 
{ 
private String name; 
private String stuNum; 
private int firstTest; 
private int secondTest; 
private int thirdTest; 

public Student(String n,String sN,int fT,int sT,int tT) 
{ 
    name=n; 
    stuName=sN; 
    firstTest=fT; 
    secondTest=sT; 
    thirsTest=tT; 
} 

//setters 
public void setName(String n) 
{ 
    name=n; 
}  

public void setStuNum(String sN) 
{ 
    stuNum=sN; 
} 

public void setFirstTest(int fT) 
{ 
    firstTest=fT; 
} 

public void setSecondTest(int sT) 
{ 
    secondTest=sT; 
} 

public void setThirdTest(int tT) 
{ 
    thirdTest=tT; 
} 

//getters 

public String getName() 
{ 
    return name; 
} 

public String getStuNum() 
{ 
    return stuNum; 
} 

public int getFirstTest() 
{ 
    return firstTest; 
} 

public int getSecondTest() 
{ 
    return secondTest; 
} 

public int getThirdTest() 
{ 
    return thirdTest; 
} 

//course mark computer 

public double courseMark() 
{ 
    double crseMark=(firstTest*0.25)+(secondTest*0.25)+(thirdTest*0.50); 
    return crseMark; 
} 

}

+0

당신은'되는 java.util.LinkedList '클래스를 개혁하고 있습니다. 링크 된 목록을 직접 작성해야합니까? 그렇지 않은 경우 라이브러리 클래스를 사용하십시오. Linked List 구현을 작성하는 것이 코스 작업의 일부라면 교과서와 강의 노트를 공부해야합니다. 'Iterable'과'Iterator'를 공부 했습니까? 이것들은리스트 작업에 중요한 개념입니다. –

답변

1

당신은 courseMark을 얻을 수있는 학생의 데이터 노드에서 통과해야합니다.

while (current != null) { ... double courseMark = current.data.courseMark(); ... }

+0

고마워요. current.data.courseMark를 할 생각이었습니다. 그러나 이상하게 보였고 허용 될지 확실하지 않았습니다. – DinoMeme

관련 문제