Data structures: stacks (implemented with arrays)

Source: Internet
Author: User

The following is the complete code:

/* * this file if an implementation of stack with array  list * file name: ArrayStack.c * author: John Woods *  date: 2015/5/10 * statement: anyone can use this file for  any purpose */#include  <stdio.h> #include  <stdlib.h> #include  <malloc.h > #define  INCREMENT 5#define MAX_LEN 20#define BOOL int#define TRUE  1#define false 0/* structure of the array stack */typedef struct  AStack {    int Length;    int MaxLength;     int * stackarray;} * astack;/* operation declaration */void stackinit (AStack * p_myAStack); Void stackdestroy (Astack * p_myastack); Void stackclear (astack mYastack); Void pop (Astack myastack); Void push (Astack myastack); Bool isexist (Astack myastack);/* menu declaration */void menu ();/* entrance :  main function */int main (void)  {    astack myastack =  NULL;    int choice;    char c;     while (TRUE)  {        menu ();         while (!scanf ("%d",  &choice))  while (' \ n '  !=  (C=getchar ())  & &AMP;&NBSP;EOF&NBSP;!=&NBSP;C);         switch (choice)  {             case 1:                 stackinit (&myAStack);            &nbsP;    break;            case  2:                 Stackclear (Myastack);                 break;            case 3:                 push (MyAStack);                 break;             case 4:                 pop (Myastack);                 break;             case 5:                 Stackdestroy (&myastack);                 break;            default:                 exit (EXIT_SUCCESS);                 break;         }    }    return 0;} /* menu implementation */void menu ()  {    printf ("\n\t*********** Menu***************\n ");     printf (" \t* 1.initial stack  2.clear  stack *\n ");     printf (" \t* 3.push           &nbsP;4.pop         *\n ");     printf (" \t* 5. destroy stack  6.exit        *\n ");     printf ("\t***************menu***************\n");     printf ("\tYour choice:   ");} /* operation implementation */void stackinit (Astack * p_myastack)  {     if (Isexist (*p_myastack))  {        printf ("This  stack is already exist! please destroy it first!\n ");         return;    }    *p_myastack =   (astack) malloc (sizeof (Struct astack));     if (Null == *p_myastack)  {        printf ("out memory! initial  Unsuccessfully!\n ");         return;    }     (*p_myAStack)- >StackArray =  (int *) malloc (sizeof (int)  * max_len);     if (NULL  ==  (*p_myastack)->stackarray)  {        printf ("Out  memory! initial unsuccessfully!\n ");         printf (" Destroy the stack now...\n ");         stackdestroy (p_ Myastack);        return;    }      (*p_myastack)->length = 0;     (*p_myastack)->maxlength =  max_len;    printf ("initial successfully!\n");} Void stackdestroy (Astack * p_myastack)  {    if (!isExist (*p_myAStack))  {        prinTF ("this stack is not exist! please initial it first!\n");         return;    }    free ((*p_myAStack) ->stackarray);     free (*p_myastack);    *p_myastack =  null;    printf ("destroy successfully!\n");} Void stackclear (Astack myastack)  {    if (!isexist (myAStack))  {         printf ("This stack is not exist! please  initial it first!\n ");        return;     }    myastack->length = 0;    printf ("Clear  successfully!\n ");} Void pop (Astack myastack)  {    int value;    char  c;    if (!isexist(Myastack))  {        printf ("this stack is not exist!  please initial it first!\n ");        return;     }    while (TRUE)  {         if (0 == myastack->length)  {             printf ("this stack has no element! pop unsuccessfully!\n");             return;         }        value = * (MyAStack->StackArray &NBSP;+&NBSP;MYASTACK-&GT;LENGTH&NBSP;-&NBSP;1);        myastack-> length--;        printf ("pop successfully! the popped  value is&Nbsp;%d\n ",  value);                         while (' \ n '  !=  (C=getchar ())  & &AMP;&NBSP;EOF&NBSP;!=&NBSP;C);         printf ("Go on?" ( y/n)           if (' Y '  !=  (C=getchar ())  &&   ' Y '  != c)  break;    }}void push (astack myastack)  {     int value;    char c;    if (!isExist (Myastack))  {        printf ("this stack is not exist!  please initial it first!\n ");        return;     }    while (TRUE)  {         if (myastack->lengtH >= myastack->maxlength)  {             if (null ==  (myastack =  (astack) realloc (myastack, sizeof (int)  *  INCREMENT))  {                 printf ("out of memory! push unsuccessfully!\n");                 return;             }             myastack->maxlength += increment;        }         printf ("Please input value:");         while (!scanf ("%d",  &value))  while (' \ n '  !=  (C=getchar ())  & &AMP;&NBSP;EOF&NBSP;!=&NBSP;C);         * (Myastack->stackarray + myastack->length)  = value;        myAStack->Length++;         printf ("push successfully! the pushed value is %d\ n ",  value);             while (' \ n '  !=   (C=getchar ())  && eof != c);         printf ("Go on?" ( y/n)           if (' Y '  !=  (C=getchar ())  &&   ' Y '  != c)  break;    }}bool isexist (astack myastack)  {     if (Null == myastack)  return false;    else  return true;}

PS: Today is Mother's Day Oh!

Data structures: stacks (implemented with arrays)

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.