C程序之5:堆栈的函数

来源:互联网 发布:php 二进制转base64 编辑:程序博客网 时间:2024/06/10 15:00

#include <cstdlib>
#include <iostream>
using namespace std;

#define m0 100

typedef struct
   {
    char s[m0];
    int top;
    }stack;
   
void push(stack *ST,char x)
{
    if(ST->top==m0)printf("栈上溢出!\n");
    else
      {
       ST->top=ST->top+1;
       ST->s[ST->top]=x;
       }
    }
   
void pop(stack *ST)
{
    if(ST->top==0)printf("栈下溢出!\n");
    else
        ST->top--;
}

char top(stack *ST)
{
    if(ST->top==0) printf("无栈顶元素!\n");
    else
        return(ST->s[ST->top]);
    }
   
int sempty(stack *ST)
{
      if(ST->top==0)return(1);
      else return(0);
      }
     
char ptop(stack *ST)
{
     char x;
      x=top(ST);
      pop(ST);
      return(x);
   }    
      
int main(int argc, char *argv[])
{
    int i;
    stack st;
    char x;
    for(i=0;i<5;i++)
     {
       scanf("%c",&x);
       push(&st,x);
       }
      
     while (!sempty(&st) )
      {
         x=ptop(&st);           
         printf("%c\n" ,  x);
        } 
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

原创粉丝点击