-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
738 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package day03; | ||
//Comparator인터페이스를 상속받는 클래스를 inner class로 만들자 | ||
import java.util.*; | ||
public class Emp { | ||
private String name;//이름 | ||
private int height;//키 | ||
public Emp() { | ||
|
||
} | ||
public Emp(String n, int h) { | ||
name=n; | ||
height=h; | ||
} | ||
public String toString() { | ||
return name+" : "+height+"cm"; | ||
} | ||
public static final Comparator<Emp> HEIGHT_ORDER=new HeightOrderComparator(); | ||
//키 오름차순 정렬을 위한 Comparator클래스를 만들고 해당 객체를 생성하자 | ||
static class HeightOrderComparator implements Comparator<Emp>{ | ||
@Override | ||
public int compare(Emp e1, Emp e2) { | ||
/*if(e1.height==e2.height) return 0; | ||
else if(e1.height>e2.height) return 1; | ||
else return -1;*/ | ||
// return e1.height-e2.height; //오름차순 | ||
return e2.height-e1.height; //내림차순 | ||
} | ||
|
||
|
||
}//////////////// | ||
}////////////////////// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package day03; | ||
/*# 큐와 스택 | ||
1. 큐 - Queue : Fisrt In First Out 구조 | ||
ex) 택시 줄서기 : 가장 먼저 줄 선 사람이 먼저 택시를 탄다. | ||
- 큐에서 데이터를 한 개 집어넣는 동작을 인큐(enqueue), | ||
큐 안에 있는 데이터를 한 개 꺼내는 동작을 디큐(dequeue)라고 표현한다. | ||
2. 스택- Stack : Last In First Out | ||
ex) 접시 쌓기 : 접시를 쌓아 올리면, 꺼낼 때 가장 마지막 접시부터 꺼내게 됨 | ||
- 스택에 데이터 하나 넣는 동작을 push, | ||
스택에 있는 데이터를 하나 꺼내는 동작을 pop라고 표현한다. | ||
* */ | ||
import common.*; //예외 클래스 | ||
public class IntStack { | ||
|
||
private int capacity; //용량 | ||
private int ptr; //배열의 인덱스로 사용되는 포인터 push()할때는 증가, pop할 때는 감소 | ||
private int[] stk; //정수값을 저장할 배열 | ||
|
||
public IntStack(int capa) { | ||
this.capacity=capa; | ||
ptr=0; | ||
stk=new int[capacity]; | ||
} | ||
|
||
/**데이터 삽입하는 메서드, 스택(배열)이 가득 차서 추가할 수 없을 때 OverflowException을 발생시킨다*/ | ||
public int push(int val) throws OverflowException { | ||
if(ptr>=capacity) throw new OverflowException(); | ||
return stk[ptr++]=val;//push한 값을 반환하고 포인터값을 하나 증가시킨다. | ||
} | ||
|
||
/**스택에서 꼭대기에 있는 데이터를 꺼내 반환한다 포인터 값은 하나 감소시킨다*/ | ||
public int pop() throws EmptyException{ | ||
if(ptr<=0) throw new EmptyException(); | ||
return stk[--ptr]; | ||
|
||
} | ||
|
||
/**스택에서 꼭대기(top)에 있는 데이터를 반환*/ | ||
public int peek() { | ||
if(ptr<=0) throw new EmptyException(); | ||
return stk[ptr-1]; | ||
} | ||
|
||
/**스택에 저장되어 있는 데이터수를 반환하는 메서드*/ | ||
public int size() { | ||
return ptr; | ||
} | ||
/**스택의 모든 데이터를 비우는 메서드*/ | ||
public void clear() { | ||
ptr=0; | ||
} | ||
|
||
public int getCapacity() { | ||
return this.capacity; | ||
} | ||
|
||
//스택이 비었는지 여부를 판단 | ||
public boolean isEmpty() { | ||
return ptr<=0; | ||
} | ||
|
||
//스택이 가득찼는지 여부를 판단 | ||
public boolean isFull() { | ||
return ptr>=this.capacity; | ||
} | ||
//스택에 저장된 요소값들을 반복문 돌면서 출력하는 메서드 | ||
public void printStack() { | ||
for(int i=ptr-1; i>=0; i--) { | ||
System.out.println(stk[i]); | ||
} | ||
|
||
} | ||
//스택에서 특정 데이터를 검색해서 있으면 해당 데이터의 인덱스번호를 반환. 없으면 -1를 반환하는 메서드 | ||
public int indexOf(int val) { | ||
for(int i=ptr-1; i>=0; i--) { | ||
if(stk[i]==val) | ||
return i; | ||
}//for---------- | ||
return -1; | ||
} | ||
|
||
|
||
|
||
}/////////////////////////////////////// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package day03; | ||
|
||
public class IntStackTest { | ||
|
||
public static void main(String[] args) { | ||
IntStack sk=new IntStack(5); //초기용량 5 | ||
sk.push(10); | ||
sk.push(20); | ||
sk.push(30); | ||
sk.push(40); | ||
sk.push(50); | ||
// sk.push(60); | ||
System.out.println(sk.peek()); | ||
sk.pop(); | ||
System.out.println(sk.peek()); | ||
System.out.println(sk.size()+"<-요소 갯수"); | ||
sk.clear(); | ||
System.out.println(sk.size()+"<-clear()이후 요소 갯수"); | ||
System.out.println("sk is Empty?"+sk.isEmpty()); | ||
System.out.println("sk isFull?"+sk.isFull()); | ||
for(int i=1; i<6; i++) { | ||
sk.push(i*100); | ||
} | ||
System.out.println("sk isEmpty? "+sk.isEmpty()); | ||
System.out.println("sk isFull? "+ sk.isFull()); | ||
System.out.println(sk.peek()); | ||
System.out.println(sk.indexOf(200)); | ||
System.out.println(sk.indexOf(205)); | ||
sk.pop(); //500 | ||
sk.pop(); //400 | ||
sk.printStack(); | ||
System.out.println("sk.capacity(): "+sk.getCapacity()); | ||
System.out.println("sk.size(): "+sk.size()); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package day03; | ||
|
||
import java.util.*; | ||
import common.*; | ||
|
||
public class IntStackTest2 { | ||
|
||
public static void main(String[] args) { | ||
Scanner stdIn = new Scanner(System.in); | ||
IntStack s = new IntStack(64); // 최대 64 개를 푸시할 수 있는 스택 | ||
|
||
while (true) { | ||
System.out.println(); // 메뉴 구분을 위한 빈 행 추가 | ||
System.out.printf("현재 데이터 개수: %d / %d\n", s.size(), s.getCapacity()); | ||
System.out.print("(1)푸시 (2)팝 (3)피크 (4)덤프 (0)종료: "); | ||
|
||
int menu = stdIn.nextInt(); | ||
if (menu == 0) | ||
break; | ||
int x; | ||
switch (menu) { | ||
case 1: // 푸시 | ||
System.out.print("데이터: "); | ||
x = stdIn.nextInt(); | ||
try { | ||
s.push(x); | ||
} catch (OverflowException e) { | ||
System.out.println("스택이 가득 찼습니다."); | ||
} | ||
break; | ||
|
||
case 2: // 팝 | ||
try { | ||
x = s.pop(); | ||
System.out.println("팝한 데이터는 " + x + "입니다."); | ||
} catch (EmptyException e) { | ||
System.out.println("스택이 비어있습니다."); | ||
} | ||
break; | ||
|
||
case 3: // 피크 | ||
try { | ||
x = s.peek(); | ||
System.out.println("피크한 데이터는 " + x + "입니다."); | ||
} catch (EmptyException e) { | ||
System.out.println("스택이 비어있습니다."); | ||
} | ||
break; | ||
|
||
case 4: // 덤프 | ||
s.printStack(); | ||
break; | ||
} | ||
|
||
} // while------ | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package day03; | ||
//객체를 비교할 수 있도록 만들기 위해서는 Comparable인터페이스를 상속받아 compareTo()메서드를 재정의해야 한다 | ||
//또는 Comparator인터페이스를 상속받아 compare()메서드를 재정의해야 한다. | ||
/* Comparable과 Comparator의 차이는? | ||
[1] Comparable의 compareTo(T o) 메소드 | ||
[2] Comparator의 compare(T o1, T o2) 메소드 | ||
- Comparable은 "자기 자신과 매개변수 객체를 비교"하는 것이고, | ||
- Comparator는 "두 매개변수 객체를 비교"한다는 것이다. | ||
쉽게 말하자면, Comparable은 자기 자신과 파라미터로 들어오는 객체를 비교하는 것이고, | ||
Comparator는 자기 자신의 상태가 어떻던 상관없이 파라미터로 들어오는 두 객체를 | ||
비교하는 것이다. 즉, 본질적으로 비교한다는 것 자체는 같지만, 비교 대상이 다르다는 것이다. | ||
* */ | ||
|
||
public class Member implements Comparable<Member>{ | ||
|
||
private String name; | ||
private int age; | ||
private int height; | ||
public Member() { | ||
|
||
}//---------------------------------------------- | ||
public Member(String n, int a, int h) { | ||
this.name=n; | ||
this.age=a; | ||
this.height=h; | ||
}//--------------------------------------------- | ||
@Override | ||
public boolean equals(Object obj) { | ||
if(obj instanceof Member) { | ||
Member user=(Member)obj; | ||
if(this.name.equals(user.name)&&this.age==user.age&&this.height==user.height) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}//------------------------------------------ | ||
@Override | ||
public String toString() { | ||
return "Member[name="+name+", age="+age+", height="+height+"]"; | ||
}//----------------------------------------------- | ||
//this하고 매개변수로 들어온 obj하고비교를 함 | ||
//동일한 값을 가지면 0을 반환 | ||
//나이 오름차순 정렬을 한다면 | ||
/*오름차순: | ||
* 나 상대방 | ||
* 7 3 | ||
* 내림차순: | ||
* 7 3 | ||
* 10 13 | ||
* */ | ||
@Override | ||
public int compareTo(Member obj) { | ||
// if(this.age==obj.age) { | ||
// return 0; | ||
// }else if(this.age>obj.age) {//>양수반환: 오름차순, <양수반환: 내림차순 | ||
// //자기 나이가 상대방 보다 크면 양수를 반환=>오름차순 | ||
// return 1; | ||
// }else { | ||
// return -1; | ||
// } | ||
return this.age-obj.age;//오름차순 | ||
} | ||
|
||
}//////////////////////////////////// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package day03; | ||
|
||
import java.util.Arrays; | ||
|
||
public class MemberTest { | ||
|
||
public static void main(String[] args) { | ||
Member u1 = new Member("홍길동",22,177); | ||
Member u2 = new Member("홍길동",22,177); | ||
Member u3 = new Member("홍길동",22,178); | ||
|
||
//equals():원래는 주소값 비교 | ||
//=> 오버라이드함(이름,나이,키값들을 비교) | ||
System.out.println(u1.equals(u2)); | ||
System.out.println(u3.equals(u2)); | ||
System.out.println(u1==u2); | ||
System.out.println(u3==u2); | ||
|
||
System.out.println(u1); | ||
|
||
Member u4=new Member("김길동",27,177); | ||
Member u5=new Member("이길동",12,178); | ||
Member[] users = {u1,u4,u5};//22,27,12 | ||
Arrays.sort(users); | ||
System.out.println(Arrays.toString(users)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package day03; | ||
|
||
import common.EmptyException; | ||
import common.OverflowException; | ||
|
||
public class MyQueue { | ||
private Object[] que; //배열 => 큐 | ||
private int capacity; //큐의 크기 | ||
private int front; //맨 앞 요소 커서 | ||
private int end; //맨 끝 요소 커서 | ||
private int cnt; //현재 데이터 수 | ||
|
||
|
||
public MyQueue(int capacity) { | ||
this.capacity=capacity; | ||
cnt=0; | ||
front=0; | ||
end=0; | ||
que=new Object[this.capacity]; | ||
}//------------------------ | ||
//인큐할 때 데이터는 맨 뒤로 들어간다. 큐=>FIFO | ||
public Object enque(Object val) { | ||
if(cnt>=this.capacity) throw new OverflowException(); | ||
|
||
que[end++]=val; | ||
cnt++; | ||
//맨 뒤의 커서 end가 최대용량을 넘어설 경우 end값을 0으로 초기화 | ||
if(end>= this.capacity) { | ||
end=0; | ||
} | ||
return val; | ||
}//----------------------------------- | ||
//프런트 데이터를 바라봄 | ||
public Object peek() { | ||
if(cnt<=0) { | ||
throw new EmptyException(); | ||
} | ||
return que[front]; | ||
|
||
}//------------ | ||
//디큐할 때는 데이터가 맨 앞에서부터 나와야 한다. FIFO구조로 | ||
public Object deque() { | ||
if(cnt<=0) throw new EmptyException(); | ||
Object val = que[front++]; | ||
cnt--; | ||
if(front==this.capacity) { | ||
front=0; | ||
} | ||
return val; | ||
}//--------------- | ||
|
||
//큐를 비우는 메서드 | ||
public void clear() { | ||
cnt=0; | ||
front=0; | ||
end=0; | ||
}//---------------------- | ||
//현재 큐에 저장된 데이터 개수를 반환 | ||
public int size() { | ||
return cnt; | ||
} | ||
//큐가 비어있는지 여부 | ||
public boolean isEmpty() { | ||
return cnt<=0; | ||
} | ||
|
||
public boolean isFull() { | ||
return cnt>=this.capacity; | ||
}//------------------------ | ||
//큐 내용을 출력하는 메서드 구현 | ||
public void printQueue() { | ||
for(int i=0; i<cnt; i++) { | ||
System.out.println(que[(i+front)%capacity]); | ||
} | ||
|
||
} | ||
//큐에서 검색하는 메서드 구현 | ||
public int indexOf(Object val) { | ||
for(int i=0; i<cnt; i++) { | ||
int idx=(i+front)%capacity; | ||
if(que[idx].equals(val)) { | ||
return i; | ||
} | ||
}//for--------------------- | ||
return -1; | ||
} | ||
|
||
}//////////////////////////////////////// |
Oops, something went wrong.