q IT COOKBOOK ADT Stack ADT 7 1
q 스택의 추상 자료형 IT COOKBOOK § 스택에 대한 추상 자료형 ADT Stack [ADT 7 -1] 데이터 : 0개 이상의 원소를 가진 유한 순서 리스트 연산 : S ∈ Stack; item ∈ Element; create. Stack() : : = create an empty Stack; // 공백 스택을 생성하는 연산 is. Empty(S) : : = if (S is empty) then return true else return false; // 스택 S가 공백인지 아닌지를 확인하는 연산 push(S, item) : : =insert item onto the top of S; // 스택 S의 top에 item(원소)을 삽입하는 연산 pop(S) : : = if (is. Empty(S)) then return error else { delete and return the top item of S }; // 스택 S의 top에 있는 item(원소)을 스택에서 삭제하고 반환하는 연산 delete(S) : : = if (is. Empty(S)) then return error else delete the top item; a // 스택 S의 top에 있는 item(원소)을 삭제하는 연산 peek(S) : : = if (is. Empty(S)) then return error else return (the top item of the S); // 스택 S의 top에 있는 item(원소)을 반환하는 연산 End Stack 8
q 스택의 구현 IT COOKBOOK § 순차 자료구조 방식을 이용하여 구현한 스택 프로그램 01 02 03 04 05 06 interface Stack{ boolean is. Empty( ); void push(char item); char pop( ); void delete( ); char peek( ); [예제 7 -1] 07 } 08 09 class Array. Stack implements Stack{ 10 private int top; 11 12 13 14 15 16 17 18 private int stack. Size; private char item. Array[]; public Array. Stack(int stack. Size){ top = -1; this. stack. Size = stack. Size; item. Array = new char[this. stack. Size]; } 13
q 스택의 구현 IT COOKBOOK § 순차 자료구조 방식을 이용하여 구현한 스택 프로그램 19 20 21 22 23 24 [예제 7 -1] public boolean is. Empty(){ return (top == -1); } public boolean is. Full(){ 25 26 return (top == this. stack. Size-1); } 27 28 29 30 31 32 33 34 35 36 public void push(char item){ if(is. Full()){ System. out. println("Inserting fail! Array Stack is full!!"); } else{ item. Array[++top] = item; System. out. println("Inserted Item : " + item); } } 14
q 스택의 구현 IT COOKBOOK § 순차 자료구조 방식을 이용하여 구현한 스택 프로그램 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 [예제 7 -1] public char pop(){ if(is. Empty()) { System. out. println("Deleting fail! Array Stack is empty!!"); return 0; } else{ return item. Array[top--]; } } public void delete(){ if(is. Empty()){ System. out. println("Deleting fail! Array Stack is empty!!"); } else { top--; } } 15
q 스택의 구현 IT COOKBOOK § 순차 자료구조 방식을 이용하여 구현한 스택 프로그램 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 [예제 7 -1] public char peek(){ if(is. Empty()){ System. out. println("Peeking fail! Array Stack is empty!!"); return 0; } else return item. Array[top]; } public void print. Stack(){ if(is. Empty()) System. out. printf("Array Stack is empty!! %n %n"); else{ System. out. printf("Array Stack>> "); for(int i=0; i<=top; i++) System. out. printf("%c ", item. Array[i]); System. out. println(); } 16
q 스택의 구현 IT COOKBOOK § 순차 자료구조 방식을 이용하여 구현한 스택 프로그램 75 } 76 } 77 78 class Ex 7_1{ 79 public static void main(String args[]){ 80 int stack. Size = 5; 81 char deleted. Item; 82 Array. Stack S = new Array. Stack(stack. Size); [예제 7 -1] 83 84 S. push('A'); 85 86 87 88 89 90 91 S. print. Stack( ); S. push('B'); S. print. Stack(); S. push('C'); S. print. Stack( ); 17
q 스택의 구현 IT COOKBOOK § 순차 자료구조 방식을 이용하여 구현한 스택 프로그램 92 93 94 95 96 97 [예제 7 -1] deleted. Item = S. pop(); if(deleted. Item != 0) System. out. println("deleted Item : " + deleted. Item); S. print. Stack(); } 98 } 18
q 스택의 구현 IT COOKBOOK § 연결 자료구조 방식을 이용하여 구현한 스택 프로그램 01 interface Stack{ 02 boolean is. Empty(); 03 void push(char item); 04 char pop(); 05 void delete(); 06 char peek(); [예제 7 -2] 07 } 08 09 class Stack. Node{ 10 char data; 11 Stack. Node link; 12 } 13 14 class Linked. Stack implements Stack{ 15 private Stack. Node top; 16 17 public boolean is. Empty(){ 23
q 스택의 구현 IT COOKBOOK § 연결 자료구조 방식을 이용하여 구현한 스택 프로그램 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 } return (top == null); [예제 7 -2] public void push(char item){ Stack. Node new. Node = new Stack. Node(); new. Node. data = item; new. Node. link = top; top = new. Node; System. out. println("Inserted Item : " + item); } public char pop(){ if(is. Empty()) { System. out. println("Deleting fail! Linked Stack is empty!!"); return 0; } else{ char item = top. data; top = top. link; 24
q 스택의 구현 IT COOKBOOK § 연결 자료구조 방식을 이용하여 구현한 스택 프로그램 37 38 39 40 41 42 return item; [예제 7 -2] } } public void delete(){ if(is. Empty()){ 43 System. out. println("Deleting fail! Linked Stack is empty!!"); 44 45 } 46 else { 47 48 49 50 51 51 52 top = top. link; } } public char peek(){ if(is. Empty()){ System. out. println("Peeking fail! Linked Stack is empty!!"); 25
q 스택의 구현 IT COOKBOOK § 연결 자료구조 방식을 이용하여 구현한 스택 프로그램 53 54 55 56 57 } 58 return 0; [예제 7 -2] } else return top. data; 59 public void print. Stack(){ 60 if(is. Empty()) 61 62 63 64 65 66 67 68 69 70 System. out. printf("Linked Stack is empty!! %n %n"); else{ Stack. Node temp = top; System. out. println("Linked Stack>> "); while(temp != null){ System. out. printf("t %c n", temp. data); temp = temp. link; } System. out. println(); } 26
q 스택의 구현 IT COOKBOOK § 연결 자료구조 방식을 이용하여 구현한 스택 프로그램 71 } 72 } 73 74 class Ex 7_2{ 75 public static void main(String args[]){ 76 char deleted. Item; 77 [예제 7 -2] Linked. Stack LS = new Linked. Stack(); 78 79 LS. push('A'); 80 LS. print. Stack(); 81 82 83 84 85 86 87 LS. push('B'); LS. print. Stack(); LS. push('C'); LS. print. Stack(); 27
q 스택의 구현 IT COOKBOOK § 연결 자료구조 방식을 이용하여 구현한 스택 프로그램 88 89 90 91 92 93 } [예제 7 -2] deleted. Item = LS. pop(); if(deleted. Item != 0) System. out. println("deleted Item : " + deleted. Item); LS. print. Stack(); } 28
q 스택의 응용 IT COOKBOOK § 수식의 괄호 검사 알고리즘 test. Pair( ) exp ← Expression; Stack ← null; while (true) do { symbol ← get. Symbol(exp); case { [알고리즘 7 -3] symbol = "(" or "[" or "{" : push(Stack, symbol); symbol = ")" : open_pair ← pop(Stack); if (open_pair ≠ "(") then return false; symbol = "]" : open_pair ← pop(Stack); if (open_pair ≠ "[") then return false; symbol = "}" : open_pair ← pop(Stack); if (open_pair ≠ "{") then return false; 37
q 스택의 응용 IT COOKBOOK § 수식의 괄호 검사 알고리즘 symbol = null : if (is. Empty(Stack)) then return true; else return false; else : [알고리즘 7 -3] } } end test. Pair( ) 38
q 스택의 응용 IT COOKBOOK § 연결 자료구조 방식을 이용하여 구현한 스택 프로그램 001 interface Stack{ 002 boolean is. Empty( ); 003 void push(char item); 004 char pop( ); 005 void delete( ); 006 char peek( ); 007 } 008 009 class Stack. Node{ 010 char data; 011 Stack. Node link; 012 } 013 014 class Linked. Stack implements Stack{ 015 private Stack. Node top; 016 017 public boolean is. Empty(){ 018 return (top == null); 019 } [예제 7 -3] 43
q 스택의 응용 IT COOKBOOK § 연결 자료구조 방식을 이용하여 구현한 스택 프로그램 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 [예제 7 -3] public void push(char item){ Stack. Node new. Node = new Stack. Node(); new. Node. data = item; new. Node. link = top; top = new. Node; // System. out. println("Inserted Item : " + item); } public char pop(){ if(is. Empty()) { System. out. println("Deleting fail! Linked Stack is empty!!"); return 0; } else{ char item = top. data; top = top. link; return item; } 44
q 스택의 응용 IT COOKBOOK § 연결 자료구조 방식을 이용하여 구현한 스택 프로그램 039 040 041 042 043 044 045 046 047 048 049 } 050 051 052 053 054 055 056 057 } [예제 7 -3] public void delete(){ if(is. Empty()){ System. out. println("Deleting fail! Linked Stack is empty!!"); } } else { top = top. link; public char peek(){ if(is. Empty()){ System. out. println("Peeking fail! Linked Stack is empty!!"); return 0; } else return top. data; 45
q 스택의 응용 IT COOKBOOK § 연결 자료구조 방식을 이용하여 구현한 스택 프로그램 058 059 060 061 062 063 [예제 7 -3] } public void print. Stack(){ if(is. Empty()) System. out. printf("Linked Stack is empty!! %n %n"); else{ 064 Stack. Node temp = top; 065 System. out. println("Linked Stack>> "); 066 while(temp != null){ 067 System. out. printf("t %c n", temp. data); 068 069 070 071 072 073 } 074 temp = temp. link; } System. out. println(); } } 46
q 스택의 응용 IT COOKBOOK § 연결 자료구조 방식을 이용하여 구현한 스택 프로그램 075 class Opt. Exp{ 076 private String exp; 077 private int exp. Size; 078 private char test. Ch, open. Pair; 079 080 public boolean test. Pair(String exp){ 081 this. exp = exp; 082 Linked. Stack S = new Linked. Stack(); 083 exp. Size = this. exp. length(); 084 for(int i=0; i<exp. Size; i++){ 085 086 087 088 089 090 091 092 093 test. Ch = this. exp. char. At(i); switch(test. Ch){ case '(' : case '{' : case '[' : S. push(test. Ch); break; case ')' : case '}' : case ']' : [예제 7 -3] 47
q 스택의 응용 IT COOKBOOK § 연결 자료구조 방식을 이용하여 구현한 스택 프로그램 075 class Opt. Exp{ 076 private String exp; 077 private int exp. Size; 078 private char test. Ch, open. Pair; 079 080 public boolean test. Pair(String exp){ 081 this. exp = exp; 082 Linked. Stack S = new Linked. Stack(); 083 exp. Size = this. exp. length(); 084 for(int i=0; i<exp. Size; i++){ 085 086 087 088 089 090 091 092 [예제 7 -3] test. Ch = this. exp. char. At(i); switch(test. Ch){ case '(' : case '{' : case '[' : S. push(test. Ch); break; case ')' : case '}' : 48
q 스택의 응용 IT COOKBOOK § 연결 자료구조 방식을 이용하여 구현한 스택 프로그램 093 094 095 096 097 098 case ']' : if(S. is. Empty()) return false; else{ open. Pair = S. pop(); if((open. Pair == '(' && test. Ch != ')') || (open. Pair == '{' && test. Ch != '}') || 099 (open. Pair == '[' && test. Ch != ']')) 100 return false; 101 else break; 102 103 104 105 106 107 108 109 110 [예제 7 -3] } } } if (S. is. Empty()) return true; else return false; } public char[] to. Postfix(String infix){ char test. Ch; 49
q 스택의 응용 IT COOKBOOK § 연결 자료구조 방식을 이용하여 구현한 스택 프로그램 111 112 113 114 115 116 exp = infix; int exp. Size = 10; int j=0; char postfix[] = new char[exp. Size]; Linked. Stack S = new Linked. Stack(); 117 for(int i=0; i<=exp. Size; i++){ 118 test. Ch = this. exp. char. At(i); 119 switch(test. Ch){ 120 case '0': 121 122 123 124 125 126 127 128 case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': [예제 7 -3] 50
q 스택의 응용 IT COOKBOOK § 연결 자료구조 방식을 이용하여 구현한 스택 프로그램 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 } case '9': postfix[j++] = test. Ch; break; [예제 7 -3] case '+' : case '-' : case '*' : case '/' : S. push(test. Ch); break; case ')' : postfix[j++] =S. pop(); break; } } default: } postfix[j] = S. pop(); return postfix; 51
q 스택의 응용 IT COOKBOOK § 연결 자료구조 방식을 이용하여 구현한 스택 프로그램 148 149 class Ex 7_3{ 150 public static void main(String args[]){ 151 Opt. Exp opt = new Opt. Exp(); 152 String exp = "(3*5)-(6/2)"; 153 char postfix[]; 154 int value; 155 System. out. println(exp); 156 if(opt. test. Pair(exp)) 157 158 159 160 161 162 163 164 165 } [예제 7 -3] System. out. println("괄호 맞음!"); else System. out. println("괄호 틀림!!!"); System. out. printf("n후위표기식 : "); postfix = opt. to. Postfix(exp); System. out. println(postfix); } 52
q 스택의 응용 IT COOKBOOK § 중위 표기법에 대한 후위 표기법 변환 알고리즘 infix_to_postfix(exp) while(true) do { symbol ← get. Symbol(exp); case { symbol = operand : // 피연산자 처리 print(symbol); [알고리즘 7 -4] symbol = operator : // 연산자 처리 push(stack, symbol); symbol = ")" : // 오른쪽 괄호 처리 print(pop(stack)); symbol = null : // 중위 수식의 끝 while(top > -1) do print(pop(stack)); else : } } end infix_to_postfix( ) 62
q 스택의 응용 IT COOKBOOK § 후위 표기 수식의 연산 알고리즘 eval. Postfix(exp) while (true) do { symbol ← get. Symbol(exp); case { symbol = operand : // 피연산자 처리 push(Stack, symbol); [알고리즘 7 -5] symbol = operator : // 연산자 처리 opr 2 ← pop(Stack); opr 1 ← pop(Stack); result ← opr 1 op(symbol) opr 2; // 스택에서 꺼낸 피연산자들을 연산자로 연산 push(Stack, result); symbol = null : // 후위 수식의 끝 print(pop(Stack)); } } end eval. Postfix() 64
q 스택의 응용 IT COOKBOOK § 예) AB*CD/- 65
q 스택의 응용 IT COOKBOOK § 예) AB*CD/- 66
q 스택의 응용 IT COOKBOOK § 예) AB*CD/- 67
q 스택의 응용 IT COOKBOOK § 후위 표기 수식의 연산 프로그램 01 class Stack. Node{ 02 int data; 03 Stack. Node link; 04 } 05 06 class Linked. Stack{ 07 private Stack. Node top; 08 09 public boolean is. Empty(){ 10 return (top == null); 11 } 12 13 public void push(int item){ 14 Stack. Node new. Node = new Stack. Node(); 15 new. Node. data = item; 16 new. Node. link = top; 17 top = new. Node; 18 } 19 [예제 7 -4] 68
q 스택의 응용 IT COOKBOOK § 후위 표기 수식의 연산 프로그램 [예제 7 -4] 20 public int pop(){ 21 if(is. Empty()) { 22 System. out. println("Deleting fail! Linked Stack is empty!!"); 23 return 0; 24 } 25 else{ 26 int item = top. data; 27 top = top. link; 28 return item; 29 } 30 } 31 } 32 33 class Opt. Exp 2{ 34 private String exp; 35 36 public int eval. Postfix(String postfix){ 37 Linked. Stack S = new Linked. Stack(); 38 exp = postfix; 69
q 스택의 응용 IT COOKBOOK § 후위 표기 수식의 연산 프로그램 39 40 41 42 43 44 46 47 48 49 50 51 52 53 54 55 56 57 58 [예제 7 -4] int opr 1, opr 2, value; char test. Ch; for(int i=0; i<7; i++){ test. Ch = exp. char. At(i); if(test. Ch != '+' && test. Ch != '-' && test. Ch !='*' && test. Ch != '/'){ value = test. Ch - '0'; 45 S. push(value); } else{ opr 2 = S. pop(); opr 1 = S. pop(); switch(test. Ch){ case '+' : S. push(opr 1 + opr 2); break; case '-' : S. push(opr 1 - opr 2); break; case '*' : S. push(opr 1 * opr 2); break; case '/' : S. push(opr 1 / opr 2); break; } } } return S. pop(); 70
q 스택의 응용 IT COOKBOOK § 후위 표기 수식의 연산 프로그램 59 } 60 } 61 62 class Ex 7_4{ 63 public static void main(String args[]){ 64 Opt. Exp 2 opt = new Opt. Exp 2(); 65 int result; 66 String exp = "35*62/-"; 67 System. out. printf("n후위표기식 : %s", exp); 68 result = opt. eval. Postfix(exp); 69 System. out. printf("n 연산결과 = %d n", result); 70 } 71 } [예제 7 -4] 71
- Slides: 71