====== C++ Crashkurs / Übung 5 ======
Die Iteratoren und Listen hängen eng zusammen. Die Iteratoren sollten daher innerhalb der ListenKlasse untergebracht werden.
**Ueb2d.h:**
// File : Ueb2d.h
//
#include "..\stdafx.h"
#include "Ueb2d.VerketteteListe.h"
using namespace Ueb2d;
void ListeAusgeben (const CListe& liste, const char* pszHeader)
{
std::cout << pszHeader << std::endl;
for (auto iter = liste.begin(); iter != liste.end(); ++iter)
{
std::cout << " " << *iter << std::endl;
}
}
int _ueb (int argc, _TCHAR* argv[])
{
CListe liste1;
liste1.ElementAnhaengen(4711);
liste1.ElementAnhaengen(815);
liste1.ElementAnhaengen(42);
// step 1:
ListeAusgeben(liste1, "Liste 1:");
// step 2:
{
CListe liste2 = liste1;
liste2.ElementAnhaengen(88);
ListeAusgeben(liste2, "Liste 2:");
}
ListeAusgeben(liste1, "Liste 1 nach Liste 2:");
// step 3:
{
CListe liste3;
liste3.ElementAnhaengen(77);
liste3 = liste1;
liste3.ElementAnhaengen(88);
ListeAusgeben(liste3, "Liste 3:");
}
ListeAusgeben(liste1, "Liste 1 nach Liste 3:");
return 0;
}
// EOF
**Ueb2d.VerketteteListe.h:**
// File : Ueb2d.VerketteteListe.h
//
namespace Ueb2d
{
class CListe;
class CListenElement
{
friend CListe;
private:
int nInhalt;
CListenElement* pNaechster;
public:
CListenElement (int newInhalt)
{
nInhalt = newInhalt;
pNaechster = nullptr;
}
~CListenElement ()
{
delete pNaechster;
pNaechster = nullptr;
}
int GetInhalt ()
{
return nInhalt;
}
CListenElement* GetNaechster ()
{
return pNaechster;
}
};
class CListeIterator
{
private:
CListenElement* element; // Iterator muss das Listenelement kennen
public:
CListeIterator (CListenElement* pElem)
{
element = pElem;
}
bool operator!=(CListeIterator another) // Iterator muss zwei Elemente vergleichen können
{
return (this->element != another.element);
}
CListeIterator& operator++() // Iterator muss zum nächsten Element wechseln können
{
element = element->GetNaechster();
return *this;
}
int operator*() // Iterator muss den Inhalt aus dem referenzierten Element liefern können
{
return element->GetInhalt();
}
};
class CListe
{
public:
CListenElement* pStart;
CListe ()
{
pStart = nullptr;
}
CListe (const CListe& other) // Copy-Constructor: alle Elemente des other-Objekts werden kopiert
{
pStart = nullptr;
for (auto iter = other.begin(); iter != other.end(); ++iter) ElementAnhaengen(*iter);
}
~CListe ()
{
delete pStart;
pStart = nullptr;
}
CListe& operator=(const CListe& other) // Zuweisungsoperator: alle Elemente des other-Objekts werden kopiert
{
if (pStart != nullptr) delete pStart;
pStart = nullptr;
for (auto iter = other.begin(); iter != other.end(); ++iter) ElementAnhaengen(*iter);
return *this;
}
CListeIterator begin () const // Liefert Referenz auf das erste Element
{
return CListeIterator(pStart);
}
CListeIterator end () const // Liefert Referenz hinter dem letzten Element
{
return CListeIterator(nullptr);
}
void ElementAnhaengen (int nInhalt)
{
auto pNeu = new CListenElement(nInhalt);
if (pStart == nullptr)
{
pStart = pNeu;
}
else
{
CListenElement* pLetztes = pStart;
while (pLetztes->GetNaechster() != nullptr) pLetztes = pLetztes->GetNaechster();
pLetztes->pNaechster = pNeu;
} // if
}
};
}
// EOF
**Programm.cpp:** - Der Einstiegspunkt
// Programm.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Ueb2d.h"
int _tmain (int argc, _TCHAR* argv[])
{
return _ueb(argc, argv);
}
// EOF
**stdafx.h:**
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include
#include
// TODO: reference additional headers your program requires here
#include
#include
// EOF
**targetver.h:**
// Including SDKDDKVer.h defines the highest available Windows platform.
// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
#pragma once
#include
----
Stand: 19.09.2014
--- //[[feedback.jk-wiki@kreick.de|: Jürgen Kreick]]//
EOF