The basic operation of the sequential stack: initialization, stack, stack, read the top element of the stack

Source: Internet
Author: User

Sequential stack: A set of contiguous storage units from the bottom of the stack to the top of the data element, and due to the particularity of the stack operation, also must be attached to a position pointer top (stack top pointer) to dynamically indicate the position of the stack top element in the sequence stack. The empty stack is usually represented by Top=-1.

The code is as follows:

#include <iostream>

using namespace Std;

#define TRUE 1

#define FALSE 0

Storage structure of sequential stacks

#define STACK_SIZE 50//Set the number of elements in the stack is 50

typedef struct

{

int elem[stack_size];//A one-dimensional array for storing elements in the stack

int top;//is used to hold the subscript of the top element of the stack, Top=-1 represents an empty stack

}seqstack;


Initialize sequence stack

void Initstack (Seqstack *s)

{

S->top = -1;//constructs an empty stack

}


Sequential stack into the stack operation, first determine whether the stack is full, if the stack is full, but also into the stack will overflow

int Push (seqstack *s,int x)//place x in the new stack top of S stack

{

if (s->top = = stack_size-1)//Stack is full

{

return FALSE;

}

s->top++;

S->elem[s->top] = x;//x into the stack

return TRUE;

}


Sequential stack out of the stack operation, the first to determine whether the stack is empty, if the stack is empty, but also out of the stack will occur underflow

int pop (seqstack *s, int *x)//eject the top element of S stack and put it in the storage space referred to by x

{

if (s->top = =-1)//stack is empty

{

return FALSE;

}

Else

{

*x = s->elem[s->top];

s->top--;//modifying the top pointer of the stack

return TRUE;

}

}


Sequential stack reading stack top element operation

int Getpop (seqstack *s, int *x)//reads the top element of the stack S stack, puts it in the storage space referred to by x, and the stack top pointer remains unchanged

{

if (s->top = =-1)//stack is empty

{

return FALSE;

}

Else

{

*x = s->elem[s->top];

return TRUE;

}

}

Note: When implementing the Getpop operation, the parameter Seqstack *s can also be changed to Seqstack S, and the address will be changed to the method of passing the value. The value of the transfer is easier to understand than the address, but it saves space and time by transmitting the address.

This article is from the "Rock Owl" blog, please be sure to keep this source http://yaoyaolx.blog.51cto.com/10732111/1771270

The basic operation of the sequential stack: initialization, stack, stack, read the top element of the stack

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.