求java大牛,使用双向链表为数据结构,做出一个通讯录系统,不能使用数据库

a844128357 2015-12-15 09:16:55
在网上看到了C的,没见到java编的,求助
设计目的:用java双向链表作数据结构,编写一个通讯录管理系统。设计内容:本系统应完成以下几方面的功能:
1) 输入信息——enter();
2) 显示信息———display( );
3) 查找以姓名作为关键字 ———search( );
4) 删除信息———delete( );
5) 存盘———save ( );
6) 装入———load( ) ;
...全文
409 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_46119351 2020-01-02
  • 打赏
  • 举报
回复
引用 3 楼 bree06的回复:
[quote=引用 2 楼 a844128357 的回复:] 好的,但是这个程序要设置从头指针后面插入数据还是哪里插入数据呢
因为是双向链表的原因,所以插入到头尾都没有关系。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * 用java双向链表作数据结构,编写一个通讯录管理系统。设计内容:本系统应完成以下几方面的功能:
 * 1) 输入信息——enter();
 * 2) 显示信息———display( );
 * 3) 查找以姓名作为关键字 ———search( );
 * 4) 删除信息———delete( );
 * 5) 存盘———save ( );
 * 6) 装入———load( ) ;
 * @author bree06
 *
 */
public class Demo20 {
    /** 电话薄 */
    LinkedList<AddressBook> entities = null;

    /**
     * 实例化的时候就装入通迅录
     */
    public Demo20() {
        super();
        load();
    }

    /**
     * 输入信息
     * @param ab
     * @return
     */
    public boolean enter(AddressBook ab) {
        // check
        if (ab == null || nullOrEmpty(ab.getName())) {
            throw new IllegalArgumentException("通讯录用户名必须.");
        }
        int index = entities.indexOf(ab);
        // 如果不存在则追加到最后否则替换
        if (index == -1) {
            entities.add(ab);
        } else {
            entities.add(index, ab);
        }
        return true;
    }

    /**
     * 显示信息所有通讯录
     */
    public void display() {
        if (entities.isEmpty()) {
            System.out.println("通讯录为空.");
            return;
        }
        int index = 0;
        for (Iterator<AddressBook> it = entities.iterator(); it.hasNext();) {
            System.out.println("<---------------"+index+++"------------>\r\n" + it.next());
        }
        System.out.println("<---------------END------------>");
    }

    /**
     * 查找以姓名作为关键字
     * @param name
     * @return
     */
    public List<AddressBook> search(String name) {
        if (entities.isEmpty()) {
            return null;
        }
        // 查询条件为空时返回全部数据
        if (nullOrEmpty(name)) {
            return entities;
        }
        // 部分一致查询,返回所有包含关键字的用户
        List<AddressBook> result = new LinkedList<AddressBook>();
        for (Iterator<AddressBook> it = entities.iterator(); it.hasNext();) {
            AddressBook ab = it.next();
            String _name = ab.getName();
            if (_name.length() - (_name.replace(name, "").length()) > 0) {
                result.add(ab);
            }
        }
        return result;
    }

