Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. struct node{
  6. string imie;
  7. string nazwisko;
  8. int nr;
  9. node* next;
  10. node(){
  11. next=0;
  12. }
  13. };
  14.  
  15. struct lista{
  16. node* first;
  17. void wypiszliste();
  18. void dodajdolisty(string im, string naz, int nr);
  19. bool usunstudenta(string name, string surname, int index);
  20. lista(){
  21. cout<<"im in semi constuctor"<<endl;
  22. first=0;
  23. }
  24. };
  25.  
  26. void lista::dodajdolisty(string im, string naz, int nr){
  27. node* t = new node;
  28. t->imie=im;
  29. t->nazwisko=naz;
  30. t->nr=nr;
  31. if(first==0){
  32. first=t;
  33. t->next = NULL;
  34. }else{
  35. node *temp;
  36. temp=first;
  37. while(temp->next){
  38. temp=temp->next;
  39. }
  40. temp->next=t;
  41. t->next=0;
  42. }
  43.  
  44. }
  45. void lista::wypiszliste(){
  46. node* temp;
  47. temp=first;
  48. while(temp){
  49. cout<<temp->imie;
  50. cout<<temp->nazwisko;
  51. cout<<temp->nr;
  52. cout<<endl;
  53. temp=temp->next;
  54. }
  55. }
  56.  
  57. bool compareStudents(string name,string surname, int index, node* n){
  58. if(name.compare(n->imie) == 0 && surname.compare(n->nazwisko) == 0 && n->nr == index) return true;
  59. return false;
  60. }
  61.  
  62.  
  63. bool lista::usunstudenta(string name, string surname, int index){
  64. node* prev = first;
  65. node *current = first->next;
  66.  
  67. while(current != NULL) {
  68.  
  69. if(compareStudents(name,surname,index,current) == true) {
  70. break;
  71. }else {
  72. prev = current;
  73. current = current->next; // go to next value
  74. }
  75. }
  76.  
  77. if(current == NULL) { // if we reached end of list or the list is empty
  78. cout << "Can't remove value: no match found.\n";
  79. return false;
  80. } else {
  81. cout << "Deleting: " << current << "\n";
  82. prev->next = current->next; // unlink the node you remove
  83. delete current; // delete the node
  84. }
  85. return true;
  86. }
  87.  
  88.  
  89.  
  90. int main(){
  91. lista *baza=new lista;
  92. baza->dodajdolisty("Mariusz","Kowalski ",302432);
  93. baza->dodajdolisty("Tomasz","Nowak ",302433);
  94. baza->dodajdolisty("Andrzej","Golota ",323932);
  95. baza->wypiszliste();
  96. cout<<""<<endl;
  97. baza->usunstudenta("Tomasz","Nowak ",302433);
  98. baza->wypiszliste();
  99. return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement