'이즈케이'에 해당되는 글 1건

  1. 2007.11.17 [C언어소스] 스택 예제 2
반응형
스택 예제를 예전에 구현하였던것을...

이곳에 다시 올려 둡니다.

// 스택//
#include<stdio.h>
#include<conio.h>

int stack[10];
int top;

void init_stack() {
    top = -1;
}

void push(int vall) {
  if(top >= 9)
    printf("\n######## 스택이 꽉 차버렸네요 ########\n");
  else
    stack[++top] = vall;
}

void pop() {
  if(top < 0) 
    printf("########## 더이상 비울 스택이 없습니다. ##########\n");
  else
    --top;
}

void print_stack() {
  int i;
  printf("스택 top --------------------- buttom\n");

  for(i = top; i>= 0; i--)
    printf("%d ", stack[i]);
    printf("\n");
}

void main() {
  int select_num;
  int push_num;
  int exit_flag = 0;

  printf(" 배열을 이용한 스택 프로그램 입니다. \n");

  init_stack();
  while(1) {
    printf(" 1) push 2) pop 3) data print 4) Quit! \n");
    printf(" 숫자를 선택해 주세요 : ");
    scanf("%d",&select_num);
 
    if(select_num > 3) break;
    else {
      switch(select_num) {
         case 1 :
           printf("푸쉬 숫자를 입력해 주세요 =>");
           scanf("%d",&push_num);
           push(push_num);
           printf("푸쉬(push)를 실행 했습니다\n");
         break;
         case 2:
           pop();
           printf("팝(pop)을 실행했습니다\n");
         break;
         case 3:
           print_stack();
         break;
      }
    }
  }
  printf("프로그램을 종료합니다\n");
  getch();
}
반응형
Posted by onlyTheOne
,