• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

Radeon Ling


Never abandon, never give up. I belive I can Challenge everyone, everything, everyday, because I am awesome.
   首页    新随笔    联系   管理    订阅  订阅

Stack



  1/**//*
  2This Demo is Made by: Radeon LING
  3Email: Radeon_ling@eastday.com
  4Copy right: 2005 Millennium Studio
  5*/

  6#include<stdio.h>
  7#define STACK_INTSIZE 50
  8typedef char DataType;
  9typedef struct
 10{
 11    DataType s[STACK_INTSIZE];
 12    int top;
 13}
Stack;
 14
 15void Push(Stack *st , DataType x)
 16{
 17    if(STACK_INTSIZE -1 == st->top)
 18    {
 19        printf("\nStack is full, cann't push!");
 20    }

 21    else
 22    {
 23        st->top++;
 24        st->s[st->top] = x;
 25    }

 26}

 27
 28void Pop(Stack *st)
 29{
 30    DataType x;
 31    if(st->top == 0)
 32    {
 33        printf("\nStack is empty, cann't pop");
 34    }

 35    else
 36    {
 37        x = st->s[st->top];
 38        printf("\nPoping element is %c\n" , x);
 39        st->top--;
 40    }

 41}

 42
 43// Display element's value of stack.
 44DataType ReadTop(Stack *st)
 45{
 46    DataType x;
 47    if(0 == st->top)
 48    {
 49        printf("\nThe stack is empty");
 50        return 0;
 51    }

 52    else
 53    {
 54        x = st->s[st->top];
 55        printf("\nThe top of the stack's value is %c\n" , x);
 56    }

 57    return 0;
 58}

 59
 60// Display all element in the stack.
 61void ShowStack(Stack *st)
 62{
 63    int x;
 64    x = st->top;
 65    if(0 == x)
 66    {
 67        printf("\nThe stack is empty!");
 68    }

 69    else
 70    {
 71        printf("\nThe element in the stack is ");
 72        while(0 != x)
 73        {
 74            printf("%6c" , st->s[x]);
 75            x--;
 76        }

 77    }

 78}

 79
 80void main()
 81{
 82    Stack st;
 83    int flag = 1;
 84    char array[1024] , *choice ;
 85    DataType dataType;
 86    st.top = 0;
 87
 88    while(flag)
 89    {
 90        printf("\n\n\n\t\t\t--Stack Demo--\n\n");
 91        printf("\t*****************************************************\n");
 92        printf("\t*                                                   *\n");
 93        printf("\t*   1 - Push into the stack                         *\n");
 94        printf("\t*   2 - Pop out of the stack                        *\n");
 95        printf("\t*   3 - Display the top element' value of the stack *\n");
 96        printf("\t*   4 - Display all the element in the stack        *\n");
 97        printf("\t*                                                   *\n");
 98        printf("\t*                                                   *\n");
 99        printf("\t*   0 - Exit                                        *\n");
100        printf("\t*                                                   *\n");
101        printf("\t*                 This Demo is Made by: Radeon LING *\n");
102        printf("\t*                   Email: Radeon_ling@eastday.com  *\n");
103        printf("\t*                                                   *\n");
104        printf("\t*        Copy right: 2005 Millennium Studio         *\n");
105        printf("\t*****************************************************\n");
106        printf("Please chioce menu number: ");
107        
108        scanf("%s" , &array);
109        choice = array;
110
111        switch(*choice)
112        {
113        case '1':
114            printf("\nPlease enter the char that you want push.\n");
115            scanf("%c" , &dataType);
116            dataType = getchar();
117            Push(&st , dataType);
118            break;
119
120        case '2':
121            Pop(&st);
122            break;
123
124        case '3':
125            dataType = ReadTop(&st);
126            break;
127
128        case '4':
129            ShowStack(&st);
130            break;
131
132
133        case '0':
134            printf("do you want exit? (y or n)");
135            scanf("%s" , &array);
136            choice = array;
137            if('y' == *choice || 'y' == *choice)
138            {
139                flag = 0;
140                printf("end of program.\nwelcome use the millennium software.\n");
141            }

142            break;
143
144        default:
145            printf("Error, please enter 0 - 3, try angain!\n");
146            break;
147        }

148    }

149    return;
150}

Download Demo code: Stack
posted @ 2005-04-27 14:41  Radeon Ling  阅读(163)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3