Как удалить со списка последние N элементов? //#include "pch.h"
#include
#include
#include
#include
using namespace std;
template
class CList
{
public:
struct list
{
T value;
list *next;
};
struct list *head, *tail;
CList()
{
head = tail = NULL;
cout <value);
temp = temp->next;
}
return *this;
}
void Addlist(const T& a);
void removeall();
bool DoubleEveryN(int);
template
friend ostream operator << (ostream&, CList &);
~CList()
{
removeall();
cout << "\nДеструктор сработал.\n";
}
};
template
void CList::removeall()
{
while (head)
{
list *p(head);
head = head->next;
delete p;
}
tail = head;
}
template
bool CList::DoubleEveryN(int a)
{
list* curr = head;
int count = 0;
while (curr)
{
count++;
curr = curr->next;
}
curr = head;
list* ptr = NULL;
list* temp = NULL;
int live = count - a + 1;
count = 0;
while (curr)
{
count++;
if (count >= live)
{
ptr->next = curr->next;
delete curr;
curr = ptr;
}
ptr = curr;
curr = curr->next;
}
return true;
}
template
void CList::Addlist(const T& a)
{
list *newlist;
newlist = new list;
newlist->value = a;
newlist->next = 0;
if (head)
{
tail->next = newlist;
tail = newlist;
}
else
tail = head = newlist;
}
template
ostream & operator<<(ostream &out, const CList &obj)
{
cout <\n";
typename CList::list *list = obj.head;
while (list)
{
cout <value <";
list = list->next;
}
cout << "NULL\n";
return out;
}
void menu()
{
cout < \n";
}
int main()
{
setlocale(0, "RUS");
system("color f0");
CList list;
CList doublelist;
CList charlist;
CList< CList >my_list;
int key, x, curr;
int a = 1;
int temp = 0;
do
{
menu();
key = _getch();
switch (key)
{
case '1':
for (int i = 1; i < 11; i++)
{
list.Addlist(3 * i);
charlist.Addlist(char(rand() % 26 * i + 0x61));
doublelist.Addlist(1.332*i);
my_list.Addlist(list);
}
break;
case '2':
cout << list;
cout << charlist;
cout << doublelist;
cout << my_list;
break;
case '3':
cout <> x) || x

21 Авг 2019 в 06:58
299 +1
1
Ответы
1

Чтобы удалить из списка последние N элементов, необходимо вызвать метод DoubleEveryN с параметром N. Этот метод будет удалить из списка последние N элементов.

Пример кода для удаления последних N элементов из списка:

case '3':
cout << "Удалить из списка N последних элементов ";
while (!(cin >> x) || x <= 0)
{
fflush(stdin);
cin.clear();
}
list.DoubleEveryN(x);
charlist.DoubleEveryN(x);
doublelist.DoubleEveryN(x);
my_list.DoubleEveryN(x);
break;
20 Апр 2024 в 13:10
Не можешь разобраться в этой теме?
Обратись за помощью к экспертам
Гарантированные бесплатные доработки в течение 1 года
Быстрое выполнение от 2 часов
Проверка работы на плагиат
Поможем написать учебную работу
Прямой эфир