Сортировать цепочку структуры студента: функция sortbyScore

Решено ниже

Я пытаюсь использовать структура студента создать студенческая сеть и сделать некоторые удалять а также вставить студенческие вещи. Но столкнулись с ошибками ниже. Любое тело может решить эту проблему? Весь исходный код размещен в конце. *

Оставшаяся проблема:

Кроме того: может кто-нибудь сказать мне, как заказать студенческую сеть в соответствии с или Гол. Большое спасибо!! последний sortbyScore функция.


Основная ошибка:

введите описание изображения здесь

вторая ошибка:

введите описание изображения здесь

Код:

#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;
}
}
}

}

-2

Решение

#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;
}

= и ==

0

Другие решения


По вопросам рекламы [email protected]