0
#include <iostream>
#include <conio.h>
using namespace std;

struct List
{
    int a;
    List* next;
};
void Print(List* c)
{
    List* print = c;

    while (print)
    {
        cout << print->a << "->";
        print = print->next;
    }

    cout << "NULL" << endl;
}
void Remove(List** begin)
{
    List* t = new List;

    while (*begin != NULL)
    {
        t = *begin;
        *begin = t->next;
    }

    return;
}
void Add_end(List* end)
{
    List* t = new List;
    t->a = NULL;
    t->next = NULL;
    cin >> t->a;
    end->next = t;
    return;
}

int main()
{
    List* begin = new List;
    begin->a = NULL;
    begin->next = NULL;
    cout << "Input the length of the queue" << endl;
    int k;
    cin >> k;
    cout << "Input the queue" << endl;
    cin >> begin->a;
    List* end = begin;

    for (int i = 0; i < k - 1; i++)
    {
        end->next = new List;
        end = end->next;
        cin >> end->a;
        end->next = NULL;
    }

    Print(begin);
    int ind = 100;
    cout << "If you want to end the program enter '0'" << endl;

    while (ind != 0)
    {
        cout << "If you want to add element enter '1' and if you want to delete     enter '2'"
             << endl;
        cin >> ind;

        if (ind == 1)
        {
            Add_end(end);
            Print(begin);
        }

        if (ind == 2)
        {
            Remove(&begin);
            Print(begin);
        }
    }

    _getch();
    return 0;
}

Не могу удалить первый элемент, подозреваю что надо использовать delete но не уверен. Подскажите как доработать функцию Remove.

7
  • 2
    Интересно бы послушать вашу версию, что вы вообще делаете в функции Remove... Просто для интереса - как вы себе представляете, что в ней происходит...
    – Harry
    4 мар 2017 в 15:44
  • для начала посмотрите, где вам нужно поставить delete 4 мар 2017 в 15:51
  • В функции Remove здесь я началу присваиваю адресс следующего элемента. Я не знаю как это сделать правильно, поэтому и прошу помощи, не могли бы вы написать как должна выглядеть эта фунция???? 4 мар 2017 в 16:13
  • Так а что в вашей функции Remove делает цикл? Цикл выполняет повторяющиеся действия. Что за действие вы собрались повторять? 4 мар 2017 в 16:15
  • ой спасибо, я натупил, я весь список так унижтожал 4 мар 2017 в 16:23

1 ответ 1

1
#include <iostream>
#include <conio.h>
using namespace std;

struct List
{
    int a;
    List* next;
    List(int a = 0):a(a),next(nullptr){}
};

void Print(List* c)
{
    List* print = c;

    while (print)
    {
        cout << print->a << "->";
        print = print->next;
    }

    cout << "NULL" << endl;
}

void Remove(List*&begin)
{
    if (begin)
    {
        List * cur = begin->next;
        delete begin;
        begin = cur;
    }
}

void Add_end(List*&begin, int n)
{
    if (begin == nullptr)
    {
        begin = new List(n);
    }
    else
    {
        List * cur = begin;
        for(; cur->next; cur = cur->next);
        cur->next = new List(n);
    }
}

int main()
{
    List *begin = 0;
    cout << "Input the length of the queue" << endl;
    Print(begin);

    int ind = 100;
    cout << "If you want to end the program enter '0'" << endl;
    while (ind != 0)
    {
        cout << "If you want to add element enter '1' and if you want to delete     enter '2'"
             << endl;
        cin >> ind;
        if (ind == 1)
        {
            cout << "Value to add: ";
            int n;
            cin >> n;
            Add_end(begin,n);
            Print(begin);
        }
        if (ind == 2)
        {
            Remove(begin);
            Print(begin);
        }
    }

    _getch();
    return 0;
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.