DEV Community

jason2313121121
jason2313121121

Posted on

code problem

was doing a project for college but ran into a problem the radix sort that im trying to implement should sort the district name alphabetically and the algorithm is that we should start at the right most character and insert it into the wright place in the radix array and so on until we reach character zero, the problem is when i press option 3 the console return.

include

include

include

include

define size 30

//definitions
typedef struct node* list;
typedef struct node* node;
//struct
struct node{
char name[20];
int population;
struct node* next;
struct node* previous;
};

struct node_alpha{
char data;
struct node_alpha* next;
};
//global variables
struct node_alpha* radix[63];
struct node* districts[size];
struct node* temp[size];
int numOfDistrict =0;
//flags
int isSorted =0;
//functions
list makeEmpty(list l);
struct node_alpha* makeEmpty_radix(struct node_alpha* l);
void insert(char town_name[50],int population,list l);
void load(int* numOfDistrict);
void printInputFile();
void init_radix();
int find_max_length_of_districts();
void extend_String();
void Delete_radix();
void radix_sort();
void print();
void Sum();
void add_town();
void delete_town();
void delete_district();
void calc();
void print_no_detail();
void change_population();
void save();

//==========================
int main()
{
init_radix();
int op;
do{
printf("\nmenu\n");
printf("1.load the input file\n");
printf("2.print loaded information from the input file\n");
printf("3.sort the districts alphabetically\n");
printf("4.Sort the towns for each district based on population in ascending order\n");
printf("5.Print the sorted information\n");
printf("6.Add a new district to the list of sorted districts\n");
printf("7.Add a new town to a certain district\n");
printf("8.Delete a town from a specific district\n");
printf("9.Delete a complete district\n");
printf("10.Calculate the population of Palestine and min and max town\n");
printf("11.Print the districts and their total population\n");
printf("12Change the population of a town\n");
printf("13.Save to output file\n");
printf("14.exit\n");
printf("please enter an operation:");
scanf("%d",&op);
switch(op){
case 1:
load(&numOfDistrict);
break;
case 2:
printInputFile();
break;
case 3:
radix_sort();
break;
case 4:
break;
case 5:
print();
break;
case 6:
add_district();
break;
case 7:
add_town();
break;
case 8:
delete_town();
break;
case 9:
delete_district();
break;
case 10:
calc();
break;
case 11:
print_no_detail();
break;
case 12:
change_population();
break;
case 13:
save();
break;
case 14:printf("exiting system.....");
break;
case 15:
find_max_length_of_districts();
extend_String();
break;
default:printf("not a correct operation!!...please enter a valid operation");
break;

}

}while(op != 14);
}

list makeEmpty(list l){
l=(list)malloc(sizeof(struct node));
if(l==NULL)
printf("memory is full");
else
l->next= NULL;
l->previous = NULL;
return l;
}
struct node_alpha* makeEmpty_radix(struct node_alpha* l){
l=(struct node_alpha*)malloc(sizeof(struct node_alpha));
if(l==NULL)
printf("memory is full");
else
l->next= NULL;
return l;
}
void insert(char town_name[50],int population,list l){
node temp;
temp =(node)malloc(sizeof(struct node));
if(temp == NULL)
printf("memory is full");
else{
strcpy(temp->name,town_name);
temp->population = population;
temp->next = l->next;
l->next = temp;
temp->previous = l ;
if(temp->next !=NULL)
temp->next->previous =temp;
}
}
void load (int* numOfDistrict){
char line[50];
int population;
char town[20],district[20];
int i =0;

FILE *input_file;
input_file = fopen("districts.txt","r");
if(input_file == NULL){
    printf("error opening file");
}
else{
while(fgets(line,50,input_file)!=NULL){
    int reapeat = 0;
    char *ptr = strtok(line,"|");
    strcpy(district,ptr);
    ptr = strtok(NULL,"|");
    strcpy(town,ptr);
    ptr = strtok(NULL,"|");
    population = atoi(ptr);
    for(int g =0;g< *numOfDistrict;g++){
        if(strcasecmp(districts[g]->name,district)==0){
            insert(town,population,districts[g]);
             Sum();
            reapeat =1;
            break;
    }
}
if(reapeat !=1){
districts[i] = makeEmpty(NULL);
strcpy(districts[i]->name,district);
insert(town,population,districts[i]);
Sum();
(*numOfDistrict)++;
i++;
}
Enter fullscreen mode Exit fullscreen mode

}
printf("\nfile was loaded successfully\n");
fclose(input_file);
}
}
void Sum(){
node p;
for(int i =0;i int sum = 0;
p= districts[i]->next;
while(p!=NULL){
sum+=p->population;
p=p->next;
}
districts[i]->population = sum;
}
}
void printInputFile(){
char line[50];
FILE *input_file;
input_file = fopen("districts.txt","r");
if(input_file == NULL){
printf("error opening file");
return 0;
}
printf("\n");
while(fgets(line,50,input_file)!=NULL){
printf("%s\n",line);
}
}
void init_radix(){
radix[0]=makeEmpty_radix(NULL);
radix[0]->data ='$';
char digit ='0';
for(int i =1;i<11;i++){
radix[i]=makeEmpty_radix(NULL);
radix[i]->data = digit;
digit+=1;
}
char ch ='A';
for(int j =11;j<37;j++){
radix[j]=makeEmpty_radix(NULL);
radix[j]->data = ch;
ch+=1;
}
ch='a';
for(int k =37;k<63;k++){
radix[k]=makeEmpty_radix(NULL);
radix[k]->data = ch;
ch+=1;
}
}
int find_max_length_of_districts(){
int max = strlen(districts[0]->name);
for(int i=1;i if(strlen(districts[i]->name)>max)
max=strlen(districts[i]->name);
}
return max;
}
void extend_String(){
int diff=0;
int length = find_max_length_of_districts();
for(int i = 0;i if(strlen(districts[i]->name)!= length){
diff = length - strlen(districts[i]->name);
for(int j=0;j strcat(districts[i]->name,"$");
}
}
}

}
void Delete_radix(){
node p,temp;
for(int i=0;i<63;i++){
if(radix[i]->next !=NULL){
p =radix[i]->next;
radix[i]->next = NULL;
while(p!=NULL){
temp = p->next;
free(p);
p=temp;
}
}
}

}
void radix_sort() {
extend_String();
int max_length = find_max_length_of_districts();
char ch;
int pos=0;
for(int i=max_length-1;i>=0;i--){
printf("start\n");
for(int j=0;j printf("start\n");
ch=districts[j]->name[i];
printf("start\n");
for(int k=0;k<63;k++){
if(ch==radix[k]->data){
printf("start\n");
node p=radix[k];
if(p->next != NULL)
p =p->next;
p->next=districts[j];
}
}
}
printf("start\n");

    for(int h=0;h<63;h++){
        if(radix[h]->next!=NULL){
            node q = radix[h]->next;
            temp[pos] = q;
            pos++;
        }
    }
    Delete_radix();


}
for(int a=0;a<numOfDistrict;a++){
    districts[a]=temp[a];

}
Enter fullscreen mode Exit fullscreen mode

}

Top comments (0)