Data Structure-stack (implemented in C)
1/* stack header file */
2 # include <stdio. h>
3 # include <stdlib. h>
4
5 typedef struct Node
6 {
7 int data;
8 struct node * next;
9} stacknode;
10
11 void initstack (stacknode * top)
12 {
13 top-> next = NULL;
14}
15
16 int isempty (stacknode * top)
17 {
18 if (top-> next = NULL)
19 return 1;
20 return 0;
21}
22
23 void push (stacknode * Top, int item)
24 {
25 stacknode * temp = (stacknode *) malloc (sizeof (stacknode ));
26 if (temp = NULL)
27 {
28 printf ("malloc memory fail ");
29 exit (1 );
30}
31 temp-> DATA = item;
32 temp-> next = Top-> next;
33 Top-> next = temp;
34}
35
36 int POP (stacknode * top)
37 {
38 stacknode * temp;
39 int tempint;
40 if (Top = NULL)
41 {
42 printf ("there is no elements/N ");
43 exit (1 );
44}
45 temp = Top-> next;
46 tempint = temp-> data;
47 top-> next = temp-> next;
48 free (temp );
49 return tempint;
50}
51
52 int gettop (stacknode * top)
53 {
54 if (Top = NULL)
55 {
56 printf ("there is no elements/N ");
57 exit (1 );
58}
59 return top-> next-> data;
60}
61
62 void clearstack (stacknode * top)
63 {
64 stacknode * temp;
65 while (top)
66 {
67 temp = top;
68 Top = Top-> next;
69 free (temp );
70}
71}
1/* test stack */
2 # include "stacklist. H"
3
4 int main ()
5 {
6 int temp;
7 stacknode * top;
8 Top = (stacknode *) malloc (sizeof (stacknode ));
9 initstack (top );
10 push (top, 10 );
11 push (top, 20 );
12 temp = POP (top );
13 printf ("% d/N", temp );
14 temp = gettop (top );
15 printf ("% d/N", temp );
16
17 POP (top );
18 // POP (top );
19 temp = isempty (top );
20 printf ("% d/N", temp );
21 clearstack (top );
22
23}
Http://www.cnblogs.com/hstcghost/archive/2010/11/07/1871050.html