From b2f7e26d27f2bc58a20cbba98d81b9243c96467d Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Thu, 8 May 2014 12:11:20 +0200 Subject: Assignment No. 3 These are the linked listes and personen structs. --- 3.04.personenListe.cc | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 3.04.personenListe.cc (limited to '3.04.personenListe.cc') diff --git a/3.04.personenListe.cc b/3.04.personenListe.cc new file mode 100644 index 0000000..ac48021 --- /dev/null +++ b/3.04.personenListe.cc @@ -0,0 +1,113 @@ +// ===================================================================================== +// +// Filename: 3.04.personenListe.cc +// +// Description: Personen as list with Structs +// +// Version: 1.0 +// Created: 23.04.2014 21:50:42 +// Revision: none +// Compiler: gcc +// +// Author: Stefan Suhren (SSuhren), suhren.stefan@fh-swf.de +// Organization: FH Südwestfalen, Iserlohn +// +// ===================================================================================== + +#include +#include +#include +#include +#include + +using namespace std; + +// ##### TYPE DEFINITIONS - LOCAL TO THIS SOURCE FILE ############# +struct mitarbeiter{ + string identnummer; + string nachname; + string vorname; + string abteilung; + unsigned int durchwahl; + mitarbeiter *next; +}; // ---------- end of struct mitarbeiter ---------- + +// === FUNCTION ====================================================================== +// Name: main +// Description: Reading and printing the personen.dat file +// ===================================================================================== +int main ( int argc, char *argv[] ){ + + string ifs_file_name = "personen.dat"; // input file name + ifstream ifs; // create ifstream object + + ifs.open ( ifs_file_name.c_str() ); // open ifstream + if (!ifs) { + cerr << "\nERROR : failed to open input file " << ifs_file_name << endl; + exit (EXIT_FAILURE); + } + + // Creating anchor and cursor + mitarbeiter *ma = 0, *cursor = 0, *maNeu = 0; + int satz = 0; + + while ( ifs ) { + + // Create temporary object + maNeu = new mitarbeiter; + + if( ifs >> maNeu->identnummer >> maNeu->nachname >> maNeu->vorname >> maNeu->abteilung >> maNeu->durchwahl ){ + // Makes sure next is NULL + maNeu->next = NULL; + if( ma == NULL ){ + ma = maNeu; + cursor = ma; + } + else{ + cursor->next = maNeu; + cursor = maNeu; + } +// cout << "Person Nr." << satz+1 << "\n" +// << "\t ID: " << cursor->identnummer << "\n" +// << "\t Nachname: " << cursor->nachname << "\n" +// << "\t Vorname: " << cursor->vorname << "\n" +// << "\t Abteilung: " << cursor->abteilung << "\n" +// << "\t Durchwahl: " << cursor->durchwahl << "\n"; + + satz++; + } + else{ + delete maNeu; + } + } + + ifs.close (); // close ifstream + + // Reset cursor + cursor = ma; + // Walk over list to find the record + while( cursor != NULL && cursor->durchwahl != 4731 ){ + cursor = cursor->next; + } + + // Check if something was found + if( cursor != NULL ){ + cout << "Record found.\nName: " << cursor->vorname << " " << cursor->nachname << "\n"; + } + else{ + cout << "Record not found!!!" << "\n"; + } + + // Now delete the list + // Reset cursor + // Temp var for deleting + while( ma != NULL ){ + cursor = ma; + ma = cursor->next; + delete cursor; + } + // Make list sane again + // ma = NULL; + + return EXIT_SUCCESS; +} // ---------- end of function main ---------- -- cgit v1.2.3-70-g09d2