Stack 구현

  • 문자들의 스택을 테스트하는 프로그램 구현
  • 명령어 push(+< 문자 >), pop(-)
    스택 내용을 출력(S)

실행 예

image

자료구조 및 함수 구성

-

Element stack[MAX_SIZE];
int top = -1;
  • Void push(Element e)
    Requires : 스택이 포화되지 않아야 함
    Results : 스택에 e 를 삽입

  • Element pop()
    Requires : 스택이 비어 있지 않아야 함
    Results : 스택에서 원소를 반환

  • Void stack_show()
    Results : 스택의 내용을 보여줌

Source

  • 실습1.h
#pragma once
#define MAX_SIZE 10
#define boolean unsigned char
#define true 1
#define false 0

typedef char Element;

Element stack[MAX_SIZE];
int top = -1;

void push(Element e);
Element pop();
void stack_show();
  • 실습1.cpp
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include"실습1.h"

void push(Element e) {
	++top;
	stack[top] = e;
}
Element pop() {
	if (top == -1) {
		printf("\nStack is empty!");
		exit(1);
	}
	printf("\n%c\n", stack[top]);
	stack[top] = NULL;
	--top;
}
void stack_show() {
	printf("\n");
	for (int i = 0; i <= top; i++)
		printf("%c\n", stack[i]);
}

void main() {
	char c, e;

	printf("********** Command **********\n");
	printf("+<c>: Push c, -: Pop,\n");
	printf("S: Show, Q:Quit\n");
	printf("*****************************\n");

	while (1) {
		printf("\nCommand> ");
		c = getch();
		putch(c);
		c = toupper(c);

		switch (c) {
		case'+':
			e = getch();
			putch(e);
			push(e);
			break;
		case'-':
			e = pop();
			break;
		case'S':
			stack_show();
			break;
		case'Q':
			printf("\n");
			exit(1);
		default:
			break;
		}
	}
}