Implementation of the Stack (C language Implementation)

Source: Internet
Author: User

Header file #include <stdio.h>/*==================== stack data structure using arrays to implement ====================*/#define MAX 100#define OK Define error 0typedef struct stack{int Data[max];  int top;   stack top int bottom;//stack}stack,*stack;//initialization stack int initstack (stack stack);//Destroy int destroystack (stack stack);//Empty int Clearstack (Stack stack),//stack empty int stackempty (stack stack),//get stack top element int GetTop (stack stack,int* elem);//stack int Push (stack stack,int* elem);//stack int Pop (stack stack,int* elem);//returns the element length int stacklength (stack stack,int* len) in the stack;//the element int in the print stack Printstack (Stack stack);//stack full int stackfull (stack stack);
The main function implements the # include "Stack.h" int main () {int num=10, NUM1, i=0,j=0; Stack stack;int Initflag=initstack (&stack); if (!initflag) return 0;for (num1=0;num1<10;num1++) Push (&stack , &AMP;NUM1);/*int Pushflag=push (&stack,&num); if (!pushflag) return 0;*///clearstack (&stack); for (;i< 10;i++) {int Flag=pop (&AMP;STACK,&AMP;NUM1), if (flag) printf ("%d", NUM1); elseprintf ("Pop error");} int lenflag=stacklength (&AMP;STACK,&AMP;NUM1); if (Lenflag) printf ("len=%d", NUM1); elseprintf ("Stacklength error"); *int getflag=gettop (&AMP;STACK,&AMP;NUM1); if (Getflag) printf ("%d", NUM1); */printstack (&stack); return 0;} Initialize stack int initstack (stack stack) {if (stack==null) return error;stack->bottom=stack->top=0;return OK;} stack int Push (stack stack,int* elem) {int flag=stackfull (stack), if (!flag) return error;stack->data[stack->top]=* Elem;++stack->top;return OK;} All elements in the print stack int printstack (stack stack) {int i=0;int flag=stackempty (stack), if (!flag) return error;for (i=0;i<stack- >top;i++) {printf ("%d", StAck->data[i]);} printf ("\ n"); return OK;} stack empty int stackempty (stack stack) {if (Stack==null | | stack->top==stack->bottom) return Error;elsereturn OK; Out stack int Pop (stack stack,int* elem) {int flag=stackempty (stack); if (!flag) return error;*elem=stack->data[stack-> Top-1];--stack->top;return OK;} Stack full int stackfull (stack stack) {if (Stack==null | | stack->top>max) return Error;elsereturn OK; destroys the int destroystack (stack stack) {if (stack==null) return Ok;else{stack->top=stack->bottom;stack=null;return OK;}} empties the int clearstack (stack stack) {int flag=stackempty (stack); if (!flag) return error;else{stack->top=stack-> Bottom;return OK;}} Gets the top element of the stack int GetTop (stack stack,int* elem) {int flag=stackempty (stack); if (!flag) return error;*elem=stack->data[ Stack->top-1];return OK;} Returns the length of the element in the stack int stacklength (stack stack,int* len) {if (stack==null) return error;if (stack->top==stack->bottom) {* Len=0;return OK;} Else{*len=stack->top;return OK;}}


Implementation of the Stack (C language Implementation)

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.