C++ visual studio program...Write your own version of a class template that will
ID: 3766037 • Letter: C
Question
C++ visual studio program...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...Please provide notes for the functions
Explanation / Answer
Answer :
struct myStack
{
struct DLLNode *head;
struct DLLNode *mid;
int count;
};
/* Function to create the stack data structure */
struct myStack *createMyStack()
{
struct myStack *ms =
(struct myStack*) malloc(sizeof(struct myStack));
ms->count = 0;
return ms;
};
int pop(struct myStack *ms)
{
/* Stack underflow */
if (ms->count == 0)
{
printf("Stack is empty ");
return -1;
}
struct DLLNode *head = ms->head;
int item = head->data;
ms->head = head->next;
if (ms->head != NULL)
ms->head->prev = NULL;
ms->count -= 1;
if (!((ms->count) & 1 ))
ms->mid = ms->mid->next;
free(head);
return item;
}
// Driver program to test functions of myStack
int main()
{
struct myStack *ms = createMyStack();
cout<<"Item popped is %d "<< pop(ms);
cout<<"Item popped is %d "<<pop(ms);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.