File tree Expand file tree Collapse file tree 1 file changed +84
-0
lines changed Expand file tree Collapse file tree 1 file changed +84
-0
lines changed Original file line number Diff line number Diff line change 1+ #include <stdio.h>
2+ #include <stdlib.h>
3+ #define MAX 100
4+
5+ typedef struct {
6+ int top ;
7+ int Stack [MAX ];
8+ }stack ;
9+
10+ void initStack (stack * s ){
11+ s -> top = 0 ;
12+ }
13+
14+ int isFull (stack * s ) {
15+ if (s -> top == MAX )
16+ return 1 ;
17+ else
18+ return 0 ;
19+ }
20+
21+ int isEmpty (stack * s ) {
22+ if (s -> top == 0 )
23+ return 1 ;
24+ else
25+ return 0 ;
26+ }
27+
28+ int pop (stack * s , int * x ) {
29+ if (isEmpty (s ))
30+ return 0 ;
31+ else {
32+ * x = s -> Stack [-- s -> top ];
33+ return 1 ;
34+ }
35+ }
36+
37+ int push (stack * s , int x ) {
38+ if (isFull (s ))
39+ return 0 ;
40+ else {
41+ s -> Stack [s -> top ++ ] = x ;
42+ return 1 ;
43+ }
44+ }
45+
46+ int peek (stack * s , int * x ) {
47+ if (isEmpty (s ))
48+ return 0 ;
49+ else {
50+ int peaky = s -> top - 1 ;
51+ * x = s -> Stack [peaky ];
52+ return 1 ;
53+ }
54+ }
55+
56+ void printStack (stack * s ) {
57+ if (isEmpty (s ))
58+ return ;
59+ else {
60+ int i = s -> top - 1 ;
61+ while ( i >= 0 ) {
62+ printf ("%d " , s -> Stack [i ]);
63+ i -- ;
64+ } printf ("\n" );
65+ }
66+ }
67+
68+ int main (int argc , char * * argv )
69+ {
70+ stack s ;
71+ int x ;
72+ initStack (& s );
73+ push (& s , 5 );
74+ push (& s , 7 );
75+ push (& s , 15 );
76+ printStack (& s );
77+ pop (& s , & x );
78+ printf ("poped value = %d \n" , x );
79+ printStack (& s );
80+ peek (& s , & x );
81+ printf ("peeked value = %d\n" , x );
82+ printStack (& s );
83+ return 0 ;
84+ }
You can’t perform that action at this time.
0 commit comments