"Problem description"
Given n integers, the deletion of these integers equal to M
The given sequence of integers is assumed to be: 1,3,3,0,-3,5,6,8,3,10,22,-1,3,5,11,20,100,3,9,3
It should be placed in a linked list with a list length of 20
The number to delete is 3, and after deletion, only 14 elements are left in the list: 1 0-3 5 6 8 10 22-1 5 11 20 100 9
Input
Contains 3 rows:
The first line is an integer n (1 <= n <= 200000) that represents the number of elements in the array.
The second row contains n integers, which represent n elements in the array. Each integer is separated by a space, and the value of each integer is within the range of a 32-bit signed integer.
The third line is an integer k, which represents the value of the element to be deleted (the value of K is also within the 32-bit signed integer range). Output output is only 1 lines:
After all the elements in the array are deleted, the values of the remaining elements in the output array are separated by a space between each integer.
Output
The output is only 1 lines:
After all the elements in the array are deleted, the values of the remaining elements in the output array are separated by a space between each integer.
Well...... is to find the element to be deleted in turn, stop at a node in front of it, connect the node's *next to the next node of the element to be deleted, and finally release the memory of the element to be deleted.
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5#include <cmath>6 using namespacestd;7 structnode8 {9 intdata;TenNode *Next; One}*head, *p, *R; A intn, x; - intMain () - { thescanf"%d", &n); - intA; -Head =NewNode Head, Next =NULL; -R = Head;//Initialize + for(inti =1; I <= N; ++i)//Create a linked list - { +scanf"%d", &a); Ap =Newnode; atP-data =A; -P-Next =NULL; -R-Next =p; -R =p; - } -p =head; inscanf"%d", &x); - while(P! =NULL) to { +Node *s = P-Next; - if(s && s, data = =x) the { *P-Next = P-NextNext; $Free (s);//releasing the memory of the node SPanax Notoginseng } - Elsep = PNext; the /*Be sure to add this else, because we're going to judge the next number, + The next number after deletion is the next number of deleted numbers. otherwise A will skip a number, will be missing*/ the } +p = HeadNext; - while(P-Next! =NULL) $ { $printf"%d", P-data); -p = PNext; - } theprintf"%d\n", P-data); - return 0;Wuyi}
Because the elements in the list do not have a specific location, we can only know a node and who is connected, which leads us to find each node must be looked at from scratch, not very fast. But there are two sides to everything, we can also ignore the concept of location in the list, when deleting and adding nodes, we do not want to move the whole position of the same as the array or the VIS array tag.
To summarize, the operation of the table is actually the operation of the pointer field. As long as each node how to connect, even which node to understand, it will be no difficult place
Delete an element in an array (linked list)