Debian Codegraph @

src/dstack.cpp

raw symbols SCIP
1 // $Id: dstack.cpp,v 1.4 2003/04/14 05:51:03 mbridak Exp $
2 // Simple Stack Class
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include "dstack.h"
7
8 DStack::DStack()
9 {
10 bottom_ = NULL;
11 top_ = NULL;
12 number_items = 0;
13 }
14
15 DStack::~DStack() // how to reclaim memory from local
16 { // stacks when exiting functions
17 clear_stack();
18 }
19
20
21 // erase any/all items on the stack
22 void DStack::clear_stack()
23 {
24 STACKVALUE *current, *next_value;
25 current = bottom_;
26 while (current)
27 {
28 next_value = (STACKVALUE *) current->next;
29 free(current);
30 current = next_value;
31 }
32
33 bottom_ = top_ = NULL;
34 number_items = 0;
35 }
36
37
38 int DStack::num_items() // number of items currently in stack
39 {
40 return number_items;
41 }
42
43 int DStack::push(float val) // push a new value
44 {
45 STACKVALUE *new_value;
46
47 new_value = (STACKVALUE *) calloc(1, sizeof(STACKVALUE));
48 if (! new_value)
49 return FALSE;
50 new_value->value = val;
51 if (top_)
52 {
53 top_->next = new_value;
54 new_value->previous = top_;
55 top_ = new_value;
56 }
57 else // no values exist
58 {
59 bottom_ = top_ = new_value;
60 }
61 number_items++;
62 return TRUE;
63 }
64
65 float DStack::pop() // pop value from top
66 {
67 float my_return = 0.0;
68 STACKVALUE *new_top = NULL;
69
70 if (top_)
71 {
72 my_return = top_->value;
73 new_top = (STACKVALUE *) top_->previous;
74 free(top_);
75 top_ = new_top;
76 number_items--;
77 if (! top_)
78 bottom_ = NULL;
79 }
80 return my_return;
81 }
82
83
84
85 int DStack::empty() // 1 if empty, 0 otherwise
86 {
87 if (number_items)
88 return FALSE;
89 else
90 return TRUE;
91 }
92
93
94
95 void DStack::print()
96 {
97 STACKVALUE *current;
98 printf("Stack holds %d items.\n", number_items);
99 current = bottom_;
100 while (current)
101 {
102 printf("Value: %f\n", current->value);
103 current = (STACKVALUE *) current->next;
104 }
105 }
106