General Block linked list

Source: Internet
Author: User

Write down the template here. Convenience later

# Include <iostream> # include <stdio. h> # include <string. h> # include <math. h> # include <stdlib. h ># define max_size 3000 using namespace STD; const int best_size = max_size-20; struct node {int size; int date [max_size]; struct node * Next;} block_list; // create a node struct node * createnode () {struct node * temp = (struct node *) malloc (sizeof (struct node); temp-> next = NULL; temp-> size = 0; return temp;} // initialize the block array // N indicates the total number of data stored in the block array. // block_size indicates the initial number of data stored in each block. Generally, SQRT (n) // Date indicates the data source, count void Init (int n, char * Date, int block_size) {struct node * temp = & block_list; struct node * temp2 = temp; int I; int K; for (I = 0, K = 0; k <n; I ++, K ++) {if (I = block_size) {temp2 = temp; temp = temp-> next; I = 0;} If (temp! = NULL) {temp-> date [I] = date [k]; temp-> size ++;} else {temp = createnode (); temp2-> next = temp; temp-> date [I] = date [k]; temp-> size ++ ;}}} // insert data into blocks // POS is the insertion position. date is the data bool insertdate (INT POs, int date) {struct node * temp = & block_list; struct node * temp2 = temp; int sum = block_list.size; int flag; while (sum <POS) {temp2 = temp; temp = temp-> next; If (temp = NULL) {temp2-> date [temp2-> Si Ze] = date; temp2-> size ++; return true;} sum + = temp-> size;} For (flag = temp-> size; flag> = (temp-> size-(Sum-Pos); flag --) {temp-> date [flag] = temp-> date [flag-1];} temp-> date [flag] = date; temp-> size ++; return true;} // Delete the data bool deletedate (int pos) whose position is POS) {struct node * temp = & block_list; int sum = block_list.size; int flag; while (sum <POS) {temp = temp-> next; If (temp = Null) return false; sum + = temp-> size;} For (flag = temp-> size-(Sum-Pos)-1; flag <temp-> size-1; flag ++) {temp-> date [flag] = temp-> date [flag + 1];} temp-> size --; return true ;} // split a block. // The parameter is bool splitnode (struct node * aim) {int size = aim-> size; struct node * temp = createnode (); temp-> next = aim-> next; Aim-> next = aim; For (INT I = best_size, K = 0; I <size; I ++, K ++) {Aim-> date [I] = temp-> date [k]; Aim-> size --; temp-> size ++;} return true ;} // merge two nodes. // It must be two adjacent nodes !!!! Bool combinenode (struct node * aim1, struct node * aim2) {If (aim1-> size + aim2-> size> best_size) return false; for (INT I = aim1-> size, j = 0; j <aim2-> size; j ++, I ++) {aim1-> date [I] = aim2-> date [J]; aim1-> size ++;} aim1-> next = aim2-> next; free (aim2 ); return true;} int SelectDate (int pos) {struct node * temp = & block_list; int sum = block_list.size; int flag; while (sum <POS) {temp = temp-> next; If (temp = NULL) return false; sum + = temp-> size ;} return temp-> date [temp-> size-(Sum-Pos)-1];}/* char test [1000001]; int main () {char T [2]; int num; int Pos; char d [2]; scanf ("% s", test); int Len = strlen (TEST); Init (Len, test, SQRT (LEN); scanf ("% d", & num); For (INT I = 0; I <num; I ++) {scanf ("% s ", t); If (T [0] = 'q') {scanf ("% d", & Pos); printf ("% C \ n ", selectDate (POS);} else {scanf ("% S % d", D, & Pos); insertdate (Pos, d [0]) ;}} */

The code without comments can directly drop question a 2887.

General Block linked list

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.