|
http://www.hackerschool.org/HS_Boards/zboard.php?AllArticle=true&no=6404 [복사]
import java.util.Scanner;
public class Ex6_2{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
LinkedList1 L = new LinkedList1();
int num;
int i = 0;
String str;
while(i != 6){
System.out.println("1)노드 삽입");
System.out.println("2)노드 삭제");
System.out.println("3)리스트 출력");
System.out.println("4)리스트 역순");
System.out.println("5)리스트 정렬");
System.out.println("6)종료");
System.out.println("메뉴를 고르시오 :");
num = sc.nextInt();
switch(num){
case 1:
str = sc.next();
L.insertLastNode(str);
break;
case 2:
str = sc.next();
L.deleteNode(str);
break;
case 3:
L.printList();
break;
case 4:
L.reverseList();
L.printList();
break;
case 5:
L.lineup();
L.printList();
break;
case 6:
System.out.println("end");
i = 6;
break;
}
System.out.println();
}
}
}
public class LinkedList1 {
private ListNode1 head;
private int n = 0;
public LinkedList1(){
head = null;
}
public void insertLastNode(String data){
ListNode1 newNode = new ListNode1(data);
if(head == null){
this.head = newNode;
}
else{
ListNode1 temp = head;
while (temp.link != null) temp = temp.link;
temp.link = newNode;
}
n = n + 1;
}
public void lineup(){
ListNode1 temp = this.head;
ListNode1 pre = temp.link;
ListNode1 current = null;
ListNode1 x = null;
for(int i = 1 ; i < n ; i++){
for(int j = 5 ; j > i ; j--){
if(temp.getData().compareTo(pre.getData()) > 0){
if(i == 1){
current = pre.link;
pre.link = temp;
temp.link = current;
this.head = pre;
}
else{
current = pre.link;
pre.link = temp;
temp.link = current;
x.link = pre;
}
x = pre;
pre = temp.link;
}
if(temp.getData().compareTo(pre.getData()) <= 0){
temp = pre;
pre = pre.link;
}
}
}
}
public void deleteNode(String data){
ListNode1 temp = this.head;
ListNode1 pre = temp;
while(temp != null){
if(data.equals(temp.getData())){
if(head == temp){
this.head = temp.link;
temp.link = null;
}
else{
pre.link = temp.link;
temp.link = null;
}
break;
}
else{
pre = temp;
temp = temp.link;
}
}
n = n - 1;
}
}
public class ListNode1 {
private String data;
public ListNode1 link;
public ListNode1(){
this.data = null;
this.link = null;
}
public ListNode1(String data){
this.data = data;
this.link = null;
}
public ListNode1(String data, ListNode1 link){
this.data = data;
this.link = link;
}
public String getData(){
return this.data;
}
}
리스트의 정렬메소드를 짜는데 너무 힘든거 같아서 질문 드려 봄니다.
이 코드에서 lineup()메소드 부분에 무엇이 잘못된지 알수 있을까요?
다른 거는 다되는데 이 메소드만 계속에러가 뜨네요
몇게 메소드는 상관이 없고 너무긴거 같아서 지웠습니다.
|
Hit : 5280 Date : 2013/11/09 10:47
|