Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Current Trends In Database Technology Edbt 2006 Edbt 2006 Workshops Phd Datax Iidb Iiha Icsnw Qlqp Pim Parma And Reactivity On The Web Munich Germany March 2006 Revised Selected Papers Lncs 4254

Authors: Torsten Grust ,Hagen Hopfner ,Arantza Illarramendi ,Stefan Jablonski ,Marco Mesiti ,Sascha Muller ,Paula-Lavinia Patranjan ,Kai-Uwe Sattler ,Myra Spiliopoulou ,Jef Wijsen

2006th Edition

3540467882, 978-3540467885

More Books

Students also viewed these Databases questions

Question

Describe the costs associated with a lack of or inadequate HRP.

Answered: 1 week ago