728x90
[문제]
15번 문제를 활용하여 아래의 출력 형태로 만들어보자
(연결 리스트를 이용한 스택 구조를 만들어보자!)
pStart = pStart.Next;만 해줬다
pStart.Next = null; 과 같은 처리를 해주지 않은 이유는
자바는 GC(Garbage Collection에 의해) 사용되지 않는 객체가 생기면 자동으로 메모리를 회수해가기 때문이다.
[코드]
class Shape {
// Shape Next;
void paint() {
draw();
}
void draw() {
System.out.print("Shape을 만듭니다.");
}
}
class Circle extends Shape {
void draw() {
System.out.print("Circle");
}
}
class Rectangle extends Shape {
void draw() {
System.out.print("Rectangle");
}
}
class Triangle extends Shape {
void draw() {
System.out.print("Triangle");
}
}
class Node {
Shape data;
Node Next;
public Node(Shape data, Node Next) {
this.data = data;
this.Next = Next;
}
}
class ShapeExample {
void run() {
Scanner sc = new Scanner(System.in);
Node pStart = null;
Node pLast = null;
while (true) {
System.out.print("1.push 2.pop 3.print 4.all del 5.exit : ");
int num = sc.nextInt();
if (num == 1) {
System.out.print("1.Circle 2.Triangle 3.Rectangle 4.Print 5.Exit : ");
int select = sc.nextInt();
if (select == 1) {
Shape newShape = new Circle();
Node newNode = new Node(newShape, null);
if (pStart == null) {
pStart = newNode;
pLast = pStart;
} else {
newNode.Next = pStart;
pStart = newNode;
}
pStart.data.paint();
System.out.println("이 추가됨.");
System.out.println();
}
else if (select == 2) {
Shape newShape = new Triangle();
Node newNode = new Node(newShape, null);
if (pStart == null) {
pStart = newNode;
pLast = pStart;
} else {
newNode.Next = pStart;
pStart = newNode;
}
pStart.data.paint();
System.out.println("이 추가됨.");
System.out.println();
}
else if (select == 3) {
Shape newShape = new Rectangle();
Node newNode = new Node(newShape, null);
if (pStart == null) {
pStart = newNode;
pLast = pStart;
} else {
newNode.Next = pStart;
pStart = newNode;
}
pStart.data.paint();
System.out.println("이 추가됨.");
System.out.println();
}
}
else if (num == 2) {
if (pStart == null) {
System.out.println("stack이 비었습니다.");
System.out.println();
continue;
}
pStart.data.paint();
System.out.println("이 삭제됨");
System.out.println();
pStart = pStart.Next;
}
else if (num == 3) {
if (pStart == null) {
System.out.println("stack이 비었습니다.");
System.out.println();
continue;
}
// int n = 0;
for (Node cur = pStart; cur != null; cur = cur.Next) {
// n++;
cur.data.paint();
if (cur.Next == null)
continue;
System.out.print("->");
}
System.out.println();
// System.out.println(n + "개의 도형이 생성되었습니다.");
System.out.println();
}
else if (num == 4) {
if (pStart == null) {
System.out.println("stack이 비었습니다.");
System.out.println();
continue;
}
for (Node cur = pStart; cur != null; cur = cur.Next) {
cur.data.paint();
System.out.println("이 삭제됨");
pStart = pStart.Next;
}
System.out.println("모두 삭제되었습니다.");
System.out.println();
}
else if (num == 5) {
System.out.println("종료합니다.");
break;
}
}
}
public static void main(String[] args) {
ShapeExample shape = new ShapeExample();
shape.run();
}
}
all del 기능에 cur는 객체를 가리키는 용도고
pStart를 바꿔주는 건 따로 선언했는데 이를 합치는 방법이 있을까?
728x90
'Java' 카테고리의 다른 글
| [Java] (추상 클래스) (16-2) (0) | 2023.08.30 |
|---|---|
| [Java] (추상 클래스) (16-1) (0) | 2023.08.30 |
| [Java] 클래스 (상속, 오버라이딩, 다형성) (15) (0) | 2023.08.27 |
| [Java] 단축키 (0) | 2023.08.24 |
| [Java] 상속과 다형성 문제 (0) | 2023.08.18 |