작업을 수행하기위해 입력을 받아 원하는 출력을 받는 것을 뜻한다.
정확하고 효율적으로 결과값을 얻기 위해서!
이 처럼 효율적인 결과값을 얻기 위해 위와 같은 알고리즘이 쓰인다~ 라고 생각하면 된다!
자료구조 포스트를 통해서는 자료구조란 무엇인가에 대한 정의를 했었고 그에 대한 종류인 배열과 컬렉션에 대한 설명을 했었다.
다시한번 정리하자면
자료구조란 데이터 값의 모임, 데이터간의 관계, 데이터에 적용할 수 있는 함수나 명령을 의미한다.
이 자료구조를 어떤 것을 사용하냐에 따라 효율적인 알고리즘 사용이 가능해진다.
해시 함수를 사용한다.
해시함수가 키를 입력받고 그 키에 알맞는 인덱스를 알려줘 키와 인덱스를 빠르게 매핑해주는 자료구조 형태이다.

각 노드들이 그물망처럼 간선으로 연결된 자료구조이다.

여기서 잠깐!
노드에 대해서 알아보자
자료구조들을 정의하기위해 사용된 개념(데이터타입)을 말하며,
각 노드는 데이터 와 다른 노드를 참조할 공간으로 정의 되어있다.

강사님께서 예시로 노드를이용해 stack 구현하는 것을 보여주셨다.
우선 stack이란 바구니에 담고 빼는 것으로 나중에 들어간것이 처음으로 나오는 자료구조이다.
아래 3가지 클래스를 만들 예정이다.
Node.java , StackNode.java, Main.java
public class Node<T> {
protected T data;
protected Node<T> next;
public Node() {
this.data = null;
this.next = null;
}
}
public class StackNode<T> {
private Node<T> top;
private boolean isEmpty() {
//스택이 비어있는지 확인
return this.top == null;
}
public void push(T data) {
//스택에 집어넣기
Node<T> newNode = new Node<>();
//새 노드를 만들어서
newNode.data = data;
newNode.next = this.top;
//스택에 값을 집어넣고 다음 주소값을 현재 top의 next로 지정
this.top = newNode;
//그리고 현재 top의 주소값을 새노드로 바꾼다.
}
public T pop() {
//스택에서 꺼내기
if (this.isEmpty()) {
return null;
//비어있으면 null return
}
T data = this.top.data;
//data변수에 현재 top의 데이터만을 넣어 초기화시킨다.
this.top = this.top.next;
//값이 있다면 현재 top의 값을 next의 값으로 바꾼다.
return data;
}
public T peek() {
//조회만하기(삭제X)
if (isEmpty()) {
return null;
}
return this.top.data;
//현재 top의 data만을 return
}
public void print() {
System.out.println("\n현재 스택의 내용을 top부터 출력합니다.");
if (this.isEmpty()) {
System.out.println("스택이 비어 있습니다.");
} else {
Node<T> currentNode; //현재노드 변수 선언
currentNode = this.top; //현재노드를 지금 스택의 top으로 초기화
while (currentNode != null) {
//반복문을 통해 현재 노드가 null값이 아니면 실행한다.
System.out.print("[ " + currentNode.data + " ] ");
currentNode = currentNode.next;
}
}
System.out.println("\n");
}
}
package exercise1;
public class Main {
public static void main(String[] args){
StackNode<Integer> stack = new StackNode<>();
stack.push(1);
stack.push(3);
stack.push(7);
stack.push(5);
stack.push(2);
stack.push(10);
stack.print(); // 10, 2, 5, 7, 3, 1 순으로 출력
System.out.println("현재 스택의 top을 출력합니다: " + stack.peek() + "\n"); //10출력
System.out.println("pop: "+stack.pop()); // 10
System.out.println("pop: "+stack.pop()); // 2
System.out.println("pop: "+stack.pop()); // 5
stack.print(); // 7, 3, 1 순으로 출력
System.out.println("pop: "+stack.pop()); // 7
System.out.println("pop: "+stack.pop());// 3
System.out.println("pop: "+stack.pop()); // 1
stack.print(); // 스택이 비어 있습니다.
}
}
이렇게 완성! 할 수 있다!