Data structure: Stack

Source: Internet
Author: User

The following is the complete code:

/* * this file is an implementation of stack * file  name: stack.c * author: john woods * date: 2015/5/9 *  statement: anyone can use this file for any purpose */#include  <stdio.h> #include  <stdlib.h> #define  bool int#define true 1#define  FALSE 0typedef struct SNode {    int value;     struct snode * next;} * snode;typedef struct stack {    int depth;     snode top;} * stack;/* operation declaration */void stackinit (stack * p_mystack); void  pop (Stack mystack); Void push (Stack mystack); Void stackclear (Stack myStack); void  stackdestroy (Stack * p_mystack); Bool isexist (STACK&NBsp;mystack);/* menu */void menu ();/* entrance: main function */int  Main (void)  {    Stack myStack = NULL;    int  Choice;    char c;    while (TRUE)  {         menu ();         while (!scanf ("%d", & Choice)  while (' \ n '  !=  (C=getchar ())  && eof != c);         switch (choice)  {             case 1:                 stackinit (&mystack);                 break;             case 2:  &Nbsp;             stackclear (MyStack);                 break;             case 3:                 push (Mystack);                 break;             case 4:                 pop (Mystack);                 break;             case 5:                 Stackdestroy (&mystacK);                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 element    4.pop element  *\n ");     printf (" \t*  5.destroy  stack  6.exit    &Nbsp;    *\n ");     printf (" \t****************menu****************\n ");     printf ("Your choice:");} /* operation implementation */void stackinit (Stack * p_mystack)  {     if (Isexist (*p_mystack))  {        printf ("This  stack is already exist, cannot initial it!\n ");         return;    }    if (NULL ==  (*p_ mystack =  (Stack) malloc (sizeof (Struct stack)))  {         printf ("out of memory!\n");        return;     }     (*p_mystack)->top = null;     (*p_ Mystack)->depth = 0;    printf ("Initial successfuLly!\n ");} Void pop (Stack mystack)  {    SNode pNode = NULL;     int out;    char c;    if (!isExist (MyStack))  {        printf ("this stack is not exist!  please initial it first!\n ");        return;     }    if (0 == mystack->depth)  {         printf ("this stack is empty! cannot pop the  top value!\n ");        return;    }     while (TRUE)  {        out = mystack- >top->value;        pNode = myStack->top;         mystack->top = mystack->top->next;         mystack->depth--;        free (PNode);         pnode = null;        printf (" The value has been popped is %d\n ",  out);                 if (0 == mystack->depth)  {             printf ("This stack is  empty now, cannot continue popping!\n  ");             break;        }         while (' \ n '  !=  (C=getchar ())  && eof != c);          printf ("Go on?" ( y/n):         if (' Y '  !=  (C=getchar ())  &&   ' Y '  != c)  break;    }}void push (stack mystack)  {     SNode pNode = NULL;    int value;     char c;    if (!isexist (mystack))  {         printf ("this stack is not exist! please initial it first!\ n ");        return;    }     while (TRUE)  {        if (! ( pnode =  (SNode) malloc (sizeof (Struct snode)))  {             printf ("out of memory!\n");             return;        }         printf ("Please input the value:");         while (!SCANF ( "%d",  &value))  while (' \ n '  !=  (C=getchar ())  && eof != c);         pNode->value = value;         pNode->next = myStack->top;         myStack->top = pNode;        myStack->depth++;         pNode = NULL;         printf ("push successfully!\n");             while (' \ n '  !=  (C=getchar ())  && eof != c);         printf ("Go on?" ( y/n):         if (' Y '  !=  (C=getchar ())  &&   ' Y '  != c)  break;    }}void stackclear (stack mystack)  {     if (!isexist (Mystack))  {        printf (" this stack is not exist! please initial it first!\n ");         return;    }    snode pnode  = null;    while (mystack->top)  {         pnode = mystack->top;        mystack->top  = mystack->top->next;        free (PNode);     }    myStack->top = NULL;    myStack-> depth = 0;    printf ("clear successfully!\n");} Void stackdestroy (Stack * p_mystack)  {    if (!isExist (*p_myStack))  {        printf ("this stack is not exist!  please initial it first!\n ");        return;     }    stackclear (*p_mystack);     free (*p_ Mystack);     *p_mystack = null;    printf ("Destroy  Successfully!\n ");} Bool isexist (Stack mystack)  {    if (null == mystack)  return  false;    else return true;}

The point here is that Typedef,typedef will "encapsulate" a new data type, which is very different from # define.

/* typedef */typedef struct sname{int data,} * sname;/* define the following variable */sname temp = (SName) malloc (sizeof (struct SName));/* Although temp is actually a pointer, if it is called by other functions as a parameter, its value will still not be modified, and the parameters passed in Stack.c's Stackinit () and Stackdestroy () should be noted */



Data structure: Stack

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.