I am trying to write numbers from type double outside of the stack but I can't quite work it out how to do it. I have tried doing it with while loop and if statements, sometimes I get only zeros but most of the time I get nothing displayed. Thanks for helping in advance.
#include <stdio.h>
struct element
{
double data;
element * next;
};
element *top = NULL;
void Get(double &x)
{
element *p;
if(top == NULL)
{
printf("
The stack is empty!");
return;
}
x = top -> data;
p = top;
top = top -> next;
delete(p);
}
void Put(double x)
{
element *p;
p = new(element);
if(p == NULL)
{
printf("
NOT enough memory!");
return;
}
p -> data = x;
p -> next = top;
top = p;
}
int main()
{
printf("Enter integers in stack. Enter 0 to end!
");
double val;
double m;
do
{
printf("Enter number: ");
scanf("%lf", &val);
if(val >= 3697 && val <= 3698)
{
Put(val);
}
}
while(val != 0);
printf("
Stack contains: ");
while(top != NULL)
{
Get(val);
printf("%lf ", val);
}
return 0;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…