2012. 8. 13. 14:44

#include<iostream>

using namespace std;

/*

//버전 1. C 버전

//버전 1의 문제점 : stack 이 2개가 필요하다면?!  모든게 2개여야한다.

int buff[10];

int index = -1;


void push(int a) { buff[++index]=a;}

int pop() {return buff[index--];}


int main(){



push(10);

cout << pop()<<endl;


}

*/

/*

//버전 2  2개이상의 스택을 고려

//버전 2의 문제점!  : 보기에 복잡하다! 타입을 만들어 줘야한다

void push ( int * buff, int *index , int a){


buff[++(*index)]=a;


}

int main(){

int buff1[10], index1;

int buff2[10], index2;

push(buff2, &index1, 10);


}

*/


//버전 3 구조체로 스택을 구현! Main 함수가 깔끔해졌다!

/*

struct stack{


int buff[10];

int index;

};


void init(stack *s){s->index = -1 ; } 

void push(stack *s , int a){ s->buff[++(s->index)]=a;}

int main(){


stack s1, s2;

init(&s1);

push (&s1,10);



}

*/

/*

//버전 4 상태를 나타내는 data 와 상태를 조작하는 함수를 묶자!

// 버전 4의 문제점! 잘못사용했을 시?!  문제가 생긴다 main 함수에 s1.idex=100 이라고 입력하면 스택이 깨져버린다.

//

struct stack{

int buff[10];

int index;


void init() {index=-1;}

void push(int a) {buff[++index]=a;}

int pop() {return buff[index--];}


};


int main(){


stack s1,s2;

s1.init();

s1.push(20);

cout << s1.pop()<<endl;



}


*/

/*

//버전 5 외부의 잘못된 사용으로 부터 객체가 불안해 지는 것을 막는 방법!

// 접근지정자의 도입!!

// 객체지향의 3대 원칙중하나인 캡슐화이다.


struct stack{

private:

int buff[10];

int index;

public:

void init() {index=-1;}

void push(int a) {buff[++index]=a;}

int pop() {return buff[index--];}


};


int main(){


stack s1,s2;

s1.init();

s1.push(20);

//s1.index=100; // 에러가 뜬다!!

cout << s1.pop()<<endl;



}

*/


/*


//버전 6 객체의 초기화를 자동으로 하자! 

//생성자의 도입

//struct 는 디폴트 접근 지정자가 public 이고 

//class 는 디폴트 접근 지정자가 private 이다.


struct stack{

private:

int buff[10];

int index;

public:

stack() {index=-1;}

void init() {index=-1;}

void push(int a) {buff[++index]=a;}

int pop() {return buff[index--];}


};


int main(){


stack s1,s2;

//s1.init();

s1.push(20);

//s1.index=100; // 에러가 뜬다!!

cout << s1.pop()<<endl;



}



*/

/*

//버전 7 내부 버퍼의 크기는 사용자가 결정하는 것이 좋다.

// 동적 메모리 할당의 도입..

class stack{

private:

int * buff;

int index;

public:

stack( int sz = 10)

{

index=-1;

buff = new int[sz];

}

void init() {index=-1;}

void push(int a) {buff[++index]=a;}

int pop() {return buff[index--];}


};


int main(){


stack s1(100);

//s1.init();

s1.push(20);

//s1.index=100; // 에러가 뜬다!!

cout << s1.pop()<<endl;



}

*/

/*

//버전 8 동적 할당하고나니 메모리 해지가 필요하다?! 

// 소멸자의 도입 

class stack{

private:

int * buff;

int index;

public:

stack( int sz = 10)

{

index=-1;

buff = new int[sz];

}

void init() {index=-1;}

void push(int a) {buff[++index]=a;}

int pop() {return buff[index--];}


~stack(){

delete[]buff;

}

};


int main(){


stack s1(100);

//s1.init();

s1.push(20);

//s1.index=100; // 에러가 뜬다!!

cout << s1.pop()<<endl;



}

*/

/*


//버전 9 다양한 type의 Stack 을 자동 생성되게 하자! 

// template 도입!!


template<typename T> class stack

{

T *buff;

int index;


private:

int * buff;

int index;

public:

stack( int sz = 10)

{

index=-1;

buff = new int[sz];

}

void init() {index=-1;}

void push(T a) {buff[++index]=a;}

T pop() {return buff[index--];}


~stack(){

delete[]buff;

}

};


int main(){


stack<int> s1(100);

//s1.init();

s1.push(20);

//s1.index=100; // 에러가 뜬다!!

cout << s1.pop()<<endl;



}

*/



/*

//버전 9 좀더 객체화시키고 역할을 쪼갠다. 

template<typename T> class stack

{

T *buff;

int index;


private:

int * buff;

int index;

public:

stack( int sz = 10)

{

index=-1;

buff = new int[sz];

}

void init() {index=-1;}



void push(const T& a) {buff[++index]=a;}


//pop 이 제거와 리턴을 동시에 하면 절대 최적화 할수 없다. 분리하자!!

//T pop() {return buff[index--];}


//제거만하는 함수

void pop(){ --index;}

//리턴만 하는 함수

T &top() {return buff[index];}

//하나의 함수는 한가지 일만 할떄가 좋다.


~stack(){

delete[]buff;

}

};


int main(){


stack<int> s1(100);

//s1.init();

s1.push(10);

s1.push(20);

//s1.index=100; // 에러가 뜬다!!

cout << s1.top()<<endl;

s1.pop();

cout << s1.top()<<endl;

s1.pop();



}

*/


//버젼 10 이미 있다!!

#include<stack>  //C++ 표준인 STL의 STACK 클래스


int main(){


stack<int> s;

s.push(10);

s.push(20);


cout << s.top() << endl;

s.pop();

cout<<s.top() << endl;

s.pop();


}

//버젼 11 숙제!! 배열로 스택을 구현하면 미리 할당을 해줘야한다! 

//버전 10의 내부 구조를 싱글 링크드 리스트로 변경해보기


Posted by k1rha