Question: Please edit the following C++ code so that char's and doubles can also be input. Test with the following values and show output please. values

Please edit the following C++ code so that char's and doubles can also be input. Test with the following values and show output please.

values to test: 201, 203.50, 205, 205.8, 206, 206.5

then test: a, b, C, A, B, C, x, y, z

code:

#include "pch.h"

#include

#include

#include

#include

using namespace std;

struct node

{

int info;

struct node *ptr;

}*top, *top1, *temp;

int topelement();

void push(int data);

void pop();

void empty();

void display();

void destroy();

void stack_count();

void create();

int count1 = 0;

int main()

{

int no, ch, e;

cout << " --------------------- Stack using Linked List -------------------";

cout << " | 1 - Push |";

cout << " | 2 - Pop |";

cout << " | 3 - Display |";

cout << " | 4 - Top |";

cout << " | 5 - Stack Count |";

cout << " | 6 - Empty |";

cout << " | 7 - Destroy stack |";

cout << " | 8 - Exit |";

cout << " ------------------------------------------------------------------ ";

create();

while (1)

{

cout << " Enter choice: ";

cin >> ch;

switch (ch)

{

case 1:

cout << " Enter data: ";

cin >> no;

push(no);

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

if (top == NULL)

cout << " No elements in the Stack ";

else

{

e = topelement();

cout << " Top element :" << e << endl;

}

break;

case 5:

stack_count();

break;

case 6:

empty();

break;

case 7:

destroy();

break;

case 8:

exit(0);

default:

cout << " Wrong choice, Please enter correct choice ";

break;

}

}

}

/* Create empty stack */

void create()

{

top = NULL;

}

/* Count stack elements */

void stack_count()

{

cout << " No. of elements in stack: " << count1 << endl;

}

/* Push data into stack */

void push(int data)

{

if (top == NULL)

{

top = (struct node *)malloc(1 * sizeof(struct node));

top->ptr = NULL;

top->info = data;

}

else

{

temp = (struct node *)malloc(1 * sizeof(struct node));

temp->ptr = top;

temp->info = data;

top = temp;

}

count1++;

}

/* Display stack elements */

void display()

{

top1 = top;

if (top1 == NULL)

{

cout << " Stack is empty ";

return;

}

cout << " Stack: ";

while (top1 != NULL)

{

cout << " " << top1->info << endl;

top1 = top1->ptr;

}

}

/* Pop Operation on stack */

void pop()

{

top1 = top;

if (top1 == NULL)

{

cout << " Error : Trying to pop from empty stack ";

return;

}

else

top1 = top1->ptr;

cout << " Popped value: " << top->info << endl;

free(top);

top = top1;

count1--;

}

/* Return top element */

int topelement()

{

return(top->info);

}

/* Check if stack is empty or not */

void empty()

{

if (top == NULL)

cout << " Stack is empty ";

else

cout << " Stack is not empty. It has " << count1 << " elements ";

}

/* Destroy entire stack */

void destroy()

{

top1 = top;

while (top1 != NULL)

{

top1 = top->ptr;

free(top);

top = top1;

top1 = top1->ptr;

}

free(top1);

top = NULL;

cout << " All stack elements destroyed ";

count1 = 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!