链表的概念与单链表使用

链表

链表是由一组不必相连(不必相连:可以连续也可以不连续)的内
存结构(节点),按特定的顺序链接在一起的抽象数据类型。是离散存储线性结构,n 个节点离散分配,彼此通过指针相连,每个节点只有一个前驱节点,每个节点只有一个后续节点,首节点没有前驱节点,尾节点没有后续节点。

  1. 链表优点:
    • 空间没有限制
    • 插入删除元素很快
  2. 链表缺点:
    • 存取速度很慢
  3. 链表的种类
    • 单链表
    • 双向链表
    • 循环链表。

链表示意图
在这里插入图片描述
链表的结构图
在这里插入图片描述
单链表代码:

public class userList {
	//data可以为基本数据类型,也可以为引用数据类型。
    User data;
    //next为下一个链表节点的地址
    userList next;

	//构造方法
    public userList() {
        data = foundUser();
        next = null;
    }

	//进行用户类创建
    public User foundUser(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入编号");
        String no = scanner.next();
        System.out.println("请输入名字");
        String name = scanner.next();
        User user = new User(no, name);
        return user;
    }


}

//用户类
class User {
    String no;
    String name;

	//用户类构造方法
    public User(String no, String name) {
        this.no = no;
        this.name = name;
    }

	//打印方法
    @Override
    public String toString() {
        return "User{" +
                "no='" + no + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

链表操作:

  1. 尾插法:
public void  insertTail(){
        userList list = head;
        userList newList = new userList();
        //遍历至链表尾部
        while (list.next!=null){
            list = list.next;
        }
        //将新建链表加入尾部
        list.next = newList;
        newList.next = null;
        System.out.println("插入成功");
    }
  1. 指定位置插入:
public void insert(int index) throws NullPointerException{
		//头结点
        userList list = head;
        //创建新节点
        userList newList = new userList();
        //因为本身即第一位置,number为1.
        int number = 1;
        while (number<index){
            list=list.next;
            number++;
        }
        //将指定位置的next地址赋给新节点的next
        newList.next=list.next;
        //将指定位置的next地址替换为新节点
        list.next = newList;
        System.out.println("插入成功");
    }
  1. 指定位置删除:
    public void delete(int index) throws  NullPointerException{
    	//定义两个节点,分别存放当前节点与当前节点的下一个结点。
        userList list =head;
        userList list1 =list.next;
        int number =1;
        while (number<index){
            list=list.next;
            list1 = list1.next;
            number++;
            if (list1.next==null){
                list.next=null;
                return;
            }
        }
        list.next = list1.next;
        list1 = null;
        System.out.println("删除成功");
    }
  1. 指定位置查询:
public void selete(int index) throws NullPointerException{
        userList list = head;
        int number = 1;
        while (number<index){
            list=list.next;
            number++;
        }
        //toString方法为用户类重写
        System.out.println(list.data.toString());
    }
  1. 指定编号查询:
 public void seleteNo(String no){
        userList list = head;
        boolean flag = false;
        while (list !=null){
            if (list.data.no.equals(no)){
                flag = true;
                break;
            }
            list = list.next;

        }
        if (flag){
            System.out.println(list.data.toString());
        }else {
            System.out.println("没有该编号");
        }

    }
  1. 指定姓名查询:
public void seleteName(String name){
        userList list = head;
        boolean flag = false;
        while (list.next !=null){
            if (list.data.name.equals(name)){
                flag = true;
                break;
            }
            list = list.next;
        }
        if (flag){
            System.out.println(list.data.toString());
        }else {
            System.out.println("没有该姓名");
        }
    }
  1. 查询全部:
public void  seleteAll(){
        userList list = head;
        while (list!= null){
            System.out.println(list.data.toString());
            list = list.next;
        }
    }

操作界面:

public class listTest {
    private static linkedList linkedList = new linkedList();

    //链表操作面板
    public static void List(){
        Scanner scanner = new Scanner(System.in);
        loop:while (true){
            try {
                System.out.println("请输入选择1.尾部插入 2.指定位置插入 3.指定位置删除 4.指定位置查询 5.指定编号查询 6.指定姓名查询 7.查询全部 8.退出");
                int number = scanner.nextInt();
                switch (number){
                    case 1:linkedList.insertTail();break;
                    case 2:
                        System.out.println("请输入指定位置");
                        linkedList.insert(scanner.nextInt()-2);
                        break;
                    case 3:
                        System.out.println("请输入指定位置");
                        linkedList.delete(scanner.nextInt()-2);
                        break;
                    case 4:
                        System.out.println("请输入指定位置");
                        linkedList.selete(scanner.nextInt()-1);
                        break;
                    case 5:
                        System.out.println("请输入编号");
                        linkedList.seleteNo(scanner.next());
                        break;
                    case 6:
                        System.out.println("请输入姓名");
                        linkedList.seleteName(scanner.next());
                        break;
                    case 7:linkedList.seleteAll();break;
                    case 8:break loop;
                    default:
                        System.out.println("输入错误");break;
                }
            }catch (InputMismatchException e){
                System.out.println("输入错误");
                List();
                return;
            }catch (NullPointerException e){
                System.out.println("长度不足,请重新输入");
                List();
                return;
            }
        }
    }


    public static void main(String[] args) {
        //链表面板
        List();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值