프로그래밍 언어

C언어 Stack클래스의 구조

행복회로 풀가동 2018. 5. 19. 21:04

.스택 클래스의 UML클래스 다이어그램

 

#include <iostream>

using namespace std;

 

class Stack

{

protected:

int m_size;

int m_top;

int m_buffer;

 

public:

void Initialize(int size=10);

void Remove All();

bool Push(int value);

bool Pop(int& value);

};

 

void Stack::Initialize(int size)

{

m_size=size;

m_top=-1;

m_buffer=new int[m_size];

memset=(m_buffer,0,sizeof(int)*m_size);

}

 

void Stack::Remove All()

{

m_size=0;

m_top=-1;

delete [] m_buffer;

m_buffer=NULL;

}

 

 

 

 

 

 

 

bool Stack::Push(int value)

{

if(m_top>=m_size-1) return false;

m_buffer[++m_top]=value;

return true;

}

 

bool Stack::Pop(int& value)

{

if(m_top<0) return false;

value=m_buffer[m_top--];

return true;

}

 

int main()

{

stack s1;

s1.Initialize(5);

while(s1.Push(rand()%100));

cout<<“s1에 저장된 데이터 : ”;

for(int 1=0;i<s1.m_size;i++)

cout<<s1.m_buffer[i]<<“ ”;

cout<<“\n”;

cout<<“s1에서 꺼낸 데이터 : ”;

int data;

while(s1.Pop(data))

cout<<data<<“ ”;

cout<<“\n”;

 

system(“pause”);

return 0;

}