当前位置:网站首页>Linked list de duplication

Linked list de duplication

2022-06-11 05:03:00 bolite

3-2-1 Linked list The chain watch is weightless (25 branch )

Given a linked list with integer keys L, You need to delete the key node where the absolute value is repeated . For each key value K, Only the first absolute value is equal to K The nodes of are reserved . meanwhile , All deleted nodes must be saved in another linked list . For example, given L by 21→-15→-15→-7→15, You need to output the list after de duplication 21→-15→-7, And the deleted list -15→15.

Input format :
Enter... On the first line L The address of the first node and a positive integer N(≤1e​5, Is the total number of nodes ). The address of a node is nonnegative 5 An integer , Empty address NULL use -1 To express .

And then N That's ok , Each line describes a node in the following format :

Address Key value Next node

Where the address is the address of the node , The key value is an absolute value that does not exceed 10
​4
​​ The integer of , The next node is the address of the next node .

Output format :
First output the list after de duplication , Then output the deleted list . Each node occupies a row , Output... In input format .

sample input :

00100 5
99999 -7 87654
23854 -15 00000
87654 15 -1
00000 -15 99999
00100 21 23854

sample output :

00100 21 23854
23854 -15 99999
99999 -7 -1
00000 -15 87654
87654 15 -1

Code display ( Linked list version ):

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<malloc.h>
#define N 100010
struct LNode {
    
    int idex;// The address of the node 
    int data;// The data of this node 
    int nextidex;// The address of the next node 
    struct LNode* next;
};
typedef struct LNode* List;
int main()
{
    
    int a[N] = {
     0 };// Record address data
    int b[N] = {
     0 };// Record repetition 
    int c[N] = {
     0 };// Record the next address 
    int n;// Make a cycle 
    int m;// Header 
    int zb;// to b Array positioning 
    int cb;// to a Array positioning 
    int sb;// save data Of 
    scanf("%d %d", &m, &n);
    int i = 0;
    for (i = 0; i < n; i++) {
    
        scanf("%d %d %d", &zb, &sb, &cb);
        a[zb] = sb;
        c[zb] = cb;
        // b[abs(sb)]++;
    }
    if (n == 1) {
    
        printf("%05d %d -1", zb, sb);
        return 0;
    }
    if (n == 0) {
    
        return 0;
    }
    int aim = m;
    List head, tail, p1, tou, wei;
    //head For the linked list after weight removal ,tou For a list of repeated numbers 
    //tail Is the tail pointer of the linked list after de duplication ,wei Is the tail pointer of the linked list with repeated numbers 
    head = tail = p1 = tou = wei = NULL;
    for (aim = m; aim != -1; aim = c[aim]) {
    
        p1 = (List)malloc(sizeof(struct LNode));
        p1->idex = aim;
        p1->data = a[aim];
        p1->nextidex = c[aim];// Buckle the contents of the node on the structure 
        if (b[abs(p1->data)]) {
    
            if (tou == NULL) {
    
                tou = p1;
            }
            else {
    
                wei->next = p1;
                wei->nextidex=p1->idex;
            }
            wei = p1;
        }
        else {
    
            b[abs(p1->data)]++;
            if (head == NULL) {
    
                head = p1;
            }
            else {
    
                tail->next = p1;
                tail->nextidex=p1->idex;
            }
            tail = p1;
        }
    }
    List L, l;
    L = head;
    l = L->next;
    while (l) {
    // Output the linked list without repetition 
        printf("%05d %d %05d\n", L->idex, L->data, L->nextidex);
        L = l;
        l = l->next;
    }
    printf("%05d %d -1\n", L->idex, L->data);
    L=tou;
    l=L->next;
    if (tou) {
    // Output duplicate digital linked list 
        while (l) {
    
            printf("%05d %d %05d\n", L->idex, L->data, L->nextidex);
            L = l;
            l = l->next;
        }
        printf("%05d %d -1\n", L->idex, L->data);
    }
}

The following is an illustration of the processing of the input address
 Insert picture description here

原网站

版权声明
本文为[bolite]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020543428718.html