Graphviz  2.41.20170921.2350
stack.c
Go to the documentation of this file.
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 
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 
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 
DATA_TYPE info
Definition: stack.h:45
void StackDestroy(stk_stack *theStack, void DestFunc(void *a))
Definition: stack.c:71
struct stk_stack_node * next
Definition: stack.h:46
intptr_t StackNotEmpty(stk_stack *theStack)
Definition: stack.c:12
void StackPush(stk_stack *theStack, DATA_TYPE newInfoPointer)
Definition: stack.c:37
stk_stack_node * tail
Definition: stack.h:51
#define DATA_TYPE
Definition: stack.h:41
DATA_TYPE StackPop(stk_stack *theStack)
Definition: stack.c:55
#define NULL
Definition: logic.h:39
stk_stack * StackJoin(stk_stack *stack1, stk_stack *stack2)
Definition: stack.c:16
stk_stack_node * top
Definition: stack.h:50
stk_stack * StackCreate()
Definition: stack.c:28
void * SafeMalloc(size_t size)
Definition: misc.c:56