鱼C论坛

 找回密码
 立即注册
查看: 454|回复: 1

对已按平均成绩排好序的4个班的同学的构造一个所有按平均成绩的以非递增方式班内排序

[复制链接]
发表于 2024-1-7 00:03:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
平均成绩已经算好了,如何让这个链表在主函数中实现题目的意思呢,就是说
template<typename T>
struct Node {
    T data;
    Node* next;
    Node(T value) : data(value), next(nullptr) {}
};
template<typename T>
class LinkedList {
private:
    Node<T>* head;
    Node<T>* tail;

public:
    LinkedList() : head(nullptr), tail(nullptr) {}

    ~LinkedList() {
        while (head != nullptr) {
            Node<T>* temp = head;
            head = head->next;
            delete temp;
        }
    }
    void insert(T value) {
        Node<T>* newNode = new Node<T>(value);
        if (head == nullptr) {
            head = newNode;
            tail = newNode;
        }
        else {
            tail->next = newNode;
            tail = newNode;
        }
    }

    void display() const {
        Node<T>* current = head;
        while (current != nullptr) {
            std::cout << current->data << " -> ";
            current = current->next;
        }
        std::cout << "nullptr" << std::endl;
    }
};
void swap(double &a, double &b)
{
    if (a > b)
    {
        double temp = a;
        a =b;
        b = temp;
    }
}
LinkedList<int> list;
void makelist(student students[])
{
    double arr1[] = { 0 };
    double arr2[] = { 0 };
    double arr3[] = { 0 };
    double arr4[] = { 0 };

    double* begin1 = arr1;
    double* end1 = arr1 + sizeof(arr1) / sizeof(arr1[0]);
    double a = accumulate(begin1, end1, 0);
    double* begin2 = arr2;
    double* end2 = arr2 + sizeof(arr2) / sizeof(arr2[0]);
    double b = accumulate(begin2, end2, 0);
    double* begin3 = arr3;
    double* end3 = arr3 + sizeof(arr3) / sizeof(arr3[0]);
    double c = accumulate(begin3, end3, 0);
    double* begin4 = arr4;
    double* end4 = arr4 + sizeof(arr4) / sizeof(arr4[0]);
    double d = accumulate(begin4, end4, 0);

    swap(a, b);
    swap(b, c);
    swap(c, d);
    swap(a, b);
    swap(b, c);
    list.insert(a);
    list.insert(b);
    list.insert(c);
    list.insert(d);
    list.display();
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-1-7 00:04:07 | 显示全部楼层
要按照题目的要求对链表进行非递增排序,可以通过以下步骤实现:

1. 在`LinkedList`类中新增一个函数`sortDescending()`用于排序。在该函数内部,我们将使用插入排序的方法对链表进行排序。

2. 在`LinkedList`类中添加一个私有函数`insertNode(Node* newNode)`,该函数用于将一个节点插入到已排序的链表中,并保持链表的非递增顺序。

3. 在`sortDescending()`函数中,遍历链表中的每个节点,然后调用`insertNode()`函数将每个节点插入到已排序的部分链表中。

4. 在主函数中,创建一个`LinkedList`对象,并插入已按平均成绩排好序的节点。

下面是修改后的代码示例:

  1. template<typename T>
  2. struct Node {
  3.     T data;
  4.     Node* next;
  5.     Node(T value) : data(value), next(nullptr) {}
  6. };

  7. template<typename T>
  8. class LinkedList {
  9. private:
  10.     Node<T>* head;
  11.     Node<T>* tail;

  12. public:
  13.     LinkedList() : head(nullptr), tail(nullptr) {}

  14.     ~LinkedList() {
  15.         while (head != nullptr) {
  16.             Node<T>* temp = head;
  17.             head = head->next;
  18.             delete temp;
  19.         }
  20.     }

  21.     void insert(T value) {
  22.         Node<T>* newNode = new Node<T>(value);
  23.         if (head == nullptr) {
  24.             head = newNode;
  25.             tail = newNode;
  26.         }
  27.         else {
  28.             tail->next = newNode;
  29.             tail = newNode;
  30.         }
  31.     }

  32.     void display() const {
  33.         Node<T>* current = head;
  34.         while (current != nullptr) {
  35.             std::cout << current->data << " ";
  36.             current = current->next;
  37.         }
  38.     }

  39.     void sortDescending() {
  40.         if (head == nullptr || head->next == nullptr) {
  41.             return;
  42.         }

  43.         Node<T>* current = head->next;
  44.         head->next = nullptr;

  45.         while (current != nullptr) {
  46.             Node<T>* nextNode = current->next;
  47.             insertNode(current);
  48.             current = nextNode;
  49.         }
  50.     }

  51. private:
  52.     void insertNode(Node<T>* newNode) {
  53.         if (newNode->data >= head->data) {
  54.             newNode->next = head;
  55.             head = newNode;
  56.         }
  57.         else {
  58.             Node<T>* current = head;
  59.             while (current->next != nullptr && newNode->data < current->next->data) {
  60.                 current = current->next;
  61.             }
  62.             newNode->next = current->next;
  63.             current->next = newNode;
  64.         }
  65.     }
  66. };

  67. int main() {
  68.     LinkedList<int> list;
  69.    
  70.     // 假设已经按平均成绩排好序,如:80, 75, 70, 60
  71.     list.insert(80);
  72.     list.insert(75);
  73.     list.insert(70);
  74.     list.insert(60);
  75.    
  76.     list.sortDescending();
  77.     list.display();  // 输出结果为:80 75 70 60

  78.     return 0;
  79. }
复制代码


在主函数中,我们首先创建一个`LinkedList`对象`list`并插入已按平均成绩排好序的4个节点。然后调用`list.sortDescending()`函数对链表进行排序,并调用`list.display()`函数来展示排序后的链表结果。最后,输出结果为`80 75 70 60`,符合题目要求的非递增顺序。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-10 08:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表