对学生结构链进行排序:sortbyScore功能

时间:2014-07-16 06:13:25

标签: c++ pointers structure chain

解决方案

我尝试使用学生结构来创建学生链并执行删除插入学生的事情。但是遇到了下面的错误。任何机构都可以解决这个问题?所有源代码都在最后发布。 *

剩余问题:

此外:任何人都可以告诉我如何根据得分订购学生链。非常感谢!!最后一个 sortbyScore 函数。


主要错误:

enter image description here

第二个错误:

enter image description here

代码:

#include<iostream>
#include <cstddef>
using namespace std;

struct student{    //define the student structure
    long num;
    float score;
    student *next;
};

student * creat();
void print(student *head);
student * del(student *head, int num);
student * insert(student *head, student *stud);

int n = 0;  //the number of nodes

int main(){
    student *head = creat();
    cout << "The created chain is: " << endl
        << "Number\tScore" << endl;
    print(head);

    int num; //delete the node
    cout << "Please enter the number of student to delete: ";
    cin >> num;
    head = del(head, num);
    cout << "Current chain is: " << endl;
    print(head);

    student *pt = new student; //insert the node
    cout << "Please enter the number and score to insert: ";
    cin >> pt->num >> pt->score;
    head = insert(head, pt);
    cout << "Current chain is: " << endl;
    print(head);
}
student * creat(){  //create the chain which return a point pointing to the head of the chain
    student *head, *p1, *p2;
    head = NULL;    //without any nodes the head of the chain is null
    p1 = new(student);
    p2 = p1;
    cout << "please enter the student number and score, end input when the student number is 0" << endl;
    cin >> p1->num >> p1->score;  //input data of the first node
    while (p1->num != 0){
        n++;
        if (n == 1)
            head = p1;
        else{
            p2->next = p1;  //previous end node point to a newly created node.
            p2 = p1;        //newly created node become the new end node.
        }
        p1 = new(student);
        cin >> p1->num >> p1->score;
    }
    delete p1;
    p2->next = NULL;
    return head;
}

void print(student *head){
    student *p;
    p = head;
    if (p == NULL) return;
    do{
        cout << p->num << "     " << p->score << endl;
        p = p->next;
    } while (p != NULL);
}

student * del(student *head, int num){ //delete the specidic numbered student node
    student *p1, *p2 = 0;
    if (head = NULL){
        cout << "List NULL" << endl;
        return head;
    }
    p1 = head;
    cout << p1->num << endl;
    while (num != p1->num && p1->next != NULL){
        p2 = p1;         //p2 is the previous node
        p1 = p1->next;
    }
    if (num == p1->num){
        if (p1 == head)
            head = p1->next;   //if p1 point to the head, head point to the second the node
        else
            p2->next = p1->next;   //give the next node's address to the previous node's next point
        cout << "delete: " << num << endl;
        n--;
    }
    else
        cout << num << "not been found" << endl;
    return head;
}

student * insert(student *head, student *stud)  //add node to the chain
{
    student *p0, *p1, *p2 = 0;
    p1 = head;              //p1 point to the head node
    p0 = stud;              //p0 point to the waiting to add node
    if (head = NULL){
        head = p0;
        p0->next = NULL;
    }
    else
        while ((p0->num>p1->num) && (p1->next != NULL)){
        p2 = p1;     //p2 point to the node pointed by p1
        p1 = p1->next;  //p1 move to the next node
        }
    if (p0->num <= p1->num){
        if (head = p1)
            head = p0;       //
        else
            p2->next = p0;   //insert after p2
        p0->next = p1;
    }
    else{
        p1->next = p0;
        p0->next = NULL;
    }
    n++;
    return head;
}

void sortByScore(student *head){

    student *p, *cur, *next;

    for(int i = 0; i < n - 1; i++){
        p = head;
        cur = p->next;
        next = cur->next;
        for (int j = 0; j < n - 1 - i; j++){
            if (cur->score < p->score){  //change the order
                p->next = next;
                cur->next = p;
                p = cur;

                p = cur->next;
                cur = cur->next;
            }
            else{
                p = cur;
                cur = next;
                next = cur->next;
            }
        }
    }

}

1 个答案:

答案 0 :(得分:0)

#include<iostream>
#include <cstddef>
using namespace std;

struct student{    //define the student structure
    long num;
    float score;
    student *next;
};

student * creat();
void print(student *head);
student * del(student *head, int num);
student * insert(student *head, student *stud);

int n = 0;  //the number of nodes

int main(){
    student *head = creat();
    cout << "The created chain is: " << endl
        << "Number\tScore" << endl;
    print(head);

    int num; //delete the node
    cout << "Please enter the number of student to delete: ";
    cin >> num;
    head = del(head, num);
    cout << "Current chain is: " << endl;
    print(head);

    student *pt = new student; //insert the node
    cout << "Please enter the number and score to insert: ";
    cin >> pt->num >> pt->score;
    head = insert(head, pt);
    cout << "Current chain is: " << endl;
    print(head);
}
student * creat(){  //create the chain which return a point pointing to the head of the chain
    student *head, *p1, *p2;
    head = NULL;    //without any nodes the head of the chain is null
    p1 = new(student);
    p2 = p1;
    cout << "please enter the student number and score, end input when the student number is 0" << endl;
    cin >> p1->num >> p1->score;  //input data of the first node
    while (p1->num != 0){
        n++;
        if (n == 1)
            head = p1;
        else{
            p2->next = p1;  //previous end node point to a newly created node.
            p2 = p1;        //newly created node become the new end node.
        }
        p1 = new(student);
        cin >> p1->num >> p1->score;
    }
    delete p1;
    p2->next = NULL;
    return head;
}

void print(student *head){
    student *p;
    p = head;
    if (p == NULL) return;
    do{
        cout << p->num << "     " << p->score << endl;
        p = p->next;
    } while (p != NULL);
}

student * del(student *head, int num){ //delete the specidic numbered student node
    student *p1, *p2 = 0;
    // not '=' but '=='
    // use NULL == head this style can void this problem
    if (NULL == head){
        cout << "List NULL" << endl;
        return head;
    }
    p1 = head;
    cout << p1->num << endl;
    while (num != p1->num && p1->next != NULL){
        p2 = p1;         //p2 is the previous node
        p1 = p1->next;
    }
    if (num == p1->num){
        if (p1 == head)
            head = p1->next;   //if p1 point to the head, head point to the second the node
        else
            p2->next = p1->next;   //give the next node's address to the previous node's next point
        cout << "delete: " << num << endl;
        n--;
        // free node memory
        delete p1;
    }
    else
        cout << num << "not been found" << endl;
    return head;
}

student * insert(student *head, student *stud)  //add node to the chain
{
    student *p0, *p1, *p2 = 0;
    p1 = head;              //p1 point to the head node
    p0 = stud;              //p0 point to the waiting to add node
    /*if (head = NULL){*/
    if (NULL == head){
        head = p0;
        p0->next = NULL;
    }
    else
        while ((p0->num>p1->num) && (p1->next != NULL)){
            p2 = p1;     //p2 point to the node pointed by p1
            p1 = p1->next;  //p1 move to the next node
        }
        if (p0->num <= p1->num){
            /*if (head = p1)*/
            if (head == p1)
                head = p0;       //
            else
                p2->next = p0;   //insert after p2
            p0->next = p1;
        }
        else{
            p1->next = p0;
            p0->next = NULL;
        }
        n++;
        return head;
}

=和==