数据结构 链式栈
栈有别于队列,属于先进后出,比较简单,直接看源代码:
view plaincopy to clipboardprint?
1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <string.h>
4.
5. #define NUM 10
6.
7. typedef struct _StackNode
8. {
9. int data;
10. struct _StackNode *next;
11. }StackNode;
12.
13. typedef struct _StackList
14. {
15. StackNode *top; //栈顶指针,标记栈当前位置,动态地随push和pop函数调用变化而变化
16. }StackList;
17.
18. /**
19. * 栈置空
20. *
21. */
22. void InitStack( StackList *s )
23. {
24. s->top = NULL;
25. }
26.
27. /**
28. * 判断栈是否为空
29. *
30. */
31. int IsStackEmpty( StackList *s )
32. {
33. return s->top == NULL;
34. }
35.
36. /**
37. * 进栈
38. *
39. */
40. void Push( StackList *s, int data )
41. {
42. //将新元素插入栈的头部
43. StackNode *pNode = ( StackNode * )malloc( sizeof( StackNode ) );
44. pNode->data = data;
45. pNode->next = s->top;
46. s->top = pNode;
47. }
48.
49. /**
50. * 出栈
51. *
52. */
53. int Pop( StackList *s )
54. {
55. int x;
56. StackNode *pNode = s->top; //保存栈顶指针
57. if( IsStackEmpty( s ) )
58. {
59. printf( "Stack underflow" );
60. exit( 0 );
61. }
62. x = pNode->data; //保存栈顶节点数据
63. s->top = pNode->next; //将栈顶指针下移一个位置
64. free( pNode ); //释放原栈顶空间
65. return x;
66. }
67.
68. /**
69. * 取栈顶元素
70. *
71. */
72. int GetTopStack( StackList *s )
73. {
74. if( IsStackEmpty( s ) )
75. {
76. printf( "Stack is empty" );
77. exit( 0 );
78. }
79. return s->top->data;
80. }
81.
82.
83. int main( void )
84. {
85. StackList *s;
86. int i;
87.
88. s = ( StackList * )malloc( sizeof( StackList ) );
89.
90. InitStack( s ); //初始化栈
91.
92. for( i = 0; i < NUM; i++ )
93. {
94. Push( s, i*2+1 ); //入栈处理
95. }
96.
97. i = GetTopStack( s ); //获取栈顶数据
98. printf( "%d \n", i );
99.
100. printf( "出栈数据:\n" );
101. while( !IsStackEmpty( s ) )
102. {
103. i = Pop( s );
104. printf( "%d ", i );
105. }
106. return 0;
107. }
view plaincopy to clipboardprint?
1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <string.h>
4.
5. #define NUM 10
6.
7. typedef struct _StackNode
8. {
9. int data;
10. struct _StackNode *next;
11. }StackNode;
12.
13. typedef struct _StackList
14. {
15. StackNode *top; //栈顶指针,标记栈当前位置,动态地随push和pop函数调用变化而变化
16. }StackList;
17.
18. /**
19. * 栈置空
20. *
21. */
22. void InitStack( StackList *s )
23. {
24. s->top = NULL;
25. }
26.
27. /**
28. * 判断栈是否为空
29. *
30. */
31. int IsStackEmpty( StackList *s )
32. {
33. return s->top == NULL;
34. }
35.
36. /**
37. * 进栈
38. *
39. */
40. void Push( StackList *s, int data )
41. {
42. //将新元素插入栈的头部
43. StackNode *pNode = ( StackNode * )malloc( sizeof( StackNode ) );
44. pNode->data = data;
45. pNode->next = s->top;
46. s->top = pNode;
47. }
48.
49. /**
50. * 出栈
51. *
52. */
53. int Pop( StackList *s )
54. {
55. int x;
56. StackNode *pNode = s->top; //保存栈顶指针
57. if( IsStackEmpty( s ) )
58. {
59. printf( "Stack underflow" );
60. exit( 0 );
61. }
62. x = pNode->data; //保存栈顶节点数据
63. s->top = pNode->next; //将栈顶指针下移一个位置
64. free( pNode ); //释放原栈顶空间
65. return x;
66. }
67.
68. /**
69. * 取栈顶元素
70. *
71. */
72. int GetTopStack( StackList *s )
73. {
74. if( IsStackEmpty( s ) )
75. {
76. printf( "Stack is empty" );
77. exit( 0 );
78. }
79. return s->top->data;
80. }
81.
82.
83. int main( void )
84. {
85. StackList *s;
86. int i;
87.
88. s = ( StackList * )malloc( sizeof( StackList ) );
89.
90. InitStack( s ); //初始化栈
91.
92. for( i = 0; i < NUM; i++ )
93. {
94. Push( s, i*2+1 ); //入栈处理
95. }
96.
97. i = GetTopStack( s ); //获取栈顶数据
98. printf( "%d \n", i );
99.
100. printf( "出栈数据:\n" );
101. while( !IsStackEmpty( s ) )
102. {
103. i = Pop( s );
104. printf( "%d ", i );
105. }
106. return 0;
107. }
浙公网安备 33010602011771号