    /**
     * 存盘
     */
    public synchronized void save() {
        if (entities == null)
            return;
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(new File("AddressBooks.data")));
            oos.writeObject(entities);
        } catch (IOException e) {
        } finally {
        try {
            if (oos != null) oos.close();
        } catch (IOException e) {
        }
        }
    }

    /**
     * 装入
     */
    @SuppressWarnings("unchecked")
    public synchronized void load() {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("AddressBooks.data"));
            if (ois != null) {
                entities = (LinkedList<AddressBook>) ois.readObject();
            }
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } catch (ClassNotFoundException e) {
        } finally {
        try {
            if (ois != null)
                ois.close();
        } catch (IOException e) {
        }
        // none entity
        if (entities == null) {
            entities = new LinkedList<AddressBook>();
        }
        }
    }

    /**
     * 删除信息
     * @param ab
     * @return
     */
    public void delete(AddressBook ab) {
        if (entities.isEmpty()) {
            return;
        }
        entities.remove(ab);
    }

    /**
     * 删除信息(删除相同名字的通讯录)
     * @param ab
     * @return
     */
    public void delete(String name) {
        if (entities.isEmpty()) {
            return;
        }
        AddressBook ab = new AddressBook();
        ab.setName(name);
        entities.remove(ab);
    }

    /**
     * 空判断
     * @param s
     * @return
     */
    public boolean nullOrEmpty(String s) {
        return s == null || "".equals(s);
    }

    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {
        Demo20 demo = new Demo20();
        demo.display();
        
//        demo.delete("abc");
//        demo.save();
        
//        // enter
//        AddressBook ab = new AddressBook();
//        ab.setName("abc");
//        ab.setPhone_number("123456789");
//        demo.enter(ab);
//        demo.display();
//        // search
//        List<AddressBook> list = demo.search("cua");
//        System.out.println("抽出结果:");
//        if (list.isEmpty()) {
//            System.out.println("满足条件的通讯录为空.");
//            return;
//        }
//        int index = 0;
//        for (Iterator<AddressBook> it = list.iterator(); it.hasNext();) {
//            System.out.println("<---------------"+index+++"------------>");
//            System.out.println(it.next());
//        }
//        System.out.println("<---------------END------------>");
//        // enter
//        ab = new AddressBook();
//        ab.setName("eee");
//        ab.setPhone_number("000000000");
//        demo.enter(ab);
//        demo.display();
//        
//        demo.save();
    }

}

/**
 * 通讯录
 * @author bree06
 *
 */
class AddressBook implements Serializable {
    private static final long serialVersionUID = 1L;
    /** 姓名 */
    private String name;
    /** 电话号码 */
    private String phone_number;
    /** 住址 */
    private String address;
    /** 邮箱地址 */
    private String email;
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the phone_number
     */
    public String getPhone_number() {
        return phone_number;
    }
    /**
     * @param phone_number the phone_number to set
     */
    public void setPhone_number(String phone_number) {
        this.phone_number = phone_number;
    }
    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }
    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }
    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }
    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return String.format("姓名:%-10s电话:%-11s\r\n地址:%s\r\n邮箱:%s", name, phone_number, address, email);
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || !(o instanceof AddressBook)) {
            return false;
        }
        return name.equals(((AddressBook) o).getName());
    }
}
[/quote] 这个程序有排序功能吗?
weixin_45846869 2019-11-13
  • 打赏
  • 举报
回复
你好,这个具体实例化怎么导入
bree06 2015-12-16
  • 打赏
  • 举报
回复
引用 2 楼 a844128357 的回复:
好的,但是这个程序要设置从头指针后面插入数据还是哪里插入数据呢
因为是双向链表的原因,所以插入到头尾都没有关系。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * 用java双向链表作数据结构,编写一个通讯录管理系统。设计内容:本系统应完成以下几方面的功能:
 * 1) 输入信息——enter();
 * 2) 显示信息———display( );
 * 3) 查找以姓名作为关键字 ———search( );
 * 4) 删除信息———delete( );
 * 5) 存盘———save ( );
 * 6) 装入———load( ) ;
 * @author bree06
 *
 */
public class Demo20 {
    /** 电话薄 */
    LinkedList<AddressBook> entities = null;

    /**
     * 实例化的时候就装入通迅录
     */
    public Demo20() {
        super();
        load();
    }

    /**
     * 输入信息
     * @param ab
     * @return
     */
    public boolean enter(AddressBook ab) {
        // check
        if (ab == null || nullOrEmpty(ab.getName())) {
            throw new IllegalArgumentException("通讯录用户名必须.");
        }
        int index = entities.indexOf(ab);
        // 如果不存在则追加到最后否则替换
        if (index == -1) {
            entities.add(ab);
        } else {
            entities.add(index, ab);
        }
        return true;
    }

    /**
     * 显示信息所有通讯录
     */
    public void display() {
        if (entities.isEmpty()) {
            System.out.println("通讯录为空.");
            return;
        }
        int index = 0;
        for (Iterator<AddressBook> it = entities.iterator(); it.hasNext();) {
            System.out.println("<---------------"+index+++"------------>\r\n" + it.next());
        }
        System.out.println("<---------------END------------>");
    }

    /**
     * 查找以姓名作为关键字
     * @param name
     * @return
     */
    public List<AddressBook> search(String name) {
        if (entities.isEmpty()) {
            return null;
        }
        // 查询条件为空时返回全部数据
        if (nullOrEmpty(name)) {
            return entities;
        }
        // 部分一致查询,返回所有包含关键字的用户
        List<AddressBook> result = new LinkedList<AddressBook>();
        for (Iterator<AddressBook> it = entities.iterator(); it.hasNext();) {
            AddressBook ab = it.next();
            String _name = ab.getName();
            if (_name.length() - (_name.replace(name, "").length()) > 0) {
                result.add(ab);
            }
        }
        return result;
    }

    /**
     * 存盘
     */
    public synchronized void save() {
        if (entities == null)
            return;
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(new File("AddressBooks.data")));
            oos.writeObject(entities);
        } catch (IOException e) {
        } finally {
        try {
            if (oos != null) oos.close();
        } catch (IOException e) {
        }
        }
    }

    /**
     * 装入
     */
    @SuppressWarnings("unchecked")
    public synchronized void load() {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("AddressBooks.data"));
            if (ois != null) {
                entities = (LinkedList<AddressBook>) ois.readObject();
            }
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } catch (ClassNotFoundException e) {
        } finally {
        try {
            if (ois != null)
                ois.close();
        } catch (IOException e) {
        }
        // none entity
        if (entities == null) {
            entities = new LinkedList<AddressBook>();
        }
        }
    }

    /**
     * 删除信息
     * @param ab
     * @return
     */
    public void delete(AddressBook ab) {
        if (entities.isEmpty()) {
            return;
        }
        entities.remove(ab);
    }

    /**
     * 删除信息(删除相同名字的通讯录)
     * @param ab
     * @return
     */
    public void delete(String name) {
        if (entities.isEmpty()) {
            return;
        }
        AddressBook ab = new AddressBook();
        ab.setName(name);
        entities.remove(ab);
    }

    /**
     * 空判断
     * @param s
     * @return
     */
    public boolean nullOrEmpty(String s) {
        return s == null || "".equals(s);
    }

    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {
        Demo20 demo = new Demo20();
        demo.display();
        
//        demo.delete("abc");
//        demo.save();
        
//        // enter
//        AddressBook ab = new AddressBook();
//        ab.setName("abc");
//        ab.setPhone_number("123456789");
//        demo.enter(ab);
//        demo.display();
//        // search
//        List<AddressBook> list = demo.search("cua");
//        System.out.println("抽出结果:");
//        if (list.isEmpty()) {
//            System.out.println("满足条件的通讯录为空.");
//            return;
//        }
//        int index = 0;
//        for (Iterator<AddressBook> it = list.iterator(); it.hasNext();) {
//            System.out.println("<---------------"+index+++"------------>");
//            System.out.println(it.next());
//        }
//        System.out.println("<---------------END------------>");
//        // enter
//        ab = new AddressBook();
//        ab.setName("eee");
//        ab.setPhone_number("000000000");
//        demo.enter(ab);
//        demo.display();
//        
//        demo.save();
    }

}

/**
 * 通讯录
 * @author bree06
 *
 */
class AddressBook implements Serializable {
    private static final long serialVersionUID = 1L;
    /** 姓名 */
    private String name;
    /** 电话号码 */
    private String phone_number;
    /** 住址 */
    private String address;
    /** 邮箱地址 */
    private String email;
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the phone_number
     */
    public String getPhone_number() {
        return phone_number;
    }
    /**
     * @param phone_number the phone_number to set
     */
    public void setPhone_number(String phone_number) {
        this.phone_number = phone_number;
    }
    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }
    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }
    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }
    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return String.format("姓名:%-10s电话:%-11s\r\n地址:%s\r\n邮箱:%s", name, phone_number, address, email);
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || !(o instanceof AddressBook)) {
            return false;
        }
        return name.equals(((AddressBook) o).getName());
    }
}
a844128357 2015-12-16
  • 打赏
  • 举报
回复
好的,但是这个程序要设置从头指针后面插入数据还是哪里插入数据呢
bree06 2015-12-15
  • 打赏
  • 举报
回复
java.util.LinkedList就是双向链表的实现,可以解决1-4部分,并且LinkedList实现了Serializable接口可以直接使用WriteObject和ReadObject进行存盘和装入的操作

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