Лаб2
.docx
МИНИСТЕРСТВО ОБРАЗОВАНИЯ И НАУКИ УКРАИНЫ
ОДЕССКИЙ НАЦИОНАЛЬНЫЙ ПОЛИТЕХНИЧЕСКИЙ УНИВЕРСИТЕТ
Институт компьютерных систем
Кафедра информационных систем
Лабораторная работа №2
По предмету: «Теория алгоритмов»
Тема: «Структуры данных. Стек. Очередь. Дек. Сортировка»
Выполнил:
Студент группы АИ-171
Анищенко Н.А
Проверил:
Шибаева Н.О.
Одесса 2018
Цель работы: Приобретение практических навыков программирования с использованием: стеков, очередей и деков.
Вариант №1
-
Создать список из заданного количества элементов. Выполнить циклический сдвиг этого списка на N элементов вправо или влево.
-
Определить, сколько элементов одномерного массива А(9) больше, чем число 3.
Задание 1
#include<iostream>
using namespace std;
class ListNode{ public: int value; ListNode *next; ListNode *prev;
ListNode(int v, ListNode *n, ListNode *p){ this->value = v; this->next = n; this->prev = p; } };
class List{ public: int length; ListNode *head; ListNode *tail;
List(){ this->length = 0; }
void pushFront(int val){ ListNode *tmp = new ListNode(val, this->head, NULL); if(this->length > 0) this->head->prev = tmp; this->head = tmp; if(this->length == 0) this->tail = head; this->length++; return; }
void pushBack(int val){ ListNode *tmp = new ListNode(val, NULL, this->tail); if(this->length > 0) this->tail->next = tmp; this->tail = tmp; if(this->length == 0) this->head = tail; this->length++; return; }
int popBack(){ ListNode *tmp = this->tail; this->tail = tmp->prev; this->length--; return tmp->value; }
int popFront(){ ListNode *tmp = this->head; this->head = tmp->next; this->length--; return tmp->value; }
|
void print(){ ListNode *tmp = this->head; for(int i = 0; i < this->length; i++){ cout << tmp->value << " "; tmp = tmp->next; } cout << endl; return; }
void shift(int N){ N = N % length;
if(N <= this->length/2){ for(int i = 0; i < N; i++) this->pushFront(this->popBack()); } else if(N > this->length/2){ for(int i = 0; i < this->length - N; i++) this->pushBack(this->popFront()); } return; }
};
int main(int argc, char const *argv[]) {
List *arr = new List(); cout << "Enter list length: "; int L = 0; cin >> L; cout << "Enter list elements: " << endl; for(int i = 0; i < L; i++){ int t = 0; cin >> t; arr->pushBack(t); } cout << endl;
cout << "List: "; arr->print();
cout << "Enter right shift length: "; int N = 0; cin >> N; arr->shift(N); cout << endl << "Result: "; arr->print(); return 0; }
|
Пример выполнения
Задание 2
#include<iostream>
int countHigerThen3(int a[9]){
int counter = 0;
for(int i = 0; i < 9; i++){
if(a[i] > 3)
counter++;
}
return counter;
}
int main(int argc, char const *argv[]) {
int arr[9];
std::cout << "Enter arrey elements: " << std::endl;
for(int i = 0; i < 9; i++){
std::cin >> arr[i];
}
std::cout << std::endl;
std::cout << countHigerThen3(arr) << " elements higer then 3" << std::endl;
return 0;
}
Пример выполнения
Выводы
В ходе лабораторной работы были созданы две программы: первая – осуществляет циклический сдвиг вправо односвязного списка; вторая – определяет кол-во элементов соответствующих заданному условию в массиве.