양 끝에서 쌓이는 다중 스택을 만들어 봅니다.
/*===========================================================================*/ /* 실습) 다중스택.(이중) /* - 삽입,삭제시에 인자를 Left stack과 Right stack두개로 받음. /* - 삽입,삭제마다 배열내용(결과) 출력할것. - 스택은 int형으로 [20]정도, 초기값은 0(빈자리)로 채울것. /* - 스택 모양 /* stack[20]={ㅁㅁㅁㅁㅁㅁㅁㅁㅁㅁ ㅁㅁㅁㅁㅁㅁㅁㅁㅁㅁ} | top[0] | top[1] /* b[0] b[1] /*===========================================================================*/ #include<stdio.h> #define MAX_STACK_SIZE 20 int stack[MAX_STACK_SIZE]; // 스택 전역 선언.(0으로 자동 초기화.) int top[2]={0,10}; // 이중스택 각각의 인덱스. int b[3]={0,10,20}; // boundary - 스택의 구분벽. int i; // top의 인덱스. void Add(int i, int item); // 스택에 add함수 int Del(int i); // 스택에 del함수 /**************************************************************************/ /* main 함수. /**************************************************************************/ int main(void){ int choice; // 삽입인지 삭제인지 받는 값. int item=0; // 스택에 입력할 값. int c; // getchar할 변수. while(getchar()!=EOF){ item=0; printf("입력이면 1 출력이면 2를 입력하시오.\n"); while((c=getchar())!='\n') choice=c-'0'; //scanf("%d",&choice); if(choice==1){ printf("입력을 원하는 값과 입력될 스택(0 or 1)을 쓰시오. ex) 3769 1 \n"); // item=getchar(); while((c=getchar())!='\n'){ item=item*10+(c-'0'); } while((c=getchar())!='\n') i=c-'0'; //scanf("%d %d",&item,&i); Add(i,item); } else if(choice==2){ printf("출력(삭제)할 스택(0 or 1)을 선택하시오.\n"); //scanf("%d",&i); while((c=getchar())!='\n') i=c-'0'; printf("삭제된 값은 %d\n",Del(i)); } } return 0; } /**************************************************************************/ /* add 함수. /**************************************************************************/ void Add(int i, int item) { int j; if(top[i]>=b[i+1]) { printf("i-th stack FULL!\n"); //포화상태. exit(1); } else stack[top[i]++]=item; puts(""); for(j=0;j<MAX_STACK_SIZE-10;j++) printf("%d\t%d\n",stack[j],stack[10+j]); puts(""); } /**************************************************************************/ /* del 함수. /**************************************************************************/ int Del(int i) { int x; int j; if(top[i]<=b[i]) { printf("i-th stack Empty!!\n"); exit(1); } else x= stack[top[i]--]; puts(""); for(j=0;j<MAX_STACK_SIZE-10;j++) printf("%d\t%d\n",stack[j],stack[10+j]); return(x); puts(""); }
이 글은 스프링노트에서 작성되었습니다.
'Programming Language > C' 카테고리의 다른 글
C Tutorial (D.L_list QUE) (0) | 2010.10.14 |
---|---|
C Tutorial (L_list 선택정렬) (2) | 2010.10.14 |
_Mini_Project2_MAZE (0) | 2010.10.14 |
C Tutorial ([C.L.A] 저수준) (0) | 2010.10.14 |
C Tutorial ([C.L.A] 화일 생성) (0) | 2010.10.14 |