数据结构:快状链表(数组链表联合)



#include #define _MAX_ 10using namespace std;//块状链表。struct MyNode{ //按理说每块数据的数组长度应该是根号N到2倍的根号N之间,暂时 //我为了测试就使用100个左右的数据。 int *data; int size;//大小。 int currentIndex;//当前下标。 MyNode *prev; MyNode *next; MyNode() :prev(NULL), next(NULL) { data = new int[_MAX_]; size = _MAX_; currentIndex = 0; }};class MyBlock{public: MyBlock() { first = last = new MyNode();//不循环了吧。 } void Insert(int a[],int n ) { MyNode *p = first; for (int i = 0; i < n; i++) { if (p == first)//一快还没有创建。 { MyNode *s = new MyNode(); s->data[s->currentIndex] = a[i];//刚在第一快第一个位置。 s->currentIndex++; s->prev = p; p->next = s; p = s;//p后移动。 } else { p = first->next;//从开头开始找,我才可以保证我的每一块都有序,快与快之间也有序。 while (p->next != NULL) { if (p->next->data[0] > a[i]) break; p = p->next;//找到a[i]的位置。 } //先插入,如果满了就拆分,满了是_MAX_(10)个元素,按一般拆分,就是5个数据。 int j; for (j = p->currentIndex; j > 0; j--) { if (p->data[j - 1] < a[i]) break; p->data[j] = p->data[j - 1];//插入排序。 } p->data[j] = a[i]; p->currentIndex++; //判断是否满了,满了就取半拆分。 if (p->currentIndex == p->size) { MyNode *s = new MyNode(); for (j = 5; j < p->size; j++) { s->data[s->currentIndex++] = p->data[j]; } p->currentIndex -= 5; if (p->next != NULL) { p->next->prev = s; s->next = p->next; s->prev = p; p->next = s; }


#include #define _MAX_ 10using namespace std;//块状链表。struct MyNode{ //按理说每块数据的数组长度应该是根号N到2倍的根号N之间,暂时 //我为了测试就使用100个左右的数据。 int *data; int size;//大小。 int currentIndex;//当前下标。 MyNode *prev; MyNode *next; MyNode() :prev(NULL), next(NULL) { data = new int[_MAX_]; size = _MAX_; currentIndex = 0; }};class MyBlock{public: MyBlock() { first = last = new MyNode();//不循环了吧。 } void Insert(int a[],int n ) { MyNode *p = first; for (int i = 0; i < n; i++) { if (p == first)//一快还没有创建。 { MyNode *s = new MyNode(); s->data[s->currentIndex] = a[i];//刚在第一快第一个位置。 s->currentIndex++; s->prev = p; p->next = s; p = s;//p后移动。 } else { p = first->next;//从开头开始找,我才可以保证我的每一块都有序,快与快之间也有序。 while (p->next != NULL) { if (p->next->data[0] > a[i]) break; p = p->next;//找到a[i]的位置。 } //先插入,如果满了就拆分,满了是_MAX_(10)个元素,按一般拆分,就是5个数据。 int j; for (j = p->currentIndex; j > 0; j--) { if (p->data[j - 1] < a[i]) break; p->data[j] = p->data[j - 1];//插入排序。 } p->data[j] = a[i]; p->currentIndex++; //判断是否满了,满了就取半拆分。 if (p->currentIndex == p->size) { MyNode *s = new MyNode(); for (j = 5; j < p->size; j++) { s->data[s->currentIndex++] = p->data[j]; } p->currentIndex -= 5; if (p->next != NULL) { p->next->prev = s; s->next = p->next; s->prev = p; p->next = s; }
http://h7gmq5.v.vote8.cn
http://bztugr.v.vote8.cn
http://jz673g.v.vote8.cn
http://5cltzh.v.vote8.cn
http://3e2mrf.v.vote8.cn
http://pjunat.v.vote8.cn
http://648l3t.v.vote8.cn
http://9zmyse.v.vote8.cn
http://tu7tkb.v.vote8.cn
http://u7b3wy.v.vote8.cn
http://6dj3uz.v.vote8.cn
http://wrq3lb.v.vote8.cn
http://3mlj2u.v.vote8.cn
http://nq2pzc.v.vote8.cn
http://nmggue.v.vote8.cn
http://ypdtva.v.vote8.cn
http://vblah4.v.vote8.cn
http://b9twck.v.vote8.cn
http://jrywhw.v.vote8.cn
http://zg6rnp.v.vote8.cn
http://4pge9h.v.vote8.cn
http://pfwjtb.v.vote8.cn
http://t2h966.v.vote8.cn
http://79w6sj.v.vote8.cn
http://98lej9.v.vote8.cn
http://qcdn26.v.vote8.cn
http://rjpy4m.v.vote8.cn
http://jtr7dt.v.vote8.cn
http://flpwwu.v.vote8.cn
http://6n87mq.v.vote8.cn
http://79vl29.v.vote8.cn
http://em549g.v.vote8.cn
http://h6eaqh.v.vote8.cn
http://rr5mv8.v.vote8.cn
http://azr7wx.v.vote8.cn
http://m9nv5q.v.vote8.cn
http://abw3r5.v.vote8.cn
http://835unl.v.vote8.cn
http://l5a24z.v.vote8.cn
http://3gsqvy.v.vote8.cn
http://gzdcgs.v.vote8.cn
http://5pvwu5.v.vote8.cn
http://bdayma.v.vote8.cn
http://lbxwbn.v.vote8.cn
http://tcun7g.v.vote8.cn
#include #define _MAX_ 10 using namespace std ; //块状链表。 struct MyNode{ //按理说每块数据的数组长度应该是根号N到2倍的根号N之间,暂时 //我为了测试就使用100个左右的数据。 int *data; int size; //大小。 int currentIndex; //当前下标。 MyNode *prev; MyNode *next; MyNode() :prev(NULL), next(NULL) { data = new int [_MAX_]; size = _MAX_; currentIndex = 0 ; }}; class MyBlock{ public : MyBlock() { first = last = new MyNode(); //不循环了吧。 } void Insert( int a[], int n ) { MyNode *p = first; for ( int i = 0 ; i < n; i++) { if (p == first) //一快还没有创建。 { MyNode *s = new MyNode(); s->data[s->currentIndex] = a[i]; //刚在第一快第一个位置。 s->currentIndex++; s->prev = p; p->next = s; p = s; //p后移动。 } else { p = first->next; //从开头开始找,我才可以保证我的每一块都有序,快与快之间也有序。 while (p->next != NULL) { if (p->next->data[ 0 ] > a[i]) break ; p = p->next; //找到a[i]的位置。 } //先插入,如果满了就拆分,满了是_MAX_(10)个元素,按一般拆分,就是5个数据。 int j; for (j = p->currentIndex; j > 0 ; j--) { if (p->data[j - 1 ] < a[i]) break ; p->data[j] = p->data[j - 1 ]; //插入排序。 } p->data[j] = a[i]; p->currentIndex++; //判断是否满了,满了就取半拆分。 if (p->currentIndex == p->size) { MyNode *s = new MyNode(); for (j = 5 ; j < p->size; j++) { s->data[s->currentIndex++] = p->data[j]; } p->currentIndex -= 5 ; if (p->next != NULL) { p->next->prev = s; s->next = p->next; s->prev = p; p->next = s; }

你可能感兴趣的:(数据结构:快状链表(数组链表联合))