C + + data structure and algorithm analysis -- single linked list (array implementation)

Single linked list

introduce

Linked list is used to store a collection of data with the same property, and each data unit with this property becomes a node.

Definition of single linked list

The single linked list has two basic elements, the data item val in the single linked list and the subsequent node next. Therefore, the simplest structure of the single linked list node is defined as:

struct Node{ // Node definition
	int val; // Data items stored in a single linked list
	Node *next; // Rear drive node, pointing to the next node
};

Why use arrays to simulate linked lists

We know that we need new to create a new node when implementing a linked list with a structure, for example:

Node A = new Node(NULL);

It means to create a node and assign it NULL. However, the new operation is very time-consuming. It is easy to time out when we use the structure when doing questions, and the array will obviously be much faster.

How to use array simulation

There are value val and next pointers in the node. Obviously, it can't be completed with an array. Therefore, we need an array e[N] storing the value val and an array ne[N] storing the next pointer. Of course, these are not enough, because if we do not know which array subscript has been used and which has not been used when we want to create a node, we need a variable idx marking the currently available array subscript, and we also need a header node head.

int head,e[N],ne[N],idx; // Array simulates the variables required by the linked list

We can regard idx as a node, while e[idx] stores the value val stored in the node, ne[idx] stores the next pointer of the point, and head points to the chain header.


Then there is another problem, how to represent NULL? We treat - 1 as NULL by default.

Basic operation of linked list

Creation of linked list

When creating a linked list, we only need to point the head node to NULL and initialize the node to 0.

void init(){
	head = -1; // The header node points to null
	idx = 0; // idx initialized to 0
}

Insert Linked List

Suppose we want to insert a node with val = a into the linked list by header interpolation. At this time, we need to create a new node idx (because idx represents the currently available array subscript, it can be created by default).

void add(int x){ // Head insertion
	e[idx] = x; // Assign the node to x
	ne[idx] = head; // The next pointer of the node points to the head node, and the next pointer points to the node
	head = idx ++; // The chain header points to the inserted node, and the available node subscript + 1
}

Delete Linked List

If you want to delete the head node, you only need to update the node pointed to by the head to the node pointed to by the head.

void remove(){ // Delete header node
	head = ne[head];
}

There are some less commonly used operations that will not be described in detail. As long as you use this array to simulate the idea of linked list, other methods are also easy to push out.

Examples

Implement a single linked list. The linked list is initially empty and supports three operations:

  1. Insert a number into the chain header;
  2. Delete the number after the k-th inserted number;
  3. Insert a number after the k-th inserted number.

Now, you need to perform M operations on the linked list. After all operations, output the whole linked list from beginning to end.

Note: the k-th inserted number in the title does not refer to the k-th number of the current linked list. For example, if a total of n numbers are inserted during the operation, according to the insertion time sequence, the n numbers are: the number of the first insertion, the number of the second insertion,... The number of the nth insertion.

Input format

The first line contains the integer M, indicating the number of operations.

The next M lines contain an operation command. The operation commands may be the following:

  1. H x means to insert a number x into the chain header.
  2. D, K, the number after the k-th insertion number is deleted (when k is 0, the header node is deleted).
  3. I k x indicates that a number x is inserted after the k-th inserted number (k is greater than 0 in this operation).

Output format

A total of one row, the entire linked list output from beginning to end.

Data range

1 ≤ M ≤ 100000 1≤M≤100000 1≤M≤100000
All operations shall be legal.

Input example:

10
H 9
I 1 1
D 1
D 0
H 6
I 3 6
I 4 5
I 4 5
I 3 4
D 6

Output example:

6 4 6 5

Problem solving ideas

Operation 1: insert a number into the chain header
I have talked too much about interpolation above, so I won't repeat it
Operation 2: delete the number after the k-th inserted number
Similar to deleting a header node, because ne[k - 1] points to k, only ne[k-1] = ne[ne[k - 1]]; Just.
Operation 3: insert a number x after the k-th inserted number
It is similar to the header insertion method, except that the head should be replaced with ne[k], which means that the next value of the new node points to the node pointed to by the next of the K inserted node, and then the next pointer of node k points to the new node.

code

#include<iostream>
using namespace std;

const int N = 100010;
int head,e[N],ne[N],idx;

void init(){ // initialization
    head = -1;
    idx = 0;
}

void add_to_head(int x){ // Head insertion
    e[idx] = x;
    ne[idx] = head;
    head = idx ++;
}

void add(int k,int x){ // Insert a number x after the k-th inserted number
    e[idx] = x;
    ne[idx] = ne[k];
    ne[k] = idx ++;
}

void remove(int k){ // Delete the number after the k + 1 inserted number
    ne[k] = ne[ne[k]];
}

int main(){
    int n;
    cin >> n;
    init();
    while(n --){
        int k,x;
        char op[2];
        scanf("%s",op);
        if(op[0] == 'H'){
            cin >> x;
            add_to_head(x);
        }
        else if(op[0] == 'I'){
            cin >> k >> x;
            add(k - 1,x);
        }
        else {
            cin >> k;
            if(!k) head = ne[head]; // If k == 0, delete the header node
            remove(k - 1);
        }
    }
    for(int i = head; i != -1; i = ne[i]) cout << e[i] << ' '; // Output linked list
    return 0;
}

Tags: Algorithm data structure linked list

Posted on Sun, 12 Sep 2021 03:23:16 -0400 by Maharg105