2. Stack
๐ ์ ์ : ํ์ชฝ ๋์์๋ง ๋ฐ์ดํฐ๋ฅผ ๋ฃ๊ณ ๋บ ์ ์๋ LIFO ํํ์ ์๋ฃ๊ตฌ์กฐ
1. ์ฐ์ฐ
- push : ์คํ์ ๋งจ ์๋จ์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์
- pop : ์คํ์ ๋งจ ์๋จ ๋ฐ์ดํฐ๋ฅผ ์ญ์
- top : ์คํ์ ๋งจ ์๋จ ๋ฐ์ดํฐ
- isEmpty : ์คํ์ด ๋น์ด ์๋ ์ง๋ฅผ ํ์ธ return boolean
- isFull : ์คํ์ด ๊ฐ๋ ์ฐจ ์๋ ์ง๋ฅผ ํ์ธ return boolean
2. ๊ตฌํ ๋ฐฉ์
โ Array
- ์ฅ์ : ๊ตฌํ์ด ์ฝ๊ณ , ์ํ๋ ๋ฐ์ดํฐ์ ์ ๊ทผ ์๋๊ฐ ๋น ๋ฅด๋ค
Public class Stack{
int top;
int maxSize;
Object[] stackArr;
public Stack(int size){
this.top = -1;
this.maxSize = size;
this.stackArr = new Object[maxSize];
}
public boolean isEmpty(){
return (top == -1);
}
public void push(Object item){
if(top >= maxSize - 1){
System.out.pritnln("Exception");
// ์คํ์ด ๊ฐ๋ ์ฐผ์ ๋์ ์์ธ์ฒ๋ฆฌ
}
stackArr[++top] = item;
}
public Object peek(){
if(isEmpty()) throw new ArrayIndexOutOfBoundsException(e);
return stackArr[top];
}
public Object pop(){
Object item = peek();
top--;
return item;
}
}
โก Linked List
- ์ฅ์ : ๋ฐ์ดํฐ์ ์ต๋ ๊ฐ์๊ฐ ํ์ ๋์ด ์์ง ์๊ณ , ๋ฐ์ดํฐ์ ์ฝ์ ์ญ์ ๊ฐ ์ฉ์ดํ๋ค
public class Node{
Object data;
Node nextNode;
public Node(Object data){
this.data = data;
this.nextNode = null;
}
public void linkNode(Node top){
this.nextNode = top;
}
public Object getData(){
return this.data;
}
public Node getNextNode(){
return this.nextNode;
}
}
public class LinkedList{
Node top;
public LinkedList(){
top = null;
}
public boolean isEmpty(){
return (top == null);
}
public void push(Object item){
Node newNode = new Node(item);
newNode.linkNode(top);
top = newNode;
}
public Object peek(){
return top.getData();
}
public Object pop(){
if(isEmpty()) throw new ArrayIndexOutOfBoundsException();
else{
Object item = peek();
top = top.getNextNode();
retun item;
}
}
}
3. ์คํ์ ์ฅ๋จ์
์ฅ์ | ๋จ์ | |
Array | - ๋๋ค ์์ธ์ค๊ฐ ๋น ๋ฆ - ๋น ๋ฅด๊ฒ ์ ๊ทผ ๊ฐ๋ฅ |
- ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ ๋นํจ์จ์ - ๋ฐฐ์ด ๋ด ๋ฐ์ดํฐ ์ด๋ ์ฌ๊ตฌ์ฑ ์ด๋ ค์ |
Linked List | - ๋์ ์ผ๋ก ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ - ๋ฐ์ดํฐ ์ฌ๊ตฌ์ฑ์ด ์ฉ์ด - ๋์ฉ๋ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ๊ฐ๋ฅ |
- ํน์ index์ ๋ฐ์ดํฐ๋ฅผ ๊ฒ์ํ๊ธฐ ์ด๋ ค์ - ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ถ๊ฐ์ ์ผ๋ก ์ฌ์ฉํด์ผ ํจ |
4. ์๊ฐ ๋ณต์ก๋
์ฐ์ฐ | Big-O |
insert | O(1) |
delete | O(1) |
search | O(n) |
5. ์ฌ์ฉ ์์
- ๊ฐ์ฅ ์ด์ ์ ์ผ์ด๋ ์ด๋ฒคํธ ๊ด๋ฆฌ์ ์ ์ฉ ex) undo, redo
- ์ฌ๊ท ํจ์๋ฅผ ํ์๋ก ํ๋ ๊ฒฝ์ฐ ์คํ์ผ๋ก ๊ตฌํ ๊ฐ๋ฅ
- ํ์ ํ๊ธฐ ๊ณ์ฐ์์์ ํ์ฉ ๊ฐ๋ฅ
'Computer Science > Data Structure' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Data Structure/์๋ฃ๊ตฌ์กฐ][CS] 4. Heap (0) | 2022.04.03 |
---|---|
[DataStructure/์๋ฃ๊ตฌ์กฐ][CS] 3. Queue (0) | 2021.12.28 |
[Data Structure/์๋ฃ๊ตฌ์กฐ][CS] 1. Array์ List (0) | 2021.12.21 |