首页 > 解决方案 > 这种结构的 LinkedList 有什么问题?

问题描述

我有以下代码来创建一个填充有“Job”结构的单链表。

我想根据结构的“time_to_complete”成员对它们进行排序,但我似乎无法让排序工作。我和一些导师一起研究了它,花了很多时间试图找到一个适合我的解决方案,但没有运气。

我对 C 很陌生,所以它可能没有它可以的那么漂亮,但我只需要基本的功能来进行排序,而且我已经用尽了我的资源。

下面的代码在带有 OSX Catalina 的 Macbook Air 上运行:

//Struct for nodes which will comprise the priority queue
struct Job{
    int type;                   //Keeps track of the type
    int ID;                     //Keeps track of the unique node ID to differentiate between jobs
    float time_to_complete;     //Keeps track of time the job will take
    struct Job* next;           //Pointer to the next shortest Job
};

//Constructor for Job structs for easier initialization
struct Job jobCreator(int type, int ID, float time_to_complete, struct Job* next){
    struct Job job;
    job.ID = ID;
    job.time_to_complete = time_to_complete;
    job.type = type;
    job.next = next;
    return job;
}

//Helper function to print the members of any given job struct
void printJob(struct Job* job){
    if(job->ID){
        printf("Data for job%i\n", job->ID);
    }else{
        printf("Error, job is not initialized\n");
    }
    if(job->type){
        printf("--Type: %i\n", job->type);
    }else{
        printf("Error, job type is not initialized\n");
    }
    if(job->time_to_complete){
        printf("--TTC: %f\n", job->time_to_complete);
    }else{
        printf("Error, job TTC is not initialized\n");
    }
    if(job->next){
        printf("--Points to: job%i\n", ((job)->next)->ID);
    }else{
        printf("Error, job next is not initialized\n");
    }
    printf("\n");
};

//Stub for swap
void Swap(struct Job* parent, struct Job* child){
    struct Job tempNode = jobCreator(parent->type, parent->ID, parent->time_to_complete, parent->next);
    
    //Copy over child members to parent members
    parent->ID = child->ID;
    parent->type = child->type;
    parent->time_to_complete = child->time_to_complete;
    child->next = parent->next;
    parent->next = child->next;
    
    //Copy the tempNode members to child members
    child->ID = tempNode.ID;
    child->type = tempNode.type;
    child->time_to_complete = tempNode.time_to_complete;
    child->next = NULL;
}


//Adds a new job to the end of the priority queue
void addJobPriority(struct Job* currentNode, struct Job* newJob){
    while(currentNode->next){
        currentNode = currentNode->next;
    }
    currentNode->next = newJob;
}

//Helper method to sort the queue
void sortQueue(struct Job* rootNode, int jobCounter){
    struct Job* minNode = rootNode; //Temp copy of the root at start
    float minNodeVal = minNode->time_to_complete;
    struct Job* tempNode = rootNode;
    
    printf("Entering the while loop\n");
    for(int i=0;i<jobCounter;i++){
        while(minNode->next){ //While the node has a child
            if((minNode->next)->time_to_complete < minNodeVal){ //If the next node TTC is less than the minNodeVal, move to the next node
    //            minNode = minNode->next;
                minNodeVal = (minNode->next)->time_to_complete;
            }else{ //If the parent TTC is greater than or equal to its child TTC
                //Swap
                break;
            }
            tempNode = minNode;
            minNode = minNode->next;
        }
        Swap(tempNode, minNode);
    }
}

//Driver
int main(){
    //TEST
    //Job declarations
    struct Job job1;
    struct Job job2;
    job1 = jobCreator(1, 1, 1.0, NULL);
    job2 = jobCreator(2, 2, 2.0, NULL);
    int jobCounter = 2;

//TEST FOR SORT
    addJobPriority(&job2, &job1);
    printf("job2, job1:\n");
    printJob(&job1);
    printJob(&job2);
    printf("Sorting...\n");
    sortQueue(&job1, jobCounter);
    printf("Printing states after the swap job1, job2\n");
    printJob(&job1);
    printJob(&job2);
    
    //Return 0 on success
    return 0;
    
}

理想情况下,这应该显示job1链接到job2,job2链接到job3,job3链接到job4。

以两个作业为例:

job2, job1:
Data for job1
--Type: 1
--TTC: 1.000000
Error, job next is not initialized

Data for job2
--Type: 2
--TTC: 2.000000
--Points to: job1

Sorting...
Entering the while loop
Printing states after the swap job1, job2
Data for job1
--Type: 1
--TTC: 1.000000
Error, job next is not initialized.  <----This should point to job2

Data for job2
--Type: 2
--TTC: 2.000000
--Points to: job1.                   <----This should point to Null and 
                                          print "error, job next is not 
                                          initialized"

标签: csortingstructsingly-linked-list

解决方案


推荐阅读