Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Смирнова_2370_ЛР4

.docx
Скачиваний:
0
Добавлен:
05.12.2024
Размер:
21.98 Кб
Скачать

Дисциплина: «Программирование»

Отчет по лабораторной работе №4

Выполнила: Смирнова М. В.

Группа: №2370

Цели работы:

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

- название города;

- название региона;

- количество жителей.

Над списком требуется произвести следующие операции:

а) построение и заполнение узлов списка данными с клавиатуры;

б) вывести на экран названия регионов в порядке убывания суммарной численности городского населения;

в) удалить узлы, хранящие информацию о городах указанного (с клавиатуры) региона;

г) очистить список.

Код программы:

#include <iostream>

#include <string>

#include <windows.h>

using namespace std;

struct CityNode {

string city_name; // Название города

string region_name; // Название региона

int population; // Количество жителей

CityNode* prev; // Указатель на предыдущий узел списка

CityNode* next; // Указатель на следующий узел списка

};

void addNode(CityNode*& head, CityNode*& tail, string city_name, string region_name, int population) {

CityNode* newNode = new CityNode;

newNode->city_name = city_name;

newNode->region_name = region_name;

newNode->population = population;

newNode->prev = NULL;

newNode->next = NULL;

if (head == NULL) {

head = newNode;

tail = newNode;

}

else {

tail->next = newNode;

newNode->prev = tail;

tail = newNode;

}

}

void deleteNodesByRegion(CityNode*& head, CityNode*& tail, string region_name) {

CityNode* current = head;

while (current != NULL) {

if (current->region_name == region_name) {

if (current == head) {

head = current->next;

if (head != NULL) {

head->prev = NULL;

}

else {

tail = NULL;

}

}

else if (current == tail) {

tail = current->prev;

if (tail != NULL) {

tail->next = NULL;

}

else {

head = NULL;

}

}

else {

current->prev->next = current->next;

current->next->prev = current->prev;

}

CityNode* temp = current;

current = current->next;

delete temp;

}

else {

current = current->next;

}

}

}

void printList(CityNode* head) {

CityNode* current = head;

while (current != NULL) {

cout << "Город: " << current->city_name << endl;

cout << "Регион: " << current->region_name << endl;

cout << "Население: " << current->population << endl;

cout << endl;

current = current->next;

}

}

void deleteList(CityNode** head) {

CityNode* current = *head;

CityNode* next = NULL;

while (current != NULL) {

next = current->next;

delete current;

current = next;

}

*head = NULL;

}

int main() {

SetConsoleCP(1251);

SetConsoleOutputCP(1251);

CityNode* head = NULL;

CityNode* tail = NULL;

int n;

cout << "Введите количество городов: ";

cin >> n;

for (int i = 0; i < n; i++) {

string city_name, region_name;

int population;

cout << "Введите название города: ";

cin >> city_name;

cout << "Введите название региона: ";

cin >> region_name;

cout << "Введите количество жителей: ";

cin >> population;

addNode(head, tail, city_name, region_name, population);

}

// Вывод списка на экран

cout << "Список городов:" << endl;

printList(head);

// Удаление узлов, хранящих информацию о городах указанного региона

string region_to_delete;

cout << "Введите название региона, информацию о городах которого нужно удалить: ";

cin >> region_to_delete;

deleteNodesByRegion(head, tail, region_to_delete);

// Вывод списка на экран после удаления узлов

cout << "Список городов после удаления узлов:" << endl;

printList(head);

// Вывод названий регионов в порядке убывания суммарной численности населения

cout << "Названия регионов в порядке убывания суммарной численности населения:" << endl;

string last_region = "";

int total_population = 0;

CityNode* current = head;

while (current != NULL) {

total_population += current->population;

current = current->next;

}

while (total_population > 0) {

int max_population = 0;

string max_region = "";

current = head;

while (current != NULL) {

int region_population = 0;

CityNode* region_current = current;

while (region_current != NULL && region_current->region_name == current->region_name) {

region_population += region_current->population;

region_current = region_current->next;

}

if (region_population >= max_population) {

max_population = region_population;

max_region = current->region_name;

}

current = region_current;

}

if (last_region == max_region) {

break;

}

current = head;

CityNode* prev = NULL;

while (current != NULL) {

if (current->region_name == max_region) {

if (prev != NULL) {

prev->next = current->next;

}

else {

head = current->next;

}

total_population -= current->population;

delete current;

current = NULL;

}

else {

prev = current;

current = current->next;

}

}

cout << max_region << endl;

last_region = max_region;;

}

deleteList(&head);

}

//Кисловодск Ставропольский 300 Мурино Ленинградская 2000 Павловск Ленинградская 400 Ессентуки Ставропольский 500 Грусть Печальная 100