Report 3
Report 3
Code:
#include <iostream>
struct Node {
int data;
Node* next;
};
class SinglyLinkedList {
private:
Node* head;
public:
// Constructor
SinglyLinkedList() : head(nullptr) {}
~SinglyLinkedList() {
delete current;
current = nextNode;
head = nullptr;
newNode->next = head;
head = newNode;
if (head == nullptr) {
head = newNode;
return;
current = current->next;
current->next = newNode;
void deleteFirst() {
if (head == nullptr) {
std::cout << "List is empty. Cannot delete first node." << std::endl;
return;
head = head->next;
delete temp;
void deleteLast() {
if (head == nullptr) {
std::cout << "List is empty. Cannot delete last node." << std::endl;
return;
delete head;
head = nullptr;
return;
current = current->next;
delete current->next;
current->next = nullptr;
if (head == nullptr) {
return;
}
if (current->next != nullptr) {
current = current->next;
int count = 0;
count++;
current = current->next;
return count;
return true;
current = current->next;
return false;
if (pos < 0) {
int currentPos = 0;
current = current->next;
currentPos++;
if (current == nullptr) {
return current->data;
return;
if (pos == 0) {
deleteFirst();
return;
if (pos < 0) {
return;
int currentPos = 0;
prev = current;
current = current->next;
currentPos++;
if (current == nullptr) {
std::cout << "Position " << pos << " is out of bounds. Cannot
delete." << std::endl;
return;
}
prev->next = current->next;
delete current;
if (pos < 0) {
return;
if (pos == 0) {
addAtBeginning(val);
return;
int currentPos = 0;
current = current->next;
currentPos++;
}
if (current == nullptr) {
std::cout << "Position " << pos << " is out of bounds. Cannot
insert." << std::endl;
return;
newNode->next = current->next;
current->next = newNode;
if (otherList.head == nullptr) {
if (this->head == nullptr) {
this->head = otherList.head;
} else {
current = current->next;
}
current->next = otherList.head;
// k) Merge two sorted singly linked lists p1 and p2 into a sorted list.
// This is a static method as it creates a new list from two existing ones.
SinglyLinkedList mergedList;
if (head1 == nullptr) {
mergedList.head = head2;
return mergedList;
if (head2 == nullptr) {
mergedList.head = head1;
return mergedList;
}
if (head1->data <= head2->data) {
mergedList.head = head1;
currentMerged = head1;
head1 = head1->next;
} else {
mergedList.head = head2;
currentMerged = head2;
head2 = head2->next;
currentMerged->next = head1;
head1 = head1->next;
} else {
currentMerged->next = head2;
head2 = head2->next;
currentMerged = currentMerged->next;
if (head1 != nullptr) {
currentMerged->next = head1;
currentMerged->next = head2;
}
list1.head = nullptr;
list2.head = nullptr;
return mergedList;
};
int main() {
std::cout << "--- Problem 1: Singly Linked List Management ---" <<
std::endl;
p.addAtEnd(10);
p.addAtBeginning(5);
p.addAtEnd(20);
p.addAtBeginning(2);
p.printList();
std::cout << "\n2. Count nodes in p: " << p.countNodes() << std::endl;
std::cout << "\n3. Search for value 10 in p: " << (p.search(10) ? "Found" :
"Not Found") << std::endl;
std::cout << "Search for value 15 in p: " << (p.search(15) ? "Found" : "Not
Found") << std::endl;
try {
std::cout << "\n5. Delete first node from p:" << std::endl;
p.deleteFirst();
p.printList();
std::cout << "\n6. Delete last node from p:" << std::endl;
p.deleteLast();
p.printList();
p.printList();
p.insertAtPosition(1, 0);
p.printList();
p.insertAtPosition(100, 5);
p.printList();
p.deleteAtPosition(1);
p.printList();
p.deleteAtPosition(0);
p.printList();
p.deleteAtPosition(p.countNodes() - 1);
p.printList();
p1.addAtEnd(1);
p1.addAtEnd(2);
p1.addAtEnd(3);
p2.addAtEnd(5);
p1.appendList(p2);
std::cout << "\n10. Merge two sorted lists (function k):" << std::endl;
sorted1.addAtEnd(1);
sorted1.addAtEnd(3);
sorted1.addAtEnd(5);
sorted2.addAtEnd(2);
sorted2.addAtEnd(4);
sorted2.addAtEnd(6);
SinglyLinkedList mergedResult =
SinglyLinkedList::mergeSortedLists(sorted1, sorted2);
return 0;
Result:
Report:
Đánh giá biểu thức: Sử dụng cấu trúc dữ liệu Stack (ngăn xếp) để
chuyển đổi biểu thức trung tố (infix) sang hậu tố (postfix) và sau đó tính toán
giá trị của biểu thức hậu tố. Stack được cài đặt bằng Danh sách liên kết để
quản lý linh hoạt các toán tử và toán hạng.
Kiểm tra dấu ngoặc đơn: Sử dụng Stack để kiểm tra tính hợp lệ của các
cặp dấu ngoặc đơn (), {}, [] trong một biểu thức. Thuật toán đảm bảo mọi
dấu ngoặc mở đều có dấu ngoặc đóng tương ứng và đúng thứ tự.
Bài toán Josephus: Giải quyết bài toán bằng cách sử dụng cấu trúc dữ
liệu Queue (hàng đợi) để mô phỏng quá trình loại bỏ người trong một vòng
tròn, tìm ra người sống sót cuối cùng.
Code:
#include <iostream>
#include <vector>
struct SetNode {
int data;
SetNode* next;
};
class IntegerSet {
private:
SetNode* head;
// Private helper to insert in sorted order (and maintain uniqueness)
newNode->next = head;
head = newNode;
return;
return;
prev = current;
current = current->next;
newNode->next = current;
prev->next = newNode;
}
public:
// Constructor
IntegerSet() : head(nullptr) {}
std::sort(sorted_elements.begin(), sorted_elements.end());
// Destructor
~IntegerSet() {
delete current;
current = nextNode;
head = nullptr;
}
// a) isSet(): checks if s is a set or not.
if (head == nullptr) {
return true;
return false;
current = current->next;
return true;
found = true;
break;
currentOther = currentOther->next;
if (!found) {
currentThis = currentThis->next;
IntegerSet result;
if (currentS1 == nullptr) {
result.addElement(currentS2->data);
currentS2 = currentS2->next;
result.addElement(currentS1->data);
currentS1 = currentS1->next;
} else if (currentS1->data < currentS2->data) {
result.addElement(currentS1->data);
currentS1 = currentS1->next;
result.addElement(currentS2->data);
currentS2 = currentS2->next;
currentS1 = currentS1->next;
currentS2 = currentS2->next;
return result;
IntegerSet result;
currentS1 = currentS1->next;
currentS2 = currentS2->next;
} else { // Elements are equal, add to intersection
result.addElement(currentS1->data);
currentS1 = currentS1->next;
currentS2 = currentS2->next;
return result;
if (head == nullptr) {
return;
if (current->next != nullptr) {
current = current->next;
}
// Helper to add an element (maintains set properties)
insertSortedUnique(val);
if (head == nullptr) {
return;
if (head->data == val) {
head = head->next;
delete temp;
return;
prev = current;
current = current->next;
prev->next = current->next;
delete current;
if (current->data == val) {
return true;
current = current->next;
return false;
};
int main() {
std::cout << "--- Problem 2: Integer Set Management ---" << std::endl;
std::cout << "s5 (from {1,5,3}): "; s5.printSet(); std::cout << std::endl;
std::cout << "s1 is a set? " << (s1.isSet() ? "Yes" : "No") << std::endl;
std::cout << "s5 is a set? " << (s5.isSet() ? "Yes" : "No") << std::endl;
std::cout << "s4 is subset of s1? " << (s4.isSubset(s1) ? "Yes" : "No") <<
std::endl; // Expected: Yes
std::cout << "s1 is subset of s4? " << (s1.isSubset(s4) ? "Yes" : "No") <<
std::endl; // Expected: No
std::cout << "s2 is subset of s3? " << (s2.isSubset(s3) ? "Yes" : "No") <<
std::endl; // Expected: No
// Test add/remove/contains
IntegerSet mySet;
mySet.addElement(10);
mySet.addElement(20);
mySet.addElement(5);
std::cout << "mySet contains 10? " << (mySet.contains(10) ? "Yes" : "No")
<< std::endl;
std::cout << "mySet contains 15? " << (mySet.contains(15) ? "Yes" : "No")
<< std::endl;
mySet.removeElement(10);
std::cout << "mySet after removing 10: "; mySet.printSet(); std::cout <<
std::endl;
return 0;
}
Report:
Quản lý Danh sách Liên kết Đơn: Triển khai một lớp Danh sách Liên
kết Đơn (SinglyLinkedList) với các thao tác cơ bản và nâng cao. Các chức
năng chính bao gồm thêm/xóa node ở đầu, cuối và tại vị trí bất kỳ; đếm số
node; tìm kiếm giá trị hoặc theo vị trí; nối hai danh sách; và hợp nhất hai
danh sách đã sắp xếp.
Triển khai Tập hợp số nguyên (Set): Xây dựng một lớp Tập hợp số
nguyên (IntegerSet) sử dụng danh sách liên kết đơn được sắp xếp. Tập hợp
này tự động đảm bảo tính duy nhất của các phần tử. Các thao tác chính bao
gồm kiểm tra tập hợp con, hợp (union) và giao (intersection) của hai tập hợp.