Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct Element
- {
- string napis;
- int klucz;
- Element *nast;
- };
- Element *glowa;
- void dodajNaKoniec(Element *nowy)
- {
- if (glowa==NULL){
- glowa = nowy;
- nowy->nast = NULL;
- }
- else {
- Element *tmp = glowa;
- while (tmp->nast != NULL)
- tmp = tmp->nast;
- tmp->nast = nowy;
- nowy->nast = NULL;
- }
- }
- bool usunElement(int n)
- {
- if(glowa==NULL){
- return false;
- }
- if(n==1){
- Element *tmpDel = glowa;
- glowa = glowa->nast;
- delete(tmpDel);
- return true;
- }
- else{
- int i = 2;
- Element *tmpDel = glowa->nast;
- Element *tmp = glowa;
- while(tmpDel->nast != NULL && i<n){
- tmp = tmp->nast;
- tmpDel = tmpDel->nast;
- i++;
- }
- if(n==i){
- tmp->nast = tmp->nast->nast;
- delete(tmpDel);
- return true;
- }
- else{
- return false;
- }
- }
- }
- void wypiszListe()
- {
- Element *tmp = glowa;
- while (tmp != NULL){
- cout << tmp->napis << ' ' << tmp->klucz << endl;
- tmp = tmp->nast;
- }
- }
- int main()
- {
- glowa = NULL;
- ifstream plik("dane.txt");
- if(!plik.good()){
- cout << "Błąd otwarcia pliku";
- return 0;
- }
- else{
- cout << "Plik ok"<<endl;
- while(!plik.eof()){
- Element *tmp = new Element;
- plik >> tmp->napis;
- plik >> tmp->klucz;
- dodajNaKoniec(tmp);
- }
- cout << endl;
- plik.close();
- }
- wypiszListe();
- usunElement(1);
- wypiszListe();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement