数组版本:
/*--------------------------------------------------------------------
* Project: Stack.h
* Name: zwp
* Date: Do it myself
*---------------------------------------------------------------------*/
#ifndef STACK_H_
#define STACK_H_
#include <iostream>
#include <new>
#define SIZE 100 //栈的大小
typedef int StackType; //栈元素的类型
class Stack
{
private:
StackType number[SIZE];
StackType size;
public:
Stack(int si = 0);
bool empty(void);
bool full(void);
void push(StackType value);
StackType top(void)const;
void pop(void);
friend std::ostream& operator<<(std::ostream& os, const Stack& stack);
friend std::istream& operator>>(std::istream& is, Stack& stack);
};
#endif
/*----------------------------------------------------------------------
* Project: Stack.cpp
* Name: zwp
* Date: 2013.4
*----------------------------------------------------------------------*/
#include "Stack.h"
Stack::Stack(int siz)
{
size = siz;
}
bool Stack::empty(void)
{
return (size == 0);
}
bool Stack::full(void)
{
if(size == SIZE-1)
return true;
else
return false;
}
void Stack::push(StackType value)
{
if(!full())
{
number[size] = value;
size++;
}
else
std::cerr <<"The stack is full..."<<std::endl;
}
StackType Stack::top(void)const
{
return (number[size]);
}
void Stack::pop(void)
{
if(!empty())
size--;
}
std::ostream& operator<<(std::ostream& os, const Stack& stack)
{
for(int index = stack.size - 1; index >= 0; index --)
os <<stack.number[index]<<" ";
return os;
}
std::istream& operator>>(std::istream& is, Stack& stack)
{
std::cout <<"Please input a number: "<<std::endl;
is >>stack.number[stack.size];
stack.size++;
return is;
}
/*----------------------------------------------------------------
* Project: Test.cpp
* Name: zwp
* Date: This is myself stack
*----------------------------------------------------------------*/
#include "Stack.h"
int main(void)
{
Stack stack;
for(int index = 0; index < 5; index ++)
{
std::cout <<"Input a number to stack..."<<std::endl;
std::cin >>stack;
}
std::cout <<stack<<std::endl;
system("pause");
return 0;
}
动态内存分配版本:
/*--------------------------------------------------------------------------------
* Project: Stack.h
* Name: zwp
* Date: 2013.4
*--------------------------------------------------------------------------------*/
#ifndef STACK_H_
#define STACK_H_
#include <iostream>
typedef int StackType; // 栈元素类型
class Stack
{
private:
StackType* myArray;
StackType capacity;
StackType size;
public:
Stack();
Stack(int cap = 0, int si = 0);
bool empty(void);
bool full(void);
void push(StackType value);
StackType top(void);
void pop(void);
Stack(const Stack& stack);
const Stack& operator=(const Stack& stack);
friend std::ostream& operator<<(std::ostream& os, const Stack& stack);
friend std::istream& operator>>(std::istream& is, Stack& stack);
};
#endif
/*-----------------------------------------------------------------------------
* Project: Stack.cpp
* Name: zwp
* Date: 2013.4
*-----------------------------------------------------------------------------*/
#include "Stack.h"
#include <new>
Stack::Stack()
:size(0),capacity(0),myArray(0)
{ }
Stack::Stack(int cap, int si)
:size(si)
{
myArray = new StackType[cap];
}
bool Stack::empty(void)
{
return (size == capacity);
}
bool Stack::full(void)
{
StackType* newValue = new StackType; // dynamic memory allocated
return (newValue == 0);
}
void Stack::push(StackType value)
{
if(!full())
{
myArray[size] = value;
size++;
}
else
{
std::cerr <<"The Stack overflow..."<<std::endl;
}
}
StackType Stack::top(void)
{
if(!empty())
return (myArray[size-1]);
else
std::cerr <<"The Stack is empty..."<<std::endl;
}
void Stack::pop(void)
{
if(!empty())
size--;
else
std::cout <<"The Stack is empty..."<<std::endl;
}
Stack::Stack(const Stack& stack)
{
if(capacity < stack.capacity)
{
delete []myArray;
size = stack.size;
myArray = new StackType[size]; // Dynamic memory allocated
if(myArray == 0)
std::cerr <<"The allocated is fail..."<<std::endl;
else
for(int index = 0; index < size; index ++)
myArray[index] = stack.myArray[index];
}
else
for(int index = 0; index < stack.size; index ++)
myArray[index] = stack.myArray[index];
}
const Stack& Stack::operator=(const Stack& stack)
{
if(this != &stack) // 检查是不是自赋值
{
if(size < stack.size)
{
delete []myArray;
size = stack.size;
myArray = new StackType[size]; // Dynamic memory allocated
if(myArray == 0)
std::cerr <<"The allocated is fail..."<<std::endl;
else
for(int index = 0; index < size; index ++)
myArray[index] = stack.myArray[index];
}
else
for(int index = 0; index < stack.size; index ++)
myArray[index] = stack.myArray[index];
}
else
std::cerr <<"copy myself is error..."<<std::endl;
return *this;
}
std::ostream& operator<<(std::ostream& os, const Stack& stack)
{
for(int index = stack.size-1; index >= 0; index --)
os <<stack.myArray[index]<<" ";
return os;
}
std::istream& operator>>(std::istream& is, Stack& stack)
{
std::cout <<"Please input some numbers we will add to stack.."<<std::endl;
is >>stack.myArray[stack.size];
stack.size++;
return is;
}
/*---------------------------------------------------------------------
* Project: Test.cpp
* Name: zwp
* Date: 2013.4
*---------------------------------------------------------------------*/
#include <iostream>
#include "Stack.h"
int main(void)
{
Stack stack(10);
Stack stac(20);
for(int index = 0; index < 10; index ++)
stack.push(index);
std::cout <<stack<<std::endl;
for(int index = 0; index < 20; index ++)
stac.push(index*index);
std::cout <<stac<<std::endl;
stack = stac;
std::cout <<stack<<std::endl;
std::cout <<"stack.top(): "<<stack.top()<<std::endl;
system("pause");
return 0;
}