/* * *********************************************************** * I hope somebody could tell me a implementation way * that is also valid for the following code. */stack* stc = (stack*)malloc(sizeof(stack));
int i = 0
for(i = 0; i < 3; i++)
{
stack_push(&stc, &i);
/* addresses of i are always the same!!! */ stack_push(&stc, i);
/* i is chaning for ++. */}
/* * I get no way to make it's possible to accept these two kind of data * as stack data stored. and I have to write a implementation for proto * data type data and another implementation for pointer data type. * In C++, it's possible to achieve these goals. but how to get it in C? */free(stc); stc = NULL;
/* ************************************************************ *
* here is a implementation for stack pointer data */
1
2 /******************************************************************************
3 * pstack
4 * used for pointers. never use this pstack to store char, short, int or other
5 * nonpointers.
6 * if you really want store int data into pstack, please define a struct as a
7 * package or a kind of encapsulation. then just store the pointer of struct.
8 ******************************************************************************/
9 struct _pstack {
10 struct _pstack* next;
11 void* data; // to store the pointer.
12 };
13 typedef struct _pstack pstack;
14
15 pstack* pstack_create() {
16 return NULL;
17 }
18
19 bool pstack_push(pstack** ptr, const void* data) {
20 pstack* nod = (pstack*)malloc(sizeof(pstack));
21 if(nod == NULL) return false; // failure!
22 nod->next = *ptr;
23 nod->data = data;
24 *ptr = nod;
25 return true; // success!
26 }
27
28 void* pstack_pop(pstack** ptr) {
29 assert(ptr != NULL && *ptr != NULL);
30 pstack* top = *ptr;
31 void* rtv = top->data;
32 *ptr = (*ptr)->next;
33 free(top);
34 return rtv;
35 }
36
37 void* pstack_top(const pstack* ptr) {
38 assert(ptr != NULL);
39 return ptr->data;
40 }
41
42 bool pstack_isempty(const pstack* ptr) {
43 return ptr == NULL ? true : false;
44 }
45
46 bool pstack_hasmore(const pstack* ptr) {
47 return ptr == NULL ? false : true;
48 }
49
50 size_t pstack_size(const pstack* ptr) {
51 size_t rtv = 0;
52 while(ptr != NULL) {
53 rtv ++;
54 ptr = ptr->next;
55 }
56 return rtv;
57 }
58
59 void pstack_free(pstack** ptr)
60 {
61 if(ptr == NULL || *ptr == NULL) return;
62 pstack* nod = *ptr;
63 pstack* nxt = NULL;
64 while(nod != NULL) {
65 nxt = nod->next;
66 free(nod);
67 nod = nxt;
68 }
69 *ptr = NULL;
70 }
71 /******************************************************************************/
/* * running demo */
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdext.h>
4
5 int main()
6 {
7 pstack* stc = NULL;
8 struct _s
9 {
10 int i;
11 };
12 typedef struct _s s;
13
14 s a; a.i = 1;
15 s b; b.i = 2;
16 s c; c.i = 3;
17 s d; d.i = 4;
18 s e; e.i = 5;
19
20 pstack_push(&stc, &a);
21 pstack_push(&stc, &b);
22 pstack_push(&stc, &c);
23 pstack_push(&stc, &d);
24 pstack_push(&stc, &e);
25
26 int siz = pstack_size(stc);
27 printf("prev siz == %d\n", siz);
28 while(!pstack_isempty(stc))
29 {
30 s* top = (s*)pstack_top(stc);
31 s* ptr = (s*)pstack_pop(&stc);
32 if(top != ptr) puts("error");
33 else printf("ptr->i = %d\n", ptr->i);
34 }
35 pstack_free(&stc);
36 siz = pstack_size(stc);
37 printf("post siz == %d\n", siz);
38
39 return 0;
40 }
41
/* outcome */
[root@localhost tmp]# gcc pstack.c
[root@localhost tmp]# a.out
prev siz == 5
ptr->i = 5
ptr->i = 4
ptr->i = 3
ptr->i = 2
ptr->i = 1
post siz == 0
[root@localhost tmp]#