C/C++使用模板实现数据栈
来源:互联网 发布:python caffe 命令行 编辑:程序博客网 时间:2024/05/18 01:56
// 栈.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#define MAXSIZE 100
using namespace std;
/*
栈的基本运算
1. 栈的初始化 (initStack)
2. 判断栈是否为空(isEmpty)
3.入栈 (push)
4.出栈 (pop)
5.读取栈元素 (GetTop)
*/
template <typename T>
class Stack
{
public:
friend ostream& operator<<(ostream& os, Stack<T>& rhs)
{
for (int i = 0; i < rhs.Count; i++)
{
os << rhs.Top[i] << " ";
}
os << endl;
return os;
}
Stack();
Stack(unsigned long EmeCount);
~Stack();
bool IsEmpty();
bool Push(T Element);
bool Pop();
T GetTop();
private:
bool InitStack(unsigned long EmelentCount);
T* Top;
int Count;
};
template <typename T>
bool Stack<T>::InitStack(unsigned long EmelentCount)
{
Top = (T*)malloc(sizeof(T)*EmelentCount);
if (Top == NULL)
return false;
return true;
}
template <typename T>
Stack<T>::Stack()
{
if (!InitStack(MAXSIZE))
cout << "栈初始化失败!" << endl;
Count = 0;
}
template <typename T>
Stack<T>::Stack(unsigned long ElementCount)
{
if (!InitStack(ElementCount))
cout << "栈初始化失败!" << endl;
Count = 0;
}
template <typename T>
bool Stack<T>::Push(T Element)
{
Count++;
for (int i = Count; i > 0; i--)
{
Top[i] = Top[i - 1];
}
Top[0] = Element;
return true;
}
template <typename T>
T Stack<T>::GetTop()
{
return Top[0];
}
template <typename T>
bool Stack<T>::Pop()
{
for (int i = 0; i < Count; i++)
{
Top[i] = Top[i + 1];
}
Count--;
return true;
}
template <typename T>
bool Stack<T>::IsEmpty()
{
if (Count <= 0)
return true;
else
return false;
}
template <typename T>
Stack<T>::~Stack()
{
}
int _tmain(int argc, _TCHAR* argv[]) 9
{
Stack<int> binary;
int Num = 0;
cout << "请输入你想要转换的数字" << endl;
cin >> Num;
while (Num)
{
binary.Push(Num % 2);
Num = Num / 2;
}
cout << binary << endl;
system("pause");
return 0;
}
//
#include "stdafx.h"
#include <iostream>
#define MAXSIZE 100
using namespace std;
/*
栈的基本运算
1. 栈的初始化 (initStack)
2. 判断栈是否为空(isEmpty)
3.入栈 (push)
4.出栈 (pop)
5.读取栈元素 (GetTop)
*/
template <typename T>
class Stack
{
public:
friend ostream& operator<<(ostream& os, Stack<T>& rhs)
{
for (int i = 0; i < rhs.Count; i++)
{
os << rhs.Top[i] << " ";
}
os << endl;
return os;
}
Stack();
Stack(unsigned long EmeCount);
~Stack();
bool IsEmpty();
bool Push(T Element);
bool Pop();
T GetTop();
private:
bool InitStack(unsigned long EmelentCount);
T* Top;
int Count;
};
template <typename T>
bool Stack<T>::InitStack(unsigned long EmelentCount)
{
Top = (T*)malloc(sizeof(T)*EmelentCount);
if (Top == NULL)
return false;
return true;
}
template <typename T>
Stack<T>::Stack()
{
if (!InitStack(MAXSIZE))
cout << "栈初始化失败!" << endl;
Count = 0;
}
template <typename T>
Stack<T>::Stack(unsigned long ElementCount)
{
if (!InitStack(ElementCount))
cout << "栈初始化失败!" << endl;
Count = 0;
}
template <typename T>
bool Stack<T>::Push(T Element)
{
Count++;
for (int i = Count; i > 0; i--)
{
Top[i] = Top[i - 1];
}
Top[0] = Element;
return true;
}
template <typename T>
T Stack<T>::GetTop()
{
return Top[0];
}
template <typename T>
bool Stack<T>::Pop()
{
for (int i = 0; i < Count; i++)
{
Top[i] = Top[i + 1];
}
Count--;
return true;
}
template <typename T>
bool Stack<T>::IsEmpty()
{
if (Count <= 0)
return true;
else
return false;
}
template <typename T>
Stack<T>::~Stack()
{
}
int _tmain(int argc, _TCHAR* argv[]) 9
{
Stack<int> binary;
int Num = 0;
cout << "请输入你想要转换的数字" << endl;
cin >> Num;
while (Num)
{
binary.Push(Num % 2);
Num = Num / 2;
}
cout << binary << endl;
system("pause");
return 0;
}
0 0
- C/C++使用模板实现数据栈
- [c++]栈模板的实现
- C++-使用模板解决问题
- C++:栈(stack)的模板类实现
- 【C++】使用指针实现数据交换
- C++,MD5算法的实现类,使用了模板技巧
- C语言实现stack模板
- C语言实现函数模板
- c++::用模板实现List
- 【c++】模板实现动态Vector
- 数据结构(c++)栈的模板类(使用链表实现)
- C++_class Template about Stack(使用类模板实现栈操作)
- C++ 模板使用介绍
- C++set模板的使用
- C++_模板举例_使用模板实现集合类(堆栈)
- C++_模板举例_使用模板实现集合类(堆栈)
- c语言模拟实现栈的模板化
- [数据结构]栈的实现(c++/类模板)
- html中动态的增加和删除item
- 在windows中,如何实现一个自己的浏览器
- html中如何实现自己的音乐播放器
- CSS弹性盒子Flexbox布局详解
- fd 用文件方式打开
- C/C++使用模板实现数据栈
- Can't connect to MySQL server on ‘localhost’ (10061)
- 媒体播放器探索之IMediaObject
- 移动端web开发技巧
- C/C++系列之学生管理系统
- windows内核编程之手动提升进程权限
- 移动HTML5前端性能优化指南
- MyBatis学习记录(1):牛刀小试,入门程序
- LeetCode 39. Combination Sum