数据结构 顺序栈
原创 数据结构 顺序栈 收藏
顺序栈与链式栈虽然形不似,但神似,大致一样,比较简单,以备后查,特此留证:
view plaincopy to clipboardprint?
1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <string.h>
4.
5. #define NUM 10
6. #define DataSize 10
7.
8. typedef struct _SeqStack
9. {
10. int top; //栈当前位置
11. int array[DataSize]; //放置数据
12. }SeqStack;
13. /**
14. * 栈置空
15. *
16. */
17. void InitStack( SeqStack *s )
18. {
19. s->top = -1;
20. }
21.
22. /**
23. * 判断栈是否为空
24. *
25. */
26. int IsStackEmpty( SeqStack *s )
27. {
28. return s->top == -1;
29. }
30.
31. /**
32. * 判断栈是否满
33. *
34. */
35. int IsStackFull( SeqStack *s )
36. {
37. return ( s->top > ( DataSize-1 ) );
38. }
39.
40. /**
41. * 进栈
42. *
43. */
44. void Push( SeqStack *s, int data )
45. {
46. if( IsStackFull( s ) )
47. {
48. printf( "Stack overflow!\n" );
49. exit( 0 );
50. }
51. s->array[++(s->top)] = data;
52. }
53.
54. /**
55. * 出栈
56. *
57. */
58. int Pop( SeqStack *s )
59. {
60. int x;
61. if( IsStackEmpty( s ) )
62. {
63. printf( "Stack underflow!\n" );
64. exit( 0 );
65. }
66. x = s->array[s->top];
67. --( s->top );
68. return x;
69. }
70.
71. /**
72. * 取栈顶元素
73. *
74. */
75. int GetTopStack( SeqStack *s )
76. {
77. if( IsStackEmpty( s ) )
78. {
79. printf( "Stack is empty" );
80. exit( 0 );
81. }
82. return s->array[ s->top ];
83. }
84.
85.
86. int main( void )
87. {
88. SeqStack *s;
89. int i = 0;
90. s = ( SeqStack * )malloc( sizeof( SeqStack ) );
91.
92. InitStack( s );
93. while( i < 10 )
94. {
95. Push( s, i*2+1 );
96. i++;
97. }
98. //获取顶元素
99. i = GetTopStack( s );
100. printf( "栈顶元素为:%d\n", i );
101.
102. printf( "出栈元素为:" );
103.
104. //出栈元素
105. while( !IsStackEmpty( s ) )
106. {
107. i = Pop( s );
108. printf( "%d ", i );
109. }
110. return 0;
111. }
顺序栈与链式栈虽然形不似,但神似,大致一样,比较简单,以备后查,特此留证:
view plaincopy to clipboardprint?
1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <string.h>
4.
5. #define NUM 10
6. #define DataSize 10
7.
8. typedef struct _SeqStack
9. {
10. int top; //栈当前位置
11. int array[DataSize]; //放置数据
12. }SeqStack;
13. /**
14. * 栈置空
15. *
16. */
17. void InitStack( SeqStack *s )
18. {
19. s->top = -1;
20. }
21.
22. /**
23. * 判断栈是否为空
24. *
25. */
26. int IsStackEmpty( SeqStack *s )
27. {
28. return s->top == -1;
29. }
30.
31. /**
32. * 判断栈是否满
33. *
34. */
35. int IsStackFull( SeqStack *s )
36. {
37. return ( s->top > ( DataSize-1 ) );
38. }
39.
40. /**
41. * 进栈
42. *
43. */
44. void Push( SeqStack *s, int data )
45. {
46. if( IsStackFull( s ) )
47. {
48. printf( "Stack overflow!\n" );
49. exit( 0 );
50. }
51. s->array[++(s->top)] = data;
52. }
53.
54. /**
55. * 出栈
56. *
57. */
58. int Pop( SeqStack *s )
59. {
60. int x;
61. if( IsStackEmpty( s ) )
62. {
63. printf( "Stack underflow!\n" );
64. exit( 0 );
65. }
66. x = s->array[s->top];
67. --( s->top );
68. return x;
69. }
70.
71. /**
72. * 取栈顶元素
73. *
74. */
75. int GetTopStack( SeqStack *s )
76. {
77. if( IsStackEmpty( s ) )
78. {
79. printf( "Stack is empty" );
80. exit( 0 );
81. }
82. return s->array[ s->top ];
83. }
84.
85.
86. int main( void )
87. {
88. SeqStack *s;
89. int i = 0;
90. s = ( SeqStack * )malloc( sizeof( SeqStack ) );
91.
92. InitStack( s );
93. while( i < 10 )
94. {
95. Push( s, i*2+1 );
96. i++;
97. }
98. //获取顶元素
99. i = GetTopStack( s );
100. printf( "栈顶元素为:%d\n", i );
101.
102. printf( "出栈元素为:" );
103.
104. //出栈元素
105. while( !IsStackEmpty( s ) )
106. {
107. i = Pop( s );
108. printf( "%d ", i );
109. }
110. return 0;
111. }
浙公网安备 33010602011771号