Stack C Array Implementation

Source: Internet
Author: User

Stack isAdvanced and laterData structure.

Stack operations include:Stack entry, stack exit, initialize stack, clear stack, traverse stack.


The C code is as follows:

# Include <stdio. h> # define maxsize 20 typedef int elemtype; typedef struct stack {elemtype data [maxsize]; int top ;}stack; // initialize stack void initstack (stack * s) {S-> Top =-1 ;}// void pushstackvalue (stack * s) {printf ("input the value of stack Member: \ n (0-Exit) \ n "); int value; printf (" Please input the 1st value of Stack: \ n "); scanf (" % d ", & value ); s-> data [++ S-> top] = value; while (value) {S-> top ++; printf ("Please input the % DST value of Stack: \ n ", S-> top + 1); scanf (" % d ", & value); s-> data [S-> top] = value ;}} // output stack void popstackvalue (stack * s) {If (S-> top> = 0) {printf ("the stack % DST value pop out: % d \ n ", s-> top + 1, S-> data [-- s-> top]);} else {printf ("the stack is empty \ n ");}} // determine the empty stack void isempty (stack * s) {If (S-> Top =-1) {printf ("the stack is empty. \ n ");} else {printf (" the stack is not empty. \ n ") ;}/// clear the stack void clearstack (stack * s) {S-> Top =-1;} // traverse the stack void scanstack (stack * s) {int I; int Len = s-> top-1; int stackarray [Len]; for (I = Len; I> 0; I --) {stackarray [I] = s-> data [I --];} printf ("the all stack member (from top to bottom) is: \ n "); while (LEN> = 0) {printf ("% d", S-> data [Len --]);} printf ("\ n");} void main () {stack s; initstack (& S); pushstackvalue (& S); scanstack (& S); isempty (& S); popstackvalue (& S ); popstackvalue (& S );}


The running result is as follows:




Reprinted by Liu

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.