Stack
1
/*
2
This Demo is Made by: Radeon LING
3
Email: Radeon_ling@eastday.com
4
Copy right: 2005 Millennium Studio
5
*/
6
#include<stdio.h>
7
#define STACK_INTSIZE 50
8
typedef char DataType;
9
typedef struct
10
{
11
DataType s[STACK_INTSIZE];
12
int top;
13
}Stack;
14
15
void 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
28
void 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.
44
DataType 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.
61
void 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
80
void 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
}
/*
2
This Demo is Made by: Radeon LING
3
Email: Radeon_ling@eastday.com
4
Copy right: 2005 Millennium Studio
5
*/
6
#include<stdio.h>
7
#define STACK_INTSIZE 50
8
typedef char DataType;
9
typedef struct
10
{
11
DataType s[STACK_INTSIZE];
12
int top;
13
}Stack;
14
15
void 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
28
void 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.
44
DataType 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.
61
void 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
80
void 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


浙公网安备 33010602011771号