c按字母顺序编程链接列表排序名称

时间:2018-03-18 18:27:00

标签: c

整个代码是要求用户输入学生'除了排序列表之外,所有其他功能都很好。我正在尝试创建一个名为sortlist()的函数,以便在用户输入名称后按字母顺序排列链接列表中的名称。 / p>

/*void sortlist(){
    char t[25];
    struct Node*temp=head;
    struct Node*temp1=temp->next;
    while(temp!=NULL){
        if(strcmp(temp->name,temp1->name)>0){
            strcpy(t,temp1->name);
            strcpy(temp1->name,temp->name);
            strcpy(temp->name,t);
        }temp=temp->next;
    }
}

这是我的sortlist()函数,一旦我选择案例1,程序就会崩溃。这不是实现它的方法吗?

以下是该计划的完整代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Node {
    char name[100];
    float marks;
    struct Node* next; //next is a pointer that stores the address of the next 
    node
};
struct Node* head; //head is a pointer that stores the address of first node
void printMenu();     //function that prints the menu of the choices
void Print();         //function that prints the result of ALL the students
void Insert_marks();  //function that insert marks of the students
void Insert_names();  //function that insert the names of the students
void Delete();        //function that delete a particular student
void search();        //function that search ONE student and print his or 
                      //  her report
void SaveToFile();    //function that save the data entered in text file
void OpenFile();      //function that read data from the text file
void sortlist();

int main(){
    int choice,n;
    char strings[25];
    char words[25];
    head = NULL;  // the list is empty now

    do{
        printMenu();
        scanf("%d",&choice);

        switch(choice){
        case 1:
            //Introduce the names of the students
            printf("enter the name: \n");
            scanf("%s",strings);
            Insert_names(strings);
            sortlist();
            break;
        case 2:
            // Delete the report for a particular student
            printf("enter a position");
            scanf("%d",&n);
            Delete(n);
            break;
        case 3:
            //Introduce marks for all the students
            Insert_marks();
            break;
        case 4:
            // print report for individual student
            printf("Enter the name:\n");
            scanf("%s",words);
            search(words);
             break;
        case 5:
            // print report for all the students
            printf("NO\tNames\t\tMarks\n");
            Print();
            break;
        case 6:
            //Save the data into a text file
            SaveToFile();
            break;
        case 7:
            // read the file
            OpenFile();
            break;
        }
    }while(choice!= 8);
}


void Insert_names(char strings[25]){
    struct Node*temp= (struct Node*)malloc(sizeof(struct Node)); //creating a 
                                                                 //    Node
    strcpy(temp->name,strings);
    temp->marks=-1;
    temp->next=NULL;
    if(head == NULL){
        head = temp;
        return;
    }else{
        struct Node *p =head;
        while(p->next)
            p= p->next;
        p->next=temp;
     }
     printf("%s\t%s\n",temp->name,temp->next->name);
}

void Insert_marks(){
    float x;
    struct Node* temp = head;
    while(temp->marks != -1){
        temp=temp->next;
    }
    while(temp != NULL){
        printf("enter the marks \n");
        scanf("%f",&x);
        temp->marks=x;
        temp=temp->next;
    }
}

void Print(){
    struct Node* temp = head;
    int i=0;
    while(temp!= NULL){
        i+=1;
        printf("%d\t%s\t\t%f",i,temp->name,temp->marks);
        temp=temp->next;
        printf("\n");
    }   
}

void Delete(int n){
    struct Node* temp1 = head;
    if (n ==1){
        head = temp1-> next;
        free(temp1);
        return;
    }
    int i;
    for (i=0;i<n-2;i++)
        temp1 = temp1 ->next;
    struct Node* temp2= temp1->next;
    temp1->next = temp2-> next;
    free(temp2);
}

void sortlist(){
    char t[25];
    struct Node*temp=head;
    struct Node*temp1=temp->next;
    while(temp!=NULL){
        if(strcmp(temp->name,temp1->name)>0){
            strcpy(t,temp1->name);
            strcpy(temp1->name,temp->name);
            strcpy(temp->name,t);
        }
    }
}

void search(char words[25]){
    struct Node*temp = head;
    while(temp!= NULL){
        if(strcmpi(temp->name,words)==0){
            printf("%s\t%f\n",temp->name,temp->marks);
        }
        temp=temp->next;
    }
}

void SaveToFile(){
    FILE* fp;
    fp = fopen("studentreport.txt","w");
    fprintf(fp,"No\tName\t\tMarks\n");
    struct Node* temp = head;
    int i=0;
    while(temp!= NULL){
        i+=1;
        fprintf(fp,"%d\t%s\t\t%f",i,temp->name,temp->marks);
        fprintf(fp,"\n");
        temp=temp->next;
    }
    fclose(fp);
}

void OpenFile(){
    FILE*fopen(),*fp;
    int c;
    fp = fopen("studentreport.txt","r");
    c = getc(fp);
    while(c != EOF){
        printf("%c",c);
        c=getc(fp);
    }
    fclose(fp);
}

void printMenu(){
    printf("1.Introduce Student\n2.Remove student\n3.Introduce marks\n4.Print students report\n5.Print report for all students\n6.Save to file\n7.Retrieve data from file\n8.Exit\n");
}

1 个答案:

答案 0 :(得分:3)

问题在于:

void sortlist(){
    char t[25];
    struct Node*temp=head;
    struct Node*temp1=temp->next;
    while(temp!=NULL){
        if(strcmp(temp->name,temp1->name)>0){  // NOTICE: Dereference of temp1
            strcpy(t,temp1->name);
            strcpy(temp1->name,temp->name);
            strcpy(temp->name,t);
        }
        temp=temp->next;
    }
}

如果列表中只有一个元素temp1为NULL,则执行字符串比较时程序会崩溃。

也许你想这样做:

while(temp1!=NULL){

如果列表为空,您还有一个问题。这也需要处理。

除此之外,逻辑似乎是错误的。 temp1始终相同,因此无法对列表进行排序。

总的来说,我认为更好的方法是在正确的位置插入新元素,而不是使用排序函数。