Introduction To Data Structures Using C stack 2










![3. 스택의 구현: [예제 6 -1] § 순차 자료구조를 이용한 스택 프로그램 01 02 3. 스택의 구현: [예제 6 -1] § 순차 자료구조를 이용한 스택 프로그램 01 02](https://slidetodoc.com/presentation_image_h2/adccd2b050f14c439cbaab6d342c3a5b/image-11.jpg)








![3. 스택의 구현 : [예제 6 -2] § 단순 연결 리스트를 이용한 스택 프로그램 3. 스택의 구현 : [예제 6 -2] § 단순 연결 리스트를 이용한 스택 프로그램](https://slidetodoc.com/presentation_image_h2/adccd2b050f14c439cbaab6d342c3a5b/image-20.jpg)







- Slides: 27

Introduction To Data Structures Using C 스택(stack)





2. 추상 자료형 스택 ADT Stack 데이터 : 0개 이상의 원소를 가진 유한 순서 리스트 연산 : S ∈ Stack; item ∈ Element; create. Stack(S) : : = create an empty stack S; // 공백 스택 S를 생성하는 연산 push(S, item) : : = insert item onto the top of Stack S; // 스택 S의 top에 item(원소)을 삽입하는 연산 is. Empty(S) : : = if (S is empty) then return true else return false; // 스택 S가 공백인지 아닌지를 확인하는 연산 pop(S) : : = if (is. Empty(S)) then return error else { delete and return the top item of Stack S}; // 스택 S의 top에 있는 item(원소)을 스택 S에서 삭제하고 반환하는 연산 delete(S) : : = if (is. Empty(S)) then return error else delete the top item of Stack S; // 스택 S의 top에 있는 item(원소)을 삭제하는 연산 peek(S) : : = if (is. Empty(S)) then return error else return (the top item of the Stack S); // 스택 S의 top에 있는 item(원소)을 반환하는 연산 End Stack




![3 스택의 구현 예제 6 1 순차 자료구조를 이용한 스택 프로그램 01 02 3. 스택의 구현: [예제 6 -1] § 순차 자료구조를 이용한 스택 프로그램 01 02](https://slidetodoc.com/presentation_image_h2/adccd2b050f14c439cbaab6d342c3a5b/image-11.jpg)
3. 스택의 구현: [예제 6 -1] § 순차 자료구조를 이용한 스택 프로그램 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include <stdio. h> #include <stdlib. h> #define STACK_SIZE 100 typedef int element; // int를 스택 element의 자료형으로 정의 element stack[STACK_SIZE]; int top= -1; // 스택의 top의 초기값을-1로 설정 void push(element item) // 스택의 삽입 연산 { if(top >= STACK_SIZE-1) { // 스택이 이미 Full인 경우 printf("nn Stack is FULL ! n"); return; } else stack[++top]=item; } element pop() // 스택의 삭제 후 반환 연산 { if(top==-1) { // 현재 스택이 공백인 경우 printf("nn Stack is Empty!!n"); return 0; } else return stack[top--]; }

3. 스택의 구현: 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 [예제 6 -1] void del() // 스택의 삭제 연산 { if(top==-1) { // 현재 스택이 공백인 경우 printf("nn Stack is Empty !n"); exit(1); } else top--; } element peek() // 스택의 top 원소 검색 연산 { if(top==-1){ // 현재 스택이 공백인 경우 printf("nn Stack is Empty !n"); exit(1); } else return stack[top]; } void print. Stack() // 스택 내용 출력 연산 { int i; printf("n STACK [ "); for(i=0; i<=top; i++) printf("%d ", stack[i]); printf("] "); }

3. 스택의 구현: 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 [예제 6 -1] void main(void) { int item; print. Stack(); push(1); print. Stack(); push(2); print. Stack(); push(3); print. Stack(); item = peek(); print. Stack(); printf("peek top => %d", item); del(); print. Stack();

3. 스택의 구현: 72 73 74 75 76 77 78 79 80 81 82 83 [예제 6 -1] item = pop(); print. Stack(); printf("t pop top => %d", item); pop(); } getchar();





![3 스택의 구현 예제 6 2 단순 연결 리스트를 이용한 스택 프로그램 3. 스택의 구현 : [예제 6 -2] § 단순 연결 리스트를 이용한 스택 프로그램](https://slidetodoc.com/presentation_image_h2/adccd2b050f14c439cbaab6d342c3a5b/image-20.jpg)
3. 스택의 구현 : [예제 6 -2] § 단순 연결 리스트를 이용한 스택 프로그램 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 #include <stdio. h> #include <stdlib. h> #include <string. h> typedef int element; // int를 스택 element의 자료형으로 정의 typedef struct stack. Node { // 스택의 노드 구조 정의 element data; struct stack. Node *link; }stack. Node; stack. Node* top; // 스택의top 노드를 지정하기 위한 포인터 top 선언 void push(element item) // 스택 삽입 연산 { stack. Node* temp=(stack. Node *)malloc(sizeof(stack. Node)); temp->data = item; temp->link = top; top = temp; }

3. 스택의 구현 : 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 [예제 6 -2] element pop() // 스택의 삭제 후 반환 연산 { element item; stack. Node* temp=top; } if(top == NULL) { // 현재 스택이 공백 리스트인 경우 printf("nn Stack is empty !n"); return 0; } else{ // 현재 스택이 공백 리스트가 아닌 경우 item = temp->data; top = temp->link; free(temp); return item; }

3. 스택의 구현 : 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 [예제 6 -2] void del() // 스택의 삭제 연산 { stack. Node* temp; if(top == NULL) { // 현재 스택이 공백 리스트인 경우 printf("nn Stack is empty !n"); } else { // 현재 스택이 공백 리스트가 아닌 경우 temp = top; top = top->link; free(temp); } } void print. Stack() // 스택의 내용 출력 연산 { stack. Node* p=top; printf("n STACK [ "); while(p){ printf("%d ", p->data); p = p->link; } printf("] "); }

3. 스택의 구현 : 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 [예제 6 -2] void main(void) { element item; top = NULL; print. Stack(); push(1); print. Stack(); push(2); print. Stack(); push(3); print. Stack(); item = peek(); print. Stack(); printf("peek top => %d", item); del(); print. Stack();

3. 스택의 구현 : 095 096 097 098 099 100 101 102 103 104 105 106 [예제 6 -2] item = pop(); print. Stack(); printf("t pop top => %d", item); pop(); } getchar();


element peek() //스택의 top 원소 검색 연산 { element item; if(top == NULL) { //현재 스택이 공백 리스트인 경우 printf("nn Stack is empty !n"); return 0; } else { //현재 스택이 공백 리스트가 아닌 경우 item = top->data; return item; } }
