Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include<fstream>
  6. using namespace std;
  7. struct nastepny;
  8. struct lista
  9. {
  10. nastepny *pierwszy;
  11. lista();
  12. void dodaj();
  13. void pokaz();
  14. void usun(int wart);
  15. };
  16.  
  17.  
  18.  
  19.  
  20. lista::lista()
  21. {
  22. pierwszy = NULL;
  23. }
  24.  
  25. struct nastepny
  26. {
  27. nastepny *next;
  28. int liczba;
  29. nastepny();
  30. };
  31.  
  32. nastepny::nastepny()
  33. {
  34. next = 0;
  35. }
  36.  
  37. void lista::dodaj()
  38. {
  39. nastepny *nowy = new nastepny();
  40. int x;
  41. cin >> x;
  42. nowy->liczba=x;
  43. nastepny *buf;
  44. buf = pierwszy;
  45. pierwszy = nowy;
  46. pierwszy->next = buf;
  47. }
  48.  
  49. void lista::pokaz()
  50. {
  51. cout <<endl<< "Lista"<<endl;
  52. nastepny *temp=pierwszy;
  53. while (temp)
  54. {
  55. cout<<temp->liczba<<endl;
  56. temp = temp->next;
  57. }
  58. }
  59. void lista::usun(int wart)
  60. {
  61. nastepny *poprzedni = pierwszy;
  62. if (poprzedni)
  63. {
  64. nastepny *temp = pierwszy->next;
  65. if (poprzedni->liczba == wart)
  66. {
  67. delete poprzedni;
  68. pierwszy = temp;
  69.  
  70. }
  71. else {
  72. while (temp)
  73. {
  74. if (temp->liczba == wart)
  75. {
  76. poprzedni->next = temp->next;
  77. delete temp;
  78. cout << "Usunięto podany element"<<endl;
  79. break;
  80. }
  81. else
  82. {
  83. poprzedni = temp;
  84. temp = temp->next;
  85. }
  86. }
  87. }
  88. }
  89.  
  90. }
  91.  
  92. int main()
  93. {
  94. lista lista1;
  95. cout << "Podaj 1 liczbe: ";
  96. lista1.dodaj();
  97. cout << "Podaj 2 liczbe: ";
  98. lista1.dodaj();
  99. cout << "Podaj 3 liczbe: ";
  100. lista1.dodaj();
  101. lista1.pokaz();
  102. cout << "Podaj liczbe do usunięcia: ";
  103. int x;
  104. cin >> x;
  105. lista1.usun(x);
  106. lista1.pokaz();
  107. return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement