PTA7-34 通讯录的录入与显示JAVA语言,链表实现

7-34 通讯录的录入与显示

题目

通讯录中的一条记录包含下述基本信息:朋友的姓名、出生日期、性别、固定电话号码、移动电话号码。
本题要求编写程序,录入N条记录,并且根据要求显示任意某条记录。

输入格式:

输入在第一行给出正整数N(≤10);随后N行,每行按照格式姓名 生日 性别 固话 手机给出一条记录。其中姓名是不超过10个字符、不包含空格的非空字符串;生日按yyyy/mm/dd的格式给出年月日;性别用M表示“男”、F表示“女”;固话和手机均为不超过15位的连续数字,前面有可能出现+。
在通讯录记录输入完成后,最后一行给出正整数K,并且随后给出K个整数,表示要查询的记录编号(从0到N−1顺序编号)。数字间以空格分隔。

输出格式:

对每一条要查询的记录编号,在一行中按照姓名 固话 手机 性别 生日的格式输出该记录。若要查询的记录不存在,则输出Not Found。

输入样例:

3
Chris 1984/03/10 F +86181779452 13707010007
LaoLao 1967/11/30 F 057187951100 +8618618623333
QiaoLin 1980/01/01 M 84172333 10086
2 1 7

输出样例:

LaoLao 057187951100 +8618618623333 F 1967/11/30
Not Found

想法:

本来想像C语言一样用结构体来,直接把那些属性放在一个结构体类,但是java中没有结构体,只有class,于是就用class,想单纯用class实现类似结构体一样代码,结果到一半的时候试了一下,输入的时候发现,居然报了空指针异常,后来也解决,还是乖乖的用链表实现吧,也巩固一下链表的知识,这个题用链表实现主要考察链表的输入和查找,代码如下:

代码:

import java.util.Scanner;
class Book{
    int count;//节点的序号,便于后面查询
    String name;  //姓名
    String birthday; //生日
    String  gender; //性别
    String tel; //固话
    String mobile; //手机
    Book next; //下一个节点
    public Book (int count,String name,String birthday,String  gender,String tel,String  mobile){
        this.name=name;
        this.birthday=birthday;
        this.gender=gender;
        this.tel=tel;
        this.mobile=mobile;
        this.count=count;
    }
}
 class LinkedList{
    public Book first;//首节点
    public  Book last;//尾结点
    public boolean isEmpty(){//判断链表是否为空
        return first==null;
    }
    public void print(Book ptr){ //打印函数
            System.out.println(ptr.name+" "+ptr.tel+" "+ptr.mobile+" "+ptr.gender+" "+ptr.birthday);
    }
    public void insert(Book ptr){//插入函数
        //如果为空链表就让插入的节点为首节点
        if(this.isEmpty()){
            first=ptr;
            last=ptr;
        }//不为空链表的话,就把尾结点的下一个节点指向该节点,让该节点为尾结点
        else{
            last.next=ptr;
            last=ptr;
        }
    }
}
public class PTA334 {
    public static void main(String args[]) {
        LinkedList list = new LinkedList();//创建一个链表
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();//有多少个联系人
        String name, birthday, gender, tel, mobile;//基本属性
        int i;//节点的序号,便于后面查询
        for (i = 1; i <= num; i++) {//输入
            name = sc.next();
            birthday = sc.next();
            gender = sc.next();
            tel = sc.next();
            mobile = sc.next();
            Book temp = new Book(i, name, birthday, gender, tel, mobile);//创建节点
            list.insert(temp);//插入到链表中
        }
        int K = sc.nextInt();//题目所需,要查找的数量
        int n;//查找的序号
        for (int j = 1; j <= K; j++) {
            boolean flage=true;//开关,用于后面判断输不输出"NO found"
            n = sc.nextInt();
            //创建一个节点使其等于首节点
            Book book1 = new Book(list.first.count, list.first.name, list.first.birthday, list.first.gender, list.first.tel, list.first.mobile);
            book1.next = list.first.next;
            //遍历,当要查找的序号等于链表中的某一个序号是就输出该序号对应的节点的相关属性
            for(int a=list.first.count;a<=list.last.count;a++){
                if(n==book1.count){
                    list.print(book1);
                    flage=false;//如果没有找到该序号要输出"NO Found",但是"NO found"要在遍历完之后才输出,在循环外,避免找到该序号对应的节点后还输出"NO found"
                }
                if(book1!=list.last)//避免遍历到最后变为空,出现空指针异常问题
                book1=book1.next;
            }
            if((n!=book1.count)&&flage)//没有找到该序号对应的节点,输出"NO found"
                System.out.println("NO found");
        }
    }
}

运行结果

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值