Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Лабораторная работа №5.
- /*L-кольцевой двунаправленный список с загл. звеном
- Написать функцию, которая выводит равные соседние элементы
- 2 курс, 62 группа, Петрова Анастасия.
- */
- #include "pch.h"
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct TList
- {
- int value;
- TList* next;
- TList* prev;
- };
- bool isEmpty(TList* lst)
- {
- return lst == NULL;
- }
- int addElem(TList*& lst, int newValue)
- {
- if (!isEmpty(lst))
- {
- TList* temp = new TList;
- temp->value = newValue;
- TList* lastElem = lst->prev;
- lastElem->next = temp;
- temp->prev = lastElem;
- lst->prev = temp;
- temp->next = lst;
- return 0;
- }
- else
- {
- TList* temp = new TList;
- temp->value = newValue;
- temp->next = temp;
- temp->prev = temp;
- lst = temp;
- return 0;
- }
- }
- void printList(TList* lst)
- {
- TList* tmp = lst;
- do {
- cout << tmp->value << " ";
- tmp = tmp->next;
- } while (tmp != lst);
- }
- bool Task(TList* lst)
- {
- TList* temp = lst;
- do
- {
- if (temp->value == temp->next->value)
- return true;
- temp = temp->next;
- }
- while (temp != lst);
- return 0;
- }
- int clearList(TList*& lst)
- {
- TList* tmp = lst->next;
- TList* deletedItem;
- do {
- deletedItem = tmp;
- tmp = tmp->next;
- delete deletedItem;
- } while (tmp != lst);
- delete lst;
- lst = NULL;
- return 0;
- }
- int main()
- {
- setlocale(0, "Russian");
- TList* List = NULL;
- int t;
- ifstream inputFile("input.txt");
- if (inputFile)
- {
- while (!inputFile.eof())
- {
- inputFile >> t;
- addElem(List, t);
- }
- printList(List);
- cout << endl;
- if (Task(List))
- cout << "Равные соседние элементы в списке есть";
- else cout << "Равных соседних элементов в списке нет";
- clearList(List);
- return 0;
- }
- else
- {
- cout << "Не удалось открыть файл для чтения" << endl;
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement