3 structure Stack 0 stack Stack item element

  • Slides: 21
Download presentation

3장. 스택과 큐 스택의 추상 데이터 타입 structure Stack 데이터 : 0개 이상의 원소를

3장. 스택과 큐 스택의 추상 데이터 타입 structure Stack 데이터 : 0개 이상의 원소를 가진 유한 순서 리스트 연산들 : 모든 stack∈ Stack, item∈ element, max_stack_size∈ 양 의 정수 Stack Create. S(max_stack_size) : : = 최대 크기가 max_stack_size인 공백 스택 을 생성 Boolean Is. Full(stack, max_stack_size) : : = if (stack의 원소수 == max_stack_size) return TRUE else return FALSE Stack Add(stack, item) : : = if (Is. Full(stack)) stack_full else stack의 톱에 item을 삽입하고 return Boolean Is. Empty(stack) : : = if (stack == Create. S(max_stack_size)) return TRUE else return FALSE Element Delete(stack) : : = if (Is. Empty(stack)) return else 스택 톱의 item을 제거해서 반환 end Array 조 미경 멀티미디어공학과 5/21 교수

3장. 스택과 큐 스택 생성과 사용 element stack[MAX_STACK_SIZE]; int main() { element data; int

3장. 스택과 큐 스택 생성과 사용 element stack[MAX_STACK_SIZE]; int main() { element data; int top = -1; push( &top, 1); //원소 삽입 push( &top, 2); data = pop( &top) //원소 삭제 return 0; } 7/21 멀티미디어공학과 조 미경 교수

3장. 스택과 큐 스택 연산들 – 원소 삽입 연산 삽입 전에 스택이 포화상태인지 검사

3장. 스택과 큐 스택 연산들 – 원소 삽입 연산 삽입 전에 스택이 포화상태인지 검사 void push( int *top, int item ) { if( is. Full(*top) ) printf( “Stack is full”); else stack[++(*top)]. key = item ; } int is. Full( int top) { if( top == (MAX_STACK_SIZE -1) ) return 1; else return 0; } 멀티미디어공학과 조 미경 교수 8/21

3장. 스택과 큐 스택 연산들(계속) – 원소 삭제 연산 삭제 전에 스택이 비어있는지 검사

3장. 스택과 큐 스택 연산들(계속) – 원소 삭제 연산 삭제 전에 스택이 비어있는지 검사 element pop ( int *top) { if( is. Empty(*top) ) { printf(“Stack Empty”); exit(-1); } else return stack[(*top)--]; } int is. Empty( int top) { if( top == -1 ) return 1; else return 0; } 9/21 멀티미디어공학과 조 미경 교수

3장. 스택과 큐 큐의 추상 데이터 타입 Structure Queue 데이터 : 0개 이상의 원소를

3장. 스택과 큐 큐의 추상 데이터 타입 Structure Queue 데이터 : 0개 이상의 원소를 가진 유한 순서 리스트 연산들 : 모든 queue∈Queue, item∈element, max_queue_size∈positive integer Queue Create. Q(max_queue_size) : : = 최대 크기가 max_queue_size인 공백 큐를 생성 Boolean Is. Full. Q(queue, max_queue_size ) : : = if (queue의 원소수 == max_queue_size) return TRUE else return FALSE Queue Add. Q(queue, item) : : = if (Is. Full(queue)) queue_full else queue의 뒤에 item을 삽입하고 이 queue 를 반환 Boolean Is. Empty. Q(queue) : : = if (queue == Create. Q(max_queue_size)) return TRUE else return FALSE Element Delete. Q(queue) : : = if (Is. Empty(queue)) return else queue의 앞에 있는 item을 제거해서 반환 end Queue 멀티미디어공학과 조 미경 교수 11/21

3장. 스택과 큐 큐의 연산들 – 원소 삽입 연산 삽입 전 큐가 포화상태인지 검사

3장. 스택과 큐 큐의 연산들 – 원소 삽입 연산 삽입 전 큐가 포화상태인지 검사 void addq( int *rear, int item ) { if( is. Full(*rear) ) printf( “Queue is full”); else queue[++(*rear)]. key = item ; } int is. Full( int rear) { if( rear == (MAX_QUEUE_SIZE -1) ) 1; else return 0; } 멀티미디어공학과 조 미경 교수 return 13/21

3장. 스택과 큐 큐의 연산들(계속) – 원소 삭제 연산 삭제 전 큐가 비워있는지 검사

3장. 스택과 큐 큐의 연산들(계속) – 원소 삭제 연산 삭제 전 큐가 비워있는지 검사 element deleteq ( int *front, int rear ) { if( is. Empty(*front, rear) ) printf(“Queue is Empty”); else return queue[++(*front)]; } int is. Empty( int front, int rear) { if( front == rear ) return 1; else return 0; } 14/21 멀티미디어공학과 조 미경 교수

3장. 스택과 큐 원형 큐의 연산 – 원소 삽입 연산 void addq( int front,

3장. 스택과 큐 원형 큐의 연산 – 원소 삽입 연산 void addq( int front, int *rear, int item ) { *rear = (*rear+1) % MAX_QUEUE_SIZE; if( front == *rear ) printf( “Queue is full”); else queue[*rear]. key = item ; } 18/21 멀티미디어공학과 조 미경 교수

3장. 스택과 큐 원형 큐의 연산(계속) – 원소 삭제 연산 element deleteq ( int

3장. 스택과 큐 원형 큐의 연산(계속) – 원소 삭제 연산 element deleteq ( int *front, int rear ) { if( *front == rear ) { printf(“Queue is Empty”); exit(-1); } *front = (*front+1)%MAX_QUEUE_SIZE; return queue[*front]; } 19/21 멀티미디어공학과 조 미경 교수