728x90
반응형
정의
Stack이란 나중에 넣은 데이터가 먼저 나오는 후입선출방식의 자료구조입니다. top이라는 한 쪽 끝에서 데이터의 추가와 삭제가 이루어집니다.
동작
1) 삽입(Push)
2) 삭제(Pop)
3) 읽기 - Peek
배열을 통한 구현
package ex1.stack.array;
public class ArrayStack {
int[] stack;
int size;
int top;
public ArrayStack(int size) {
this.size=size;this.stack=new int[size];this.top=0;
}
// push
public void Push(int num) {
if(top==size) {System.out.println("Overflow");return;}
else {stack[top++]=num;}
}
// pop
public void Pop() {
if(top==0){System.out.println("Underflow");return; }
else {stack[top-1]=0;top--;}
}
public void Peek() {
System.out.println(stack[top-1]);
}
public void Search(int num) {
for(int i=0;i<top;i++) {
if(stack[i]==num) {System.out.println("index : "+i);return;}
}
}
public void remove() {
this.stack = new int[size];
this.top=0;
}
}
728x90
반응형
'👩🏻💻Technical things > Algorithm' 카테고리의 다른 글
02. 기본 자료구조 (0) | 2020.08.17 |
---|---|
01. 기본 알고리즘 (1) | 2020.08.16 |
선택정렬 / 삽입정렬 / 버블정렬 (4) | 2020.08.12 |