Data Structures and Algorithms (eight) - Stack

                                                                Stack

Defined stack: (Stack) only at one insertion or deletion linear form.

  S = (a1, a2, a3, a4, a5)

                                                                                                         -------------------------> push last-out order

The basic operation of the stack: InitStack (& ​​S): initialize an empty stack S

                         StackEmpty (S): Analyzing empty stack;

Push (& S): push (push), if not full stack S, x will join as the top element of the new stack top, on top need a shift

Pop (& S): a stack (pop), if the stack is not empty, then pop the top element, returns with x, top move down a

GetTop (S, & x): read the top element, with the non-empty element returns x pointed top.

Sequential storage structure stack

#define MaxSize 50
tydef struct{
    ElemType data[MaxSize];    //栈的数据域
    int top;    //栈顶指针
}SqStack;

Stack empty condition: S.top == -1 (the initial position of the stack) the stack full condition: S.top = MaxSize-1 stack length requirements: S.top + 1

Early stack the basic operation

void InitStack(SqStack &s){        //初始化
    S.top == -1;
}

bool StackEmpty(SqStack s){    //判空
    if(s.top == -1)
        return true;
    else
        return false;
}

bool Push(SqStack &s, ElemType x){    //进栈操作
    if(s.top == MaxSize-1)        //栈满溢出
        return false;
    s.data[++top] = x;        //插入到栈顶,同时栈顶指针上移
    return true;
}

bool Pop(SqStack &s, ElemType &x){
    if(s.top == -1)
        return false;
    x = s.data[top--];       //x获取栈顶元素,同时top指针下移
    return true;
}

bool GetTop(SqStack &s, ElemType &x){
    if(s.top == -1)
        return false;
    x=data[s.top];    
    return true;
}

Shared stack: the bottom two stacks disposed at both ends of the shared space, the initial position of top0 provided at the top of the stack the stack 1, an initial position 0 top1 provided in the top of the stack the stack bottom of the stack are No. 1, the maximum storage space for half of the original stack, when top0 pointer collide with top1 pointer, stack full.

Empty judgment: 0 Stack: top == -1 1 stack: top = MaxSize-1 stack becomes full: top1-top == 1;

Shared stack advantage: storage time complexity is O (1), a high space utilization.

The chain storage stack: the stack link stack pointer via internal link elements

top-> next points to the top element

typedef strcut LinkNode{      //定义链栈的结点
    ElemType data;
    struct LinkNode *next;
}LinkedStackNode,*LinkedStack;

typedef struct LStack{
    LinkedStackNode base;    //定义栈底指针
    LinkedStackNode top;     //定义栈顶指针
}LStack;

All operations are performed in the link stack header (top of the stack). top -> next

The basic operation of the link stack:

LinkedStack Init_LinkedStack(){    //初始化链栈,为空栈
     //根据top指针的移动动态分配结点空间
    LinkedStack top = (LinkedStackNode *)malloc(sizeof(Node));   
    if(top ! = NULL)
        top ->next = null;   //开始为栈顶,指向空、
    return top;
}    

bool LinkeStack_Empty(LinkedStack top){    //判空
    if(top->next == null){
        return true;
    }else
        return false;
}

//入栈:将元素x插入链栈栈顶,设置头结点指针域指向新插入位置的,设置为栈顶元素
bool Push_LinkedStack (LinkedStack top ,ElemType x){
    LinkedStackNode *node = (LinkedStackNode)malloc (sizeof(LinkedStackNode));
    if(node == null)
        return false;
    else{
        node->data = x;
        node->next = top->next;   //设置为栈顶
        top ->next = node;    //原栈顶指向新结点 
    return true;
    }
}

//出栈:删除栈顶数据元素,通过x返回被删元素的数据值,设置top指向链栈中的下一个元素
int Pop_LinkedStack(LinkedStack top, ElemType &x){
    LinkedStackNode *node;       //不用分配空间,已存在目标结点
    if(top -> next == null){
        return 0;
    }else{
        node = top->next;    //找到栈顶结点
        *x = node->data;
        top->next = node->next;    //被删元素的指向赋值给top;
        free(node);        //释放结点空间
        return x;
    }
}

//读取栈顶元素
int Get_LinkedStack(LinkedStack top, ElemType &x){
    if(top->next == null)
        return 0;
    else{
        x = top->next->data;    //栈顶元素的数据域赋值给x
        return 1; 
    }
}

 

Published 58 original articles · won praise 31 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_37504771/article/details/105422745