Giter Club home page Giter Club logo

Comments (4)

WonYong-Jang avatar WonYong-Jang commented on June 15, 2024

스택

  • 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.

WonYong-Jang avatar WonYong-Jang commented on June 15, 2024

스택 계산기 (전위/ 중위/ 후위)
http://yahma.tistory.com/5

http://yahma.tistory.com/7?category=640326

from algorithm.

WonYong-Jang avatar WonYong-Jang commented on June 15, 2024

링크드 리스트 사이클 판별 알고리즘

토끼와 거북이 알고리즘

  • 두개의 포인터를 사용하는데 한개의 포인터는 한칸씩 이동(거북이) / 다른 한개의 포인터는
    두칸씩 이동(토끼) 해서 진행하면 사이클이 존재한다면 토끼가 반드시 거북이를 잡는다는 알고리즘

  • 문제 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.

WonYong-Jang avatar WonYong-Jang commented on June 15, 2024

더블 링크드 리스트 reverse

해커랭크 Reverse a doubly linked list

from algorithm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.