Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
лабораторний практикум інф сис та стр даних.doc
Скачиваний:
1
Добавлен:
01.07.2025
Размер:
2 Мб
Скачать
☆
  1. Результати виконання програми

Додаток

//main.cpp

#include"List.h"

#include<iostream>

#include<stdio.h>

void main()

{

setlocale(0,"");

int

new_element,

count;

List<int> Sp;

//Занесення даних в список з клавіатури

cout <<"Скiльки елементiв хочете ввести?.. ";

cin >> count;

for(int i = count; i > 0; i--){

cout <<"Введiть елемент ... ";

cin >> new_element;

if (new_element == 0)

{

cout <<"Елемент рiвний 0"<<endl;

Sp.push(new_element);

}

else

if (new_element > 0)

Sp.push(new_element);

else

Sp.pop(new_element);

Sp.print();

}

//Виведення видалених елементів

Sp.Vudal();

}

//List.cpp

#include<iostream>

#include<stdio.h>

usingnamespace std;

int a[10], i=0;

template<class item>class List

{

private:

//Вузол елемента списку

struct node

{

item data;

struct node *next;

};

node *end; //кінець списку

node *head; //початок списку

public:

List()//Конструктор

{

//Список не визначений

head = 0;

end = 0;

}

//Деструктор

~List()

{

node *tmp = head; //Видалення всіх елементів списку

while(head)

{

tmp = head;

head = head->next;

delete tmp;

}

}

//Виведення списку на екран

void print()

{

node *tmp = head;

//якщо список не поржній

cout <<"Список: ";

while(tmp)

{

cout << tmp->data;

if (tmp->next)

cout <<", ";

tmp = tmp->next;

}

//якщо список порожній

if (!head)

cout <<"Список порожнiй";

cout << endl;

}

//Додати елемент в кінець списку

void push_back(item it)

{

node *tmp;

if (!head)

{

end = head = tmp = new node;

end->data = it;

end->next = NULL;

}

else

{

end->next = tmp = new node;

end = end->next;

end->data = it;

end->next = NULL;

}

}

//Додати елемент на початок списку

void push_front(item it)

{

node *tmp = new node;

tmp->next = head;

tmp->data = it;

head = tmp;

}

//Видалити елемент з початку

void pop_front()

{

node *tmp;

if (head)

{

tmp = head->next;

delete head;

head = tmp;

}

}

//Видалити елемент з кінця

void pop_back()

{

if (head)

{

node *tmp = head;

while (tmp->next != end)

tmp = tmp->next;

delete end;

end = tmp;

end->next = NULL;

}

else

{

delete end;

end = 0;

head = 0;

}

}

//Занесення в список даної послідовності

bool push(item it)

{

node *tmp = head;

node *temp = new node;

temp->data = it;

node *prev = head;

if(!head)

{

head = temp;

temp->next = NULL;

return 0;

}

else

{

while(tmp->data < it)

{

if(tmp->next == NULL)

{

tmp->next = temp;

temp->next = NULL;

return 0;

}

if(tmp != head)

prev = prev->next;

tmp = tmp->next;

}

if(tmp == head)

{

temp->next = head;

head = temp;

return 0;

}

prev->next = temp;

temp->next = tmp;

return 0;

}

}

//Видалити всі елементи значення яких рівне it

bool pop(item it)

{

it = -it;

int k = 1;

node *tmp = head;

node *temp = head;

if(head == NULL)

return 1;

while(tmp)

{

if(tmp->data == it)

{

if(tmp == head)

{

a[i] = tmp->data;

i++;

head = head->next;

delete tmp;

k = 0;

tmp = head;

temp = head;

continue;

};

temp->next = tmp->next;

a[i] = tmp->data;

i++;

delete tmp;

k = 0;

tmp = temp->next;

}

else

{

if(tmp != head)

temp = temp->next;

tmp = tmp->next;

}

}

return 0;

}

//Необхідний список

void Vudal()

{

cout <<"Видаленi елементи: "<<endl;

for(int j = 0; j<i; j++)

cout << a[j] <<endl;

}

};