From ec8df556721da3e8d3df70064bced9628eca8a81 Mon Sep 17 00:00:00 2001 From: Frank Filthaut Date: Thu, 14 Nov 2019 20:45:16 +0100 Subject: [PATCH] add Stack input --- Stack/Stack.cc | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Stack/Stack.cc diff --git a/Stack/Stack.cc b/Stack/Stack.cc new file mode 100644 index 0000000..15ee7e5 --- /dev/null +++ b/Stack/Stack.cc @@ -0,0 +1,57 @@ +#include +using namespace std ; + +const int LEN = 80 ; // default stack length + +struct Stack { + // Implementation + double s[LEN] ; + int count ; + + // Interface + void init() { count = 0 ; } + int nitems() { return count ; } + bool full() { return (count==LEN) ; } + bool empty() { return (count==0) ; } + + void push(double c) { + if (full()) { + cout << "Stack::push() Error: stack is full" << endl ; + return ; + } + s[count++] = c ; + } + + double pop() { + if (empty()) { + cout << "Stack::pop() Error: stack is empty" << endl ; + return 0 ; + } + return s[--count] ; + } +} ; + + +int main() { + + Stack s ; + s.init() ; // initialize Stack + + // Write doubles into Stack + int i ; + for (i=0 ; i<10 ; i++) { + cout << "pushing value " << i*i << " in stack" << endl ; + s.push(i*i) ; + } + + // Count doubles in fifo + cout << s.nitems() << " value in stack" << endl ; + + // Read doubles back from fifo + while (!s.empty()) { + double val = s.pop() ; + cout << "popping value " << val << " from stack" << endl ; + } + + return 0 ; +}