| :: [intv] Stack implementation in C++ :: | ||||
| HOME |
|
[Date Prev][Date Next][Date Index] [intv] Stack implementation in C++--Makefile-- CC=g++ COPTS = -g DEPENDS= stack.o CFLAGS= $(COPTS) $(DEFINES) all: stack main stack: stack.cpp stack.h $(CC) $(CFLAGS) -c $@.cpp $(LIBS) main: stack $(CC) $(CFLAGS) -o $@ $@.cpp $(DEPENDS) clean: rm *.o *~ main --Makefile--
const int MAXSTACKSIZE = 4; class stack{
private:
int stacklist[MAXSTACKSIZE];
int top;
public:
stack(void);
void push(int Item);
int pop(void);
int peek(void);
int isEmpty();
int isFull();
};#endif --stack.h-- --stack.cpp-- #include <iostream> #include "stack.h" using namespace std; stack::stack(){
top = -1;
}void stack::push(int Item){
if (!isFull()){
top++;
stacklist[top] = Item;
}else{
cout << "Stack Overflow" << endl;
}
}int stack::pop(void){
if (!isEmpty()){
int tem;
tem = stacklist[top];
top--;
return tem;
}else{
cout << "Stack Underflow" << endl;
}
}int stack::peek(void){
if (!isEmpty()){
return stacklist[top];
}else{
cout << "Attempt to peek into an empty stack" << endl;
}
}int stack::isEmpty(void){
if (top == -1) return 1; else return 0;
}int stack::isFull(void){
if (top == (MAXSTACKSIZE-1)) return 1; else return 0;
}
--stack.cpp----main.cpp-- //Problem: Stack implementation #include <iostream> #include "stack.h" using namespace std; int main(){
stack S;
S.push(1);
S.push(2);
S.push(3);
S.push(4);
cout << S.peek() <<endl;
cout << S.pop() << endl;
cout << S.pop() << endl;
|