This commit is contained in:
e2hang
2025-07-30 16:46:08 +08:00
parent ecb484a9e8
commit d510f4c75b
3 changed files with 250 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
#include <iostream>
#include <list>
using namespace std;
int main(){
list<string> a;
a.push_back("1.Ohyeah");
a.push_back("2.Ohno");
std::list<string>::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<string> 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;
}

Binary file not shown.

196
LinearList/skipList/sl.cpp Normal file
View File

@@ -0,0 +1,196 @@
#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <cstdlib>
#include <time.h>
#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; i<maxLevel; ++i)
{
slist->head->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;
}