Stack

1、Definition

A stack is a list with restriction that insertions and deletions can be performed in only one position, namely, the end of the list, called the pop.

                                               

                                          last in first out

 

2、Abstract data type

ADT Stack
Data
   ...
Operation:
  InitStack                      //初始化空栈
      Post-condition: None;

  EmptyStack(b)             //判空栈
      Output: b:boolean;
      Post-condition: Return True if stack is empty, else return False;

  Pop(e)
       Output: Top element e of stack;
       Pre-condition: Stack is not empty;
       Pos-condition: Remove top element from stack;

  Top(e)
       Output: Top element e of stack;
       Pre-condition: Stack is not empty;
       Pos-condition: Return top element from stack;
   
  Push(e)
       Input: e:ElementType;
       Post-condition: Add element e at the top of stack;

END ADT  

Example:

If we input n alphabets into the stack, how many types can we get from the stack?

3、Implementation

①Linked stack

#include<iostream>
using namespace std;

//创建链表结点
struct Node
{
  int info;  //先假设储存数据为整形,也可以是其他不同的类型
  Node* link;
};

//创建链表栈
struct LinkStack
{
   Node* top;
};

//创建一个空栈
LinkStack*create()
{
   LinkStack *stack=new LinkStack;
   if(stack==NULL)
      cout<<"分配失败"<<endl;
   else
     stack->top=NULL;
   return stack;
};

//判断栈是否为空
bool isEmpty(LinkStack* stack)
{
    return(stack->top==NULL);
}

//入栈
void push(LinkStack*stack, int  x)
{
  Node* p=new Node;
  if(p==NULL)
    cout<<"入栈失败"<<endl;
  else
    {
         p->info=x;
         p->link=stack->top;
         stack->top=p;
    }
}

//出栈
void pop(LinkStack*stack)
{
   if(isEmpty(stack))
       cout << "空栈" <<endl;
  else
    {
       Node* p;
       p=stack->top;
       stack->top=stack->top->link;
       delete p;
    }
}

//输出栈顶元素
void top(LinkStack*stack)
{
   if(isEmpty())
    cout<<"空栈"<<endl;
   else
    {
       cout<<stack->top->info<<endl;
    }
}

②Array stack

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

const int STACK_SIZE=50;

tydef struct seqStack
{
  int data[STACK_SIZE];
  int top;
}stack;

//创建空栈
void InitStack(stack*s)
{
  s->top=-1;
}

//判断栈是否满
bool isFull(stack* s)
{
  return (s->top==STACK_SIZE-1);
}

//判空
bool isEmpty(stack*s)
{
   return (s->top==-1);
}

//入栈
void push(stack*s , int n)
{
   assert(!isFull(s));
   s->top++;   
   s->data[s->top]=n;
}

//出栈
void pop(stack*s)
{
  assert(!isEmpty(s));
  s->top--;
}

//输出栈顶元素
void top(stack*s)
{
  assert(!isEmpty(s)); 
  cout<<s->data[s->top];
}

C++style code

#include<iostream>
using namespace std;

const int size = 50;

//C++ style array stack
template<typename ElementType>
class ArrayStack
{
private:
	int top;
	ElementType arr[size];
public:
	ArrayStack()
	{
		top = -1;
	}
	bool empty()const;
	bool full()const;
	void pop();
	void push(ElementType item);
	void top();
};

template<typename ElementType>
bool ArrayStack<ElementType>::empty()const
{
	if (top == -1)
		return true;
	return false;
}

template<typename ElementType>
bool ArrayStack<ElementType>::full()const
{
	if (top == size - 1)
		return true;
	return false;
}

template<typename ElementType>
void ArrayStack<ElementType>::pop()
{
	if (!empty())
		top--;
	else
		cout << "空栈" << endl;
}

template<typename ElementType>
void ArrayStack<ElementType>::push(ElementType item)
{
	if (!full())
	{
		top++;
		arr[top] = item;
	}
	else
		cout << "满栈" << endl;
}

template<typename ElementType>
void ArrayStack<ElementType>::top()
{
	if (!empty())
	    cout << arr[top] << endl;
}

利用数组实现双端双栈

#include<iostream>
using namespace std;

//数组大小
const int size = 50;

//数组双端栈
template<typename ElementType>
class DoubleStack
{

	//data
private:
	int frontTop;
	int backTop;
	ElementType arr[size];

	//opeartion
public:
	DoubleStack()
	{
		frontTop = -1;
		backTop = size;
	}
	bool empty()const;
	bool full()const;
	void frontPush(ElementType item);
	void backPush(ElementType item);
	void frontPop();
	void backPop();
	void display()const;
};

template<typename ElementType>
bool DoubleStack<ElementType>::empty()const
{
	if (frontTop == -1 && backTop == size)
		return true;
	return false;
}

template<typename ElementType>
bool DoubleStack<ElementType>::full()const
{
	if ((frontTop + 1) == backTop)
		return true;
	return false;
}

template<typename ElementType>
void DoubleStack<ElementType>::frontPush(ElementType item)
{
	if (full())
		cout << "满栈" << endl;
	else
	{
		arr[frontTop + 1] = item;
		frontTop++;
	}
}

template<typename ElementType>
void DoubleStack<ElementType>::backPush(ElementType item)
{
	if (full())
		cout << "满栈" << endl;
	else
	{
		arr[backTop-1] = item;
		backTop--;
	}
}

template<typename ElementType>
void DoubleStack<ElementType>::frontPop()
{
	if (!empty())
		frontTop -= 1;
}

template<typename ElementType>
void DoubleStack<ElementType>::backPop()
{
	if (!empty())
		backTop += 1;
}

template<typename ElementType>
void DoubleStack<ElementType>::display()const
{
	if (!empty())
	{
		cout << "前端栈" << endl;
		for (int i = frontTop; i != -1; i--)
			cout << arr[i] << endl;
		cout << "后端栈" << endl;
		for (int i = backTop; i != size; i++)
			cout << arr[i] << endl;
	}
	else
		cout << "空栈" << endl;
}

 

转载于:https://www.cnblogs.com/KennyRom/p/5892773.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值