Fehler im Algotithmus
-
Hallo,
kann mir wer sagen, was an diesem Algorithmus falsch ist?
Er soll Daten einlesen und sie nach der Priorität ordnen.
Wenn als erstes die Priorität 2 gewählt wurde, werden zwischen dem Kopf und dem Knoten mit Priorität 2, Platzhalter eingefügt.An Stelle 2 einfügen klappt noch, dann aber an Stelle 5 anfügen nicht mehr - er fügt diesen Knoten dann an der Stelle 6 an.
Woran kann das liegen?#include <iostream.h> #include <conio.h> class Kset { private: struct Knoten { int Inhalt; int Priority; Knoten *pNext; Knoten *pFront; }; Knoten *Kopf; Knoten *Sentinel; int Anzahl; int x; public: Kset(); Kset(const Kset & obj); Kset(const int *obj, const int Array); ~Kset(); void insert_front(int obj); void insert_back(int obj); void insert_pos(int obj, int pri); void delete_front(); void delete_back(); void display(); void push_array(const int *obj, const int Array); friend ostream & operator << (ostream &os, Kset & kl); friend istream & operator >> (istream& is, Kset & kl); Kset& operator = (const Kset &alt); }; #include "impl.hpp"//insert_position/////////////////////////////////////////////////////////////// void Kset::insert_pos(int obj, int pri) { Knoten *pNew = new Knoten; pNew->Inhalt = obj; pNew->Priority = pri; pNew->pNext = NULL; pNew->pFront = NULL; if(Anzahl == 0) { Knoten *pLeer = new Knoten; pLeer->Inhalt = -1; pLeer->Priority = -1; Kopf->pNext = pLeer; pLeer->pFront = Kopf; pLeer->pNext = Sentinel; Sentinel->pFront = pLeer; Knoten *pTemp = pLeer; for(;pri > 1; pri--) { Knoten *pLeer1 = new Knoten; pLeer1->Inhalt = -1; pLeer1->Priority = -1; pLeer1->pFront = pTemp; pTemp->pNext = pLeer1; pLeer1->pNext = Sentinel; Sentinel->pFront = pTemp; pTemp = pLeer1; } pTemp->pNext = pNew; pNew->pFront = pTemp; pNew->pNext = Sentinel; Sentinel->pFront = pNew; Anzahl=1; } if(Anzahl <= pri && Anzahl != 0) { Knoten *pTemp = Sentinel->pFront; int zahlhinten = ((pri-Anzahl)-x); for(int z=zahlhinten; (z) > 1; z--) { Knoten *pLeer = new Knoten; pLeer->Inhalt = -1; pLeer->Priority = -1; pTemp->pNext = pLeer; pLeer->pFront = pTemp; Sentinel->pFront = pLeer; pLeer->pNext = Sentinel; pTemp = pLeer; ++Anzahl; } pTemp->pNext = pNew; pNew->pFront = pTemp; pNew->pNext = Sentinel; Sentinel->pFront = pNew; } if(Anzahl > pri && Anzahl != 0) { Knoten *pTemp = Kopf->pNext; Knoten *pHilf = Kopf->pNext; Knoten *pDel = Kopf->pNext; for(; pri > 1; pri--) { pTemp = pTemp->pNext; } pHilf = pTemp->pNext->pNext; pDel = pTemp->pNext; delete pDel; pTemp->pNext = pNew; pNew->pFront = pTemp; pNew->pNext = pHilf; pHilf->pFront = pNew; Anzahl++; } }
-
ich hab mir jetzt nicht alles genau angeschaut... allerdings solltest du 'else if' verwenden, da beim Einfügen den ersten Elements die ersten beiden Bedingungen erfüllt sind:
du setzt nämlich im ersten Rumpf Anzahl auf 1 und prio wird bis auf 1 runtergezählt, also ist die zweite Bedingung auch erfüllt.. Genau das verhinderst du, indm du 'else if' verwendest
-
okay - ist erledigt ... das Problem besteht aber leider noch weiterhin
