1
0
Fork 0

add Stack input

This commit is contained in:
Frank Filthaut 2019-11-14 20:45:16 +01:00 committed by Eric Teunis de Boone
parent d1547c1611
commit ec8df55672

57
Stack/Stack.cc Normal file
View file

@ -0,0 +1,57 @@
#include <iostream>
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 ;
}