C++ class that implements a “stack” data structure for storing floating-point values. Required methods: Push, Pop, isEmpty, and isFull.

/*
 * pshrestha_a2.cpp
 *
 *
 *      Created on: Nov 7, 2010
 *
Write a C++ class that implements a “stack” data structure for storing floating-point values. Required
methods: Push, Pop, isEmpty, and isFull. Feel free to come up with any additional methods you think a
programmer would find useful.

Implement your “stack” object using a singly linked list.

Write an associated “driver” C++ program that demonstrates the use of your “stack” class.
 */

#include <iostream>
#include <cctype>
#include <iomanip>

using namespace std;
const int MAX_ELEMENTS = 10; // Maximum Number of nodes our stack supports.

struct Stack_Node {
// Structure for the node containing a value and the next node.
double Value;
struct Stack_Node* Next;
};

class Stack {
private:
struct Stack_Node* p; // Current
struct Stack_Node* top;
int nodeCounter; // To count the nodes.
public:
Stack();
bool push(double&);
bool isEmpty();
bool isFull();
double pop(bool&);
~Stack();
};

Stack::Stack() {
// Default Constructor
top = NULL;
nodeCounter = 0;
}

bool Stack::push(double& input) {
// Method for push operation.
if(!isFull()) { // operates only when the stack is not full.
p = new struct Stack_Node; // Dynamic Allocation of node.
p->Value = input; // input value is put in to the node p.
p->Next = NULL; // the node is pointed to nothing.
if(top){
p->Next = top;
}
top = p;
++ nodeCounter;
return true;
}
return false;
}

double Stack::pop(bool& logicVal) {
// Performs the pop operation.
double popValue; // variable where we store the value to be popped.
if(!isEmpty()) {
// operates only when the stack is not empty.
popValue = top->Value; //putting the top's value in to popValue variable.
p = top;
top = top->Next; //
--nodeCounter;
delete p; // deleting the node.
logicVal = true;
return popValue; // Returning the topmost node to the function.
} else {
logicVal = false;
return 7;
}
}

bool Stack::isEmpty() {
// Bool method to test if the stack is empty or not.
if(top == NULL) {
return true;
} else {
return false;
}

// Alternative way for the same code above.
// return Top == NULL;
}

bool Stack::isFull() {
// Bool method to test if the stack is full or not.
return nodeCounter == MAX_ELEMENTS;

// Alternative way for the same code above.
/*if(nodeCounter == MAX_ELEMENTS) {
return true;
} else {
return false;
} */
}


Stack::~Stack() {
// Destructor for the De-allocation of the memory.
while(top) {
p = top;
top = top->Next;
delete p;
}
}

int main() {
Stack stackOne; // Instance of the class Stack.
double userInput; // Variable to hold the user Input.
int numTimesPush; // Variable to hold the number of time a user want to perform the push operation

cout << endl << "How many times you want to perform push() Operation? ";
cin >> numTimesPush;
for (int i = 1; i <= numTimesPush; ++i ) {
// For loop for the number of time user want to push the item in to stack.
cout << endl << "Enter the floating point number to push in the stack: ";
cin >> userInput;
//stackOne.push(userInput);
if (stackOne.push(userInput)) {
// if we are able to push user input then,
cout << endl << "Your Input is pushed in to the Stack " << i << " times." << endl;
} else {
// if we are not able to push the user input then,
cout << endl << "The Stack is full." << "It is more than " << MAX_ELEMENTS << " elements." << endl;
}
}

int numTimesPop; // Variable to hold the number of times the user want to perform the pop operation.
bool logicValue; // Boolian variable to hold the logical state of true or false.
double popedValue; // Variable to hold the value that would be popped out of the stack.

cout << endl << "How many times you want to perform pop() Operation? ";
cin >> numTimesPop;

for (int j = 1; j <= numTimesPop; ++j) {
// For loop for the number of times user want to operate pop operation.
popedValue = stackOne.pop(logicValue);
if (logicValue) {
cout << endl << "The value popped out of Stack = " << popedValue;
} else {
cout << endl << "Sorry!! The Stack is Empty.";
}
}
return 0;
}

Advertisement: 

Contact us for Software Development, AI, and SEO Marketing - I Code Mind LLC

Comments

Popular posts from this blog

C++ program that reads a textual data file and builds linked lists using the data therein.

Concept of recursive function to take user input of two numbers and performs an addition of those two numbers