Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. struct Element
  7. {
  8. string napis;
  9. int klucz;
  10. Element *nast;
  11. };
  12.  
  13. Element *glowa;
  14.  
  15. void dodajNaKoniec(Element *nowy)
  16. {
  17. if (glowa==NULL){
  18. glowa = nowy;
  19. nowy->nast = NULL;
  20. }
  21. else {
  22. Element *tmp = glowa;
  23. while (tmp->nast != NULL)
  24. tmp = tmp->nast;
  25.  
  26. tmp->nast = nowy;
  27. nowy->nast = NULL;
  28. }
  29. }
  30.  
  31. bool usunElement(int n)
  32. {
  33. if(glowa==NULL){
  34. return false;
  35. }
  36. if(n==1){
  37. Element *tmpDel = glowa;
  38. glowa = glowa->nast;
  39. delete(tmpDel);
  40. return true;
  41. }
  42. else{
  43. int i = 2;
  44. Element *tmpDel = glowa->nast;
  45. Element *tmp = glowa;
  46. while(tmpDel->nast != NULL && i<n){
  47. tmp = tmp->nast;
  48. tmpDel = tmpDel->nast;
  49. i++;
  50. }
  51. if(n==i){
  52. tmp->nast = tmp->nast->nast;
  53. delete(tmpDel);
  54. return true;
  55. }
  56. else{
  57. return false;
  58. }
  59. }
  60. }
  61.  
  62. void wypiszListe()
  63. {
  64. Element *tmp = glowa;
  65. while (tmp != NULL){
  66. cout << tmp->napis << ' ' << tmp->klucz << endl;
  67. tmp = tmp->nast;
  68. }
  69. }
  70.  
  71. int main()
  72. {
  73. glowa = NULL;
  74.  
  75. ifstream plik("dane.txt");
  76.  
  77. if(!plik.good()){
  78. cout << "Błąd otwarcia pliku";
  79. return 0;
  80. }
  81. else{
  82. cout << "Plik ok"<<endl;
  83.  
  84. while(!plik.eof()){
  85. Element *tmp = new Element;
  86. plik >> tmp->napis;
  87. plik >> tmp->klucz;
  88. dodajNaKoniec(tmp);
  89. }
  90.  
  91. cout << endl;
  92.  
  93. plik.close();
  94. }
  95.  
  96. wypiszListe();
  97. usunElement(1);
  98. wypiszListe();
  99.  
  100. return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement