7.1 application stack

Stack is a kind of LIFO data structure.

Generally have a stack pointer, a stack element of the uppermost point,

When implemented with an array of the stack, the stack pointer is a variable of type int, when no element is referred to as -1, 0 is the first element, denoted by TOP. When implemented using a linked list stack, it is a pointer to type int *

Realization array

typedef int Position;
struct SNode {
    ElementType *Data; /* 存储元素的数组 */
    Position Top;      /* 栈顶指针 */
    int MaxSize;       /* 堆栈最大容量 */
};
typedef struct SNode *Stack;
 
Stack CreateStack( int MaxSize )
{
    Stack S = (Stack)malloc(sizeof(struct SNode));
    S->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
    S->Top = -1;
    S->MaxSize = MaxSize;
    return S;
}
 
bool IsFull( Stack S )
{
    return (S->Top == S->MaxSize-1);
}
 
bool Push( Stack S, ElementType X )
{
    if ( IsFull(S) ) {
        printf("堆栈满");
        return false;
    }
    else {
        S->Data[++(S->Top)] = X;
        return true;
    }
}
 
bool IsEmpty( Stack S )
{
    return (S->Top == -1);
}
 
ElementType Pop( Stack S )
{
    if ( IsEmpty(S) ) {
        printf("堆栈空");
        return ERROR; /* ERROR是ElementType的特殊值,标志错误 */
    }
    else 
        return ( S->Data[(S->Top)--] );
}

Linked list implementation 

typedef struct SNode *PtrToSNode;
struct SNode {
    ElementType Data;
    PtrToSNode Next;
};
typedef PtrToSNode Stack;
 
Stack CreateStack( ) 
{ /* 构建一个堆栈的头结点,返回该结点指针 */
    Stack S;
 
    S = (Stack)malloc(sizeof(struct SNode));
    S->Next = NULL;
    return S;
}
 
bool IsEmpty ( Stack S )
{ /* 判断堆栈S是否为空,若是返回true;否则返回false */
    return ( S->Next == NULL );
}
 
bool Push( Stack S, ElementType X )
{ /* 将元素X压入堆栈S */
    PtrToSNode TmpCell;
 
    TmpCell = (PtrToSNode)malloc(sizeof(struct SNode));
    TmpCell->Data = X;
    TmpCell->Next = S->Next;
    S->Next = TmpCell;
    return true;
}
 
ElementType Pop( Stack S )  
{ /* 删除并返回堆栈S的栈顶元素 */
    PtrToSNode FirstCell;
    ElementType TopElem;
 
    if( IsEmpty(S) ) {
        printf("堆栈空"); 
        return ERROR;
    }
    else {
        FirstCell = S->Next; 
        TopElem = FirstCell->Data;
        S->Next = FirstCell->Next;
        free(FirstCell);
        return TopElem;
    }
}

A common use of STL library stack

stack<int> name;

st.push(i);        //将i压入栈
st.top();        //获得栈顶元素
st.pop();        //用以弹出栈顶元素
st.empty();        //返回true为空,返回false为非空
st.size();        //大小

//在使用top()和pop()函数之前必须先用empty()判断栈是否空


//stl没有实现栈的清空,可以用while循环pop出元素
while(!st.empty()){
    st.pop();
}

A1051

#include<cstdio>
#include<stack>
using namespace std;
const int maxn=1010;
int arr[maxn];      //保存题目给定的出栈序列
stack<int> st;      //定义栈st,用以存放int型元素
int main()
{
    int m,n,T;
    scanf("%d%d%d",&m,&n,&T);
    while(T--){
        //循环执行T次
        while(!st.empty()){
            //清空栈
            st.pop();
        }
        for(int i=1;i<=n;i++)       //读入数据
        {
            scanf("%d",&arr[i]);
        }
        int current=1;      //指向出栈序列中的待出栈元素
        bool flag=true;
        for(int i=1;i<=n;i++)
        {
            st.push(i);     //把i压入栈
            if(st.size()>m){
                //如果此时栈中元素的个数大于容量m,则序列非法
                flag=false;
                break;
            }
            //栈顶元素与出栈序列当前位置的元素相同时
            while(!st.empty()&&st.top()==arr[current]){
                st.pop();       //反复弹栈并令current++
                current++;
            }
        }
        if(st.empty()==true&&flag==true){
            printf("YES\n");        //栈空且flag==true时表明合法
        }else{
            printf("NO\n");
        }
    }
    return 0;
}

 

Published 24 original articles · won praise 6 · views 462

Guess you like

Origin blog.csdn.net/qq_39965800/article/details/102257672