C++ 如何在c+;中没有STL的情况下对多个动态队列进行排序+;

C++ 如何在c+;中没有STL的情况下对多个动态队列进行排序+;,c++,algorithm,sorting,queue,C++,Algorithm,Sorting,Queue,我刚开始学习堆叠、队列和甲板。从课本和互联网上我所看到的,我已经掌握了队列的工作方式。我试图解决一个我作为家庭作业得到的问题,所以我不是要求完整的解决方案,我只是想要一个关于如何解决这个问题的建议 我想做的是取两个动态队列,根据元素的大小对它们进行排序,然后将所述元素放入第三个队列,该队列也将被排序。对我来说,最简单的方法是将所有元素弹出到一个数组中进行排序,然后将它们推送到队列中,但任务的一部分是不使用任何额外的空间 我已经想到了实现这一点的方法,下面是我的想法:每个队列将分别进行排序,方法是

我刚开始学习堆叠、队列和甲板。从课本和互联网上我所看到的,我已经掌握了队列的工作方式。我试图解决一个我作为家庭作业得到的问题,所以我不是要求完整的解决方案,我只是想要一个关于如何解决这个问题的建议

我想做的是取两个动态队列,根据元素的大小对它们进行排序,然后将所述元素放入第三个队列,该队列也将被排序。对我来说,最简单的方法是将所有元素弹出到一个数组中进行排序,然后将它们推送到队列中,但任务的一部分是不使用任何额外的空间

我已经想到了实现这一点的方法,下面是我的想法:每个队列将分别进行排序,方法是弹出其2个元素,然后比较它们,然后将较小的元素推回,同时保留较大的元素以将其与队列的下一个元素进行比较。到了第n轮,他们应该已经被分类了

让我困惑的是如何做比较它们的功能。我已经尝试过编写代码,但是做不了很多,因为我们不允许使用
stl
库。我知道这个问题很愚蠢,但我花了整整一周的时间来研究这个问题,我不知道该怎么办了。即使它是一个链接到一个网站,解释了这应该如何工作,我仍然会感激它

#include <iostream>
using namespace std;

struct elem {
    int key;
    elem* next;
}* first = NULL, * last = NULL;

struct elem2 {
    int key2;
    elem2* next3;
}* first2 = NULL, * last2 = NULL;

struct elem3 {
    int key3;
    elem3* next3;
}* first3 = NULL, * last3 = NULL;

//elem *push(int n, elem *&first, elem *&last);
//elem *pop(int &n, elem *&first, elem *&last);

elem* push(int n, elem*& first, elem*& last)
{
    elem* p = last;
    last = new elem;
    last->key = n;
    last->next = NULL;
    if (p != NULL)
        p->next = last;
    else
        first = last;
    return p;
}

elem* pop(int& n, elem*& first, elem*& last)
{
    elem* p = NULL;
    if (first) {
        n = first->key;
        p = first;
        first = first->next;
        ;

        if (first == NULL)
            last = first;

        return p;
        delete p;
    }
    else
        return nullptr;
}

void main()
{
    int ch, num, amount, i = 1;
    do {
        cout << "\n\t Menu";
        cout << "\n 1.Add elements";
        cout << "\n 2.Add elements to second queue";
        cout << "\n 3. Merge queues and sort them";
        cout << "\n 4.Exit";
        do {
            cout << "\n Your choice is:";
            cin >> ch;
        } while (ch < 1 || ch > 4);
        switch (ch)

        {
        case 1:
            cout << "How many elements would you like to add? \n";
            cin >> amount;
            cout << "Input queue elements:\n";

            for (i = 0; i < amount; i++) {
                cin >> num;
                elem* push(int num, elem*& first, elem*& last);
            }
            break;

        case 2:
            cout << "How many elements would you like to add? \n";
            cin >> amount;
            cout << "Input queue elements:\n";

            for (i = 0; i < amount; i++) {
                cin >> num;
                elem2* push(int num, elem2*& first2, elem2*& last2);
            }
            break;

        case 3:
            break;
        }
    } while (ch != 4);
}
#包括
使用名称空间std;
结构元素{
int键;
elem*下一步;
}*first=NULL,*last=NULL;
结构元素2{
int键2;
elem2*next3;
}*first2=NULL,*last2=NULL;
结构元素3{
int键3;
elem3*next3;
}*first3=NULL,*last3=NULL;
//元素*推送(整数n、元素*&第一个、元素*&最后一个);
//元素*波普(整数和数字、元素*和第一个、元素*和最后一个);
元素*推送(整数n、元素*&第一个、元素*&最后一个)
{
elem*p=最后一个;
最后=新元素;
最后->键=n;
last->next=NULL;
如果(p!=NULL)
p->next=last;
其他的
第一个=最后一个;
返回p;
}
元素*波普(整数与否、元素*与第一、元素*与最后)
{
elem*p=NULL;
如果(第一){
n=第一个->键;
p=第一;
第一个=第一个->下一个;
;
if(first==NULL)
最后=第一;
返回p;
删除p;
}
其他的
返回空ptr;
}
void main()
{
int ch,num,amount,i=1;
做{
金额;
cout>num;
elem2*推送(整数、elem2*&第一个2、elem2*&最后一个2);
}
打破
案例3:
打破
}
}而(ch!=4);
}

