Stack of data Structures (1)--Sequential stacks

Source: Internet
Author: User

stack as a special linear table, in the computer has the sequential storage structure and the chain storage structure two kinds of storage structure, according to this we divide the stack into the sequential stack and the chain stack

Sequential stacks

top : Use top to dynamically represent the position of the top element of the stack in the sequence stack, when top=-1 indicates that the stack is empty

This is the definition of the data type of the stack.

typedef char ELEMTYPESTACK;TYPEDEF struct{        elemtypestack elem[maxsize];        int top;} Firstack;

Using an array to store the data, the corresponding subscript indicates his position in the stack, top represents the subscript of the top element of the stack, and if the stack is empty, the Top=-1

initialization of the stack

void Init (Firstack *s) {    

We use a sequential stack pointer to represent a sequential stack, and when we initialize a sequence stack to NULL, we set his top value to 1.

stacking operations on sequential stacks

into the stack bool Pushstack (firstack *s,elemtypestack x) {    if (s->top==maxsize-1)    return false;    else    {        (s->top) + +;        s->elem[s->top]=x;        return true;    

In the stack, first check whether the stack is full, if not full, then top self-increment, and put X into the stack top

overflow : If the stack is full, but also into the stack

Stacking of sequential stacks

BOOL Popstack (firstack *s,elemtypestack x) {    if (s->top==-1)    return false;    else    {        x=s->elem[s->top];        s->top--;        return true;    }}

You should first check if the stack is empty, and if it is not empty, assign the value of the top element of the stack to x and then the top self-decrement

underflow : If the stack is empty and the stack is out, the stack underflow will occur

Reading stack top element of sequential stack

Before reading the top element of the stack, first determine whether the stack is empty, if not empty, the top element of the stack is assigned to X

Stack of data Structures (1)--Sequential stacks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.