Basic operation of stack

From , 4 Years ago, written in C++, viewed 54 times.
URL https://pastebin.vip/view/f0bbac6f
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<conio.h>
  4.  
  5. #define ERROR 0
  6. #define TRUE 1
  7. #define FALSE 0
  8. #define OK 1
  9. #define EQUAL 1
  10. #define OVERFLOW -1
  11. #define STACK_INIT_SIZE 100
  12. #define STACKINCREMENT 10
  13.  
  14. typedef int Status ;
  15.  
  16. struct STU
  17. {
  18.         char name[20];
  19.         char stuno[10];
  20.         int age;
  21.         int score;
  22. };
  23. typedef struct STU SElemType;
  24.  
  25. struct STACK
  26. {
  27.         SElemType *base;
  28.         SElemType *top;
  29.         int stacksize;
  30. };
  31.  
  32. typedef struct STACK SqStack;
  33. typedef struct STACK *pSqstack;
  34.  
  35. Status InitStack ( SqStack  **S );
  36. Status DestroyStack ( SqStack *S );
  37. Status ClearStack ( SqStack *S );
  38. Status StackEmpty ( SqStack S );
  39. int StackLength ( SqStack S );
  40. Status GetTop ( SqStack S,SElemType *e );
  41. Status Push ( SqStack *S,SElemType e );
  42. Status Pop ( SqStack *S,SElemType *e );
  43. Status StackTraverse ( SqStack S,Status ( *visit ) () );
  44.  
  45. Status InitStack ( SqStack **S )
  46. {
  47.         ( *S ) = ( SqStack * ) malloc ( sizeof ( SqStack ) );
  48.         ( *S )->base= ( SElemType * ) malloc ( STACK_INIT_SIZE *sizeof ( SElemType ) );
  49.         if ( ! ( *S )->base ) exit ( OVERFLOW );
  50.         ( *S )->top= ( *S )->base;
  51.         ( *S )->stacksize=STACK_INIT_SIZE;
  52.         return OK;
  53. }
  54.  
  55. Status DestroyStack ( SqStack *S )
  56. {
  57.         free ( S->base );
  58.         free ( S );
  59. }
  60.  
  61. Status ClearStack ( SqStack *S )
  62. {
  63.         S->top=S->base;
  64. }
  65.  
  66. Status StackEmpty ( SqStack S )
  67. {
  68.         if ( S.top==S.base ) return TRUE;
  69.         else
  70.                 return FALSE;
  71. }
  72.  
  73. int StackLength ( SqStack S )
  74. {
  75.         int i;
  76.         SElemType *p;
  77.         i=0;
  78.         p=S.top;
  79.         while ( p!=S.base )
  80.         {
  81.                 p++;
  82.                 i++;
  83.         }
  84. }
  85.  
  86. Status GetTop ( SqStack S,SElemType *e )
  87. {
  88.         if ( S.top==S.base ) return ERROR;
  89.         *e=* ( S.top-1 );
  90.         return OK;
  91. }
  92.  
  93. Status Push ( SqStack *S,SElemType e )
  94. {
  95.         /*
  96.          if(S->top - S->base>=S->stacksize)
  97.           {
  98.  
  99.             S->base=(SElemType *) realloc(S->base,
  100.                (S->stacksize + STACKINCREMENT) * sizeof(SElemType));
  101.             if(!S->base)exit(OVERFLOW);
  102.             S->top=S->base+S->stacksize;
  103.             S->stacksize += STACKINCREMENT;
  104.           }
  105.          */
  106.  
  107.         * ( S->top++ ) =e;
  108.         return OK;
  109. }
  110.  
  111. Status Pop ( SqStack *S,SElemType *e )
  112. {
  113.         if ( S->top==S->base ) return ERROR;
  114.         *e=*--S->top;
  115.         return OK;
  116. }
  117.  
  118. Status StackPrintElem ( SElemType * e )
  119. {
  120.         printf ( "%s  %s  %d  %d\n",e->name,e->stuno,e->age,e->score );
  121. }
  122. Status StackTraverse ( SqStack S,Status ( *visit ) () )
  123. {
  124.         while ( S.top!=S.base )
  125.                 visit ( --S.top );
  126. }
  127.  
  128. main()
  129. {
  130.         SElemType e;
  131.         SqStack *Sa;
  132.  
  133.         clrscr();
  134.  
  135.         printf ( "\n\n-------------------SqStack Demo is running...----------------\n\n" );
  136.         printf ( "First is Push function.\n" );
  137.  
  138.         InitStack ( &Sa );
  139.  
  140.         strcpy ( e.name,"stu1" );
  141.         strcpy ( e.stuno,"100001" );
  142.         e.age=80;
  143.         e.score=1000;
  144.  
  145.         printf ( "   Now Stack is Empty.\n" );
  146.         StackTraverse ( *Sa,StackPrintElem );
  147.  
  148.         Push ( Sa,e );
  149.  
  150.         printf ( "   Now Stack has one element.\n" );
  151.         StackTraverse ( *Sa,StackPrintElem );
  152.  
  153.         strcpy ( e.name,"stu3" );
  154.         strcpy ( e.stuno,"100002" );
  155.         e.age=80;
  156.         e.score=1000;
  157.         Push ( Sa,e );
  158.         printf ( "   Now Stack has another element.\n" );
  159.         StackTraverse ( *Sa,StackPrintElem );
  160.  
  161.         printf ( "   Now Pop Stack,the top elem put into variable e.\n" );
  162.         Pop ( Sa,&e );
  163.         printf ( "%s\n%s\n%d\n%d\n",e.name,e.stuno,e.age,e.score );
  164.  
  165.         printf ( "   Let's see the left of Stack's elem:\n" );
  166.         StackTraverse ( *Sa,StackPrintElem );
  167.  
  168.         getch();
  169.         printf ( "\n\n\nWelcom to visit http://zmofun.topcool.net\n\n" );
  170. }
  171.  

Reply to "Basic operation of stack"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website