到目前为止,我已经解决了大部分问题。我现在唯一要做的就是把他们排到第三排。关于如何继续的想法?

我从您的问题中了解到,您希望在不使用任何额外空间的情况下实现队列合并排序的合并功能。我写了一些代码;考虑一下这个问题。如果您需要更多的澄清,请随时发表评论

#include <bits/stdc++.h>
using namespace std;

// Ref.: https://www.geeksforgeeks.org/queue-linked-list-implementation/

struct QNode {
    int data;
    QNode *next;
    QNode(int d) {
        data = d;
        next = NULL;
    }
};

struct Queue {
    QNode *front, *rear;

    int size;

    Queue() {
        front = rear = NULL;
        size = 0;
    }

    void enQueue(int x) {
        QNode *temp = new QNode(x);
        if (rear == NULL) {
            front = rear = temp;
            size++;
            return;
        }
        rear->next = temp;
        rear = temp;
        size++;
    }

    void deQueue() {
        if (front == NULL)
            return;
        QNode *temp = front;
        front = front->next;
        if (front == NULL)
            rear = NULL;
        delete (temp);
        size--;
    }

    void display() {
        if (front == NULL) {
            cout << "The queue is empty!" << endl;
            return;
        }
        QNode *temp = front;
        while (temp) {
            cout << temp->data << " --> ";
            temp = temp->next;
        }
        cout << endl;
    }

    int at(int i) { // gets q[i]
        if (i < 0 or i >= size)
            throw out_of_range("The index is not valid!");
        QNode *temp = front;
        for (int j = 0; j < i; j++)
            temp = temp->next;
        return temp->data;
    };

    void replace_at(int x, int i) { // sets q[i] = x
        if (i < 0 or i >= size)
            throw out_of_range("The index is not valid!");
        QNode *temp = front;
        for (int j = 0; j < i; j++)
            temp = temp->next;
        temp->data = x;
    }
};

// Ref.: https://www.geeksforgeeks.org/merge-two-sorted-arrays-o1-extra-space/

void merge(Queue q1, Queue q2) {
    int m = q1.size;
    int n = q2.size;
    for (int i = n - 1; i >= 0; i--) {
        int j, last = q1.at(m - 1);
        for (j = m - 2; j >= 0 and q1.at(j) > q2.at(i); j--)
            q1.replace_at(q1.at(j), j + 1);
        if (j != m - 2 || last > q2.at(i)) {
            q1.replace_at(q2.at(i), j + 1);
            q2.replace_at(last, i);
        }
    }
}

int main() {

    Queue q1, q2;
    char ch;
    int data;
    do {
        cout << endl
             << "MENU" << endl
             << "1.\t Add elements to queue 1" << endl
             << "2.\t Add elements to queue 2" << endl
             << "3.\t Merge and sort the two queues" << endl
             << "4.\t Exit" << endl
             << endl
             << "Enter your choice : ";
    lb:
        cin >> ch;
        switch (ch) {
            case '1':
                cout << "Enter the data you want to insert : ";
                cin >> data;
                q1.enQueue(data);
                cout << "Queue after this operation : ";
                q1.display();
                break;
            case '2':
                cout << "Enter the data you want to insert : ";
                cin >> data;
                q2.enQueue(data);
                cout << "Queue after this operation : ";
                q2.display();
                break;
            case '3':
                merge(q1, q2);
                cout << "Queue 1 after this operation : ";
                q1.display();
                cout << "Queue 2 after this operation : ";
                q2.display();
                break;
            case '4':
                break;
            default:
                cout << "Please check your choice!" << endl
                     << "Re-enter your choice : ";
                goto lb;
        }
    } while (ch != '4');

    return 0;
}

