http://www.hackerschool.org/HS_Boards/zboard.php?id=QNA_programming&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 : 3933 Date : 2013/11/09 10:47
|