C + + (implementation of data structure stacks)

Source: Internet
Author: User

The implementation characteristics of the stack filo (advanced post-out)

Assuming that the stack has a space of 8
top = = 0 cannot be out of the stack, has reached the bottom of the stack
top = = 8 can not be in the stack, has reached the top of the stack
Top always points to a position to be inserted
Push operation, 1. Write data, 2.top++ 3. If the stack is not full
Pop operation, 1.top--, 2. Eject Data 3. If the stack is not empty

#include <stdio.h>//need a continuous spacetypedef struct_stack{Charmem[1024x768];intTop;} Stack; Stack s;//Determine if the stack is fullintIsfull (Stack *ps) {returnPs->top = =1024x768;//ps-> = = 1024 is true, return 1, that is full when the return True}//Determine if the stack is emptyintIsEmpty (Stack *ps) {returnPs->top = =0;//ps->top = = 0; The time is empty stack}//In-stack operationvoidPush (Stack *ps,CharCH) {ps->mem[(Ps->top) + +] = ch;//Let Ps.mem's pointer ps.top++ and then assign a value}//Play OperationsCharPop (Stack *ps) {returnps->mem[--(Ps->top)];//First pointer to Ps.mem--, and returns}intMain () {Stack s = {{0},0}; for(Charch = ' a '; Ch <= ' z '; ch++) {if(!isfull (&s)) push (&AMP;S,CH); } while(!isempty (&s)) {Putchar (pop (&s));//EjectPuts" "); }}/*ZyxWvuTsRQPoNmLkJIhgFeDCba*/

C + + (implementation of data structure 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.