zoukankan      html  css  js  c++  java
  • 单链表操作 分类: 链表 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;
    }


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

  • 相关阅读:
    iOS中Block介绍(一)基础
    iOS消息推送机制的实现
    iOS AvPlayer AvAudioPlayer音频的后台播放问题
    git 解决fatal: Not a git repository
    执行git命令出现 xcrun: error:
    ios UIView的clipsTobounds属性
    UIWindow的层级问题Level
    解决方案:The file * couldn't be opened because you don't have permission to view it
    NSString 中包含中文字符时转换为NSURL
    UIView动画
  • 原文地址:https://www.cnblogs.com/juechen/p/4722067.html
Copyright © 2011-2022 走看看