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

I need expert help with this program. Please provide source code in C++. Please

ID: 3869347 • Letter: I

Question

I need expert help with this program. Please provide source code in C++. Please comment code; show headings for .cpp and .h files; show sample output of functioning program. The first part of the program uses a class template to create a dynamic stack. The second part of the program must integrate this stack class to handle inventory items. Please see the details for the program as follows:

/* Program Requirements Listed Below This Line */

Write your own version of a class template that will create a dynamic stack of any data type. The pop function must return a bool; it should return a false if it was not able to pop an item off the stack. Otherwise, it returns true. The parameter to the pop function is passed by reference and should be the item on the list if it was able to pop something. Create a driver program (main) that shows that the stack works for at least two different data types.

Then, using the previously created stack class, you will create a class called InventoryItem. This class will have its class declaration in InventoryItem.h and its implementation in InventoryItem.cpp. It will have three private data members, an integer serialNum which holds the part’s serial number, manufactDate which should be a string that holds the date the item was manufactured, then lotNum which will be an integer that holds the part’s lot number. The program should then create a stack with a data type of InventoryItem (stack<InventoryItem>). The program should loop asking the user to enter in new items to the inventory stack or to remove an item from the inventory stack. The loop should continue until the user indicates they are done. This should be menu driven. When adding an item, the program should ask the user for the information it needs for the 3 data members of the InventoryItem class and add a new item to the stack. When removing an item from the stack, the program should display all of the information in the InventoryItem object that was popped off the stack. When the program ends, it should pop all of the remaining items off the stack and display the data that is in the Inventory items as it pops them off. There should be 3 utility functions that main uses.

    void popItem(DynStack<InventoryItem>* stack) // pops the item off the stack and displays it.
    void pushItem(DynStack<InventoryItem>* stack) // pushes the item onto the stack
    int menu(); // displays the menu and returns the user’s choice.

Explanation / Answer

//Print function is added to the stack.h header file

//and test program to test the generic stack
//Stack.h
#ifndef STACK_H
#define STACK_H
#include <iostream>
using namespace std;

// This class template creates a dynamic stack of
// of any data type. It has a pop function that
// returns a bool. The parameter to the pop function
// is passed by reference and should be the item on
// the list if it was able to pop something.

template <class T>
class Stack
{
private:
   // Structure for the stack nodes
   struct StackNode
   {
       T value; // Value in the node
       StackNode *next; // Pointer to the next node
   };

   StackNode *top; // Pointer to the stack top

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

   // Destructor
   ~Stack();

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

};

//*********************************************************
// Destructor *
//*********************************************************
template <class T>
Stack<T>::~Stack()
{
   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. *
//*********************************************************
template <class T>
void Stack<T>::push(T item)
{
   StackNode *newNode; // Pointer to a new node

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

   // 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 to print elements from the stack*
//*********************************************************
template <class T>
void Stack<T>::print()
{
   StackNode *temp=top;
   T item;
   // Traverse the stack until the temp node is not null
   while (temp != NULL)
   {
       item = temp->value;
       cout<<item<<endl;
       temp = temp->next;
   }
}

//*********************************************************
// Member function pop pops the value at the top of the *
// stack off, and copies it into the variable passed as an*
// argument. *
//*********************************************************
template <class T>
void Stack<T>::pop(T &item)
{
   StackNode *temp; // Temporary pointer

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

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

   if (!top)
   {
       status = true;
   }
   else
   {
       status = false;
   }
   return status;
}
#endif

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


//test program for stack
#include <iostream>
#include <string>
#include "Stack.h"
using namespace std;

int main()
{

   //stack for integers
   Stack<int> stack;
   stack.push(2);
   stack.push(3);
   stack.push(4);

   int top=0;
  
   cout<<"--->integer stack<---"<<endl;
   cout<<"Integer stack"<<endl;
   cout<<"---------------"<<endl;
   stack.print();
   cout<<"Popping integers"<<endl;
   cout<<"---------------"<<endl;
   stack.pop(top);
   cout<<top<<endl;
   stack.pop(top);
   cout<<top<<endl;
   stack.pop(top);
   cout<<top<<endl;

   //stack for characters
   Stack<char> charstack;
   charstack.push('a');
   charstack.push('b');
   charstack.push('c');

   char ch;
  
   cout<<"--->Character stack<---"<<endl;
   cout<<"Character stack"<<endl;
   cout<<"---------------"<<endl;
   charstack.print();
   cout<<"Popping characters elements"<<endl;
   cout<<"---------------"<<endl;
   charstack.pop(ch);
   cout<<ch<<endl;
   charstack.pop(ch);
   cout<<ch<<endl;
   charstack.pop(ch);
   cout<<ch<<endl;

   //stack for string
   Stack<string> stringStack;
   stringStack.push("windows");
   stringStack.push("apple");
   stringStack.push("unix");

   string os;
  
   cout<<"--->String stack<---"<<endl;
   cout<<"String stack"<<endl;
   cout<<"---------------"<<endl;
   stringStack.print();
   cout<<"Popping strings elements"<<endl;
   cout<<"---------------"<<endl;
   stringStack.pop(os);
   cout<<os<<endl;
   stringStack.pop(os);
   cout<<os<<endl;
   stringStack.pop(os);
   cout<<os<<endl;

  

   system("pause");
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote