- #include <cstdlib>
- #include <iostream>
- using namespace std;
- class stack
- {
- private:
- int nArray[10];
- int top;
- public:
- stack();
- void push(int data);
- int pop();
- void print();
- };
- stack::stack()
- {
- top = -1;
- }
- void stack::push(int data)
- {
- if(top == 9)
- {
- cout << "더이상 들어갈곳이 없습니다!" << endl;
- return ;
- }
- nArray[++top] = data;
- }
- int stack::pop()
- {
- if(top == -1)
- {
- cout << "스택이 비었습니다!" << endl;
- return 0;
- }
- return nArray[top--];
- }
- void stack::print()
- {
- int i;
- for(i=0; i<=top; i++)
- {
- cout << nArray[i] << " ";
- }cout << endl;
- }
- int main(int argc, char *argv[])
- {
- stack s1;
- char getChar;
- int pushNum=0;
- do{
- cout << "a)push b)pop c)print d)end" << endl;
- cin >> getChar;
- switch(getChar)
- {
- case 'a':
- cout << "Push your number : ";
- cin >> pushNum;
- s1.push(pushNum);
- break;
- case 'b':
- s1.pop();
- break;
- case 'c':
- s1.print();
- break;
- case 'd':
- return 0;
- default:
- cout << "다시 입력하세요" << endl;
- }
- }while(~(getChar=='d'));
- system("PAUSE");
- return EXIT_SUCCESS;
- }
반응형
'IT > [Everyday]Coding' 카테고리의 다른 글
배열 회전 (0) | 2014.12.19 |
---|---|
두 원소 바꾸기 swap() (0) | 2014.12.19 |
셀 정렬 in C# (0) | 2014.12.19 |
삽입정렬 in C# (0) | 2014.12.19 |
선택정렬 in C# (0) | 2014.12.19 |