yeahzzz
archive
yeahzzz
전체 방문자
오늘
어제
  • 분류 전체보기 (164)
    • Language (41)
      • Python (12)
      • JAVA (21)
      • C&C++ (8)
    • Algorithms (25)
      • programmers (9)
      • study log (16)
    • Problems & Solutions (14)
    • Major (29)
      • Data Structure & Algorithm (14)
      • Linux(Ubuntu) (9)
      • Security (2)
      • Linear Algebra (4)
    • FE (44)
      • Web(HTML5, CSS, JS) (5)
      • React & TS (26)
      • 코딩일기 (10)
    • BE (1)
      • Node.js (1)
    • Pytorch (8)
    • Server (2)

블로그 메뉴

  • 홈

공지사항

인기 글

태그

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
yeahzzz

archive

후위식 연산
Major/Data Structure & Algorithm

후위식 연산

2022. 4. 22. 21:16

후위연산식은 컴퓨터가 계산하기에 훨씬 편한 연산식 형태이다. 피연산자를 스택에 push하고 연산자를 만나면 스택에서 두개의 피연산자를 꺼내서 계산하고 다시 스택에 push한다. 스택이 공백이 될 때까지 진행하고 결과를 출력한다. 

package stack_self;

class StackNode {
	int data;
	StackNode link;
}

class LinkedStack02 implements Stack02{
	private StackNode top;
	
	public boolean isEmpty() {
		return (top == null);
	}
	
	public void push (int item) {
		StackNode newNode = new StackNode();
		newNode.data = item;
		newNode.link = top;
		top = newNode;
		
	}
	public int pop() {
		if(isEmpty()) {
			System.out.println("Deleting fail. Linked Stack is empty");
			return 0;
		}
		else {
			int item = top.data;
			top = top.link;
			return item;
		}
	}
	
	private String exp; 
	public int evalPostfix(String postfix) {
		LinkedStack02 LS = new LinkedStack02();
		exp = postfix;
		int opr1, opr2, value;
		char testCh;
		
		for(int i =0; i < postfix.length(); i++) {
			testCh = exp.charAt(i);
			if(testCh != '+' && testCh != '*' && testCh != '/' && testCh != '-') {
				value = testCh - '0';
				LS.push(value);
			}
			else {
				opr2 = LS.pop();
				opr1 = LS.pop();
				switch(testCh) {
				case '+' : LS.push(opr1 + opr2); break;
				case '-' : LS.push(opr1 + opr2); break;
				case '*' : LS.push(opr1 + opr2); break;
				case '/' : LS.push(opr1 + opr2); break;
				
				}
			}
		}
		return LS.pop();
	}
	
}

 

'Major > Data Structure & Algorithm' 카테고리의 다른 글

DFS(Depth First Search)  (0) 2022.06.01
Binary Tree  (0) 2022.05.22
Stack , 괄호검사(Valid Braces), Infix to Postfix algorithm  (0) 2022.04.16
review  (0) 2022.04.16
원형 연결 리스트  (0) 2022.04.16
    'Major/Data Structure & Algorithm' 카테고리의 다른 글
    • DFS(Depth First Search)
    • Binary Tree
    • Stack , 괄호검사(Valid Braces), Infix to Postfix algorithm
    • review
    yeahzzz
    yeahzzz

    티스토리툴바