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

时间:2018-06-05 20:45:53

标签: c++ bubble-sort

这是一个人员注册系统,在选项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;
}

1 个答案:

答案 0 :(得分:1)

你从来没有这样做过。

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

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