关闭

数据结构探险—栈篇

标签:
190人阅读 评论(0) 收藏 举报
分类:

慕课网、、学习。

栈是一种机制,后进先出 LIFO: last in first out

栈的原理:


r任务

MyStack.h

#ifndef MYSTACK_H
#define MYSTACK_H


template<typename T>
class MyStack
{public:
	MyStack (int size);
	~MyStack ();
	bool stackEmpty ();
	bool stackFull ();
	void clearStack ();
	int stackLength ();    //已有元素个数
	bool push (T elem);  //元素入栈、栈顶上升
	bool pop (T &elem);   //出栈、栈顶下降
	void stackTraverse (bool isFromButtom);
private:
	T *m_pBuffer;  //指向栈空间
	int m_iSize;            //栈容量
	int m_iTop;  //栈顶、栈中元素个数


};
//#include<iostream>
//#include"stdlib.h"
//#include"MyStack.h"
//using namespace std;
template<typename T>
MyStack<T>::MyStack (int size)
{
	m_iSize = size;
	m_pBuffer = new T[size];
	m_iTop = 0;


}
template<typename T>
MyStack<T>::~MyStack ()
{
	delete[]m_pBuffer;
}
template<typename T>
bool MyStack<T>::stackEmpty ()
{
	if ( 0 == m_iTop )
	{
		return true;
	}
	return false;
}
template<typename T>
bool MyStack<T>::stackFull ()
{
	if ( m_iTop == m_iSize )
	{
		return true;


	}
	return false;
}
template<typename T>
void MyStack<T>::clearStack ()
{
	m_iTop = 0;
}
template<typename T>
int MyStack<T>::stackLength ()    //已有元素个数
{
	return m_iTop;
}
template<typename T>
bool MyStack<T>::push (T elem)  //元素入栈、栈顶上升
{
	if ( stackFull () )
	{
		return false;
	}
	m_pBuffer[m_iTop] = elem;
	m_iTop++;
	return true;
}


//char MyStack::pop ()   //出栈、栈顶下降 但出栈是char类型的、没有参数
//{
//	if ( stackEmpty () )
//	{
//		throw 1;
//	}
//	else
//	{
//		m_iTop--;
//	return m_pBuffer[m_iTop];
//	}
//	
//}
template<typename T>
bool MyStack<T>::pop (T &elem)   //出栈、栈顶下降 参数使用的是引用、就是说之后用这个函数的时候可以使用变量
{
	if ( stackEmpty () )
	{
		return false;
	}
	m_iTop--;
	elem = m_pBuffer[m_iTop];
	return true;
}
template<typename T>
void  MyStack<T>::stackTraverse (bool isFromButtom)//有一些是写了visit()函数指针作为参数,去访问复杂的栈中元素
{
	if ( isFromButtom )
	{
		for ( int i = 0; i < m_iTop; i++ )
		{
			cout << m_pBuffer[i];
			//m_pBuffer[i].printCoordinate ();
		}
	}
	else
	{
		for ( int i = m_iTop - 1; i >= 0; i-- )
		{
			cout << m_pBuffer[i];
			//m_pBuffer[i].printCoordinate ();
		}
	}


}
#endif
<pre name="code" class="cpp">Coordinate.h


#ifndef COORDINATE_H
#define COORDINATE_H
#include<ostream>
using namespace std;
class Coordinate
{
	friend ostream &operator<<(ostream &out, Coordinate &coor);
public:
	Coordinate (int x=0,int y=0);
	void printCoordinate ();
	
public:
	int m_iX;
	int m_iY;
};
#endif

Coordinate.cpp


#include"Coordinate.h"
#include"iostream"
using namespace std;
Coordinate::Coordinate (int x, int y)
{
	m_iX = x;
	m_iY = y;
	
}
void Coordinate::printCoordinate ()
{
	cout << "{" << m_iX << "," << m_iY<<"}" << endl;
}
ostream &operator<<(ostream &out, Coordinate &coor)
{
	out << "(" << coor.m_iX << ","<< coor. m_iY << ")" << endl;
	return out;
}
实例一


#define BINARY   2
#define OCTONARY 8
#define HEXADECIMAL 16
#include<iostream>
#include"stdlib.h"
#include"MyStack.h"
#include"Coordinate.h"
using namespace std;
int main (void)
{
	char num[] = "0123456789ABCDEF";  //因为十六进制有ABCDEF所以要想办法、num的索引10正好对应元素A
	MyStack<int> *pStaxk = new MyStack<int> (30);
	int N = 10;
	int mod = 0;
	while ( N != 0 )
	{
		mod = N% HEXADECIMAL;
		pStaxk->push (mod);//取余的数依次放入栈中
		N = N / HEXADECIMAL;  //得到下一个除完的数               //十进制转换成二进制十二进制十六进制
	}
	int elem = 0;
	while ( !pStaxk->stackEmpty () ) 只要栈不为空就输出
		
	{
		pStaxk->pop (elem); 把栈里面的取余得到的是依次出栈付给elem
		cout<<num[elem];  输出对应的元素
	}
	

	
	delete pStaxk;
	pStaxk = NULL;
	system ("pause");
		return 0;
}

实例二;


demo.cpp

include<iostream>
#include"stdlib.h"
#include"MyStack.h"
#include"Coordinate.h"
using namespace std;
int main (void)
{
	MyStack<char> *pStaxk = new MyStack<char> (30); 
	MyStack<char> *pneedStaxk = new MyStack<char> (30);
	char str[] = "[()]]";
	char currentNeed = 0;  //当前急需要匹配的括号
	for ( int i = 0; i < strlen (str); i++ )
	{
		if ( str[i] != currentNeed ) 如果当前括号与急需匹配的括号不匹配,就把当前括号压栈,并改变相应的急需括号
		{
			pStaxk->push (str[i]);
			switch ( str[i] )
			{
			case'[':
					if ( currentNeed != 0 )
					{
						pneedStaxk->push (currentNeed);//<span style="font-family: Arial, Helvetica, sans-serif;">改变相应的急需括号之前要把一开始的急需括号压入需匹配括号栈中</span>

					}
					currentNeed = ']';
						break;
			case'(':
						if ( currentNeed != 0 )
						{
							pneedStaxk->push (currentNeed);
						}
						currentNeed = ')';
							break;
			default:
				cout << "字符串括号不匹配" << endl; //不是上述情况、比如出现’]’则直接判断出是不匹配的
				
				system ("pause");
				return 0;
			}
		}
		else   // 如果正好与需匹配的括号匹配了,那么进行出栈
		{
			char elem;
			pStaxk->pop (elem);
			if ( !(pneedStaxk->pop (currentNeed) ))//出栈不成功,即里面没有值
			{
				currentNeed = 0;
			}
		}
	}
	if ( pStaxk->stackEmpty () ) //依次出栈成功了,栈为空,匹配成功
	{
		cout << "字符串括号匹配" << endl;
	}
	else
	{
		cout << "字符串括号不匹配" << endl;
	}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:14339次
    • 积分:366
    • 等级:
    • 排名:千里之外
    • 原创:20篇
    • 转载:2篇
    • 译文:1篇
    • 评论:14条
    文章分类
    最新评论