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;
}

 

posted @ 2016-09-21 15:24  KennyRom  阅读(228)  评论(0编辑  收藏  举报