Ihr Problem ist Zeiger. Wenn Sie n1 verwenden, enthält void * immer den letzten n1-Wert, da er auf n1 zeigt. Dann, wenn Sie die Liste (queue_remove_double) freizugeben, ist immer der Wert von n1 zeigen (die aktuellste) ...
Einige Lösungen:
(1) Array - unten zeigte - zeitlich begrenzt ist ....
(2) neuer Doppel Wert mit der Kopie von variablen (malloc Doppel) - „unbegrenzter,“ (Grenze des Speichers ...)
im neuen Doppel Fall:
List* insert_queue_end (List* end, void* v) {
List* p = (List*) malloc(sizeof(List));
double *data=(double*)malloc(sizeof(double)); /*New double new address*/
*data=*((double*)v); /*Copy the VALUE and not the address to new double in new and fixed address*/
/*void* points to a new double with a new address in memory */
p->info=data;
p->prox=NULL;
if(end!=NULL) {
end->prox=p;
}
return p;
}
vergessen Sie nicht, Befreie den Speicher vom neuen Double in der Remove-Funktion!
einige Lösungen:
LÖSUNG 1 - Das Array von Doppel:
#include "stdio.h"
#include "stdlib.h"
typedef struct list {
void *info;
struct list *prox;
} List;
typedef struct queue {
List* begin;
List* end;
} Queue;
Queue* create_queue (void) {
Queue* f = (Queue*) malloc(sizeof(Queue));
f->begin = f->end = NULL;
return f;
}
/*Yes the type of return is List* and not Queue* - check your code */
List* insert_queue_end (List* end, void* v) {
List* p = (List*) malloc(sizeof(List));
///If you want create a new double this is the local - don't use the same address in v !
p->info=v;
p->prox=NULL;
if(end!=NULL) {
end->prox=p;
}
return p;
}
double queue_empty_double(Queue* f) {
return (f->begin==NULL);
}
double queue_remove_double(Queue* f) {
List* t;
double v;
double isRemoved=0;
if(queue_empty_double(f)) {
exit(1);
}
t=f->begin;
v=*((double*)(t->info));
f->begin=t->prox;
if (f->begin==NULL) {
f->end = NULL;
}
free(t);
printf("%.3lf\n",v);
}
void insert_queue(Queue* f, void* v) {
f->end = insert_queue_end(f->end,v);
if(f->begin==NULL) {
f->begin=f->end;
}
}
void print_queue_double(Queue* f) {
List* i;
for(i=f->begin;i!=NULL;i=i->prox)
printf("%.3lf\n",*((double*)i->info));
}
int main() {
Queue* f;
f = create_queue();
char ent1[100];
///One Double Allocated - only one address in memory !!!
///double n1;
double listOfDoubles[20]; ///All doubles allocated and one per one have a address in memory !!!
int countDoubles=0;
scanf("%s",ent1);
while(ent1[0]!='X' && countDoubles < 20)
{
if(ent1[0]=='E') {
///IN YOUR CODE:
///Fill the same one double...
///scanf("%lf",&n1);
///Testing new code
scanf("%lf",&listOfDoubles[countDoubles]); ///a new double with your address
///IN YOUR CODE:
///The same address of one variable. The same result.
///Remember void * is a pointer and use the address of n1, and then, print the most recent &n1 content...
///insert_queue(f,&n1);
///A new double in a new address, the content is not equal
insert_queue(f,&listOfDoubles[countDoubles]);
countDoubles++;
}
else if (ent1[0]=='D') {
///free the address and decrement the index of array to reuse a double
queue_remove_double(f);
countDoubles--;
}
else
{
///Print the list - and see the result "debug"
print_queue_double(f);
}
scanf("%s",ent1);
}
free(f);
}
einige Lösungen:
SOLUTION 2 - Eine neue Doppel mit der Kopie der Wert
#include "stdio.h"
#include "stdlib.h"
typedef struct list {
void *info;
struct list *prox;
} List;
typedef struct queue {
List* begin;
List* end;
} Queue;
Queue* create_queue (void) {
Queue* f = (Queue*) malloc(sizeof(Queue));
f->begin = f->end = NULL;
return f;
}
/*Yes the type of return is List* and not Queue* check your code */
List* insert_queue_end (List* end, void* v) {
List* p = (List*) malloc(sizeof(List));
double *data=(double*)malloc(sizeof(double)); /*New double new address*/
*data=*((double*)v); /*Copy the VALUE and not the address to new double in new and fixed address*/
/*void* points to a new double with a new address in memory */
p->info=data;
p->prox=NULL;
if(end!=NULL) {
end->prox=p;
}
return p;
}
double queue_empty_double(Queue* f) {
return (f->begin==NULL);
}
double queue_remove_double(Queue* f) {
List* t;
double v;
double isRemoved=0;
if(queue_empty_double(f)) {
exit(1);
}
t=f->begin;
v=*((double*)(t->info));
f->begin=t->prox;
if (f->begin==NULL) {
f->end = NULL;
}
free(t->info); ///free the double
free(t);
printf("%.3lf\n",v);
}
void insert_queue(Queue* f, void* v) {
f->end = insert_queue_end(f->end,v);
if(f->begin==NULL) {
f->begin=f->end;
}
}
void print_queue_double(Queue* f) {
List* i;
for(i=f->begin;i!=NULL;i=i->prox)
printf("%.3lf\n",*((double*)i->info));
}
int main() {
Queue* f;
f = create_queue();
char ent1[100];
double n1;
int countDoubles=0;
scanf("%s",ent1);
while(ent1[0]!='X' && countDoubles < 20)
{
if(ent1[0]=='E') {
scanf("%lf",&n1);
insert_queue(f,&n1);
}
else if (ent1[0]=='D') {
queue_remove_double(f);
}
else
{
///Print the list - and see the result "debug"
print_queue_double(f);
}
scanf("%s",ent1);
}
free(f);
}
von 'Prox' meinst du "next"? Und gibst du ein "double *" oder ein "double"? Das wird einen Unterschied machen. Woher weißt du, dass es nicht mit doppelten Werten funktioniert? Gibt der Compiler einen Fehler aus? –
Und woher wissen Sie, wenn der Rückgabewert ein 'int' (oder Zeiger auf' int') vs ein 'double' (oder Zeiger auf' double') ist? Wenn Sie einen einzelnen Wert zurückgeben, warum brauchen Sie den 'next' (' prox'?) Zeiger? Wenn Sie eine Liste mit Werten zurückgeben, sollten Sie es auch sagen, aber vielleicht sollten Sie das auch sagen. Wie ist der Raum, auf den das 'info' Mitglied zeigt, zugewiesen? Wie wird ein Knoten (oder eine ganze Liste von Knoten) freigegeben? –
Es scheint wahrscheinlich, dass hier ein [XY Problem] (http://mywiki.wooledge.org/XyProblem) lauert. Während das, was Sie tun möchten, getan werden kann, ist es nicht klar, dass es so gemacht werden kann, wie Sie es versuchen, und es ist nicht klar, warum Sie es zuerst versuchen. Also, vielleicht müssen Sie ein Beispiel finden, das zeigt, wie/warum dies nützlich wäre. –