elem*推送(int-num、elem*&第一、elem*&最后)是函数的声明,而不是函数调用。所以它在那些
for
循环中什么都不做。在尝试解决更复杂的任务(如本任务)之前,请先掌握基本知识。还有更多类似这样的错误,所以尝试修复代码没有意义。我已经想到了一些方法,下面是我的想法:--您刚刚描述了合并排序的合并部分。问题在于掌握语言的基础知识,而不是任务本身,所以问题应该解决(因此没有适当的选项来关闭此问题).但是,它不需要包含才能工作吗?我必须做的事情之一就是不要使用任何stl库
> solution.exe

MENU
1.       Add elements to queue 1
2.       Add elements to queue 2
3.       Merge and sort the two queues
4.       Exit

Enter your choice : 1
Enter the data you want to insert : 1
Queue after this operation : 1 --> 

MENU
1.       Add elements to queue 1
2.       Add elements to queue 2
3.       Merge and sort the two queues
4.       Exit

Enter your choice : 1
Enter the data you want to insert : 5
Queue after this operation : 1 --> 5 --> 

MENU
1.       Add elements to queue 1
2.       Add elements to queue 2
3.       Merge and sort the two queues
4.       Exit

Enter your choice : 1
Enter the data you want to insert : 9
Queue after this operation : 1 --> 5 --> 9 --> 

MENU
1.       Add elements to queue 1
2.       Add elements to queue 2
3.       Merge and sort the two queues
4.       Exit

Enter your choice : 1
Enter the data you want to insert : 10
Queue after this operation : 1 --> 5 --> 9 --> 10 --> 

MENU
1.       Add elements to queue 1
2.       Add elements to queue 2
3.       Merge and sort the two queues
4.       Exit

Enter your choice : 1
Enter the data you want to insert : 15
Queue after this operation : 1 --> 5 --> 9 --> 10 --> 15 --> 

MENU
1.       Add elements to queue 1
2.       Add elements to queue 2
3.       Merge and sort the two queues
4.       Exit

Enter your choice : 1
Enter the data you want to insert : 20
Queue after this operation : 1 --> 5 --> 9 --> 10 --> 15 --> 20 --> 

MENU
1.       Add elements to queue 1
2.       Add elements to queue 2
3.       Merge and sort the two queues
4.       Exit

Enter your choice : 2
Enter the data you want to insert : 2
Queue after this operation : 2 --> 

MENU
1.       Add elements to queue 1
2.       Add elements to queue 2
3.       Merge and sort the two queues
4.       Exit

Enter your choice : 2
Enter the data you want to insert : 3
Queue after this operation : 2 --> 3 --> 

MENU
1.       Add elements to queue 1
2.       Add elements to queue 2
3.       Merge and sort the two queues
4.       Exit

Enter your choice : 2
Enter the data you want to insert : 8
Queue after this operation : 2 --> 3 --> 8 --> 

MENU
1.       Add elements to queue 1
2.       Add elements to queue 2
3.       Merge and sort the two queues
4.       Exit

Enter your choice : 2
Enter the data you want to insert : 13
Queue after this operation : 2 --> 3 --> 8 --> 13 --> 

MENU
1.       Add elements to queue 1
2.       Add elements to queue 2
3.       Merge and sort the two queues
4.       Exit

Enter your choice : 3
Queue 1 after this operation : 1 --> 2 --> 3 --> 5 --> 8 --> 9 -->
Queue 2 after this operation : 10 --> 13 --> 15 --> 20 -->

MENU
1.       Add elements to queue 1
2.       Add elements to queue 2
3.       Merge and sort the two queues
4.       Exit

Enter your choice : 4