H
da es ja zum guten stil gehört, auch die lösungen anzugeben, hier meine lösung zur fifo ein und ausgabe...
//Definition der Klassen
#ifndef _P3_h_
#define _P3_h_
#include <string>
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
class FifoEl{
FifoEl* next;
string s;
public:
FifoEl(string el, FifoEl* n) : s(el), next(n) {};
~FifoEl() {}
inline operator string () { return s; };
friend class FifoClass;
};
class FifoClass
{
FifoEl *first;
FifoEl *end;
public:
FifoClass() : first (0) {}
FifoClass(const char* dat)
{
count = 0;
ifstream ein;
ein.open (dat, ifstream::in);
if(!ein) cout << "Konnte Datei nicht oeffnen.";
int start (0), ende (0);
string Zeile(""), PushString("");
while(!ein.eof())
{
Zeile = "";
getline(ein, Zeile, '\n');
push(Zeile);
}
}
~FifoClass() {delete first; }
FifoClass& operator<<(const string& s) {return push(s);}
FifoClass& operator>>(string& s) {return pop(s);}
inline operator string() {
string temp;
pop(temp);
return temp;
}
operator int() const {return count;}
const char* Error() const
{
cout << "HIER FEHLER" << endl;
}
inline bool empty() {return 0 == count; }
int count;
private:
string s;
FifoClass& push(string s);
FifoClass& pop(string &s);
};
FifoClass& FifoClass::push (string s) {
if(count == 0)
{
first = new FifoEl(s, first);
end = first;
end->next = NULL;
}
else {
end->next = new FifoEl(s, end);
end = end->next;
end->next = NULL;
}
count++;
return *this;
}
FifoClass& FifoClass::pop(string &s) {
if(empty()) throw "Stack underflow";
s = *first;
FifoEl *help=first;
first = first->next;
delete help;
count--;
return *this;
}
es wurden zwei zeiger angelegt. einer auf das erste und einer auf das letzte element...elemente werden hinten angefügt. gelesen wird jedoch von vorne...