美文网首页
数据结构之 栈

数据结构之 栈

作者: jokerlee | 来源:发表于2020-04-14 22:16 被阅读0次

    栈结构

    16931375-b01a80943ec5ef92.png

    链式栈

    16931375-aef539f07eef4a6c.png

    一.栈结构体

    typedef int Status;
    typedef int Elemtype;
    
    typedef struct {
        Elemtype data[MAXSIZE];
        int top;
    }Stack;
    
    1构建空栈
    Status InitStack(Stack *S){
        S->top = -1;
        return OK;
    }
    
    2栈置空
    Status  ClearStack(Stack *S){
        S->top = -1;
        return OK;
    }
    
    3判断栈空
    Status StackEmpty(SqStack S){
        if (S.top == -1)
            return 1;
        else
            return 0;
    }
    
    4获取栈顶
    Status GetTop(SqStack S,SElemType *e){
        if (S.top == -1)
            return 0;
        else
            *e = S.data[S.top];
        return 1;
    }
    
    5入栈
    Status PushData(SqStack *S, SElemType e){
    
        if (S->top == MAXSIZE -1) {
            return 0;
        }
        S->top ++;
        S->data[S->top] = e;
        return 1;
    }
    
    6出栈
    Status Pop(SqStack *S,SElemType *e){
       
        if (S->top == -1) {
            return 0;
        }
        
        *e = S->data[S->top];
        printf("%d ",*e);
        S->top--;
        
        return 1;
    }
    
    7便利栈
    Status StackTraverse(SqStack S){
        int i = 0;
        printf("此栈中所有元素");
        while (i<=S.top) {
            printf("%d ",S.data[i++]);
        }
        printf("\n");
        return 1;
    }
    

    二.链式栈

    1链式栈结构
    typedef struct StackNode
    {
        SElemType data;
        struct StackNode *next;
    }StackNode,*LinkStackPtr;
    
    typedef struct
    {
        LinkStackPtr top;
        int count;
    }LinkStack;
    
    2空链栈
    Status InitStack(LinkStack *S)
    {
        S->top=NULL;
        S->count=0;
        return 1;
    }
    
    3链栈置空
    Status ClearStack(LinkStack *S){
        LinkStackPtr p,q;
        p = S->top;
        while (p) {
            q = p;
            p = p->next;
            free(q);
        }
        S->count = 0;
        return OK;
        
    }
    
    4获取链栈顶
    Status GetTop(LinkStack S,SElemType *e){
        if(S.top == NULL)
            return 0;
        else
            *e = S.top->data;
        return 1;
    }
    
    5入栈
    Status Push(LinkStack *S, SElemType e){
        
        LinkStackPtr temp = (LinkStackPtr)malloc(sizeof(StackNode));
        temp->data = e;
        temp->next = S->top;
        S->top = temp;
        S->count++;
        return 1;
    }
    
    6出栈
    Status Pop(LinkStack *S,SElemType *e){
        LinkStackPtr p;
        if (StackEmpty(*S)) {
            return 0;
        }
        
        *e = S->top->data;
        printf("%d ",*e);
        p = S->top;
        S->top= S->top->next;
        free(p);
        S->count--;
        
        return 1;
    }
    
    6便利栈
    Status StackTraverse(LinkStack S){
        LinkStackPtr p;
        p = S.top;
        while (p) {
            printf("%d ",p->data);
            p = p->next;
        }
        printf("\n");
        return OK;
    }
    

    相关文章

      网友评论

          本文标题:数据结构之 栈

          本文链接:https://www.haomeiwen.com/subject/enwxvhtx.html