首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在C ++中使用冒泡排序按升序对列表进行排序

在C ++中使用冒泡排序按升序对列表进行排序
EN

Stack Overflow用户
提问于 2018-06-06 04:45:53
回答 1查看 193关注 0票数 -3

这是一个人员注册系统,在选项1中我添加了年龄和姓名,我需要我的列表在注册后按年龄按升序自动排序,在选项2中显示的是按年龄排序的列表,而不是注册的顺序。

我不知道该怎么做:/

有人能帮我吗?

谢谢:)

#include <iostream>
#include<stdio.h>    
#include<stdlib.h>
#include <fstream>

using namespace std;
int contadorid = 0;

template<class T>
class Node
{
    T element;
    Node *next;
public:
    Node(T element, Node *n)
    {
        this->element = element;
        this->next = n;
    }
    Node(T element)
    {
        this->element = element;
        this->next = NULL;
    }
    T getElement()
    {
        return this->element;
    }
    void setElement(T element)
    {
        this->element = element;
    }
    Node* getNext()
    {
        return this->next;
    }
    void setNext(Node *next)
    {
        this->next = next;
    }

};

template<class T>
class List
{
private:
    Node<T> *head;
    Node<T> *tail;
    int count;
public:
    List()
    {
        head = tail = 0;
        count = 0;
    }
    bool isEmpty()
    {
        return head == 0;
    }

    void lista_push_back(T element)
    {
        bool empty = this->isEmpty();
        Node<T> *node = new Node<T>(element);
        node->setNext(NULL);
        if (empty)
            this->head = node;
        else
            this->tail->setNext(node);
        this->tail = node;
        this->count++;
    }

    //percorre e mostra na tela
    void percorre_list()
    {
        Node<T>* current = this->begin();

        while (current != NULL)
        {
            cout << current->getElement() << endl;
            current = current->getNext();
        }
    }

    Node<T>* begin()
    {
        return this->head;

    }
    Node<T>* end()
    {
        return this->tail;

    }

    unsigned int size()
    {
        return this->count;
    }

};

string adicionapacote()
{
    int qos;
    char conteudo[1024];
    char pacote[1024];

    cout << "Age\n";

    cin >> qos;
    cout << "Name\n";
    cin >> conteudo;

    contadorid = contadorid + 1;

    sprintf(pacote, "%d\t%d\t%s", contadorid, qos, conteudo);

    return (pacote);

}

int main()
{
    List<string> *L = new List<string>();
    string pacotao;
    while (1)
    {

        cout << endl;
        cout << "1 - Add" << endl;
        cout << "2 - View" << endl;
        cout << "3 - Dump" << endl;
        cout << "4 - Exit" << endl;
        cout << " " << endl;
        int op;
        cin >> op;

        switch (op)
        {
            case 1:
            {

                pacotao = adicionapacote();
                L->lista_push_back(pacotao);
                //L->dumpqos();
                break;

            }
            case 2:
            {
                L->percorre_list();
                break;
            }
            case 3:
            {
                break;
            }

            case 4:
            {

                cout << "Bye" << endl;
                return (0);

            }

        }
    }
    return 0;
}
EN

回答 1

Stack Overflow用户

发布于 2018-06-06 04:57:56

你从来没有做过这种事。

    pacotao=adicionapacote();
    L->lista_push_back(pacotao);

您可能希望基本上执行插入排序,并在正确的位置插入,而不是向后推。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50708674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档