zoukankan      html  css  js  c++  java
  • 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;
    }
    

     

  • 相关阅读:
    Android TabActivity中onKeyDown无效问题
    原生App切图的那些事儿
    android评分条RatingBar自定义设置
    android 软键盘自动弹出和关闭
    android 编程之 PopupWindow 窗口的弹出
    使用Android拨打电话功能
    实景三维系列1 | 倾斜摄影发展历程
    规划设计系列4 | 盘活电脑里的规划方案,想看就看
    规划设计系列3 | SketchUp+实景三维,方案现状一起看
    规划设计系列2 | 项目沟通非小事,实景三维帮大忙
  • 原文地址:https://www.cnblogs.com/KennyRom/p/5892773.html
Copyright © 2011-2022 走看看