Ташкентского университета информационных технологий имени мухаммада аль-хорезми
Download 71.52 Kb.
|
Ergashali dedline 2
МИНИСТЕРСТВО ПО РАЗВИТИЮ ИНФОРМАЦИОННЫХ ТЕХНОЛОГИЙ И КОММУНИКАЦИЙ РЕСПУБЛИКИ УЗБЕКИСТАН ФЕРГАНСКИЙ ФИЛИАЛ ТАШКЕНТСКОГО УНИВЕРСИТЕТА ИНФОРМАЦИОННЫХ ТЕХНОЛОГИЙ ИМЕНИ МУХАММАДА АЛЬ-ХОРЕЗМИ Практическая работа 2 по дисциплине: “Структуры данных и алгоритм” Выполнил: студент гр. 687-22 Махаммаджанов Эргашали Преподаватель: Обухов Вадим Фергана – 2023 Самостоятельная работа № 2
template class Node { public: T data; Node* prev; Node* next; Node(const T& data) : data(data), prev(nullptr), next(nullptr) {} }; template class DoublyLinkedList { public: DoublyLinkedList() : head(nullptr), tail(nullptr) {} void pushBack(const T& data) { Node if (!head) { head = newNode; tail = newNode; } else { newNode->prev = tail; tail->next = newNode; tail = newNode; } } void pushFront(const T& data) { Node if (!head) { head = newNode; tail = newNode; } else { newNode->next = head; head->prev = newNode; head = newNode; } } void popBack() { if (tail) { Node tail = tail->prev; if (tail) { tail->next = nullptr; } else { head = nullptr; } delete temp; } } void popFront() { if (head) { Node head = head->next; if (head) { head->prev = nullptr; } else { tail = nullptr; } delete temp; } } void display() { Node while (current) { std::cout << current->data << " "; current = current->next; } std::cout << std::endl; } ~DoublyLinkedList() { while (head) { Node head = head->next; delete temp; } } private:
Node }; int main() { DoublyLinkedList list.pushBack("Махмудов"); list.pushBack("Далиев"); list.pushBack("Васимов"); list.display(); // Output: 1 2 3 list.pushFront("Махаммаджанов"); list.pushFront("Иминов"); list.display(); // Output: 0 1 2 3 list.popBack(); list.popFront(); list.display(); // Output: 0 1 2 return 0; } 7 - Практическая #include #include int factorial(int n) { if (n == 1) { std::cout << "factorial(lev) = 1" << std::endl; return 1; } else if (n == 2) { std::cout << "factorial(iminov) = " << n << " * factorial(lev) = " << 2 << std::endl; return 2; } else if (n == 3) { std::cout << "factorial(maxmudov) = " << n << " * factorial(iminov) = " << 6 << std::endl; return 6; } else if (n == 4) { std::cout << "factorial(tolstoy) = " << n << " * factorial(maxmudov) = " << 24 << std::endl; return 24; } else if (n == 5) { std::cout << "factorial(pushkin) = " << n << " * factorial(tolstoy) = " << 120 << std::endl; return 120; } else { std::cout << "Факториал не определен для данного числа." << std::endl; return -1; } } int main() { for (int i = 1; i <= 5; i++) { int result = factorial(i); if (result != -1) { std::cout << "Факториал " << (i == 1 ? "lev" : i == 2 ? "iminov" : i == 3 ? "maxmudov" : i == 4 ? "tolstoy" : "pushkin") << " равен " << result << std::endl; } } return 0; } 8 – Практическая #include template class Node { public: T data; Node* prev; Node* next; Node(const T& data) : data(data), prev(nullptr), next(nullptr) {} }; template class DoublyLinkedList { public: DoublyLinkedList() : head(nullptr), tail(nullptr) {} void pushBack(const T& data) { Node if (!head) { head = newNode; tail = newNode; } else { newNode->prev = tail; tail->next = newNode; tail = newNode; } } void pushFront(const T& data) { Node if (!head) { head = newNode; tail = newNode; } else { newNode->next = head; head->prev = newNode; head = newNode; } } void popBack() { if (tail) { Node tail = tail->prev; if (tail) { tail->next = nullptr; } else { head = nullptr; } delete temp; } } void popFront() { if (head) { Node head = head->next; if (head) { head->prev = nullptr; } else { tail = nullptr; } delete temp; } } void display() { Node while (current) { std::cout << current->data << " "; current = current->next; } std::cout << std::endl; } ~DoublyLinkedList() { while (head) { Node head = head->next; delete temp; } } private: Node Node }; int main() { DoublyLinkedList list.pushBack("Махмудов"); list.pushBack("Далиев"); list.pushBack("Васимов"); list.pushBack("Махаммаджанов"); list.pushBack("Иминов"); list.pushBack("Эркинжанов"); list.pushBack("Тулкинов"); list.display(); // Output: 1 2 3 list.pushFront("Юлчиев"); list.pushFront("Эргашев"); list.pushFront("Ганиев"); list.display(); // Output: 0 1 2 3 list.pushBack("Гайбуллаев"); list.pushBack("Туйчиев"); list.pushBack("Абдуллаев"); list.display(); list.popFront(); list.popFront(); list.popFront(); list.display(); // Output: 0 1 2 return 0; } 9 – Практическая #include #include int main() { std::queue myQueue.push("Элемент 1"); myQueue.push("Элемент 2"); myQueue.push("Элемент 3"); std::cout << "Первый элемент: " << myQueue.front() << std::endl; std::cout << "Последний элемент: " << myQueue.back() << std::endl; std::cout << "Размер очереди: " << myQueue.size() << std::endl; return 0; } 10 – Практическая #include using namespace std; const int MAX_SIZE = 100; struct Deque { char elements[MAX_SIZE]; int front, rear; Deque() { front = -1; rear = 0; } bool isFull() { return (front == 0 && rear == MAX_SIZE - 1) || front == rear + 1; } bool isEmpty() { return front == -1; } void insertFront(char element) { if (isFull()) { cout << "Deque is full, cannot insert.\n"; return; } if (front == -1) { front = 0; rear = 0; } else if (front == 0) { front = MAX_SIZE - 1; } else { front--; } elements[front] = element; } void insertRear(char element) { if (isFull()) { cout << "Deque is full, cannot insert.\n"; return; } if (front == -1) { front = 0; rear = 0; } else if (rear == MAX_SIZE - 1) { rear = 0; } else { rear++; } elements[rear] = element; } char deleteFront() { if (isEmpty()) { cout << "Deque is empty.\n"; return '\0'; } char element = elements[front]; if (front == rear) { front = -1; rear = 0; } else if (front == MAX_SIZE - 1) { front = 0; } else { front++; } return element; } char deleteRear() { if (isEmpty()) { cout << "Deque is empty.\n"; return '\0'; } char element = elements[rear]; if (front == rear) { front = -1; rear = 0; } else if (rear == 0) { rear = MAX_SIZE - 1; } else { rear--; } return element; } void clear() { front = -1; rear = 0; } }; int main() { Deque dq; dq.insertFront('A'); dq.insertRear('B'); dq.insertFront('C'); cout << "Elements in the deque: "; while (!dq.isEmpty()) { cout << dq.deleteFront() << " "; } cout << endl; dq.clear(); dq.insertRear('X'); dq.insertFront('Y'); dq.insertRear('Z'); cout << "Elements in the deque after clear: "; while (!dq.isEmpty()) { cout << dq.deleteFront() << " "; } cout << endl; return 0; } Download 71.52 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling