السلام عليكم.
تمام، سأستخدم أساليب أساسية وأقل تعقيدًا:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Node structure
struct Node {
string s;
string language;
Node* next;
Node* prev;
Node(string str, string lang) : s(str), language(lang), next(nullptr), prev(nullptr) {}
};
// DoublyLinkedList class
class DoublyLinkedList {
private:
Node* head;
Node* tail;
public:
DoublyLinkedList() : head(nullptr), tail(nullptr) {}
// Add a word to the front of the list
void AddFront(string str, string lang);
// Add a word to the end of the list
void AddBack(string str, string lang);
// Add a word in alphabetical order
void AddInOrder(string str, string lang);
// Remove the front node
void RemoveFront();
// Remove the back node
void RemoveBack();
// Count words of a specific language
int CountLanguage(string lang);
// Search for a word
int Search(string str);
// Remove a specific word
void RemoveWord(string str);
// Overload the equality operator to compare lists
bool operator==(const DoublyLinkedList& other);
// Print the list for debugging
void Print();
// Load data from file
void LoadFromFile(const string& filename);
};
// Add a word to the front of the list
void DoublyLinkedList::AddFront(string str, string lang) {
Node* newNode = new Node(str, lang);
if (!head) {
head = tail = newNode;
} else {
newNode->next = head;
head->prev = newNode;
head = newNode;
}
}
// Add a word to the end of the list
void DoublyLinkedList::AddBack(string str, string lang) {
Node* newNode = new Node(str, lang);
if (!tail) {
head = tail = newNode;
} else {
newNode->prev = tail;
tail->next = newNode;
tail = newNode;
}
}
// Add a word in alphabetical order
void DoublyLinkedList::AddInOrder(string str, string lang) {
Node* newNode = new Node(str, lang);
if (!head || head->s >= str) {
AddFront(str, lang);
} else if (tail->s <= str) {
AddBack(str, lang);
} else {
Node* current = head;
while (current->next && current->next->s < str) {
current = current->next;
}
newNode->next = current->next;
newNode->prev = current;
if (current->next) {
current->next->prev = newNode;
}
current->next = newNode;
}
}
// Remove the front node
void DoublyLinkedList::RemoveFront() {
if (!head) return;
Node* temp = head;
if (head == tail) {
head = tail = nullptr;
} else {
head = head->next;
head->prev = nullptr;
}
delete temp;
}
// Remove the back node
void DoublyLinkedList::RemoveBack() {
if (!tail) return;
Node* temp = tail;
if (head == tail) {
head = tail = nullptr;
} else {
tail = tail->prev;
tail->next = nullptr;
}
delete temp;
}
// Count words of a specific language
int DoublyLinkedList::CountLanguage(string lang) {
int count = 0;
Node* current = head;
while (current) {
if (current->language == lang) {
count++;
}
current = current->next;
}
return count;
}
// Search for a word
int DoublyLinkedList::Search(string str) {
int pos = 1;
Node* current = head;
while (current) {
if (current->s == str) {
return pos;
}
pos++;
current = current->next;
}
return 0; // Not found
}
// Remove a specific word
void DoublyLinkedList::RemoveWord(string str) {
Node* current = head;
while (current) {
if (current->s == str) {
if (current == head) {
RemoveFront();
} else if (current == tail) {
RemoveBack();
} else {
current->prev->next = current->next;
current->next->prev = current->prev;
delete current;
}
return;
}
current = current->next;
}
}
// Overload the equality operator
bool DoublyLinkedList::operator==(const DoublyLinkedList& other) {
Node* thisCurrent = head;
Node* otherCurrent = other.head;
while (thisCurrent && otherCurrent) {
if (thisCurrent->s != otherCurrent->s || thisCurrent->language != otherCurrent->language) {
return false;
}
thisCurrent = thisCurrent->next;
otherCurrent = otherCurrent->next;
}
return !thisCurrent && !otherCurrent;
}
// Print the list
void DoublyLinkedList::Print() {
Node* current = head;
while (current) {
cout << "Word: " << current->s << ", Language: " << current->language << endl;
current = current->next;
}
}
// Load data from file
void DoublyLinkedList::LoadFromFile(const string& filename) {
ifstream file(filename);
string str, lang;
if (file.is_open()) {
while (file >> str >> lang) {
AddBack(str, lang);
}
file.close();
} else {
cerr << "Unable to open file." << endl;
}
}
// Main function
int main() {
DoublyLinkedList list1;
DoublyLinkedList list2;
DoublyLinkedList list3;
list1.LoadFromFile("list1.txt");
list2.LoadFromFile("list2.txt");
list3.LoadFromFile("list3.txt");
// Test the functions
list1.Print();
list2.Print();
list3.Print();
// Additional test code here...
return 0;
}
التوضيحات
- **Node Structure**: يحتوي على الحقلين `s` و `language` مع مؤشرين للإشارة إلى العنصر التالي والسابق.
- **DoublyLinkedList Class**: ينفذ الطرق المطلوبة لتعديل القائمة.
- **AddFront** و **AddBack**: لإضافة العقد في البداية أو النهاية.
- **AddInOrder**: لإضافة العقد بترتيب أبجدي.
- **RemoveFront** و **RemoveBack**: لإزالة العقد من البداية أو النهاية.
- **CountLanguage**: لحساب الكلمات بلغة معينة.
- **Search**: للبحث عن كلمة وإرجاع موقعها.
- **RemoveWord**: لإزالة كلمة معينة.
- **operator==**: لمقارنة قائمتين.
- **Print**: لطباعة العناصر.
- **LoadFromFile**: لتحميل البيانات من ملف.
يمكنك استخدام هذا الكود كأساس وتجربة تشغيله مع ملفات نصية تتبع التنسيق المحدد.
حظ موفق صديقي