Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

DynIntStack.h // Specification file for the DynIntStack class #ifndef DYNINTSTAC

ID: 3713735 • Letter: D

Question

DynIntStack.h

// Specification file for the DynIntStack class
#ifndef DYNINTSTACK_H
#define DYNINTSTACK_H

class DynIntStack
{
private:
// Structure for stack nodes
struct StackNode
{
int value; // Value in the node
StackNode *next; // Pointer to the next node
};

StackNode *top; // Pointer to the stack top

public:
// Constructor
DynIntStack()
{ top = NULL; }

// Destructor
~DynIntStack();

// Stack operations
void push(int);
void pop(int &);
bool isEmpty();

};
#endif

DynIntStack.cpp

#include <iostream>
#include "DynIntStack.h"
using namespace std;

//**************************************************
// Destructor *
// This function deletes every node in the list. *
//**************************************************

DynIntStack::~DynIntStack()
{
StackNode *nodePtr, *nextNode;

// Position nodePtr at the top of the stack.
nodePtr = top;

// Traverse the list deleting each node.
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}

//************************************************
// Member function push pushes the argument onto *
// the stack. *
//************************************************

void DynIntStack::push(int num)
{
StackNode *newNode; // Pointer to a new node

// Allocate a new node and store num there.
newNode = new StackNode;
newNode->value = num;

// If there are no nodes in the list
// make newNode the first node.
if (isEmpty())
{
top = newNode;
newNode->next = NULL;
}
else // Otherwise, insert NewNode before top.
{
newNode->next = top;
top = newNode;
}
}

//****************************************************
// Member function pop pops the value at the top *
// of the stack off, and copies it into the variable *
// passed as an argument. *
//****************************************************

void DynIntStack::pop(int &num)
{
StackNode *temp; // Temporary pointer

// First make sure the stack isn't empty.
if (isEmpty())
{
cout << "The stack is empty. ";
}
else // pop value off top of stack
{
num = top->value;
temp = top->next;
delete top;
top = temp;
}
}

//****************************************************
// Member function isEmpty returns true if the stack *
// is empty, or false otherwise. *
//****************************************************

bool DynIntStack::isEmpty()
{
bool status;

if (!top)
status = true;
else
status = false;

return status;
}

I am using C++ .

Homework 12 All the following assignments are selected from Gaddis edition 7. The purpose is that you practice with stakes. First do the reading assignment; Section 18.1-18-3 (pages 1071-1084). Run your code, submit the output and code too. Submit code in a separate, compliable file, do NOT include it in your pdf or text file. 1. Programming challenges 18.2. Dynamic Stack Template, page 1108 [1 point]. Hint: you can use the DynlntStack class from the book examples. Write a program that takes the names of 3 students and their ages from the user, saves this information in 2 stacks, and then prints the names and ages in the terminal. Use the above template to define the 2 stacks [1 point]. 2.

Explanation / Answer

//DynIntStack.h
// Specification file for the DynIntStack class
#ifndef DYNINTSTACK_H
#define DYNINTSTACK_H

// Declaration of template class
template <class T>
class DynIntStack
{
private:
// Structure for stack nodes
struct StackNode
{
// To store generic type data T, which is template type
T value; // Value in the node
StackNode *next; // Pointer to the next node
};// End of structure definition

StackNode *top; // Pointer to the stack top

public:
// Constructor
DynIntStack()
{ top = NULL; }

// Destructor
~DynIntStack();

// Stack operations
// To push generic type data parameter is T, which is template type
void push(T);
void pop(T &);
bool isEmpty();

};// End of class
#endif

---------------------------------------------------------------------

// DynIntStack.cpp
#include <iostream>
#include "DynIntStack.h"
using namespace std;

//**************************************************
// Destructor *
// This function deletes every node in the list. *
// Template type destructor definition *
//**************************************************
template <class T>
DynIntStack<T>::~DynIntStack()
{
StackNode *nodePtr, *nextNode;

// Position nodePtr at the top of the stack.
nodePtr = top;

// Traverse the list deleting each node.
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}// End of while loop
}// End of function

//***********************************************************
// Member function push pushes the argument onto the stack. *
// Template type function to push a data which is taking *
// a parameter of template type T *
//***********************************************************
template <class T>
void DynIntStack<T>::push(T num)
{
StackNode *newNode; // Pointer to a new node

// Allocate a new node and store num there.
newNode = new StackNode;
newNode->value = num;

// If there are no nodes in the list
// make newNode the first node.
if (isEmpty())
{
top = newNode;
newNode->next = NULL;
}// End of if condition
else // Otherwise, insert NewNode before top.
{
newNode->next = top;
top = newNode;
}// End of else
}// End of function

//***********************************************************
// Member function pop pops the value at the top *
// of the stack off, and copies it into the variable *
// passed as an argument. *
// Template type function to pop a data which is taking *
// a parameter of template type T *
//****************************************************
template <class T>
void DynIntStack<T>::pop(T &num)
{
StackNode *temp; // Temporary pointer

// First make sure the stack isn't empty.
if (isEmpty())
{
cout << "The stack is empty. ";
}// End of if condition
else // pop value off top of stack
{
num = top->value;
temp = top->next;
delete top;
top = temp;
}// End of else
}// End of function

//****************************************************
// Member function isEmpty returns true if the stack *
// is empty, or false otherwise. *
//****************************************************
template <class T>
bool DynIntStack<T>::isEmpty()
{
bool status;

if (!top)
status = true;
else
status = false;

return status;
}// End of function

-----------------------------------------------------------------

// DynIntStackMain.cpp
#include <iostream>
#include <string>
#include "DynIntStack.cpp"
using namespace std;

// main function definition
int main()
{
// Create a stack to student name of type string
DynIntStack <string> studentName;
// Create a stack to student age of type integer
DynIntStack <int> studentAge;
// Local variable to store name and age entered by the user
string name;
int age;
// Loop variable
int x;
// Loops 3 times to push 3 students name and age
for(x = 0; x < 3; x++)
{
// Accepts name of the student
cout<<" Enter student "<<(x + 1)<<" name: ";
cin>>name;
// Calls the function to push the name to stack
studentName.push(name);
// Accepts age of the student
cout<<" Enter student "<<(x + 1)<<" age: ";
cin>>age;
// Calls the function to push the age to stack
studentAge.push(age);
}// End of for loop
// Loop variable is initializes to one
x = 1;
// Loops till either name stack or age stack is not empty
while(!studentName.isEmpty() || !studentAge.isEmpty())
{
// Calls the function to pop the name and stores it in the parameter name variable
// Which is passed as reference
studentName.pop(name);
// Calls the function to pop the age and stores it in the parameter age variable
// Which is passed as reference
studentAge.pop(age);
// Displays student the name and age
cout<<"Student "<<x<<" Name: "<<name<<" Age: "<<age<<endl;
}// End of while loop
}// End of main function

Sample Output:

Enter student 1 name: Rakesh

Enter student 1 age: 45

Enter student 2 name: Amit

Enter student 2 age: 33

Enter student 3 name: Pyari

Enter student 3 age: 25
Student 1 Name: Pyari Age: 25
Student 1 Name: Amit Age: 33
Student 1 Name: Rakesh Age: 45