3597 problem f algorithm 2-25 delete duplicate elements in ordered single chain table

Problem F: algorithm 2-25 delete duplicate elements in ordered single chain table
Time limit: 30 Sec memory limit: 128 MB
Flower offering: 30 solution: 17
[flower offering] [wreath] [TK question bank]
Title Description
According to an increasing sequence of integers, an ordered single chain table is constructed, in which repeated elements are deleted
input
The input includes multiple groups of test data, each group of test data occupies a row, the first is an integer n greater than or equal to 0, which represents the length of the single chain table, followed by N integers, which represents each element of the chain table. Spaces between integers
output
For each group of test data, the output includes two lines, which are the linked list elements before and after deletion, separated by spaces
If the list is empty, only one row will be output, list is empty
sample input
5 1 2 3 4 5
5 1 1 2 2 3
0
sample output
1 2 3 4 5
1 2 3 4 5
1 1 2 2 3
1 2 3
list is empty

#define _CRT_SECURE_NO_WARNINGS
#include <unordered_map>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <map>

using namespace std;

typedef struct LNode
{
    int data;
    LNode * next;
}LNode, *LinkList;

void CreateLink(LinkList &L,int n)
{
    L = new LNode;
    L->next = NULL;

    int data;
    LinkList p = L, q;

    if (n == 0)
        printf("list is empty\n");

    else
    {
        for (int i = 0; i < n; ++i)
        {
            scanf("%d", &data);
            q = new LNode;
            q->data = data;
            q->next = NULL;
            p->next = q;

            p = q;

            printf("%d ", data);
        }
        printf("\n");
    }
}


void DestoryList(LinkList &L)
{
    LinkList p = L,q;
    L = NULL;
    while (p)
    {
        q = p->next;
        delete p;
        p = q;
    }
}

void GetRidSameElem(LinkList & L)
{
    LinkList p = L;
    if (p->next)
        p = p->next;
    else
        return;

    LinkList q = p->next,tmp;
    while (p && q)
    {
        if (p->data == q->data)
        {
            tmp = q;
            q = q->next;
            p->next = q;
            delete tmp;
        }
        else
        {
            p = p->next;
            q = q->next;
        }
    }
}

void PrintList(const LinkList L)
{
    LinkList  p = L->next;
    if (p)
    {
        while (p)
        {
            printf("%d ", p->data);
            p = p->next;
        }
        printf("\n");
    }
}

int main()
{
#ifdef _DEBUG
    freopen("data.txt", "r+", stdin);
#endif // _DEBUG


    int n;
    LinkList L;
    while (EOF != scanf("%d", &n))
    {
        CreateLink(L,n);
        GetRidSameElem(L);
        PrintList(L);
        DestoryList(L);
    }

    return 0;
}
/**************************************************************
    Problem: 3597
    User: Sharwen
    Language: C++
    Result: Immortals
    Time:67 ms
    Memory:4744 kb
****************************************************************/

Posted on Mon, 06 Apr 2020 07:54:21 -0700 by DylanBlitz