Question
use a linked list to push new data onto array and pop old data off the top of the array complete the code for push
use a linked list to push new data onto array and pop old data off the top of the array complete the code for push and pop functions
#include
using namespace std;
string ltrim(const string &);
string rtrim(const string &);
class node{
// do not edit
public:
float val;
node *next;
node(float v, node *n=NULL){
val = v;
next = n;
}
};
class stackk{
// do not edit
private:
node *head;
int nPts;
void printAll_helper_fcn(node *);
public:
stackk();
~stackk();
void push(float);
float pop();
void printAll();
};
stackk::stackk(){
// do not edit
head = NULL;
nPts = 0; // current number of points in the buffer array
}
stackk::~stackk(){
// do not edit
while (nPts > 0){
this->pop();
}
}
void stackk::push(float x){
// your code goes here
// place x into the linked list
}
float stackk::pop(){
// your code goes here
// return the top (newest) element of the array
// if the array is empty, return the placeholder -1.234
}
void stackk::printAll_helper_fcn(node *ptr){
// no need to edit this
if (ptr->next != NULL)
printAll_helper_fcn(ptr->next);
cout << ptr -> val << endl;
}
void stackk::printAll(){
// no need to edit this
// my gift to you
printAll_helper_fcn(head);
}
void test_stack(int option) {
stackk myStack;
float v;
if (option==0){
// basic push test
myStack.push(1.2);
myStack.push(2.4);
myStack.printAll();
}
else if (option==1){
// test to see what happens if you try pushing
// when the stack is full
myStack.push(1.2);
myStack.push(2.4);
myStack.push(4.5);
myStack.push(7.1);
myStack.push(4.1);
myStack.push(-1.8);
myStack.printAll();
}
else if (option==2){
// basic pop test
myStack.push(1.2);
myStack.push(2.4);
myStack.push(4.5);
myStack.push(7.1);
v = myStack.pop();
cout << v << endl;
}
else if (option==3){
// test to see what happens when you pop from
// an empty list
myStack.push(1.2);
myStack.push(2.4);
v = myStack.pop();
v = myStack.pop();
v = myStack.pop();
cout << v << endl;
}
else if (option==4){
// test to see if you can push, pop, and then push
// without problems
myStack.push(1.2);
myStack.push(2.4);
myStack.pop();
myStack.pop();
myStack.push(4.5);
myStack.push(7.1);
myStack.printAll();
}
}
int main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started