Data Structure Learning notes: C language implementation of sequential Stack

Source: Internet
Author: User
//////////////////////////////////////// //////////////////////////////////////// //
 
More original "uC/OS-II Study Notes: Series" Basic and embedded related knowledge details, please visit the cool tiger blog:
 
Http://blog.csdn.net/dcx1205
 
I believe it will not disappoint you !! //////////////////////////////////////// //////////////////////////////////////// //
// Write an order stack to encourage everyone! // For more information, see! # Include <stdio. h> # include <math. h> # include <stdlib. h> # define true 1 # define false 0 # define stack_size 5 typedef int elementtype; // defines the struct typedef struct seqstack {elementtype array [stack_size]; elementtype top;} seqstack; // initialize the sequence stack bool initstack (seqstack * PS) {If (! PS) // memory allocation failed {return false;} PS-> Top =-1; return true;} // determines whether bool isempty (seqstack * PS) is empty) {If (PS-> top <0) {return true;} else {return false ;}/// determines whether bool isfull (seqstack * PS) is full) {If (PS-> Top = STACK_SIZE-1) {return true;} else {return false ;}// pressure stack (in stack) bool pushstack (seqstack * ps, elementtype element) {If (isfull (PS) {return false;} PS-> top ++; PS-> array [PS-> top] = element; return true ;} // output stack elementtype popstack (seqstack * PS) {elementtype element; If (isempty (PS) {return false;} element = ps-> array [PS-> top]; PS-> top --; printf ("element = % d \ n", element); Return element;} // obtain the top element of the stack elementtype gettop (seqstack * PS) {elementtype element; If (isempty (PS) {return false;} element = ps-> array [PS-> top]; printf ("element = % d \ n ", element); Return element;} // void setempty (seqstack * PS) {PS-> Top =-1; return ;} // traverse the sequential stack bool trastack (seqstack * PS) {elementtype element; If (isempty (PS) {return false;} while (PS-> top>-1) {element = ps-> array [PS-> top]; PS-> top --; printf ("element = % d \ n", element);} return true ;} // main function int main (void) {seqstack * pS; // defines a pointer to the struct variable PS = (seqstack *) malloc (sizeof (seqstack )); // The following function parameters use P and & PS as the equivalent initstack (PS); // initialize pushstack (Ps, 1); // press the stack pushstack (Ps, 2 ); pushstack (Ps, 3); pushstack (Ps, 4); pushstack (Ps, 5); gettop (PS); // obtain the top element of the stack printf ("\ n "); popstack (PS); // printf ("\ n"); trastack (PS); // traverse the stack free (PS ); // release the memory space occupied by PS. Return 0 ;}

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.