20175314薛勐 数据结构-单链表(选做)

数据结构-单链表(选做)

要求

参见附件,补充MyList.java的内容,提交运行结果截图(全屏)
课下推送代码到码云

public class MyList {
	public static void main(String [] args) {
		//选用合适的构造方法,用你学号前后各两名同学的学号创建四个结点
		//把上面四个节点连成一个没有头结点的单链表
		//遍历单链表,打印每个结点的
		//把你自己插入到合适的位置(学号升序)
		//遍历单链表,打印每个结点的
		//从链表中删除自己
		//遍历单链表,打印每个结点的
	}
}
public class Node<T>                             //单链表结点类,T指定结点的元素类型
{
    public T data;                               //数据域,存储数据元素
    public Node<T> next;                         //地址域,引用后继结点

    public Node(T data, Node<T> next)            //构造结点,data指定数据元素,next指定后继结点
    {
        this.data = data;                        //T对象引用赋值
        this.next = next;                        //Node<T>对象引用赋值
    }
    public Node()
    {
        this(null, null);
    }
    public String toString()                     //返回结点数据域的描述字符串
    {
        return this.data.toString();
    }
}

思路

  • 我的学号是20175314,用20175312、20175313、20175315、20175316创建四个结点
  • 把四个节点连成一个没有头结点的单链表
  • 遍历单链表,输出每个结点
  • 把自己插入到合适的位置(升序)
  • 遍历单链表,输出每个结点
  • 从链表中删除自己
  • 遍历单链表,输出每个结点

代码

MyList.java

public class MyList {
    public static void main(String[] args) {
        //选用合适的构造方法,用你学号前后各两名同学的学号创建四个结点
        Node<java.lang.Integer> A = new Node<java.lang.Integer>(20175312,null);
        Node<java.lang.Integer> B = new Node<java.lang.Integer>(20175313,null);
        Node<java.lang.Integer> C = new Node<java.lang.Integer>(20175315,null);
        Node<java.lang.Integer> D = new Node<java.lang.Integer>(20175316,null);
        //把上面四个节点连成一个没有头结点的单链表
        A.next=B;
        B.next=C;
        C.next=D;
        //遍历单链表,打印每个结点的
        Node<java.lang.Integer> first = A;
        Node<java.lang.Integer> current = first;
        System.out.println("建立的链表为:");
        while(current!=null){
            System.out.print(current.toString());
            current=current.next;
        }
        System.out.println();
        //把你自己插入到合适的位置(学号升序)
        Node<java.lang.Integer> MyNode = new Node<java.lang.Integer>(20175314,null);
        current=first;
        if(current.data>MyNode.data){//成为头结点
            first=MyNode;
            MyNode.next=current;
        }
        else {
            while(current.next!=null&&current.next.data<MyNode.data){//插入中间或者末尾
                current=current.next;
            }
            MyNode.next=current.next;
            current.next=MyNode;
        }
        //遍历单链表,打印每个结点
        current = first;
        System.out.println("插入自己的学号后,链表为:");
        while(current!=null){
            System.out.print(current.toString());
            current=current.next;
        }
        System.out.println();
        //从链表中删除自己
        current=first;
        if(current.data.equals(MyNode.data)){//成为头结点
            first=current.next;
        }
        else {
            while(current.next!=null&&!current.next.data.equals(MyNode.data)){//插入中间或者末尾
                current=current.next;
            }
            current.next=current.next.next;
        }
        //遍历单链表,打印每个结点
        current = first;
        System.out.println("删除自己的学号后链表为:");
        while(current!=null){
            System.out.print(current.toString());
            current=current.next;
        }
        System.out.println();
    }
}

Node.java

public class Node<T>                             //单链表结点类,T指定结点的元素类型
{
    public T data;                               //数据域,存储数据元素
    public Node<T> next;                         //地址域,引用后继结点

    public Node(T data, Node<T> next)            //构造结点,data指定数据元素,next指定后继结点
    {
        this.data = data;                        //T对象引用赋值
        this.next = next;                        //Node<T>对象引用赋值
    }
    public Node()
    {
        this(null, null);
    }
    public String toString()                     //返回结点数据域的描述字符串
    {
        return this.data.toString();
    }
}

测试结果

码云链接

参考资料

Java实现简单的链表-面向初学者
数据结构(一) 单链表的实现-JAVA

原文地址:https://www.cnblogs.com/SANFENs/p/10816744.html