From 1ce5b593ea547eb08ddfe6b10753b6eeb5c98f3e Mon Sep 17 00:00:00 2001 From: Mohanad Talat Date: Thu, 28 Apr 2022 06:21:11 +0200 Subject: [PATCH] Add sparse array --- .../Linked-Lists/SparseArray/.cproject | 125 ++++++++++++++++++ .../Linked-Lists/SparseArray/.gitignore | 1 + .../Linked-Lists/SparseArray/.project | 26 ++++ .../.settings/language.settings.xml | 25 ++++ .../org.eclipse.cdt.managedbuilder.core.prefs | 13 ++ .../SparseArray/src/ArrayLinkedList.cpp | 111 ++++++++++++++++ .../SparseArray/src/ArrayLinkedList.h | 23 ++++ .../Linked-Lists/SparseArray/src/Node.cpp | 13 ++ .../Linked-Lists/SparseArray/src/Node.h | 18 +++ .../SparseArray/src/SparseArray.cpp | 32 +++++ 10 files changed, 387 insertions(+) create mode 100644 algorithms/CPlusPlus/Linked-Lists/SparseArray/.cproject create mode 100644 algorithms/CPlusPlus/Linked-Lists/SparseArray/.gitignore create mode 100644 algorithms/CPlusPlus/Linked-Lists/SparseArray/.project create mode 100644 algorithms/CPlusPlus/Linked-Lists/SparseArray/.settings/language.settings.xml create mode 100644 algorithms/CPlusPlus/Linked-Lists/SparseArray/.settings/org.eclipse.cdt.managedbuilder.core.prefs create mode 100644 algorithms/CPlusPlus/Linked-Lists/SparseArray/src/ArrayLinkedList.cpp create mode 100644 algorithms/CPlusPlus/Linked-Lists/SparseArray/src/ArrayLinkedList.h create mode 100644 algorithms/CPlusPlus/Linked-Lists/SparseArray/src/Node.cpp create mode 100644 algorithms/CPlusPlus/Linked-Lists/SparseArray/src/Node.h create mode 100644 algorithms/CPlusPlus/Linked-Lists/SparseArray/src/SparseArray.cpp diff --git a/algorithms/CPlusPlus/Linked-Lists/SparseArray/.cproject b/algorithms/CPlusPlus/Linked-Lists/SparseArray/.cproject new file mode 100644 index 00000000..c0a95412 --- /dev/null +++ b/algorithms/CPlusPlus/Linked-Lists/SparseArray/.cproject @@ -0,0 +1,125 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/CPlusPlus/Linked-Lists/SparseArray/.gitignore b/algorithms/CPlusPlus/Linked-Lists/SparseArray/.gitignore new file mode 100644 index 00000000..3df573fe --- /dev/null +++ b/algorithms/CPlusPlus/Linked-Lists/SparseArray/.gitignore @@ -0,0 +1 @@ +/Debug/ diff --git a/algorithms/CPlusPlus/Linked-Lists/SparseArray/.project b/algorithms/CPlusPlus/Linked-Lists/SparseArray/.project new file mode 100644 index 00000000..4deda21c --- /dev/null +++ b/algorithms/CPlusPlus/Linked-Lists/SparseArray/.project @@ -0,0 +1,26 @@ + + + SparseArray + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/algorithms/CPlusPlus/Linked-Lists/SparseArray/.settings/language.settings.xml b/algorithms/CPlusPlus/Linked-Lists/SparseArray/.settings/language.settings.xml new file mode 100644 index 00000000..fcb4b2e4 --- /dev/null +++ b/algorithms/CPlusPlus/Linked-Lists/SparseArray/.settings/language.settings.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/CPlusPlus/Linked-Lists/SparseArray/.settings/org.eclipse.cdt.managedbuilder.core.prefs b/algorithms/CPlusPlus/Linked-Lists/SparseArray/.settings/org.eclipse.cdt.managedbuilder.core.prefs new file mode 100644 index 00000000..51af4fa7 --- /dev/null +++ b/algorithms/CPlusPlus/Linked-Lists/SparseArray/.settings/org.eclipse.cdt.managedbuilder.core.prefs @@ -0,0 +1,13 @@ +eclipse.preferences.version=1 +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.2018831514/CPATH/delimiter=; +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.2018831514/CPATH/operation=remove +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.2018831514/CPLUS_INCLUDE_PATH/delimiter=; +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.2018831514/CPLUS_INCLUDE_PATH/operation=remove +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.2018831514/C_INCLUDE_PATH/delimiter=; +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.2018831514/C_INCLUDE_PATH/operation=remove +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.2018831514/append=true +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.2018831514/appendContributed=true +environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.2018831514/LIBRARY_PATH/delimiter=; +environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.2018831514/LIBRARY_PATH/operation=remove +environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.2018831514/append=true +environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.2018831514/appendContributed=true diff --git a/algorithms/CPlusPlus/Linked-Lists/SparseArray/src/ArrayLinkedList.cpp b/algorithms/CPlusPlus/Linked-Lists/SparseArray/src/ArrayLinkedList.cpp new file mode 100644 index 00000000..69d10181 --- /dev/null +++ b/algorithms/CPlusPlus/Linked-Lists/SparseArray/src/ArrayLinkedList.cpp @@ -0,0 +1,111 @@ +#include "ArrayLinkedList.h" +#include + +using namespace std ; + +ArrayLinkedList::ArrayLinkedList(int length_) { + length = length_ ; + head = new Node(0,-1) ; + +} + +Node* ArrayLinkedList::get_node(int index_, bool wantCreate){ + + Node* prev = head ; + while(prev->next && prev->next->index < index_){ + prev = prev->next ; + } + bool exist = prev->next && prev->next->index == index_ ; + + if(exist) + return prev->next ; + if(!wantCreate) + return 0 ; + + Node* item = new Node(0,index_) ; + item->next = prev->next ; + prev->next = item ; + + return item ; +} + +void ArrayLinkedList::set_value(int index_, int data_){ + + get_node(index_, true)->data = data_ ; +} + +void ArrayLinkedList::printNonZero(){ + + Node* curr = head->next ; + while(curr){ + cout << curr->data << " " ; + curr = curr->next ; + } + cout << endl ; +} + +int ArrayLinkedList::get_index(int index_){ + Node* res = get_node(index_,false) ; + + if(!res) + return 0 ; + return res->data ; +} + + +void ArrayLinkedList::printAll(){ + + Node* curr = head->next ; + + for(int i=0; iindex){ + cout << curr->data << " " ; + curr = curr->next ; + } + else{ + cout << 0 << " " ; + } + } + cout << endl ; + +} + + +void ArrayLinkedList::add(ArrayLinkedList &another){ + + try{ + if(this->length != another.length){ + throw invalid_argument("The length of 2-arrays not EQUAL!! ") ; + } + }catch(exception &e){ + cout << endl << e.what() ; + throw e ; + } + + Node* curr_another = another.head->next ; + Node* curr = head->next ; + + for(int i=0; iindex == i){ + if(curr_another && curr_another->index == i){ + cout << curr_another->data + curr->data << " " ; + curr_another = curr_another->next ; + } + else{ + cout << curr->data << " " ; + } + curr = curr->next ; + } + else if(curr_another && curr_another->index == i){ + cout << curr_another->data << " " ; + curr_another = curr_another->next ; + } + else{ + cout << 0 << " " ; + } + } + + cout << endl ; +} + + diff --git a/algorithms/CPlusPlus/Linked-Lists/SparseArray/src/ArrayLinkedList.h b/algorithms/CPlusPlus/Linked-Lists/SparseArray/src/ArrayLinkedList.h new file mode 100644 index 00000000..549f5944 --- /dev/null +++ b/algorithms/CPlusPlus/Linked-Lists/SparseArray/src/ArrayLinkedList.h @@ -0,0 +1,23 @@ +#ifndef ARRAYLINKEDLIST_H_ +#define ARRAYLINKEDLIST_H_ +#include "Node.h" + +class ArrayLinkedList { +private: + int length ; + Node* head ; + + Node* get_node(int index_, bool wantCreat) ; + +public: + ArrayLinkedList(int length_) ; + void set_value(int index, int data) ; + void printNonZero() ; + int get_index(int index) ; + void printAll() ; + void add(ArrayLinkedList &another) ; +}; + +#endif /* ARRAYLINKEDLIST_H_ */ + + diff --git a/algorithms/CPlusPlus/Linked-Lists/SparseArray/src/Node.cpp b/algorithms/CPlusPlus/Linked-Lists/SparseArray/src/Node.cpp new file mode 100644 index 00000000..5653b151 --- /dev/null +++ b/algorithms/CPlusPlus/Linked-Lists/SparseArray/src/Node.cpp @@ -0,0 +1,13 @@ +#include "Node.h" + +Node::Node(int data_, int index_) { + + index = index_ ; + next = 0 ; + data = data_ ; + + +} + + + diff --git a/algorithms/CPlusPlus/Linked-Lists/SparseArray/src/Node.h b/algorithms/CPlusPlus/Linked-Lists/SparseArray/src/Node.h new file mode 100644 index 00000000..b051726d --- /dev/null +++ b/algorithms/CPlusPlus/Linked-Lists/SparseArray/src/Node.h @@ -0,0 +1,18 @@ +#ifndef NODE_H_ +#define NODE_H_ + +class Node { + +public: + + int data ; + int index ; + Node* next ; + + Node(int data, int index) ; + +}; + +#endif /* NODE_H_ */ + + diff --git a/algorithms/CPlusPlus/Linked-Lists/SparseArray/src/SparseArray.cpp b/algorithms/CPlusPlus/Linked-Lists/SparseArray/src/SparseArray.cpp new file mode 100644 index 00000000..0bdee270 --- /dev/null +++ b/algorithms/CPlusPlus/Linked-Lists/SparseArray/src/SparseArray.cpp @@ -0,0 +1,32 @@ +#include +#include "ArrayLinkedList.h" + +using namespace std; + +int main(){ + + ArrayLinkedList arr(10) ; // zero index + + arr.set_value(2,20) ; + arr.set_value(5,50) ; + arr.set_value(9,90) ; + arr.printNonZero() ; // 2 50 90 + arr.set_value(9,91) ; + arr.printNonZero() ; // 2 50 91 + + cout << arr.get_index(0) << endl ; // 0 + cout << arr.get_index(5) << endl ; // 50 + + arr.printAll() ; // 0 0 20 0 0 50 0 0 0 91 + + ArrayLinkedList arr2(10) ; + arr2.set_value(2,20) ; + arr2.set_value(5,50) ; + arr2.set_value(8,90) ; + + arr.add(arr2) ; // 0 0 40 0 0 100 0 0 90 91 + +} + + +