diff --git a/LinearList/STL-List/main.cpp b/LinearList/STL-List/main.cpp index e69de29..16a773d 100644 --- a/LinearList/STL-List/main.cpp +++ b/LinearList/STL-List/main.cpp @@ -0,0 +1,54 @@ +#include +#include +using namespace std; +int main(){ + list a; + a.push_back("1.Ohyeah"); + a.push_back("2.Ohno"); + std::list::iterator it = a.begin(); + auto ite = a.cbegin(); + cout << *ite << endl; + a.insert(++it, "0.Ohoh"); + a.push_back("3.Ohhoho"); + for(string x : a){ + cout << x << " "; + } + cout << endl; + + //Copy a to b + list b(a); + + auto itmp1 = a.begin(); + itmp1++; + auto itmp2 = a.end(); + itmp2--; + a.erase(itmp1, itmp2); + + for(string x : a){ + cout << x << " "; + } + cout << endl << endl; + //Two Methods going through The Whole List + auto iit = a.begin(); + for(int i = 0; i < a.size(); i++){ + cout << *iit << " "; + iit++; + } + cout << endl; + cout << endl; + + //insert ? + for(string x : b){ + cout << x << " "; + } + cout << endl; + auto itb = b.begin(); + itb++;itb++; + b.insert(itb, a.begin(), a.end()); //insert into pos 2 + for(string x : b){ + cout << x << " "; + } + cout << endl; + return 0; +} + diff --git a/LinearList/STL-List/main.exe b/LinearList/STL-List/main.exe new file mode 100644 index 0000000..9005809 Binary files /dev/null and b/LinearList/STL-List/main.exe differ diff --git a/LinearList/skipList/sl.cpp b/LinearList/skipList/sl.cpp new file mode 100644 index 0000000..486aff6 --- /dev/null +++ b/LinearList/skipList/sl.cpp @@ -0,0 +1,196 @@ +#include +#include +#include +#include +#include + +#define maxLevel 12 +struct Node +{ + int key; + int value; + Node* forward[1]; +}; + +struct skipList +{ + int level; + Node* head; +}; + +class SkipList +{ +public: + SkipList() + { + srand((int)time(0)); + slist = (skipList*)malloc(sizeof(skipList)); + slist->head = createNode(maxLevel, 0, 0); + slist->level = 0; + for(int i=0; ihead->forward[i] = NULL; + } + } + + int search(int key) + { + Node *p, *q; + p = slist->head; + int k = slist->level; + for(int i=k; i>=0; --i) + { + while(p->forward[i] != NULL && p->forward[i]->key <= key) + { + if(p->forward[i]->key == key) + { + q = p->forward[i]; + return q->value; + } + p = p->forward[i]; + } + } + } + bool insert(int key, int value) + { + Node *p, *q; + Node *update[maxLevel]; + p = slist->head; + int k = slist->level; + for(int i = k; i>=0; --i) + { + while(p->forward[i] != NULL && p->forward[i]->key < key) + { + p = p->forward[i]; + } + update[i] = p; + } + + q = p->forward[0]; + if(q && q->key == key) + return false; + + k = random(); + if(k > slist->level) + { + for(int i=slist->level; i<=k; ++i) + { + update[i] = slist->head; + } + slist->level = k; + } + + q = createNode(k, key, value); + for(int i = 0; i<=k; ++i) + { + q->forward[i] = update[i]->forward[i]; + update[i]->forward[i] = q; + } + + printList(); + } + bool remove(int key) + { + Node *update[maxLevel]; + Node *p,*q; + p = slist->head; + int k = slist->level; + for(int i = k ; i >=0 ; i --){ + while(p->forward[i] != NULL && (p->forward[i]->key < key)){ + p = p->forward[i]; + } + update[i] = p; + } + q = p->forward[0]; + if(q && q->key == key){ + for(int i = 0 ; i <= k; ++i){ + if(update[i]->forward[i] != q) + break; + update[i]->forward[i] = q->forward[i]; + } + free(q); + for(int i = slist->level ; i > 0 ; i --){ + if(slist->head->forward[i] == NULL){ + slist->level --; + } + } + //printList(); + return true; + } + else{ + return false; + } + } + virtual ~SkipList() + { + destroy(); + } + +private: + Node* createNode(int level, int key, int value) + { + Node* node = (Node*)malloc(sizeof(Node) + level*sizeof(Node*)); + node->key = key; + node->value = value; + return node; + } + int random() + { + int k = 0; + while(rand() %2) k++; + k = k>maxLevel?maxLevel:k; + return k; + } + void printList() + { + Node *p,*q; + int k=slist->level; + for(int i=k; i >= 0; i--) + { + p = slist->head->forward[i]; + printf("level[%d]: ",i); + while(p->forward[i] != NULL) + { + printf("%d -> ",p->value); + p = p->forward[i]; + } + printf("%d\n",p->value); + } + printf("\n"); + } + void destroy() + { + Node *p, *q; + int k = slist->level; + p = slist->head; + for(int i= k; i>=0; --i) + { + while((q = p) !=NULL) + { + free(q); + p = p->forward[i]; + } + } + free(slist); + slist = NULL; + } +private: + skipList* slist; +}; + + +int main(){ + + SkipList skip; + for(int i=1; i<20; ++i) + { + skip.insert(i, 2*i); + } + + int v = skip.search(5); + std::cout << v << "\n"; + skip.remove(5); + std::cout << skip.search(5)<< "\n"; + sleep(2); + return 0; +} \ No newline at end of file