单向链表

package singlelinkist;

/**
 * Created by guipengfei on 2020/8/5 22:34
 */
public class Node {
    int num;
    String name;
    Node next;
    public Node()
    {

    }
    public Node(int num,String name)
    {
        this.num=num;
        this.name=name;
    }
    @Override
    public String toString() {
        return "Node{" +
                "num=" + num +
                ", name='" + name + '\'' +
                '}';
    }
}

package singlelinkist;

import java.util.Scanner;

/**
 * Created by guipengfei on 2020/8/5 22:37
 */
public class LinkList {
    Node head = new Node();


    //在尾部添加结点
    public void addByTail(Node node) {
       Node temp=head;
       while (temp.next!=null)
           temp=temp.next;
       temp.next=node;
    }

    //根据编号查找结点
    public int find(int num) {
        Node temp = head.next;
        int loc = 0;
        while (temp != null && temp.num != num) {
            temp = temp.next;
            loc++;
        }
        if (temp != null)
            return loc;
        else
            return -1;
    }
   //修改结点信息
    public int update(int num) {

        Node temp = head.next;
        while (temp != null && temp.num != num) {
            temp = temp.next;

        }
        if (temp != null) {
            System.out.println("请输入新的学号和姓名:");
            Scanner scanner = new Scanner((System.in));
            int newNum = scanner.nextInt();
            String newName = scanner.next();
            temp.num=newNum;
            temp.name=newName;
            return 1;
        }
        else
        {
            return -1;
        }

        }
        //在指定位置插入新的结点,头结点位置记为0
        public int insert(int loc,Node newNode)
        {
            int count=0;
            if(loc<1||loc>size()+1)
            {
                System.out.println("插入位置有误");
                return -1;
            }
            else {
                Node pre=head;
                while (count!=loc-1)//查找loc前一个位置的结点
                {
                    count++;
                    pre=pre.next;

                }
               Node temp=pre.next;
                pre.next=newNode;
                newNode.next=temp;
                return 1;
            }

        }
        //删除指定学号的结点
        public int delete(int num)
        {
            Node pre=head;
            while (pre.next!=null&&pre.next.num!=num)
                pre=pre.next;
            if(pre.next==null)
                return -1;
            else {
                pre.next=pre.next.next;
                return 1;
            }
        }
        //获取链表结点个数
        public int size()
        {
            int count=0;
            Node temp=head.next;
            while (temp!=null)
            {
                count++;
                temp=temp.next;
            }
            return count;
        }
        //打印结点信息
        public void show ()
        {
            Node temp = head.next;
            while (temp != null) {
                System.out.println(temp);
                temp = temp.next;
            }
        }

    }

package singlelinkist;

/**
 * Created by guipengfei on 2020/8/5 22:42
 */

/**
 *链表的查找时间复杂度:O(n)    数组的查找时间复杂度O(1)
 * 链表的删除时间复杂度:O(n)+O(1)   数组的删除时间复杂度O(n)
 */
public class LinkListTest {
    public static void main(String args[])
    {
        LinkList linkList=new LinkList();
        Node node1=new Node(1001,"Tom");
        Node node2=new Node(1002,"Mike");
        Node node3=new Node(1003,"Bob");
        Node node4=new Node(1004,"Jack");
        //测试添加
        linkList.addByTail(node1);
        linkList.addByTail(node2);
        linkList.addByTail(node3);
        linkList.addByTail(node4);
        //测试打印
        linkList.show();

        //测试查找
        int x1=linkList.find(1008);
        System.out.println("1008的位置:"+x1);
        int x2=linkList.find(1003);
        System.out.println("1003的位置:"+x2);

        //测试修改
//        int flag=linkList.update(1002);
//        if(flag==1)
//            linkList.show();
//        else
//            System.out.println("修改失败,结点不存在");
        //测试求结点数
        System.out.println(linkList.size());

        //测试插入
        Node node5=new Node(1000,"Sara");
        linkList.insert(1,node5);//第一个位置插入
        linkList.show();
        System.out.println();
        Node node6=new Node(1005,"Jim");
        linkList.insert(6,node6);//最后一个位置插入
        linkList.show();
        System.out.println();


        //测试删除
        linkList.delete(1000);//测试删除第一个
        linkList.show();
        System.out.println();
        linkList.delete(1005);//测试删除最后一个
        linkList.show();




    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodePanda@GPF

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值