Частина тексту файла (без зображень, графіків і формул):
ТР-15 Фундамент Даніїл
Варіант-15
Практичне заняття (08.06)
Тема: “Черга”
Завдання:
Розробити програму для опису “черги” та реалізувати різні функції:
– додавання нових елементів у структуру; – видалення елементу із заданим номером зі структури;
– виведення на екран інформації, яка зберігається у заданому елементі;
– виведення на екран усієї інформації зі структури.
2. Створіть проект в інтегрованому середовищі розробки ...... (pract_№_призвище.cpp або pract_№_призвище.java).
3. Запустіть програму на виконання, проаналізуйте результати і переконайтеся в правильності рішення задачі.
Результат виконання:
//
Посилання на Replit: https://replit.com/join/yjbpvpwucj-tr-15fundamient
Копія коду:
#include <iostream>
using namespace std;
struct Node {
int data;
Node* pNext;
Node(int value, Node* next = NULL) {
data = value;
pNext = next;
}
};
class Queue {
public:
Queue();
void deleteElement(int index);
void push(int data);
void pop();
void print();
void printElement(int index);
private:
Node* front;
Node* tail;
int size = 0;
};
Queue::Queue() {
size = 0;
front = NULL;
}
void Queue::pop() {
Node *temp = front;
front = front->pNext;
delete temp;
size--;
}
void Queue::print() {
Node *temp = front;
for (int i = 0; i < size; i++) {
cout << temp->data << " ";
temp = temp->pNext;
}
cout << endl;
}
void Queue::push(int data) {
front = new Node(data, front);
size++;
}
void Queue::deleteElement(int index) {
if(index < 0 || index > size) return;
if (index == 0) {
pop();
return;
}
Node *previous = this->front;
for (int i = 0; i < index - 1; ++i) {
previous = previous->pNext;
}
Node *ToDelete = previous->pNext;
previous->pNext = ToDelete->pNext;
delete ToDelete;
size--;
}
void Queue::printElement(int index) {
if(index < 0 || index > size) return;
Node *temp = front;
for (int i = 0; i < index; ++i) {
temp = temp->pNext;
}
cout << "At index(" << index << ") locate this value: " << temp->data << endl;
}
int main() {
srand(time(NULL));
Queue queue;
int size;
cout << "\nPlease input queue size: ";
cin >> size;
for (int i = 0; i < size; ++i) {
queue.push(rand() % 10);
}
int choice = 4;
while(choice != 0) {
cout << "\n1. Add some elements random to queue\n2. Remove element\n3. Show one element\n4. Show queue\n0. Exit\n";
cin >> choice;
switch(choice) {
case 1: {
int queueSize;
cout << "\nPlease enter number of elements what you want to add to queue: ";
cin >> queueSize;
for (int i = 0; i < queueSize; ++i) {
queue.push(rand() % 10);
}
break;
}
case 2: {
int value;
cout << "Please enter element index what you want to delete: ";
cin >> value;
queue.deleteElement(value);
break;
}
case 3: {
int value;
cout << "Input index for show: ";
cin >> value;
queue.printElement(value);
break;
}
case 4: {
cout << endl << "Queue: ";
queue.print();
break;
}
default: {
cout << "Wrong number!";
}
}
}
return 0;
}