000
02.01.2004, 12:52
McMapper
|
Ja nun melde ich mich mal in anspruchsvollerere Mission als sonst. In der aktuellen Uebung muessen wir eine FIFO Queue programmieren. Ich kriege Speicherfehler und finde den Fehler nicht. Code ist eigentlich ziemlich simpel.
// Uebung 7 // Sebastian Altorfer, asebi@ee.ethz.ch
#include <iostream.h>
struct fifoElem { int value; fifoElem *next; };
struct fifo { fifoElem *first; fifoElem *last; };
// Initialisierung
void fifoInit (fifo *p) { p->first = 0; }
fifoElem *newElem(int v) { fifoElem *p = new fifoElem; p->next = 0; p->value = v; return p; }
// Queue-Funktionen
bool isEmpty(fifo *p) { if(p->first == 0) return true; else return false; }
void enqueue(fifo *f, int v) { fifoElem *p = newElem(v); if (f->first=0) { f->first = p; f->last = p; } else { f->last->next = p; f->last = p; } }
void dequeue(fifo *f) { if(f->first) { fifoElem *p = f->first; delete f->first; f->first = p; } }
int front(fifo *f) { if(f->first) return f->first->value; else return 0; }
// Beispielanwendung
int main() { fifo *f = new fifo; fifoInit(f); for (int i=0; i<10; i++) { enqueue(f, i); if(i%2==0) cout << f->last->value; } for (int i=0; i<10; i++) { dequeue(f); } delete f; return 0; }
--
|
|
Profil || Suche
|
001
02.01.2004, 13:07
theDon
|
valgrind und konsorten wirken wunder.
--
\o tanz den naziprau! o/
And more than ever, I hope to never fall, Where enough is not the same it was before
|
|
Profil || Suche
|
002
02.01.2004, 13:09
mani
|
// Uebung 7 // Sebastian Altorfer, asebi@ee.ethz.ch
#include <iostream> using namespace std;
struct fifoElem { int value; fifoElem *next; };
struct fifo { fifoElem *first; fifoElem *last; };
// Initialisierung
void fifoInit (fifo *p) { p->first = NULL; }
fifoElem *newElem(int v) { fifoElem *p = new fifoElem; p->next = NULL; p->value = v; return p; }
// Queue-Funktionen
bool isEmpty(fifo *p) { if(p->first == NULL) return true; else return false; }
void enqueue(fifo *f, int v) { fifoElem *p = newElem(v); if (f->first == NULL) { f->first = p; f->last = p; } else { f->last->next = p; f->last = p; } }
void dequeue(fifo *f) { if(f->first) { fifoElem *p = f->first; delete f->first; f->first = p; } }
int front(fifo *f) { if(f->first) return f->first->value; else return 0; }
// Beispielanwendung
int main() { fifo *f = new fifo; fifoInit(f); for (int i=0; i<10; i++) { enqueue(f, i); if(i%2==0) cout << f->last->value; } for (int i=0; i<10; i++) { dequeue(f); } delete f; return 0; }
thedon: brauch er garnich, reicht auch wenn er nen ordentlichen compiler (z.b. gcc) nimmt, der ordentliche warnings (wie z.b. nen = bei nem if()) ausgibt
--
|
|
Profil || Suche
|
003
02.01.2004, 14:31
[RMen]OneStone
|
Nur um's nochmal klar zu sagen: In der enqueue vergleichst du first nicht mit 0 sondern weist 0 zu und gehst dann dadurch automatisch in den else-Zweig, selbst wenn die Queue leer ist. (hat mani ja implizit gesagt)
--
georg-wicherski@pixel-house.net | http://www.pixel-house.net/ - Coding Resource| http://www.google.de/
|
|
Profil || Suche
|
004
02.01.2004, 14:32
CN
|
trotzdem schön dass du das nochmal wiederholst, ich verweise an dieser stelle an "der krampfspammer"
--
|
|
Profil || Suche
|
006
02.01.2004, 15:01
CN
|
Should I use NULL or 0? In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer. In pre-standard code, NULL was/is sometimes defined to something unsuitable and therefore had/has to be avoided. That's less common these days. src: http://www.research.att.com/~bs/bs_faq2.html#null
--
Dieser Beitrag wurde am 02.01.2004 um 15:01 von CN bearbeitet.
|
|
Profil || Suche
|
007
02.01.2004, 19:07
Leviathan
|
Ich würde das Ganze als Klasse implementieren, das ist übersichtlicher und nicht so fehleranfällig. Was du gemacht hast erinnert mich stark an die Dateifunktionen und den FILE*-Pointer in C. Außerdem ist der Header iostream.h nicht unbedingt vorhanden, empfohlen ist stattdessen iostream.
--
Entities: HL | HL² Kompilierfehler r_speeds | mehr über r_speeds
|
|
Profil || Suche
|
008
02.01.2004, 21:32
apfelkorn
|
da gibts schöne tuts auf resourcecode.de, unter anderem auch von mir *g* (objektorientierte baumstruktur)
--
|
|
Profil || Suche
|
009
06.01.2004, 18:10
Prefect
|
Ich würde das Ganze als Klasse implementieren, das ist übersichtlicher und nicht so fehleranfällig.
Und als Template. Templates sind schön ;P
cu, Prefect
--
Widelands - Gemütliche Aufbaustrategie, Free Software Noch ein Blog - Lerne, wie die Welt wirklich ist, aber vergiss niemals, wie sie sein sollte.
|
|
Profil || Suche
|