Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- class cityList;
- class City{
- friend class cityList;
- char * name;
- int data;
- public:
- City(int data, const char * name){
- this->data=data;
- this->name = new char[strlen(name)+1];
- strcpy(this->name, name);
- }
- void print(){
- cout<<data<<" "<<name<<endl;
- }
- ~City(){
- delete []this->name;
- }
- };
- class cityList{
- City *city;
- cityList *next;
- public:
- cityList() {
- this->city = NULL; this->next = NULL;
- }
- ~cityList(){
- cityList *temp=head;
- cityList *prev=head;
- while(temp){
- temp=temp->next;
- delete prev;
- prev = temp;
- }
- }
- int &ref(const char *name){
- cityList *temp = head;
- cityList *prev = head;
- if(head == NULL){
- City newcity(0,name);
- head->city = newcity;
- head->next=NULL;
- return head->city->data;
- }
- if(strcmp(head->city->name, name) > 0){
- City newcity(0,name);
- head->city = newcity;
- return head->city->data;
- }
- while(temp != NULL){
- int k = strcmp(temp->city->name, name);
- if(k < 0){
- prev = temp;
- temp = temp->next;
- }
- else if(k > 0){
- City newcity(0,name);
- prev->next->city = newcity;
- return newcity->data;
- }
- else
- return temp->city->data;
- }
- City newcity(0,name);
- prev->next->city = newcity;
- return newcity->data;
- }
- void clear(){
- if(head==NULL){
- return;
- }
- if(head->next==NULL ){
- if(head->city->data!=0){
- return;
- }
- else{
- delete []head->city->name;
- delete head;
- head=NULL;
- return;
- }
- }
- cityList *temp = head;
- cityList *prev = head;
- while(temp != NULL){
- while(head->city->data == 0) {
- head = head->next;
- delete []temp->city->name;
- delete temp;
- temp = head;
- }
- if (temp->city->data == 0){
- prev->next=temp->next;
- delete []temp->city->name;
- delete temp;
- temp = prev;
- }
- prev=temp;
- temp=temp->next;
- }
- }
- void print() { // wypisujemy miasto które ten konkretny CityList przechowuje
- this->city->print(); // lecimy dalej
- if (this->next != NULL) {
- this->next->print();
- }
- }
- };
- int main(){
- City k(34,"sd");
- k.print();
- cityList d;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement