Wieso löscht sich mein letzer Student (tail) nicht?



  • Hey ich bin am verzweifeln hat jemand ein tipp für mich?

    # include<iostream>
    #include<string>
    #include<fstream>
    #include <stdlib.h>
    #include <sstream>

    using namespace std;

    int anzahlStudenten = 0;

    struct Student
    {
    string firstname;
    string lastname;
    char sex;
    int matriculation_number = -1;
    double final_mark = -1;

    Student *prev = NULL;
    Student *next = NULL;
    int wert;
    };

    //Student students[maxAnzahlStudenten];

    void importStudenten();
    void exportStudenten();

    Student *head = NULL;
    Student *tail = NULL;

    void print() {

    Student *current = head;

    if (head == NULL)
    cout << "Liste leer..." << endl;

    while (current != NULL)
    {
    cout << current->wert << " " << current->firstname << " " << current->lastname << " " << current->sex << " " << current->matriculation_number << " " << current->final_mark << endl;

    current = current->next;
    }
    }

    void insert_Student(Student *newStudent, string vorname, string nachname, char geschlecht, double matrNr, double note)
    {
    // Fall 1, die Liste bekommt ihr erstes Element
    if (head == NULL)
    {
    head = newStudent;
    tail = newStudent;
    }
    // Fall 2, die Liste bekommt weitere Elemente
    else
    {
    newStudent->next = head;
    head->prev = newStudent;
    head = newStudent;
    }

    newStudent->firstname = vorname;

    newStudent->lastname = nachname;

    newStudent->sex = geschlecht;

    newStudent->matriculation_number = matrNr;

    newStudent->final_mark = note;

    cout << "Student wurde angelegt." << endl;

    anzahlStudenten++;
    newStudent->wert = anzahlStudenten;
    }

    void removeStudent(int searchvalue)
    {
    Student *current = head;

    while (current!=0)
    {

    if (current->matriculation_number== searchvalue)
    {

    if (current == head)
    {

    if (current == tail)
    {
    head = NULL;
    tail = NULL;
    }

    head = head->next;
    head->prev = NULL;
    return;
    }

    else if (current == tail)
    {
    if (current == head)

    {
    head = NULL;
    tail = NULL;

    }
    else
    {

    tail = tail->prev;
    tail->next = NULL;

    return;
    }

    current->prev->next = current->next;
    current->next->prev = current->prev;
    current->next = NULL;
    current->prev = NULL;

    }

    cout << "Student gelöscht";
    break;

    }

    current = current->next;

    }

    }

    /void removeStudent()
    {
    // Wenn Liste nicht leer
    if (head != NULL)
    {
    // Wenn Liste ein Element hat
    if (head == tail)
    {
    head = NULL;
    tail = NULL;
    }
    // Wenn Liste mehr wie ein Element hat
    else
    {
    head = head->next;
    head->prev = NULL;
    }
    }
    }
    /

    void importStudenten() {
    cout << "Sie haben Menuepunkt 5 gewaehlt" << endl;
    string vorname = "";
    string nachname = "";
    char geschlecht = '0';
    double MatrNr = 0;
    double note = 0;

    ifstream csvread;
    csvread.open("data.csv", ios::in);

    if (csvread)
    {
    string s = "";
    int counter_field = 0;
    int counter_student = 0;

    while (getline(csvread, s, ';'))
    {

    if (counter_field == 0)
    vorname = s;

    if (counter_field == 1)
    nachname = s;

    if (counter_field == 2)
    geschlecht = s.at(0);

    if (counter_field == 3)
    MatrNr = atoi(s.c_str());

    if (counter_field == 4)
    note = atof(s.c_str());

    counter_field++;

    if (counter_field > 4)
    {
    insert_Student(new Student, vorname, nachname, geschlecht, MatrNr, note);
    counter_field = 0;
    counter_student++;
    }
    }

    csvread.close();
    }
    else
    {
    cout << "Fehler beim Lesen!" << endl;
    }
    }

    void exportStudenten()
    {

    Student *current = head;

    cout << "Sie haben Menuepunkt 6 gewaehlt";

    // exportieren
    ofstream csvwrite;
    //ofstream streamforoutput;
    csvwrite.open("data.csv", ios::out);

    while (current != NULL)
    {
    if (current->matriculation_number != -1)
    {
    csvwrite << current->firstname << ";";
    csvwrite << current->lastname << ";";
    csvwrite << current->sex << ";";
    csvwrite << current->matriculation_number << ";";
    csvwrite << current->final_mark << ";";
    }

    current = current->next;
    }

    csvwrite.close();
    cout << "datei wurde exportiert" << endl;

    //break;

    }

    void linearesuche(int searchvalue)
    {

    Student *current = head;

    while (current != NULL)

    {

    if (current->matriculation_number == searchvalue)
    {

    cout << current->firstname << " " << current->lastname << endl;
    return;

    }

    current = current->next;

    }

    }

    void bearbeiten (int searchvalue)
    {

    Student *current = head;

    while (current != NULL)

    {

    if (current->matriculation_number == searchvalue)
    {
    cout << current->firstname << " " << current->lastname << " "<<current ->sex<<endl;

    cout << "1.Geben sie'1'ein um den studenten zu loeschen " << endl;
    cout << "2.Geben sie '2' ein um den studenten zu bearbeiten " << endl;
    int input = 0;
    cin >> input;

    if (input == 2)
    {
    int wert;
    int a = 1;
    int b = 2;
    cout << "geben sie a um studennten zu beahlten und b um es zu verändern" << endl;
    cin >> wert;
    cout << "Neue Daten eineben" << endl;

    cout << "Firstname :";
    cin >> current->firstname;
    cout << "lastname:";
    cin >> current->lastname;
    cout << "sex:";
    cin >> current->sex;
    /cout << "mariculation number:";
    cin >> current->matriculation_number;
    /
    cout << "Note:";
    cin >> current->final_mark;
    }

    if (input == 1)
    {
    cout << "input 1";
    removeStudent(searchvalue);

    }

    }

    current = current->next;

    }

    }

    int main()
    {

    /students[0].firstname = "Daniel";
    students[0].lastname = "Kreis";
    students[0].sex = 'm';
    students[0].matriculation_number = 12346;
    students[0].final_mark = 1.3;
    /

    while (true)

    {
    int choice;
    string vorname = "";
    string nachname = "";
    char geschlecht = '0';
    double MatrNr = 0.0;
    double note = 0.0;
    int searchvalue = 0;

    std::cout << "Studentenverwaltung V0.1" << endl;
    cout << "" << endl << endl;
    cout << "1)Student/in auflisten" << endl;
    cout << "" << endl << endl;
    cout << "2)Studen/in anlegen" << endl;
    cout << "
    " << endl << endl;
    cout << "3)Student/in suchen" << endl;
    cout << "
    " << endl << endl;
    cout << "4)Student/in bearbeiten" << endl;
    cout << "" << endl << endl;
    cout << "5)Studierende importieren" << endl;
    cout << "
    " << endl << endl;
    cout << "6)Studierende exportieren" << endl;
    cout << "
    ________________________________" << endl << endl;
    cout << "7)Programmende" << endl;

    cout << "Wählen Sie ein Menuepunkt aus: ";
    cin >> choice;

    switch (choice)
    {
    case 1:
    cout << "sie heben menüpunkt 1 gwählt" << endl;
    print();
    break;
    case 2:
    cout << "sie haben Menüpunkt 2 gewählt" << endl;
    cout << "Vorname: ";
    cin >> vorname;
    cout << "Nachname: ";
    cin >> nachname;
    cout << "Geschlecht: ";
    cin >> geschlecht;
    cout << "Matrikelnr: ";
    cin >> MatrNr;
    cout << "Abschluss Note: ";
    cin >> note;

    insert_Student(new Student, vorname, nachname, geschlecht, MatrNr, note);
    break;
    case 3:
    cout << "Sie haben Menuepunkt 3 gewaehlt" << endl;

    cout << "schreiben sie die Matrikelnummer des gesuchten Studenten" << endl;
    cin >> searchvalue;
    linearesuche(searchvalue);
    break;
    case 4:
    cout << "Sie haben Menuepunkt 4 gewaehlt" << endl;
    cout << "schreiben sie die Matrikelnummer des gesuchten Studenten" << endl;
    cin >> searchvalue;
    bearbeiten(searchvalue);
    break;
    case 5: importStudenten();
    break;
    case 6: exportStudenten();
    break;
    case 7:
    cout << "Das Programm wird beendet" << endl;
    return 0;
    break;

    default:
    cout << "Das Programm ist zum Ende gekommen" << endl;
    break;

    }

    } while (true);

    cout << endl;
    system("pause");
    return 0;
    }



  • Debugger benutzten!
    Codetags benutzen (so sehe ich mir das nicht an).
    Problem nachvollziehbar beschreiben.



  • Bevor du irgendwas machst, formatiere deinen Code bitte ordentlich. (hier den "C++" Button benutzen) und reduziere den Code auf die relevanten Teile!

    Dann: ich sehe überhaupt nicht, dass du irgendeinen Studenten löscht. Es befindet sich kein "delete" im Code.

    Und dann schau mal in das removeStudent:

    if (current == head) {
    
                    if (current == tail) {
                        head = NULL;
                        tail = NULL;
                    }
    
                    head = head->next;
                    head->prev = NULL;
                    return;
                }
    

    Welchen Wert hat head->next , wenn vorher current == tail war? (Hint: nicht machen!)

    Achso, und da das ja wohl C++ sein soll:
    - verwende nullptr statt NULL
    - verwende Smartpointer (oder denke an das delete beim entfernen)
    - gib deinem insert_Student nur den Studenten als Parameter, nicht noch irgendwelche Initialisierungen für Student selbst, die besser im Student-Konstruktor aufgehoben wären

    bestimmt noch viel mehr...


Log in to reply