@andrey_levushkin

Как проверить имеющийся список на пустоту?

Программа имеет на входе список, который заполняется из файла Text.txt (который в ресурсах программы) и выводит все элементы из списка и их сумму. Пример для файла с содержанием "1 2 3 4 5 6": 1 2 3 4 5 6 21
Где "21" - сумма всех чисел из файла (списка). Так же имеется функция на проверку списка на пустоту:
bool isEmpty(Tlist list) { //проверка списка на пустоту
	return list == NULL;
}

Но я не могу понять, куда надо вставить её в программу, чтобы в случае пустого списка выводилась информация о том, что список пуст. Код программы (функции в другом файле объявлены через заголовочный файл):
Основной файл:
#include "Header.h"
#include <Windows.h>
#include <ctime>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

int main() {
	setlocale(LC_ALL, "Russian");
	Tlist head;
	createByOrder(head, "Text.txt");
	cout << "\n";
	printList(head);
	cout << "\n";
	int summ=sum(head);
	cout << "\n";
	at_the_end(head, summ);
	printList(head);
	cin.get();
	cin.get();
	return 0;
}

Файл с функциями:
#include "Header.h"
#include <cmath>
#include <iostream>
#include <fstream>

using namespace std;

void initList(Tlist &list) {  //инициализация списка
	list = NULL;
}

bool isEmpty(Tlist list) { //проверка списка на пустоту
	return list == NULL;
}

void addToHead(Tlist &list, int element) { //добавление элемента в начало списка
	Tlist p = new Node;
	p->data = element;
	p->next = list;
	list = p;
}

void addAfterNode(Tlist pNode, int element) { //добавление элемента в список после заданного
	Tlist p = new Node;
	p->data = element;
	p->next = pNode->next;
	pNode->next = p;
}

Tlist findPlace(Tlist list, int elem) { //создание упорядоченного списка
	Tlist current = list;
	while ((current->next != 0) && (current->next->data < elem))
		current = current->next;
	return current;
}

void createByOrder(Tlist &list, string text) {
	initList(list);
	ifstream ifin(text);
	int elem;
	while (!ifin.eof()) {
		ifin >> elem;
		cout << elem << endl;
		if (isEmpty(list) || (list->data > elem))
			addToHead(list, elem);
		else {
			Tlist place = findPlace(list, elem);
			addAfterNode(place, elem);
		}
	}

}

int sum(Tlist list) {
	int S = 0;
	Tlist current = list;
	while (current != NULL) {
		S += current->data;
		current = current->next;
	}
	return S;
}

void at_the_end(Tlist &list, int elem) {
	Tlist current = list;
	while (current->next != NULL) {
		current = current->next;
	}
	addAfterNode(current, elem);
}

void printList(Tlist list) { //печать списка
	Tlist current = list;
	while (current != NULL) {
		cout << current->data << " ";
		current = current->next;
	}
}

Всё сразу: https://yadi.sk/d/fINfylgH5PG6mg
  • Вопрос задан
  • 2100 просмотров
Решения вопроса 1
@andrey_levushkin Автор вопроса
Проблема решена проверкой файла на пустоту до заполнения списка:
if (file.peek() == EOF)
	{
		cout << "\nНет данных для подсчёта";
		cin.get();
		cin.get();
		return;
	} else
	{
действия
	}
}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы