Sequential storage structure and beauty chain store structure data structures (six) stack

(Summary from the "Westward data structure" data structure beginner recommend this book)

table of Contents

Stack

Sequential storage structure stack

Onto the stack.

Pop operations

Stack two shared space

Stack of linked storage structure

Onto the stack.

Pop operations



Stack

Stack (Stack) is defined only insertion and deletion operations in the tail of the linear form

We stack allows insertion and deletion of one end called stack (top), and the other end is called the stack bottom (bottom), without any data elements called empty stack.

Also known LIFO stack (Last In First Out) linear form, referred LIFO structure.

The stack only the top element.

Sequential storage structure stack

Since the stack can only be inserted and removed by the end, which section do you use?

Subscript 0 as the end bottom of the stack is better, because the first end of the stack elements are present, the minimum change for the bottom of the stack it.

ok, we define a variable to indicate the position of the top stack element in the array, but it must be less than the length StackSize stack.

The determination condition is empty stack Top = -1 , when the stack means that there is an element, top = 0.

//栈的结构定义
typedef int SElemType;
typedef struct
{
    SElemType data[MAXSIZE];
    int top;    //用于栈顶指针
}SqStack;

Onto the stack.

As shown, the push operation idea is: ①, stack pointer + 1, ②, assigned to the element to be inserted into the stack space

Status Push(SqStack *s,SElemType e)
{
    if(S->top == MAXSIZE-1)
    {
        return ERROR;  //栈满,无法添加
    }
    S->top++;          //① 、栈顶指针+1
    S->data[S->top]=e;//②、待插入元素赋值给栈顶空间
    return OK;
}

Pop operations

Stack operation idea is simple: to stack pointer -1

Status Pop(SqStack *S)
{
    if(S->top ==-1)
        return ERROR;
    S->top--;
    return OK;
}

 

Stack two shared space

Stack is convenient, because the problem needs to move elements of insertions and deletions when there is no linear table. But it is equally flawed, because the array must first determine the size of the storage space. For two of the same type stack, we can maximize the use of open storage space, how to use it? Two stacks is our share!

How shared law? Share share, it must be multiple objects together using the same object. We know that there are two ends of the array, and only one stack bottom of the stack, it can not be at both ends of the security of a bottom of the stack, between the array to extend it? This is our shared two stacks.

typedef struct
{
    SElemType data[MAXSIZE];
    int top1;  //栈1的栈顶指针
    int top2;  //栈2的栈顶指针
}SqDoubleStack;

ok, now two stacks share an array of space, how do you know when it is inserted into the insert which stack it? So we need to have a little something to perform a function: Tell me before you insert, you want to insert the element to which the stack, namely stackNumber

//插入操作
Status Push(SqDoubleStack *s,SElemType e,int stackNumber)
{
    if(S->top1+1==S->top2)
        return ERROR;  //此时栈满

    if(stackNumber==1)
        S->data[++S->top1]=e;   //往top1+1位置处插入元素
    else if(stackNumber==2)
        S->data[--S->top2]=e;    //往top2-1位置处插入元素
    return OK;
}
//删除操作
//若栈不空,删除S的栈顶元素,用e返回其值,并返回OK,否则返回ERROR
Status Pop(SqDoubleStack *S, SElemType *e,int stackNumber)
{
    if(stackNumber==1)    
    {
        if(S->top1 ==-1)
            retrun ERROR;
        *e=S->data[S->top1--];
    }

    if(stackNumber==2)    
    {
        if(S->top2 ==MAXSIZE)
            retrun ERROR;
        *e=S->data[S->top2++];
    }
    return OK;
}

Then there is the question arises: Share owned shares, which it is in the array, the array is fixed-size it, then what use is it? So yeah, this data structure is generally used when space needs two stacks have an inverse relationship, ie a growth in another shortened, if the two are in constant growth, it will soon be over because of a stack overflow.

 

Stack of linked storage structure

We put the head on the top of the stack singly linked list, deleting the first node, because the stack chain structure does not require head node, does not make sense.

//链栈结构代码如下
typedef struct StackNode
{
    SElemType data;
    struct StackNode *next;
}StackNode,*LinkStackPtr;

typedef struct LinkStack
{
    LinkStackPtr top;
    int count;
}LinkStack;

Onto the stack.

Push the idea: Since the stack pointer instead of a head node, then the stack pointer is sure to play a role.

Open up a new node, data fields put a new data element, then it points to the next element of the current stack pointer, and then let the stack pointer to point to the new node.

Status Push(LinkStack *s,SElemType e)
{
    LinkStackPtr s=(LinkStackPtr)malloc(sizeof(StackNode));
    s->data=e;
    s->next=S->top;
    S->top=s;
    S->count++;
    return OK;
}

Pop operations

Stack ideas: p points to the top with a variable space, stack pointer down, free fall p

//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK,否则返回ERROR
Status Pop(LinkStack *S,SElemType *e)
{
    LinkStackPtr P;

    if(StackEmpty(*S))
        return ERROR;

    *e=S->top->data;
    p=S->top;

    S->top=S->top->next;
    free(p);
    S->count--;
    return OK;
}

Chain sequence of stack and the stack is popped into the stack and O (1) time complexity, length and sequence of stack fixed, there is a link stack pointer field, the presence of both the waste of memory space. If the stack during use, unpredictable element change, large small, preferably with a link stack, if the variation in the controllable range, recommended order stack.

Published 38 original articles · won praise 6 · views 1907

Guess you like

Origin blog.csdn.net/weixin_43827227/article/details/100856860