Sortowaniem alfabetycznym w liscie

0

Witam, problem jest dosc ogolny bo wysypuje mi sie program a nie widze bledu. Czy jest ktos na tyle spostrzegawczy by mi wskazal bledy. A jesli chodzi o program to ma towrzyc liste (dodajac elementy oraz sortujac calos alfabetycznie), kasowac wybrany element(w tym wypadku z wartoscia 0) oraz wyswietlac cala liste. to wszystko ma byc w trzech funkcjach a dzialamy na strukturze ktora bedzie widzoczna w programnie . Moim zdanie cos zle napisalem gry program mam sortowac. Z gory dzieki za poswiecony czas

#include <iostream>
#include <string.h>

using namespace std;

struct City{
    char* name;
    int data;
    City* next;
};

City* head = NULL;

int& ref(char* name){

    if(head == NULL){//just creating head
        City* temp = new City;
        temp->name = new char[strlen(name)+1];
        strcpy(temp->name, name);
        temp->data = 0;
        temp->next = NULL;
        head = temp;
        return head->data;
    }

    if(head!=NULL){//when we want put this element after head
        City* spot=NULL;
        City* cur=head;
        City* kasztan=cur->next;//our second element
        if(strcmp(kasztan->name, name)>0){//if element after head is bigger we re putting this element after head
            City* node=new City;
            node->name=new char[strlen(name)+1];
            strcpy(node->name, name);
            node->data=1;
            cur->next=node;//creating element after head. new second element
            kasztan=node->next;//previous second element moving to third position, creating it like a next of our new (node)
            return node->data;
        }

        if(strcmp(cur->name, name)==0){//situation when we try to put again one element like warsaw and warsaw
            return cur->data;
        }

        if(strcmp(cur->name, name)!=0){//sytuacja gry dodajemy element w srodku albo na koncu
            while(cur!=NULL){
                    spot=cur->next;
                while(strcmp(spot->name,name)<0){
                    spot=cur;
                    cur=cur->next;
                    node->name=new char[strlen(name)+1];
                    strcpy(node->name, name);
                    node->data=1;
                    spot->next=node;
                    node->next=cur;
                }
            }
        return node->data;
        }
    }

}

void clear()
{
    City* cur=head;
    City* temp=cur->next;
    while(cur!=NULL){
        if(cur->data==0){
            if(head->data==0){
                delete [] head;
                head=temp;
                break;
            }
            else{
                delete [] cur;
                break;
            }
        }
    }
}


void print(){
    if(head == NULL){
        cout << "The list is empty. \n";
    }	else{
        City* temp = head;
        cout << "The list contains the following cities: \n";
        while(temp != NULL){
            cout << "Name: " << temp->name << "\t data: " << temp->data << endl;
            temp = temp->next;
        }
    }
    cout<<"------------------------------------------"<<endl;
}

int main()
{
    print();
    int i=ref("Berlin")+7;
    ref("Warsaw")=i+ref("Barcelona")+i;
    ref("Amsterdam");
    ref("Edynburg");
    print();
    clear();
    print();

    return 0;
}
1
  1. to mało jest c++
  2. zamień
int& ref(char* name)

na

 int& ref(const char* name)
  1. zamiast
 if(strcmp(cur->name, name)==0)

zrob

lcmpres =  strcmp(cur->name, name);
 if(lcmpres==0)
 

bo wykonujesz takie porownania 2 razy, wiec wykorzystaj to
4) jak ci się wysypuje, bo analizować wszystkiego mi sie nie chce, jak dla mnie zdziwienie, że ci się w ogóle kompiluje, bo bez 2 punktu nie powinno.

0

dzieki za pomoc

1 użytkowników online, w tym zalogowanych: 0, gości: 1