5.스택(강의자료).key

Similar documents
03장.스택.key

슬라이드 1

Microsoft PowerPoint - 07-chap05-Stack.ppt

4장

o 스택 (stack) ~ 쌓아놓은더미 1. 스택의개요 - 2 -

03장.스택

untitled

K&R2 Reference Manual 번역본

chap01_time_complexity.key

Chapter 4. LISTS

chap 5: Trees

Chapter 4. LISTS

Microsoft PowerPoint - ch07_스택 [호환 모드]

untitled

untitled

chap x: G입력

C프로-3장c03逞풚

chap x: G입력

A Hierarchical Approach to Interactive Motion Editing for Human-like Figures

중간고사 (자료 구조)

untitled

데이터구조 (Chapter 3: Stacks and Queues) 2011 년봄학기 숙명여자대학교이과대학멀티미디어과학과박영호

프로그램을 학교 등지에서 조금이라도 배운 사람들을 위한 프로그래밍 노트 입니다. 저 역시 그 사람들 중 하나 입니다. 중고등학교 시절 학교 도서관, 새로 생긴 시립 도서관 등을 다니며 책을 보 고 정리하며 어느정도 독학으르 공부하긴 했지만, 자주 안하다 보면 금방 잊어

Microsoft PowerPoint - ch09 - 연결형리스트, Stack, Queue와 응용 pm0100

본 강의에 들어가기 전

Chapter 4. LISTS

: 1 int arr[9]; int n, i; printf(" : "); scanf("%d", &n); : : for(i=1; i<10; i++) arr[i-1] = n * i; for(i=0; i<9; i++) if(i%2 == 1) print

Microsoft PowerPoint - 제4장-스택과큐.pptx

Algorithms

PowerPoint 프레젠테이션

PowerPoint 프레젠테이션

06장.리스트

Lab 3. 실습문제 (Single linked list)_해답.hwp

Microsoft Word - FunctionCall

슬라이드 제목 없음

PowerPoint 프레젠테이션

Lab 4. 실습문제 (Circular singly linked list)_해답.hwp

(Asynchronous Mode) ( 1, 5~8, 1~2) & (Parity) 1 ; * S erial Port (BIOS INT 14H) - 1 -

UI TASK & KEY EVENT

chap7.key

OCaml

Lab 5. 실습문제 (Double linked list)-1_해답.hwp


3장. Hello World

1장. 유닉스 시스템 프로그래밍 개요

Microsoft PowerPoint - 제5장-스택의응용.pptx

05_tree

282서비스업관리-마트


(Microsoft Word - \301\337\260\243\260\355\273\347.docx)

<443A5C4C C4B48555C B3E25C32C7D0B1E25CBCB3B0E8C7C1B7CEC1A7C6AE425CBED0C3E0C7C1B7CEB1D7B7A55C D616E2E637070>

13주-14주proc.PDF

Microsoft PowerPoint - chap05-제어문.pptx

Chap 6: Graphs

<443A5C4C C4B48555C B3E25C32C7D0B1E25CBCB3B0E8C7C1B7CEC1A7C6AE425CC0E7B0EDB0FCB8AE5C53746F636B5F4D616E D656E74732E637070>

슬라이드 1

<C3CA3520B0FAC7D0B1B3BBE7BFEB202E687770>

Microsoft PowerPoint - ch10 - 이진트리, AVL 트리, 트리 응용 pm0600

untitled

chap10.PDF

chap8.PDF

11장 포인터

컴파일러

11강-힙정렬.ppt

