数据结构 栈的基本操作

将一个十进制的数转化为二进制后出入栈中,然后依次出栈得到转化后的二进制数;涉及操作为:入栈,出栈,取栈顶元素,以下为顺序栈

Stack.h

#pragma once
const int MaxSize = 100;
template<class T>
class Stack {
public:
	Stack();
	~Stack(){}
	void Push(T x);
	T GetTop();
	T ChuTop();
	bool Empty();

private:
	T data[MaxSize];
	int top;

};

Stack.cpp

#include"Stack.h"
template<class T>
Stack<T>::Stack()
{
	top = -1;
}
template<class T>
void Stack<T>::Push(T x)
{
	if (top == MaxSize - 1)throw "上溢";
	top++;
	data[top] = x;
 }
template<class T>
T Stack<T>::ChuTop()
{
	if (top != -1)
		return data[top];
}
template<class T>
T Stack <T>::GetTop()
{
	T x;
	if (top == -1)throw "下溢";
	x = data[top--];
	return x;
}
template<class T>
bool Stack<T>::Empty()
{
	if (top == -1)
		return 1;
	else
		return 0;
}

main.cpp

#include"Stack.cpp"
#include<iostream>
using namespace std;
int j = 0;
void main()
{
	Stack<int> a;      //创建模板类的实例
	if (a.Empty()) 
	{
		int  c, d, e;
		cout << "请输入一个十进制的数字:";
		cin >> d;
		try
		{
			cout << "将十进制的数转化成二进制的数的入栈顺序为:";
			while (d != 0)            //将十进制数依次对二取余,将余数入栈
			{
				e = d % 2;
				c = d / 2;
				d = c;
				j++;
				a.Push(e);

			}
			cout << endl;
		}

		catch (char* wrong)
		{
			cout << wrong;
		}
		cout << "将从键盘输入的数转换成二进制并执行入栈操作:" << endl;
		cout << "转换后的二进制数执行出栈操作:" << endl;
		cout << "二进制数为:" << endl;
		for (int m = 1; m <= j; m++)   
		{
			cout << a.ChuTop();          //执行出栈操作,即得到转换后的二进制数;
		}
	}
	else {
		cout << "栈不空" << endl;
	}
	system("pause");
}

链栈实现

LinkStack.h

#pragma once
template <class T>
struct Node
{
	T data;
	Node<T> *next;  //此处<T>也可以省略
};

template<class T>
class LinkStack {
public:
	LinkStack();/* {
		top = NULL;
	}*/
	~LinkStack()
	{}
	void Push(T x);
	T Pop();
	T GetTop();
	bool Empty();
private:
	Node<T> *top;
};

LinkStack.cpp
#include"LinkStack.h"

template<class T>
LinkStack<T>::LinkStack()
{
	top = new Node<T>; top = 0;
}

template<class T>
void LinkStack<T>::Push(T x)
{
	Node<T> *s;
	s = new Node<T>;
	s->data = x;
	s->next = top;
	top = s;
}

template<class T>
T LinkStack<T>::Pop()
{
	T x;
	Node<T> *p;
	if (top == 0)throw "下溢";
	x = top->data;
	p = top;
	top = top->next;
	delete p;
	return x;
}

template<class T>
T  LinkStack<T>::GetTop()
{
	if (!top ==0)
		return top->data;
}

template<class T>
bool LinkStack<T>::Empty()
{
	if (top == 0)
		return 1;
	else return 0;

}
LinkStackMain.cpp
#include"LinkStack.cpp"
#include<iostream>
using namespace std;
int j = 0;
void main()
{
	LinkStack <int> a;

	if (a.Empty()) {
		int  c, d, e;
		cout << "请输入一个十进制的数字:";
		cin >> d;
		try
		{
			cout << "将十进制的数转化成二进制的数的入栈顺序为:";
			while (d != 0)            //将十进制数依次对二取余,将余数入栈
			{
				e = d % 2;
				c = d / 2;
				d = c;
				j++;
				a.Push(e);
			}
			cout << endl;
		}

		catch (char* wrong)
		{
			cout << wrong;
		}
		cout << "将从键盘输入的数转换成二进制并执行入栈操作:" << endl;
		cout << "二进制数为:" << endl;
		for (int m = 1; m <= j; m++)   //j为转换为二进制后的位数
		{
			cout << a.Pop();          //执行出栈操作,即得到转换后的二进制数;
		}	
	}
	else {

		cout << "栈不空" << endl;
	}
	system("pause");

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值