心动C++情牵栈

 

数组版本:

/*--------------------------------------------------------------------
 * 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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值