Не получается отсортировать односвязный кольцевой линейный список

Помогите с кодом, после ввода чисел происходит зацикливание, другие замечания тоже принимаю, заранее спасибо.

Вот задание: Сформировать односвязный кольцевой линейный список по файлу символов. Упорядочить символы с помощью очереди: найти в списке узел с минимальным значением и добавлять его в очередь, удаляя минимальное значение из списка, пока список не будет пустой, а все элементы не будут в очереди).

Вот мой код

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>

using namespace std;

struct list
{
    int data;
    list* next;
};
typedef list* Plist;

void CreateFile(fstream& fout, char* namefile)
{
    int x;
    fout.open(namefile, ios::out | ios::binary);
    cout << "\n Enter number (enter 999 to quit) : x=";
    cin >> x;
    while (x != 999)
    {
        fout.write((char*)&x, sizeof x);
        cout << "\n x= ";
        cin >> x;
    }
    fout.close();
    cout << "\n";
}

void ReadFile(fstream& fin, char* namefile)
{
    int x;
    fin.open(namefile, ios::in | ios::binary);
    if (fin.is_open())
    {
        cout << "The current contents of the " << namefile << " file : \n";
        while (fin.read((char*)&x, sizeof x))
        {
            cout << x << ", ";
        }
    }
    fin.close();
    cout << "\n";
}

void Insert_end(Plist& last, int number)
{
    Plist temp_p;
    temp_p = new list;
    temp_p->data = number;

    if (last == nullptr)
        temp_p->next = last = temp_p;
    else
    {
        temp_p->next = last->next;
        last->next = temp_p;
        last = temp_p;
    }
}

bool Empty(Plist first, Plist last)
{
    return (first == nullptr && last == nullptr);
}

void Delete_first(Plist& first, int& number)
{
    Plist p;
    p = first;
    number = first->data;
    first = p->next;
    delete p;
}

void Del_after_q(Plist& q, int& number)
{
    Plist p;
    p = q;
    number = q->data;
    q = p->next;
    delete q;

}

void Create_list(fstream& finout, char* namefile, Plist& first, Plist& last)
{
    int a;
    finout.open(namefile, ios::in | ios::binary);
    if (!finout.is_open())
    {
        cout << "Error"; exit(1);
    }
    first = nullptr;
    last = nullptr;
    finout.clear();
    while (finout.read((char*)&a, sizeof a))
    {
        Insert_end(last, a);
        if (!first)
            first = last;
    }

    finout.close();
}

void Read_list(Plist first)
{
    Plist p;
    cout << "The current contents of the queue :\n";
    p = first;
    while (p = nullptr)
    {
        cout << p->data << " ";
        p = p->next;
    }
}

void design_queue(Plist& first, Plist& last, fstream& fin, char* namefile, Plist& q)
{
    fin.open(namefile, ios::in | ios::binary);

    int x;
    int min = 10000;

m: if (fin.eof())
{
    Read_list(first);
}
else while (fin.read((char*)&x, sizeof x))
{
    if (x < min)
        min = x;
}
Insert_end(last, min);
Del_after_q(q, min);
goto m;
fin.close();
}

void Del_list(Plist& first)
{
    Plist p, pt;
    pt = first;
    while (pt != nullptr)
    {
        cout << pt->data << " ";
        p = pt;
        pt = pt->next;
        delete p;
    }
    first = pt;
}

int main()
{
    fstream k;
    Plist first, last, p, q, pt;
    int x;
    char namefile[15], ch;
    cout << "name of file : ";
    cin >> namefile;
    cout << "create file ? (y/n)";
    cin >> ch;
    if (ch == 'y')
    {
        CreateFile(k, namefile);
        ReadFile(k, namefile);
    }
    else
    {
        ReadFile(k, namefile);
    };
    k.open(namefile, ios::in | ios::binary);
    if (!k.is_open())
    {
        cout << "Error"; exit(1);
    }
    Create_list(k, namefile, first, last);
    Read_list(first);
    fstream fin;
    design_queue(first, last, fin, namefile, q);
    Del_list(first);
    cout << endl;
}

Ответы (0 шт):