리스트 (list), 선형리스트 (linear list): 순서를가진항목들의모임 집합 : 항목간의순서의개념이없음 L = n ( item0, item1,..., item -1) l 리스트의예 l 요일 : ( 일요일, 월요일,, 토요일 ) l 한글자음의모임 : ( ㄱ, ㄴ

1장. 리스트

Javascript.pages

Microsoft PowerPoint - chap13-입출력라이브러리.pptx

PowerPoint 프레젠테이션

원형연결리스트에대한설명중틀린것은 모든노드들이연결되어있다 마지막에삽입하기가간단한다 헤더노드를가질수있다 최종노드포인터가 NULL이다 리스트의 번째요소를가장빠르게찾을수있는구현방법은무엇인가 배열 단순연결리스트 원형연결리스트 이중연결리스트 단순연결리스트의노드포인터 가마지막노드를

untitled

03_queue

ABC 10장

PowerPoint 프레젠테이션

lecture4(6.범용IO).hwp

歯9장.PDF

Microsoft PowerPoint - 08-Queue.ppt

商用

Contents v 학습목표 자료구조큐에대한개념을스택과비교하여이해한다. 큐의특징과연산방법에대해알아본다. 순차표현방법을이용한큐와연결표현방법을이용한큐를구현해본다. 큐의응용방법을알아본다. v 내용 큐 큐의구현 큐의응용 2/74

제 11 장포인터 유준범 (JUNBEOM YOO) Ver 본강의자료는생능출판사의 PPT 강의자료 를기반으로제작되었습니다.

이번장에서학습할내용 동적메모리란? malloc() 와 calloc() 연결리스트 파일을이용하면보다많은데이터를유용하고지속적으로사용및관리할수있습니다. 2

Microsoft PowerPoint - chap-11.pptx

, ( ),, ( ), 3, int kor[5]; int eng[5]; int Microsoft Windows 4 (ANSI C2 ) int kor[5] 20 # define #define SIZE 20 int a[10]; char c[10]; float


/chroot/lib/ /chroot/etc/

untitled

chap 5: Trees

À©µµ³×Æ®¿÷ÇÁ·Î±×·¡¹Ö4Àå_ÃÖÁ¾

< E20C6DFBFFEBEEE20C0DBBCBAC0BB20C0A7C7D12043BEF0BEEE20492E707074>

Microsoft PowerPoint - 자료구조2008Chap06

Chap 6: Graphs

제1장 Unix란 무엇인가?

<4D F736F F F696E74202D20B8B6C0CCC5A9B7CEC7C1B7CEBCBCBCAD202834C1D6C2F7207E2038C1D6C2F729>

<4D F736F F F696E74202D20C1A63132B0AD20B5BFC0FB20B8DEB8F0B8AEC7D2B4E7>

Microsoft PowerPoint - 3ÀÏ°_º¯¼ö¿Í »ó¼ö.ppt

int main(void) int a; int b; a=3; b=a+5; printf("a : %d \n", a); printf("b : %d \n", b); a b 3 a a+5 b &a(12ff60) &b(12ff54) 3 a 8 b printf(" a : %x \

Chap 6: Graphs

Chapter_06

Microsoft PowerPoint - ch07 - 포인터 pm0415

chap x: G입력

Transcription:

CHP 5:

https://www.youtube.com/watch?v=ns-r91557ds

? (stack):

(LIFO:Last-In First-Out):. D C B C B C B C B

(element) C (top) B (bottom)

(DT) : n element : create() ::=. is_empty(s) ::=. is_full(s) ::=. push(s, e) ::= e. pop(s) ::=. peek(s) ::=.

push(): pop():

push(): pop():

push(): pop():

push(): pop(): push()

push(): pop(): push()

push(): pop(): push()

push(): pop(): push()

push(): pop(): push() push(b)

push(): pop(): push() push(b)

push(): pop(): push() push(b) B

push(): pop(): push() push(b) B

push(): pop(): push() push(b) B

push(): pop(): push() push(b) B

push(): pop(): push() push(b) B B

push(): pop(): push() push(b) push(c) B B

push(): pop(): push() push(b) push(c) B C B

push(): pop(): push() push(b) push(c) B C B

push(): pop(): push() push(b) push(c) B C B

push(): pop(): push() push(b) push(c) C pop() B B

push(): pop(): push() push(b) push(c) C pop() B B

push(): pop(): push() push(b) push(c) C pop() B B B

is_empty(s): is_full(s): create(): peek(s): ( )pop.

(undo) 1 20 100 150 200 int main() { int i=3; sub1(i);... int sub1(int a) { int j=5; sub2(j);... void sub2(int b) {... main PC=1 sub1 PC=20 a=3 main i=3 sub2 PC=150 b=5 sub1 PC=20 a=3 j=5 main i=3 sub1 PC=20 a=3 j=5 main i=3 main i=3

1 stack[ ] top stack[0], stack[top] top -1 4 4 4 top 3 3 3 2 2 top 2 1 1 1 0-1 top 0-1 0-1

is_empty, is_full is_empty(s) is_full(s) if top = -1 then return TRUE else return FLSE if top = (MX_STCK_SIZE-1) then return TRUE else return FLSE 4 4 top 3 3 2 2 1 1 0-1 top 0-1

push push(s, x) if is_full(s) then error "overflow" else top top+1 stack[top] x 4 3 2 1 0 top 4 3 2 1 0 top

pop pop(s, x) if is_empty(s) then error "underflow" else e stack[top] top top-1 return e 4 3 2 1 0 top 4 3 2 1 0 top

C typedef int element; typedef struct { element stack[mx_stck_size]; int top; StackType; element // void init(stacktype *s) { s->top = -1; // int is_empty(stacktype *s) { return (s->top == -1); // int is_full(stacktype *s) { return (s->top == (MX_STCK_SIZE-1));

// void push(stacktype *s, element item) { if( is_full(s) ) { fprintf(stderr," \n"); return; else s->stack[++(s->top)] = item; // element pop(stacktype *s) { if( is_empty(s) ) { fprintf(stderr, " \n"); exit(1); else return s->stack[(s->top)--]; // element peek(stacktype *s) { if( is_empty(s) ) { fprintf(stderr, " \n"); exit(1); else return s->stack[s->top];

push

Lab ivis.kr

(linked stack): : :. 4 3 9 top 2 1 9 7 top 7 0 3 3 NULL

typedef int element; typedef struct StackNode { element item; struct StackNode *link; StackeNode; typedef struct { StackNode *top; LinkedStackType; 9 top 7 3 NULL

push // void push(linkedstacktype *s, element item) top { StackNode *temp=(stacknode *)malloc(sizeof(stacknode)); if( temp == NULL (2) ){ (1) fprintf(stderr, " \n"); return; temp D else{ temp->item = item; temp->link = s->top; s->top = temp; C B NULL

// pop element pop(linkedstacktype *s) { if( is_empty(s) ) { else{ fprintf(stderr, " \n"); exit(1); StackNode *temp=s->top; int item = temp->item; s->top = s->top->link; free(temp); return item; top C B NULL temp

: ( [, ] ), ( {, ), ( (, ) ) ` 1.. 2.. 3.. (a(b) a(b)c) a{b(c[d]ef)

if( ( i==0 ) && (j==0 ) ( ) ( ( ( ( ( ( ( ( ( ( { [ (i+1 ) ]=0; ( [ { { [ { ( [ [ { { {

, top., 1 2 3. 1 0( ), 1( ).

check_matching(expr) while ( expr ) ch expr switch(ch) case '(': case '[': case '{': ch break case ')': case ']': case ']': if ( ) then else open_ch if (ch open_ch ) then break if( ) then

// int check_matching(char *in) { StackType s; char ch, open_ch; int i, n = strlen(in); init(&s); for (i = 0; i < n; i++) { ch = in[i]; switch(ch){ case '(': case '[': push(&s, ch); break; case '{':

case ')': case ']': case '': if(is_empty(&s)) return FLSE; else { open_ch = pop(&s); if ((open_ch == '(' && ch!= ')') (open_ch == '[' && ch!= ']') (open_ch == '{' && ch!= '')) { return FLSE; break; if(!is_empty(&s)) return FLSE; return TRUE; int main() { if( check_matching("{ [(i+1)]=0; ") == TRUE ) printf(" \n"); else printf(" \n");

: (prefix), (infix), (postfix) 2+3*4 +2*34 234*+ a*b+5 +5*ab ab*5+ (1+2)+7 +7+12 12+7+ -> -> 2+3*4 -> 234*+ -> 14

( ) 82/3-32*+ [0] [1] [2] [3] [4] [5] [6] 8 8 2 8 2 / 4 3 4 3-1 3 1 3 2 1 3 2 * 1 6 + 7

0 1 2 3 8 8 2 / 3-0 1 2 3 8 8 2 / 3-2 0 1 2 3 4 8 2 / 3 - -> -> -> 8/2=4 0 1 2 3 4 8 2 / 3-0 1 2 3 1 8 2 / 3-0 1 2 3 1 8 2 / 3-3 -> -> 4-1=1 -> =1

s. for in do if ( ) push(s, item) if ( op ) then second pop(s) first pop(s) result first op second // op +-*/ push(s, result) final_result pop(s);

// eval(char exp[]) { int op1, op2, value, i=0; int len = strlen(exp); char ch; StackType s; init(&s); for( i=0; i<len; i++){ ch = exp[i]; if( ch!= '+' && ch!= '-' && ch!= '*' && ch!= '/' ){ value = ch - '0'; // push(&s, value); else{ // op2 = pop(&s); op1 = pop(&s); switch(ch){ // case '+': push(&s,op1+op2); break; case '-': push(&s,op1-op2); break; case '*': push(&s,op1*op2); break; case '/': push(&s,op1/op2); break; return pop(&s);

( ) ->. 2+3*4 -> 234*+ ->

( a + b ) * c (

( a + b ) * c a (

( a + b ) * c + a (

( a + b ) * c + a b (

( a + b ) * c a b +

( a + b ) * c a b + *

( a + b ) * c a b + c *

( a + b ) * c a b + c *

infix_to_postfix(exp) s while (exp ) ch switch (ch) case : while ( peek(s) ch ) do e pop(s) e push(s, ch); break; case : push(s, ch); break; case : e pop(s); while( e ) do e e pop(s) break; case : ch break; while( not is_empty(s) ) do e pop(s) e

// -> void infix_to_postfix(char exp[]) { int i = 0; char ch, top_op; int len = strlen(exp); StackType s; init(&s); for (i = 0; i<len; i++) { ch = exp[i]; // // switch (ch) { case '+': case '-': case '*': case '/': // // while (!is_empty(&s) && (prec(ch) <= prec(peek(&s)))) printf("%c", pop(&s)); push(&s, ch); break; case '(': // push(&s, ch); break;

case ')': // top_op = pop(&s); // while( top_op!= '(' ){ printf("%c", top_op); top_op = pop(&s); break; default: // printf("%c", ch); break; while(!is_empty(&s) ) // printf("%c", pop(&s)); // main() { infix_to_postfix("(2+3)*4+9");

. 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 0 0 0 0 1 1 0 0 0 1 0 0 0 0 1 1 0 1 1 1 0 0 1 0 1 1 0 0 0 1 0 0 1 0 1 1 0 1 0 1 0 0 1 0 1 1 0 1 0 1 0 0 1 0 1 1 0 1 0 1 m 0 1 0 1 1 0 1 0 0 0 0 1 0 x 1 1 1 1 1 1 1 1 1 1

s x, while( ) do ; if(,,, ) then push if( is_empty(s) ) then else ;

#define MX_STCK_SIZE 100 #define MZE_SIZE 6 typedef struct StackObjectRec { short r; short c; StackObject; StackObject stack[mx_stck_size]; int top = -1; StackObject here={1,0, entry={1,0; char maze[mze_size][mze_size] = { {'1', '1', '1', '1', '1', '1', ; {'e', '0', '1', '0', '0', '1', {'1', '0', '0', '0', '1', '1', {'1', '0', '1', '0', '1', '1', {'1', '0', '1', '0', '0', 'x', {'1', '1', '1', '1', '1', '1',

void pushloc(int r, int c) { if( r < 0 c < 0 ) return; if( maze[r][c]!= '1' && maze[r][c]!= '.' ){ StackObject tmp; tmp.r = r; tmp.c = c; push(tmp); void printmaze(char m[mze_size][mze_size]) {

void printstack() { int i; for(i=5;i>top;i--) printf(" \n"); for(i=top;i>=0;i--) printf(" (%01d,%01d) \n", stack[i].r, stack[i].c); printf("-------\n");

void main() { int r,c; here = entry; printmaze(maze); printstack(); while ( maze[here.r][here.c]!='x' ){ printmaze(maze); r = here.r; c = here.c; maze[r][c] = '.'; pushloc(r-1,c); pushloc(r+1,c); pushloc(r,c-1); pushloc(r,c+1);

printstack(); if( isempty() ){ printf(" \n"); printf(" \n"); return; else here = pop(); printmaze(maze); printstack(); getch();

Last In First Out,,

#6 5 Exercises 2-16, : 5 2