Based on the review - linear linked list data structure

- As the blogger has not systematically studied data structure, but leisure self, might had misunderstood welcome that.

Data in this chapter to explain by topic

  • List

Basic characteristics: 1) Memory is generally not continuous, a connecting block

     2) Find the slower elements, only by moving the pointer by one to find. But this drawback can usually be solved by an array simulation list - the array index number representing the content of different memory array represents the contents of the memory , which can achieve a similar index query.

     3) insertion and deletion of continuous operation compared to the memory array is more convenient

 

Title: Queue arrangements

main idea:

  To a school teacher in the class NN N classmate in a row, students are numbered 1~N1 \ N the SIM 1 ~ N, he took the following method:

  1. First 11 1 students arranged into the queue, the queue at this time only one person;

  2. -N2 of N-2 2 - N number of students into the column sequentially numbered i students enqueue way: the teacher to specify the number i is the number of students stood 1~ (i-1) 1 \ sim (i -1) . 1 ~ ( I - . 1 ) by the students (i.e. into the column before the students have) left or right side;

  3. Removed from the queue M (M <N) M (M <N) M ( M < N ) classmates, other students the same position order.

  After all of the students in the queue are arranged as described above is completed, the teacher wants to know all the students numbers from left to right.

  Very typical of the list structure - due to the linked list of memory is not continuous , delete an element interconnected to other elements of little effect. At the same time the list of topics I would not feel able to do pointer pointers, arrays can simulate it with an array of analog (analog arrays easier to find). It relates to the front insert and the rear insert so use a doubly linked list.

  Note doubly linked list point: both before and after , we should also consider the node is operated pre and nxt node every time you insert deleted.

- Create a list of nodes:

  Chain simulation using the array
  numbers on a @para pre node (note that the "memory number", not the content)
  Number @para nxt next node
  @para id contents of this node
  p [maxn] memory array can be seen as a linked list each memory block , the order has not yet determined


struct
Peo { int pre, NXT; int ID; } P [MAXN]; // structure doubly linked list

 - insert:

  The list is to insert a new memory adjacent to the original list of two memory establish new contacts (middle insert), or establish contact with the head and tail. The link between memory by memory the list of numbers .

/ * 
@Para ID insert a new memory number <==> array index
number memory @para k to be inserted adjacent positions of
contact between the memory blocks through the list @note -> "No Memory"
Record the tail and root list changes, to facilitate traversing or deletion determining section
* /
void
_insert ( int K, int P, int ID) { IF (P == . 1 ) { // after the P [ID] .nxt = P [ k] .nxt; // new link next memory is a link memory in the case of the original P [ID] .PRE = K; // a new link to the original memory memory p [p [k] .nxt] . pre = ID; // next memory on a link of a link that the original memory is updated to the new memory .nxt P [K] = ID; // next contact to be inserted in the original memory of the new memory IF (K == tail) tail =ID; convenient access list // record the tail after } the else { // before entering the P [ID] .nxt = K; P [ID] .PRE = P [K] .PRE; P [P [K] .PRE ] .nxt = ID; P [K] .PRE = ID; IF (the root == K) = the root ID; } return ; }

- deletion:

  Delete can be seen as covering, (assuming that abc) will b deleted is actually overwritten with c b, is about a next contact reset to c, because it is a doubly linked list -> before and after taking into account , but also the desired c of on a contact reset to a.

// If not noted but with the analog array pointer 
memory --delete then a // a (case of a pointer) is discarded after a release of the best;
void
_dele ( int a) { IF ( OUT [a] | ! | SZ) return ; OUT [A] = to true , SZ - = . 1 ; IF (A == the root) the root = P [A] .nxt; the else IF (A == tail) tail = P [A] .PRE ; the else { P [P [A] .PRE] .nxt = P [A] .nxt; P [P [A] .nxt] .PRE = P [A] .PRE; } return ; }
#include <cstdio> 
#include <the iostream>
 the using  namespace STD;
 const  int MAXN 1E5 + = 2 ;
 struct Peo {
     int pre, NXT;
     int ID; 
} P [MAXN];   // structure doubly linked list 
BOOL  OUT [MAXN] ;
 int POS [MAXN], SZ, the root, tail; 

void _insert ( int K, int P, int ID) {
     IF (P == . 1 ) { // after the 
        P [ID] .nxt = P [K]. NXT; 
        P [ID] .PRE = k;

        p[p[k].nxt].pre = id;
        p[k].nxt = id;
        if(k == tail) tail = id;
    }
    else { //前入
        p[id].nxt = k;
        p[id].pre = p[k].pre;

        p[p[k].pre].nxt = id;
        p[k].pre = id;
        if(k == root) root = id;
    }
    return ;
}

void _dele(int a) {
    if(out[a] || !sz) return;
    out[a] = true, sz -= 1;

    if(a == root) root = p[a].nxt;
    else if(a == tail) tail = p[a].pre;
    else {
        p[p[a].pre].nxt = p[a].nxt;
        p[p[a].nxt].pre = p[a].pre;
    }
    return;
}

int n, m;
int main() {
    scanf("%d", &n);
    root = tail = 1;
    for(int i=2; i<=n; ++i) {
        int k, p;
        scanf("%d %d", &k, &p);
        _insert(k, p, i);
    }
    sz = n;

    scanf("%d", &m);
    while(m--) {
        int a;
        scanf("%d", &a);
        _dele(a);
    }
    if(sz > 0) {
        while(root != tail) {
            printf("%d ", root);
            root = p[root].nxt;
        }
        printf("%d\n", tail);
    }
    else puts("");
    return 0;
}
A complete solution to a problem Code

Title: Joseph problems

main idea:

Personal n (n <= 100) in a circle, beginning from the first number of individual packets, the number of people out of m column, and then again by the next individual packets from a start count number m of the people and then the ring , ...... and so on, until everyone turns out, please turn out the circle of people in the output number.

  Way linked list data structure, after speaking before the next doubly-linked lists, lists more simple way just compared to after care .

  Node establishment:

struct Peo {
     int ID; // current memory contents
     int NXT; // next memory "number" 
} P [MAXN];

  This question is mainly talk about the linked list traversal:

  Singly linked list can only be traversed from start to finish, usually with a new variable (here it is set now) to represent the current traverse to the number of memory, an initial list now for the first memory node numbers 0, after-demand mobile now = list [now] .nxt (now point to the next memory number).

 

/*
模拟链表
*/
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 100 + 4;
struct Peo {
    int id;
    int nxt;
}p[maxn];
int pre, sz;
int n, m;

int main() {
    scanf("%d %d", &n, &m);
    sz = n;
    for(int i=0; i<n-1; ++i) { // I human sequence number is not the number 
        P [I] .id + = I . 1 ; 
        P [I] .nxt = I + . 1 ; 
    } 
    P [n- - . 1 ] .id = n-; 
    P [n- - . 1 ]. = NXT 0 ; 

    int now = 0 ;
     BOOL First = to true ;
     the while (SZ> . 1 ) { 

        IF (First) {
             for ( int J = 0 ; J <M- 2 ; ++ J) { 
                now = P [now] .nxt; 
            }
            first = false;
        }

        else
            for(int j=0; j<m-1; ++j) {
                now = p[now].nxt;
            }
        cout << p[p[now].nxt].id << ' ';
        p[now].nxt = p[p[now].nxt].nxt;
        sz -= 1;
    }
    if(sz)
        cout << p[now].id << endl;
    return 0;
}
A complete solution to a problem

 

Guess you like

Origin www.cnblogs.com/GorgeousBankarian/p/11422288.html