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


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

    TimeLimit: 1000ms Memory limit: 4096K

    题目描述

    输入n个整数,先按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据m,将单链表中的值为m的结点全部删除。分别输出建立的初始单链表和完成删除后的单链表。

    输入

    第一行输入数据个数n

    第二行依次输入n个整数;

    第三行输入欲删除数据m

    输出

    第一行输出原始单链表的长度;

    第二行依次输出原始单链表的数据;

    第三行输出完成删除后的单链表长度;

    第四行依次输出完成删除后的单链表数据。

    示例输入

    10

    5625 12 33 66 54 7 12 33 12

    12

    示例输出

    10

    5625 12 33 66 54 7 12 33 12

    7

    5625 33 66 54 7 33

    #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,*tail;
        tail=head;
        for(int i=1;i<=n;i++)
        {
            q=new node;
            q->next=NULL;
            cin>>q->data;
            tail->next=q;
            tail=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(int m)
    {
        node *q,*p;
        p=head->next;
        q=head;
        while(p)
        {
            if(p->data==m)
            {
                q->next=p->next;
                free(p);
                p=q->next;
                n--;
            }
            else
            {
                q=q->next;
                p=p->next;
            }
        }
    }
    int main()
    {
    
        int m;
        head=new node;
        head->next=NULL;
        cin>>n;
        Creat();
        cin>>m;
        Ouput();
        Delete(m);
        Ouput();
        return 0;
    }


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

  • 相关阅读:
    HttpModule和在Global.asax区别
    SQL Server中视图的特点与优化
    SQL中int类型与varchar类型的隐式转换
    利用SQL语句查询SQL中所有正在执行的命令
    jquery子窗体操作父窗体中的元素
    js 连接数据库
    典型的列变行,用动态语句来做
    js中with、this的用法
    SQL SERVER数据库状态(脱机,联机,可疑)及SQL设置语句详解
    UVA 10465 Homer Simpson
  • 原文地址:https://www.cnblogs.com/juechen/p/4722067.html
Copyright © 2020-2023  润新知