Comments (4)
스택
- Last In First Out
가장 최근에 입력된 데이터를 top 이라고 하며 스택은 top 에서만 삽입, 삭제, 읽기 동작이 발생 가능 - push, pop, peek
배열로 구현
- 배열에 실제 데이터를 저장하기 때문에 배열의 크기를 미리 정해 주어야 하기 때문에 배열이 꽉 차있는지 확인 하는 작업이 필요
public class ArrayStack {
private int top;
private int maxSize;
private Object[] stackArray;
// 배열 스택 생성, 스택의 최대 크기로 생성
public ArrayStack(int maxSize){
this.maxSize = maxSize;
this.stackArray = new Object[maxSize];
this.top = -1;
}
// 스택이 비어있는지 체크
public boolean empty(){
return (top == -1);
}
// 스택이 꽉찼는지 체크
public boolean full(){
return (top == maxSize-1);
}
// 스택에 item 입력
public void push(Object item){
if(full()) throw new ArrayIndexOutOfBoundsException((top+1)+">=" + maxSize);
stackArray[++top] = item;
}
// 스택의 가장 위의 데이터 반환
public Object peek(){
if(empty()) throw new ArrayIndexOutOfBoundsException(top);
return stackArray[top];
}
// 스택의 가장 위의 데이터 제거
public Object pop(){
Object item = peek();
top--;
return item;
}
}
링크드 리스트를 이용하여 구현
- 배열을 이용한 스택의 구현의 가장 큰 단점은 처음 생성한 크기를 바꿀 수 없다는 것
- 이를 해결하기 위해 링크드 리스트로 스택 구현 ( 스택이 다 찼는지 여부 체크할 필요 없음)
class ListStack {
private Node top;
// 노드 class 단순연결리스트와 같다.
private class Node{
private Object data;
private Node nextNode;
Node(Object data){
this.data = data;
this.nextNode = null;
}
}
// 생성자, stack이 비어있으므로 top은 null이다.
public ListStack(){
this.top = null;
}
// 스택이 비어있는지 확인
public boolean empty(){
return (top == null);
}
// item 을 스택의 top에 넣는다.
public void push(Object item){
Node newNode = new Node(item);
newNode.nextNode = top;
top = newNode;
}
// top 노드의 데이터를 반환한다.
public Object peek(){
if(empty()) throw new ArrayIndexOutOfBoundsException();
return top.data;
}
// top 노드를 스택에서 제거한다.
public Object pop(){
Object item = peek();
top = top.nextNode;
return item;
}
}
from algorithm.
스택 계산기 (전위/ 중위/ 후위)
http://yahma.tistory.com/5
http://yahma.tistory.com/7?category=640326
from algorithm.
링크드 리스트 사이클 판별 알고리즘
토끼와 거북이 알고리즘
-
두개의 포인터를 사용하는데 한개의 포인터는 한칸씩 이동(거북이) / 다른 한개의 포인터는
두칸씩 이동(토끼) 해서 진행하면 사이클이 존재한다면 토끼가 반드시 거북이를 잡는다는 알고리즘 -
문제 leetcode 141
public class Solution {
public boolean hasCycle(ListNode head) {
if(head == null) return false;
boolean result = false;
ListNode hare = head; // 토끼
ListNode tortoise = head; // 거북이
while(true)
{
if(hare == null || hare.next == null) break; // 사이클이 없다면 토끼먼저 도착하므로 토끼만 확인
tortoise = tortoise.next;
hare = hare.next.next;
if(hare == tortoise) {
result = true;
break;
}
}
return result;
}
}
두 링크드 리스트 만나는 지점 찾기
- leetcode 160 Intersection of Two Linked Lists
- 두 링크드 리스트를 한칸씩 전진시키고 길이가 동일하고 둘다 널값으로 끝나면 만나는 지점 없는 것!!
- 짧은 길이의 링크드 리스트가 먼저 널값으로 도착하면 다른 링크드 리스트 시작점으로 잡고 다시 출발
- 이렇게 했을때 만나면 교차점! 이렇게 출발점을 변경하면서 이동했는데도 둘다 널값으로 끝나면 교차점 없음!!
from algorithm.
더블 링크드 리스트 reverse
해커랭크 Reverse a doubly linked list
from algorithm.
Related Issues (20)
- 트리 / Lazy Propagation / Persistent Segment Tree / sqrt Decomposition HOT 5
- 크루스칼 알고리즘 - 최소 신장 트리(Minimum Spanning Tree / MST ) // Using 서로소 집합 ( Disjoint Set, Union-Find) HOT 2
- 운영체제 HOT 1
- LCS (Longest Common Subsequence)
- 데이터베이스
- 거듭제곱 알고리즘 [백준 1629번 ] HOT 1
- 접근 제어 지시자 / overloading, overriding
- 위상정렬 HOT 1
- LIS / ( lower_bound , upper_bound ) HOT 2
- dfs+dp 문제 유형
- 중앙값 찾기 / [인덱스트리, pq ] HOT 1
- 트리 HOT 2
- 투 포인터 / 슬라이딩 윈도우 HOT 2
- 큐 / 스택 HOT 3
- Chained Matrix Multiplication HOT 1
- MCMF (Minimum Cost Maximum Flow)
- 이분 매칭(Bipartite Matching)
- 기하 ( ccw / 선분 교차 / 볼록 껍질 / rotating 캘리퍼스 / Line sweeping / Convex hull trick ) HOT 4
- DP / (Bitonic Tour ) HOT 1
- 모듈러 연산
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from algorithm.