塊狀鏈表
阿新 • • 發佈:2017-12-24
def 其中 uil free null 緩存 tail 交換 都是
我們同樣需要一個"緩存區"
塊狀鏈表作為一種集成了鏈表和分塊的數據結構,
有著非常優秀的性質
查詢和刪除都是$O(\sqrt )$復雜度的
需要支持的操作
1.創建一個新節點
2.插入一個新節點
3.刪除一個舊節點
4.向已知節點中加入新值
5.刪除已知節點中某些值
6.合並某兩個相鄰的節點
7.把一個節點分離成兩個
我們同樣需要一個"緩存區"
用於存儲初始數據和交換數據
這其中某些操作可以通過其他幾種操作組合得來
例如分離兩個節點可以刪除這個節點,
同時將這個節點中的數據緩存到緩存區
然後創建新節點將緩存區內的數據讀取到新節點
所以這個代碼好難寫……233333
#include<iostream> #include<cstring> #include<cstdio> #define N 5555 using namespace std; int data[N*N]; struct Block{ int s[N],len; }; class BlockList{ struct Node{ ListNode* nxt,pre; Block data; }; Node* head,tail; int len; Block newBlock(int n){//Build and read the data from the cache to initialize the new block; Block now.len=n; for(int i=0;i<n;++i) now.s[i]=data[i]; } Node* newnode(int n){//Build and initialize a new ListNode; Node* now=(Node*)malloc(sizeof(Node)); now->nxt=now->pre=NULL; now->data=newBlock(n); return now; } BlockList(){//Initialize the BlockList; head=newnode(0); tail=newnode(0); head->nxt=tail;tail->pre=head; } bool LDTTA(int x){//Load the x‘th data to the cache; if(len<x)return false; Node* now=head; for(int i=0;i<x;++i) now=now->nxt; for(int i=0;i<now->data.len;++i) data[i]=now->data.s[i]; } bool LDTTA(Node* root){//Load root‘ data to the cache; if(root==NULL)return false; for(int i=0;i<now->data.len;++i) data[i]=now->data.s[i]; } bool InsertANode(Node* root,int n){//Insert a new n‘size Node after the Node* root; if(root==tail)return false; if(root==NULL)return false; Listnode* in=newnode(n); Listnode* tmp=root->nxt; in->nxt=tmp;in->pre=root; root->nxt=in;tmp->pre=in; len++; return true; } bool Delete(Node* root){//Detele the Node root and Load the date from the root to the cache; elem->pre->nxt=elem->nxt; elem->nxt->pre=elem->pre; if(elem==head)head=elem->nxt; if(elem==tail)tail=elem->pre; LDTTA(root);free(elem); } };
塊狀鏈表