栈和队列

#include <iostream>
#include <string>
using namespace std;

typedef string T;

class Stack
{
	T a[5];
	int cur;
public:
	Stack():cur(0)	{}
	void push(const T& d)throw(const char*);	//数据入栈成为栈顶
	T pop()throw(const char*);					//栈顶数据出栈,下一个数据成为栈顶
	const T& top()const throw(const char*);		//取得栈顶数据
	bool empty()const		//是否空栈
	{
		return cur==0;
	}
	bool full()const		//是否已满
	{
		return cur==5;
	}
	void clear()			//栈清空(复位)
	{
		cur=0;
	}
	int size()const			//栈中数据个数
	{
		return cur;
	}
};

void Stack::push(const T& d)throw(const char*)
{
	if(full()) 
		throw "满";
	else
	a[cur++] = d;
}
T Stack::pop()throw(const char*)
{
	if(empty()) 
		throw "空";
	else
	return a[--cur];
}
const T& Stack::top()const throw(const char*)
{
	if(empty()) 
		throw "空";
	else
	return a[cur-1];
}

int main()
{
	Stack s;
	try
	{
	s.push("芙蓉");
	s.push("凤姐");
	s.push("春哥");
	s.push("曾哥");
	s.push("权哥");
	s.push("犀利");
	}
	catch(const char* e)
	{
		cout << "异常:" << e << endl;
	}
	while(!s.empty())
	{
		cout << s.pop() << endl;
	}
	return 0;
}

#include <iostream>
using namespace std;

typedef int T;

class Queue
{
	T a[5];
	int b, n;		//队首位置和有效元素个数
public:
	Queue():b(0),n(0)	{}
	Queue& push(const T& d)
	{
		if(full()) 
			throw "满";
		else
		a[(b+n++)%5] = d;
		return *this;
	}
	T pop()
	{
		if(empty()) 
			throw "空";
		else
		--n;
		return a[b++%5];
	}
	const T& front()const
	{
		return a[b%5];
	}
	const T& back()const
	{
		return a[(b+n-1)%5];
	}
	int size()const
	{
		return n;
	}
	void clear()
	{
		b=0, n=0;
	}
	bool empty()const
	{
		return n==0;
	}
	bool full()const
	{
		return n==5;
	}
};

int main()
{
	Queue q;
	try
	{
	q.push(1).push(2).push(3);
	q.push(4).push(5);
	cout << q.pop() << endl;
	cout << q.pop() << endl;
	q.push(6).push(7).push(8);
	}
	catch(const char* e)
	{
		cout << "异常:" << e << endl;
	}
	while(!q.empty())
		cout << q.pop() << endl;
	return 0;
}

#include <iostream>
using namespace std;

const int maxSize=100;
const int ERROR=-1;

class stack
{
private:
	int arr[maxSize];       //栈的最大空间
	int top;                //栈顶指针
public:
    stack();
	~stack();
	void initStack();       //清空栈
	int stackEmpty();       //判断栈是否空,空则返回1
	int stackFull();        //判断栈是否满,满则返回1
	void push(int item);    //将item压入栈
	int pop();              //出栈
	int getTop();           //取栈顶元素
	void print();           //打印栈
};

stack::stack()
{
   top=0;
}

stack::~stack()
{}

void stack::initStack()
{
   top=0;
}

int stack::stackEmpty()
{
   return top==0;          //top等于0时,栈空
}

int stack::stackFull()
{
   return top==maxSize;    //top等于maxSize时,栈满
}

void stack::push(int item)
{
    if(stackFull())
	{
	  cout<<"堆栈已满,不可入栈"<<endl;
	  exit(ERROR);
	}
	arr[top++]=item;       //入栈top++;
}

int stack::pop()
{   
    if(stackEmpty())
	{
	  cout<<"堆栈已空,不可出栈"<<endl;
	  exit(ERROR);
	}
	return arr[--top];     //出栈top--;
}

int stack::getTop()
{
	  int pos;
      if(stackEmpty())
	{
	  cout<<"堆栈已空,没有栈顶元素"<<endl;
	  exit(ERROR);
	}
      pos=top-1;
	  return arr[pos];
}

void stack::print()
{
  for(int j=0;j<top;j++)
	  cout<<arr[j]<<" ";
  cout<<endl;
}


int main()
{
   stack myStack;
   int len,i,temp,item;
   cin>>len;
   for(i=0;i<len;i++)
   {
	   cin>>temp;
	   myStack.push(temp);
   }
   myStack.print();
   cin>>item;
   myStack.push(item);
   myStack.print();
   myStack.pop();
   myStack.print();
   cout<<myStack.getTop();
   cout<<endl;
   myStack.initStack();
   cin>>item;
   myStack.push(item);
   myStack.print();
   return 0;
}

栈和队列是运算受限的线性表。

栈(后进先出LIFO)
进行插入和删除的一端是浮动的,称为栈顶。
另一端是固定的,称为栈底
当栈中没有元素时称为空栈
栈的插入操作称为进栈
栈的删除操作称为出栈

栈的基本操作(一般使用顺序栈):
建栈、判断栈满或栈空、进栈、出栈、读取栈顶元素

栈的用途:
1、调用函数或子程序
2、递归运算
3、保护现场和恢复现场
4、简化程序设计

可用数组来实现顺序栈。

使用栈顶指针top来动态地指示栈顶元素在顺序栈的位置。

栈顶指针指向实际栈顶元素后的空位置,初始值为0。

top=0,栈空,此时出栈,则下溢。
(正常现象,程序控制转移的条件)
top=n,栈满,此时入栈,则上溢。
(出错状态)



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值