A
A
andrey_levushkin2018-12-16 20:12:45
C++ / C#
andrey_levushkin, 2018-12-16 20:12:45

How can I check if an existing list is empty?

The program has a list as input, which is filled from the Text.txt file (which is in the program resources) and displays all the elements from the list and their sum. An example for a file with the content "1 2 3 4 5 6": 1 2 3 4 5 6 21
Where "21" is the sum of all numbers from the file (list). There is also a function to check the list for emptiness:

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

But I can't figure out where to insert it into the program so that in the case of an empty list, information is displayed that the list is empty. Program code (functions in another file are declared via header file):
Main file:
#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;
}

Function file:
#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;
  }
}

All at once: https://yadi.sk/d/fINfylgH5PG6mg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
andrey_levushkin, 2018-12-16
@andrey_levushkin

The problem is solved by checking the file for emptiness before the list is filled:

if (file.peek() == EOF)
  {
    cout << "\nНет данных для подсчёта";
    cin.get();
    cin.get();
    return;
  } else
  {
действия
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question