Advertisement
Guest User

wejsciowka-listy

a guest
Aug 25th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <cstdio>
  6.  
  7. #define N 4
  8.  
  9. using namespace std;
  10.  
  11. struct Student{
  12.     int nr_indeksu;
  13.     string nazwisko;
  14.     float ocena;
  15.     Student *nast;
  16.     Student *poprz;
  17. };
  18.  
  19. istream& operator>>(istream& in, Student& s)        
  20. {
  21.  
  22.  
  23.     in >> s.nr_indeksu;
  24.     in >> s.nazwisko;
  25.     in >> s.ocena;
  26.     return in;
  27. }
  28.  
  29.  
  30. int main(){
  31.     Student *iter = NULL, *nowy = NULL, *usun = NULL, *glowa = NULL, *ogon = NULL;
  32.     cout<<"Podaj nazwe pliku z danymi:";
  33.     string nazwa_pliku;
  34.     cin>>nazwa_pliku;
  35.     ifstream plik;
  36.     plik.open(nazwa_pliku.c_str());
  37.     if(plik.good()){
  38.         while(!plik.eof() && plik.good()){
  39.            
  40.  
  41.                 nowy = new Student;
  42.                 plik>>*nowy;
  43.  
  44.  
  45.                 if(glowa){
  46.                     if (nowy->nr_indeksu < glowa->nr_indeksu){
  47.                         nowy->nast=glowa;
  48.                         glowa->poprz=nowy;
  49.                         glowa=nowy;
  50.                     }
  51.                     else {
  52.                         for(iter=glowa;iter!=NULL;iter=iter->nast){
  53.                             if (nowy->nr_indeksu < iter->nr_indeksu){
  54.  
  55.                                 nowy->nast = iter;
  56.                                 nowy->poprz = iter->poprz;
  57.  
  58.                                 nowy->nast->poprz = nowy;
  59.                                 nowy->poprz->nast = nowy;
  60.                                 break;
  61.                             }
  62.  
  63.                            
  64.  
  65.                        
  66.  
  67.                         if(!iter){
  68.                             nowy->poprz = ogon;
  69.                             ogon->nast = nowy;
  70.                             ogon = nowy;
  71.                         }
  72.  
  73.                         }
  74.                    
  75.                 }
  76.  
  77.                 else{
  78.                     ogon=glowa = nowy;
  79.                 }
  80.  
  81.            
  82.             plik.close();
  83.         }
  84.     }
  85.  
  86.     cout<<"lista od poczatku";
  87.     for(iter=glowa; iter!=NULL; iter=iter->nast){
  88.         cout<<iter;
  89.     }
  90.  
  91.  
  92.     iter = glowa;
  93.     while(iter!=NULL){
  94.         usun = iter;
  95.         iter=iter->nast;
  96.         delete(usun);
  97.     }
  98.  
  99.     return 0;
  100.  
  101.  
  102.  
  103.  
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement