[Algorithm and data structure] _ 3 _ linear structure _ Stack

Source: Internet
Author: User

The program code can be compiled and tested without any data.

/* This program tests the linear data structure: Stack */# include <stdio. h> # include <stdlib. h> struct stacknode {int * stack; int top; // count int size from 1;}; typedef struct stacknode stack; typedef Enum {false, true} bool; void createstack (stack * stack, int size); void initstack (stack * stack); bool isstackempty (stack * stack); bool isstackfull (stack * stack ); bool gettop (stack * stack, int * element); bool push (stack * stack, int element); bool POP (stack * stack); int main (INT argc, char * argv []) {stack; int size; stack. stack = NULL; puts ("Enter the length of stack to create:"); scanf ("% d", & size); putchar ('\ n '); createstack (& stack, size); getchar (); Return 0;}/* function: Create an empty stack function prototype: void createstack (stack * stack) function parameter: Stack * Stack: pointer to stack int size: size of the stack to be created return value: no exception: Pass NULL pointer */void createstack (stack * stack, int size) {If (stack = NULL) {puts ("error. \ n "); exit (0);} If (Stack-> stack = (int *) malloc (sizeof (INT) * size) {stack-> size = size; stack-> Top = 0;} else {puts ("error. \ n "); exit (0) ;}/ * function: Initialize the stack space and initialize all stack elements to zero function prototype: void initstack (stack * stack) function parameter: Stack * Stack: Stack pointer return value: no exception: pass a null pointer */void initstack (stack * stack) {int I; // detect NULL pointer if (null = stack | null = stack-> stack) {puts ("error. \ n "); exit (0) ;}else {I = 0; while (I <stack-> size) {stack-> stack [I] = 0; I ++ ;}}/ * function: determines whether the stack is empty. function prototype: bool isstackempty (stack * stack) function parameter: Stack * Stack: Stack pointer return value: if it is null, true is returned; otherwise, false is returned. Exception: NULL pointer */bool isstackempty (stack * stack) is passed) {If (null = stack | stack-> stack = NULL) {puts ("error. \ n "); exit (0) ;}if (0 = stack-> top) {return true ;}else {return false ;}}/* function: judge whether the stack is full function prototype: bool isstackfull (stack * stack) function parameter: Stack * Stack: Stack pointer return value: If the stack is full, true is returned; otherwise, false is returned. Exception: pass a null pointer */bool isstackfull (stack * stack) {If (null = stack | stack-> stack = NULL) {puts ("error. \ n "); exit (0);} If (Stack-> Top = stack-> size) {return true;} else {return false ;}} /* function: Get the value of the top element of the stack. function prototype: bool gettop (stack * stack, int * element) function parameter: Stack * Stack: Stack pointer return value: if possible, returns true, and sets * element = top stack value. Otherwise, false is returned, and * element = 0 is set. Exception: pass a null pointer */bool gettop (stack * stack, int * element) {If (null = stack | stack-> stack = NULL | element = NULL) {puts ("error. \ n "); exit (0);} If (isstackempty (stack) {* element = 0; return false ;} else {* element = stack-> stack [stack-> top-1]; return true;}/* function: Pressure stack function prototype: bool push (stack * stack, int element) function parameter: Stack * Stack: Stack pointer int element: returned value of the inbound stack element: true if the stack is successfully pressed; otherwise, false is returned. Exception: pass a null pointer */bool push (stack * stack, int element) {If (null = stack | stack-> stack = NULL) {puts ("error. \ n "); exit (0);} If (isstackfull (stack) {// stack full, unable to press stack return false ;} else {stack-> stack [stack-> top] = element; stack-> top ++; return true ;}/ * function: function prototype: bool POP (stack * stack) function parameter: Stack * Stack: Stack pointer return value: If the stack is successfully exited, true is returned; otherwise, false is returned: pass a null pointer */bool POP (stack * stack) {If (null = stack | stack-> stack = NULL) {puts ("error. \ n "); exit (0);} If (isstackempty (stack) {return false;} else {stack-> top --; return true ;}}

 

 

 

 

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.