Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5.  
  6. class cityList;
  7.  
  8. class City{
  9. friend class cityList;
  10. char * name;
  11. int data;
  12.  
  13. public:
  14.  
  15. City(int data, const char * name){
  16. this->data=data;
  17. this->name = new char[strlen(name)+1];
  18. strcpy(this->name, name);
  19. }
  20.  
  21.  
  22. void print(){
  23. cout<<data<<" "<<name<<endl;
  24. }
  25. ~City(){
  26. delete []this->name;
  27. }
  28. };
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41. class cityList{
  42. City *city;
  43. cityList *next;
  44.  
  45. public:
  46. cityList() {
  47. this->city = NULL; this->next = NULL;
  48. }
  49.  
  50.  
  51.  
  52. ~cityList(){
  53. cityList *temp=head;
  54. cityList *prev=head;
  55. while(temp){
  56. temp=temp->next;
  57. delete prev;
  58. prev = temp;
  59. }
  60. }
  61.  
  62.  
  63. int &ref(const char *name){
  64. cityList *temp = head;
  65. cityList *prev = head;
  66. if(head == NULL){
  67. City newcity(0,name);
  68. head->city = newcity;
  69. head->next=NULL;
  70. return head->city->data;
  71. }
  72. if(strcmp(head->city->name, name) > 0){
  73. City newcity(0,name);
  74.  
  75. head->city = newcity;
  76. return head->city->data;
  77. }
  78. while(temp != NULL){
  79. int k = strcmp(temp->city->name, name);
  80. if(k < 0){
  81. prev = temp;
  82. temp = temp->next;
  83. }
  84. else if(k > 0){
  85. City newcity(0,name);
  86.  
  87. prev->next->city = newcity;
  88. return newcity->data;
  89. }
  90. else
  91. return temp->city->data;
  92. }
  93. City newcity(0,name);
  94. prev->next->city = newcity;
  95.  
  96. return newcity->data;
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. void clear(){
  106. if(head==NULL){
  107. return;
  108. }
  109.  
  110. if(head->next==NULL ){
  111. if(head->city->data!=0){
  112. return;
  113. }
  114. else{
  115. delete []head->city->name;
  116. delete head;
  117. head=NULL;
  118. return;
  119. }
  120. }
  121. cityList *temp = head;
  122. cityList *prev = head;
  123. while(temp != NULL){
  124. while(head->city->data == 0) {
  125. head = head->next;
  126. delete []temp->city->name;
  127. delete temp;
  128. temp = head;
  129. }
  130. if (temp->city->data == 0){
  131. prev->next=temp->next;
  132. delete []temp->city->name;
  133. delete temp;
  134. temp = prev;
  135. }
  136. prev=temp;
  137. temp=temp->next;
  138. }
  139. }
  140.  
  141.  
  142.  
  143.  
  144. void print() { // wypisujemy miasto które ten konkretny CityList przechowuje
  145. this->city->print(); // lecimy dalej
  146. if (this->next != NULL) {
  147. this->next->print();
  148. }
  149. }
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159. };
  160.  
  161.  
  162.  
  163.  
  164.  
  165. int main(){
  166. City k(34,"sd");
  167. k.print();
  168. cityList d;
  169.  
  170. return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement