LibRCG  3.1.1
stack.c
Go to the documentation of this file.
1 
9 #include <stdlib.h>
10 #include "stack.h"
11 
12 
14 {
15  Stack stack=malloc(sizeof(SStack));
16  if(stack)
17  {
18  stack->size=0;
19  stack->top=NULL;
20  }
21  return stack;
22 }
23 
24 //==============================================================================
25 
26 void stackDelete(Stack stack)
27 {
28  StackNode aux1,aux2;
29  if(stack->size) free(stack);
30  else
31  {
32  for(aux1=stack->top;aux1;)
33  {
34  aux2=aux1;
35  aux1=aux1->next;
36  free(aux2);
37  }
38  free(stack);
39  }
40 }
41 
42 //==============================================================================
43 
44 int stackPush(Stack stack,void* value)
45 {
46  int result=0;
47  StackNode new;
48  new=malloc(sizeof(SStackNode));
49  if(new)
50  {
51  stack->size++;
52  new->value=value;
53  new->next=stack->top;
54  stack->top=new;
55  }
56  else result=1;
57  return result;
58 }
59 
60 //==============================================================================
61 
62 int stackPop(Stack stack,void** value)
63 {
64  int result=0;
65  StackNode aux;
66  if(!stack->size)
67  {
68  if(value) *value=NULL;
69  result=1;
70  }
71  else
72  {
73  if(value) *value=stack->top->value;
74  aux=stack->top;
75  stack->top=stack->top->next;
76  free(aux);
77  stack->size--;
78  }
79  return result;
80 }
81 
82 //==============================================================================
83 
84 int stackTop(Stack stack,void** value)
85 {
86  int result=0;
87  if(!stack->size)
88  {
89  *value=NULL;
90  result=1;
91  }
92  else *value=stack->top->value;
93  return result;
94 }
95 
96 //==============================================================================
97 
98 int stackSize(Stack stack)
99 {
100  return stack->size;
101 }
102 
103 //==============================================================================
104 
105 int stackMap(Stack stack,void(*fun)(void*))
106 {
107  int result=0;
108  StackNode aux;
109  if(!stack->size) result=1;
110  else
111  {
112  for(aux=stack->top;aux;aux=aux->next)
113  {
114  fun(aux->value);
115  }
116  }
117  return result;
118 }
119 
120 //==============================================================================
121 
123 {
124  int ctrl;
125  StackNode aux;
126  Iterator it;
127  it=newIt(stack->size);
128  for(aux=stack->top,ctrl=0;aux&&!ctrl;aux=aux->next)
129  {
130  ctrl=itAdd(it,aux->value);
131  }
132  if(ctrl)
133  {
134  itDelete(it);
135  it=NULL;
136  }
137  return it;
138 }
int stackSize(Stack stack)
Returns the size of a stack.
Definition: stack.c:98
int stackPop(Stack stack, void **value)
Removes an elements from the top of a stack.
Definition: stack.c:62
int stackMap(Stack stack, void(*fun)(void *))
Applies a function to the elements of a stack.
Definition: stack.c:105
Stack node structure.
Definition: stack.h:19
struct sStackNode * next
Next node.
Definition: stack.h:24
Iterator structure.
Definition: iterator.h:17
void itDelete(Iterator it)
Deletes an iterator.
Definition: iterator.c:36
int itAdd(Iterator it, void *val)
Adds an element to an iterator.
Definition: iterator.c:44
void stackDelete(Stack stack)
Deletes a stack.
Definition: stack.c:26
Stack newStack(void)
Creates a stack.
Definition: stack.c:13
void * value
Node's value.
Definition: stack.h:22
Stack structure.
Definition: stack.h:35
int stackPush(Stack stack, void *value)
Inserts an element at the top of the stack.
Definition: stack.c:44
Iterator stackIterator(Stack stack)
Creates an iterator from a stack.
Definition: stack.c:122
StackNode top
Top node of this stack.
Definition: stack.h:40
int stackTop(Stack stack, void **value)
Provides the value of the element at the top of a stack.
Definition: stack.c:84
Implementation of a stack as a linked list.
Iterator newIt(int size)
Creates an iterator.
Definition: iterator.c:12
int size
Number of values of this stack.
Definition: stack.h:38

LibRCG © 2004-2015   Rui Carlos Gonçalves