Versuche ein Klassentemplate für eine lineare Liste von Knoten. Der next-Zeiger scheint irgendwo auf NULL zu zeigen, was in einer Access-Violation endet. Bitte um Hilfe!
Hier der Quellcode
Alles anzeigen
Hier der Quellcode
Quellcode
- #include <iostream.h>
- #include <conio.h>
- #include <string.h>
- template <typename T> struct Node
- {
- Node() : next (0) {};
- T data;
- Node* next;
- };
- template <typename T> class Liste
- {
- public:
- typedef T Type;
- typedef Node<Type> NodeType;
- typedef NodeType* NodePointer;
- NodePointer first;
- void einfuegen(const T& wert)
- {
- NodePointer n = new NodeType;
- n->data = wert;
- n->next = first;
- first = n;
- }
- void loeschen()
- {
- NodePointer temp = first;
- first= first->next;
- delete temp;
- }
- void anzeigen()
- {
- NodePointer current = first;
- while(current != NULL)
- {//hier scheint das Problem zu liegen
- cout << current->data <<endl;
- current = current->next;
- }
- }
- };
- int main()
- {
- Liste<float> lst;
- lst.einfuegen(1.5f);
- lst.einfuegen(4.3f);
- lst.einfuegen(3.1f);
- lst.anzeigen();
- lst.loeschen();
- lst.anzeigen();
- return 0;
- getch();
- };