1 | /* $Id$Revision: */ |
---|---|
2 | /* vim:set shiftwidth=4 ts=8: */ |
3 | |
4 | /********************************************************** |
5 | * See the LICENSE file for copyright information. * |
6 | **********************************************************/ |
7 | |
8 | #include "config.h" |
9 | |
10 | #include "stack.h" |
11 | |
12 | intptr_t StackNotEmpty(stk_stack * theStack) { |
13 | return( theStack ? (intptr_t) theStack->top : 0); |
14 | } |
15 | |
16 | stk_stack * StackJoin(stk_stack * stack1, stk_stack * stack2) { |
17 | if (!stack1->tail) { |
18 | free(stack1); |
19 | return(stack2); |
20 | } else { |
21 | stack1->tail->next=stack2->top; |
22 | stack1->tail=stack2->tail; |
23 | free(stack2); |
24 | return(stack1); |
25 | } |
26 | } |
27 | |
28 | stk_stack * StackCreate() { |
29 | stk_stack * newStack; |
30 | |
31 | newStack=(stk_stack *) SafeMalloc(sizeof(stk_stack)); |
32 | newStack->top=newStack->tail=NULL; |
33 | return(newStack); |
34 | } |
35 | |
36 | |
37 | void StackPush(stk_stack * theStack, DATA_TYPE newInfoPointer) { |
38 | stk_stack_node * newNode; |
39 | |
40 | if(!theStack->top) { |
41 | newNode=(stk_stack_node *) SafeMalloc(sizeof(stk_stack_node)); |
42 | newNode->info=newInfoPointer; |
43 | newNode->next=theStack->top; |
44 | theStack->top=newNode; |
45 | theStack->tail=newNode; |
46 | } else { |
47 | newNode=(stk_stack_node *) SafeMalloc(sizeof(stk_stack_node)); |
48 | newNode->info=newInfoPointer; |
49 | newNode->next=theStack->top; |
50 | theStack->top=newNode; |
51 | } |
52 | |
53 | } |
54 | |
55 | DATA_TYPE StackPop(stk_stack * theStack) { |
56 | DATA_TYPE popInfo; |
57 | stk_stack_node * oldNode; |
58 | |
59 | if(theStack->top) { |
60 | popInfo=theStack->top->info; |
61 | oldNode=theStack->top; |
62 | theStack->top=theStack->top->next; |
63 | free(oldNode); |
64 | if (!theStack->top) theStack->tail=NULL; |
65 | } else { |
66 | popInfo=NULL; |
67 | } |
68 | return(popInfo); |
69 | } |
70 | |
71 | void StackDestroy(stk_stack * theStack,void DestFunc(void * a)) { |
72 | if(theStack) { |
73 | stk_stack_node * x=theStack->top; |
74 | stk_stack_node * y; |
75 | while(x) { |
76 | y=x->next; |
77 | DestFunc(x->info); |
78 | free(x); |
79 | x=y; |
80 | } |
81 | free(theStack); |
82 | } |
83 | } |
84 | |
85 |