배우고픈 공돌이

C++ stack! 본문

C, C++ /C++

C++ stack!

내 마음 아홉수 2017. 10. 16. 16:17

stack.h


#ifndef STACK_H

#define STACK_H


class Stack{

private: //information hiding

int* pArr;

int  tos;

int  size;

public:

Stack(int size); //void initStack(int size);

~Stack(); //void cleanupStack();

void push(int data);

int pop();

};


#endif



stack.cpp


#include "stack.h"

#include <cassert>


Stack::Stack(int size)

{

//this-> pArr = (int*) malloc(sizeof(int)*size);

this-> pArr = new int[size];

assert( this->pArr );


this-> size = size;

this-> tos = 0;

}


Stack::~Stack()

{

//free( this->pArr );

delete [] this->pArr;

}


void Stack::push(int data)

{

assert( this->tos != this->size);

this->pArr[(this->tos)++] = data;

}


int Stack::pop()

{

assert( this->tos != 0 );

return this->pArr[--(this->tos)];

}



'C, C++ > C++' 카테고리의 다른 글

c++ reference!  (0) 2017.10.16
C++ linked list!  (0) 2017.10.16
C++ Queue!  (0) 2017.10.16
C를 C++로 변경하기. (2)  (0) 2017.10.12
C를 C++로 변경하기.  (0) 2017.10.12
Comments