• 单链表操作B 分类: 链表 2015-06-07 12:42 15人阅读 评论(0) 收藏


    数据结构上机测试2-2:单链表操作B

    TimeLimit: 1000ms Memory limit: 65536K

    题目描述

    按照数据输入的相反顺序(逆位序)建立一个单链表,并将单链表中重复的元素删除(值相同的元素只保留最后输入的一个)。

    输入

    第一行输入元素个数n

    第二行输入n个整数。

    输出

    第一行输出初始链表元素个数;

    第二行输出按照逆位序所建立的初始链表;

    第三行输出删除重复元素后的单链表元素个数;

    第四行输出删除重复元素后的单链表。

    示例输入

    10

    2130 14 55 32 63 11 30 55 30

    示例输出

    10

    3055 30 11 63 32 55 14 30 21

    7

    3055 11 63 32 14 21

    #include <bits/stdc++.h>
    #define WW freopen("input.txt","r",stdin)
    #define RR freopen("ouput.txt","w",stdout)
    using namespace std;
    struct node
    {
        int data;
        node *next;
    }*head;
    int n;
    void Creat()
    {
        node *q;
        for(int i=1; i<=n; i++)
        {
            q=new node;
            q->next=head->next;
            cin>>q->data;
            head->next=q;
        }
    }
    void Ouput()
    {
        node *p;
        p=head->next;
        cout<<n<<endl;
        while(p)
        {
            if(p!=head->next)
                cout<<" ";
            cout<<p->data;
            p=p->next;
        }
        cout<<endl;
    }
    void Delete()
    {
        node *q,*p,*r;
        p=head->next;
        while(p)
        {
            q=p->next;
            r=p;
            while(q)
            {
                if(p->data==q->data)
                {
                    r->next=q->next;
                    free(q);
                    q=r->next;
                    n--;
                }
                else
                {
                    q=q->next;
                    r=r->next;
                }
            }
            p=p->next;
        }
    }
    int main()
    {
        head=new node;
    head->next=NULL;
        cin>>n;
        Creat();
        Ouput();
        Delete();
        Ouput();
        return 0;
    }


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    浅谈CSS盒子模型
    JS中的prototype
    Underscore.js(JavaScript对象操作方法)
    stylus--css 框架使用方法
    LESS CSS 框架简介与使用
    三个 CSS 预处理器(框架):Sass、LESS 和 Stylus
    三.jquery.datatables.js表格编辑与删除
    二.jquery.datatables.js表格数据添加
    git-分支使用方式
    vue2购物车ch2-(商品列表显示)
  • 原文地址:https://www.cnblogs.com/juechen/p/4722066.html
Copyright © 2020-2023  润新知