Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 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. City(int data, const char * name){
  15. this->data=data;
  16. this->name = new char[strlen(name)+1];
  17. strcpy(this->name, name);
  18. }
  19. void print(){
  20. cout<<data<<" "<<name<<endl;
  21. }
  22. ~City(){
  23. delete []this->name;
  24. }
  25. };
  26.  
  27.  
  28. class CityList{
  29. City *city;
  30. CityList *next;
  31.  
  32. public:
  33. CityList(){
  34. this->city=NULL;
  35. this->next=NULL;
  36. }
  37. ~CityList(){
  38. CityList *temp=this;
  39. while(temp!=NULL){
  40. if(temp->city!=NULL){
  41. delete temp->city;
  42. }
  43. temp=temp->next;
  44. }
  45. }
  46. void print() {
  47. if(this->city==NULL){
  48. return;
  49. }
  50. this->city->print();
  51. if (this->next != NULL) {
  52. this->next->print();
  53. }
  54. }
  55.  
  56. void clear() {
  57. if(this->city==NULL){
  58. return;
  59. }
  60. if (this->city != NULL && this->city->data == 0) {
  61. if (this->next != NULL) {
  62. CityList* tmp = this->next;
  63. this->city = this->next->city;
  64. this->next = this->next->next;
  65. delete tmp;
  66. this->clear();
  67. } else {
  68. this->city = NULL;
  69. }
  70. } else if (this->next != NULL) {
  71. this->next->clear();
  72. }
  73. }
  74.  
  75. int &ref(const char *name){
  76. CityList * temp=this;
  77. if(temp->city==NULL){
  78. temp->city=new City(0,name);
  79. return temp->city->data;
  80. }
  81. if(strcmp(temp->city->name, name) > 0){
  82. CityList *t;
  83. t=new CityList;
  84. t->city=new City(0,name);
  85. t->next=this->next;
  86. this->next = t;
  87. City *k = this->city;
  88. this->city = temp->city;
  89. t->city = k;
  90. return this->city->data;
  91. }
  92. else{
  93. CityList *p=this;
  94. while (temp!=NULL){
  95. if(strcmp(temp->city->name, name)==0){
  96. return temp->city->data;
  97. }
  98. if (strcmp(temp->city->name, name)>0){
  99. while(p->next!=temp){
  100. p=p->next;
  101. }
  102. p->next=new CityList;
  103. p->next->city=new City(0,name);
  104. p->next->next=temp;
  105. return p->next->city->data;
  106. }
  107. if(temp->next==NULL){
  108. temp->next=new CityList;
  109. temp->next->city=new City(0,name);
  110. return temp->next->city->data;
  111. }
  112. temp=temp->next;
  113. }
  114. }
  115. }
  116.  
  117. };
  118.  
  119. int main(){
  120. City d(34,"ewge");
  121. d.print();
  122. CityList a;
  123. a.print();
  124. a.clear();
  125. a.ref("ad")=99;
  126. a.print();
  127. a.clear();
  128. a.ref("aha");
  129. cout<<"``````````````````````````````"<<endl;
  130. a.ref("we");
  131. a.ref("fdwfe");
  132. a.print();
  133. a.clear();
  134. cout<<"uuuuuuuuuuuu"<<endl;
  135. a.print();
  136. cout<<"------------------"<<endl;
  137.  
  138.  
  139.  
  140. cout<<"ssssssssssssssssss"<<endl;
  141.  
  142. CityList *s=new CityList();
  143. s->ref("dsf")=7;
  144. s->print();
  145. return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement