堆棧相關操作

  在這個程序中,我將堆棧當作容器。進行壓棧跟出棧的操作! 

 

#include<stdio.h>
#define MaxSize 20                                     //允許棧的最大存儲數爲20
typedef int BOOL;

typedef  struct stack{
         int Top,MaxStack;
         int N;                                                        //用來標誌棧操作量的增減
         int  Element[MaxSize];
        }Stack;
void Creat_Stack(Stack *p,int m)
            {
             p->Top=0;                                           
             p->N=0;
             p->MaxStack=m;
            }
BOOL  Is_Empty(Stack *p)
            {
             return p->Top<1;                                    //如棧中沒有元素,既一個也沒有小於1的情況下爲空
             }
BOOL  Is_Full(Stack *p)
             {
              return p->Top>=MaxSize;                     //大於最大可存儲數的情況下爲滿
             }
void Push(Stack *p,int x)
             {
              
              if(Is_Full(p))
                 printf("Overflow/n");
         else
            {
             p->Element[++(p->Top)]=x;                                        //先將地址加1,在將值賦給堆棧
             p->N=p->N+1;                                                            //壓入一個數,N就加1
            }
                   
            }
void Pop(Stack *p)
            {
              if(Is_Empty(p))
                printf("underflow/n");
              else
                {
                 printf("%d/n",p->Element[p->Top]);
   p->Top--;                                                                                                          //出棧一個堆棧的top就減1
   p->N=p->N-1;                                                                                                //同時N也減1
  }
     
            }

PrintStack(Stack *p)
           {    
             if(Is_Empty(p))
                {                
                  printf("/nIs Bottom!/n"); 
                }
             else        
               {
                printf("%d/n",p->Element[p->Top--]);                                                //列印出堆棧的元素
                PrintStack(p);                              
               }
             return p->Top;
           }

void main()
           {
            Stack s;          
            int i,n;           
            int a[MaxSize];
     Creat_Stack(&s,MaxSize);                       /*Construction of a capacity for MaxSize air for stack*/
       for(;;)
           {
            switch(menu_select())
             {
              case 1:
                       printf("please put into number element:/n");
                       scanf("%d",&n);
                       for(i=0;i<n;i=i++)
                           {
                            scanf("%ld",a+i);  
                           }
                       printf("************************/n");              
                       for(i=0;i<n;i++)
                          {                  
                            Push(&s,a[i]);                          
                          }
                       printf("N=%d/n",s.N);     
                printf("/n");
                break;
        case 2:
               printf("pop element:");
                      Pop(&s);
                      printf("N=%d/n",s.N);
                      break;
               case 3:
                     printf("print stack element:/n/n");         
              PrintStack(&s);
              s.Top=s.N;                                                                           //將N的值賦給top
              printf("N=%d/n",s.N); 
              break;
      
       case 0:
                     printf("/Thank you for your using!/n");
                     return;
         
              }
         }
}
int menu_select()
          {
           int sn;
           printf(" /n");
           printf(" Stack operate /n");
           printf(" ======================/n");
           printf(" 1.Stack push/n");
           printf(" 2.Stack Pop/n");
           printf(" 3.information printf/n");         
           printf(" 0.exit Stack operate/n");
           printf(" ======================/n");
           printf( " please select 0-4 /n/n");
           for(;;)
            {
              scanf("%d",&sn);
              if(sn<0||sn>3)
                printf("/tInsert Error/n");
              else
                break;
            }
           return sn;
         }
           

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章