2023-10-08|閱讀時間 ‧ 約 9 分鐘

学习日记[13]

    今天主要做了三件记忆深刻的事。

    第一件事是双向表的创建,我设想的是两种方式。假设一个节点有两个指针,前面的称为before,后面的称为next。第一种方式就是before和next相互连接,这样我做了两个程序,都是因为过于混乱,两个相互赋值的语句让所有节点都一致,和头节点一样。所以似乎只有第二种方式才能建立,就是before之间相互连接,next之间相互连结。这样,before就是和next相反的操作——反向遍历。这种方式和前面一样,有向前连接和向后链接两种方式,下面就是代码。

    package Linked_Double_Behind_Notfor_Nohead;

    // next data before,这里的head和before是按照遍历顺序定的,从左到右为正,所以左边的next为头;相反,before是反向遍历,所以在后面;
    class Node
    {
    Node next;
    int data;
    Node forward;
    public Node(int data)
    {
    this.next = null;
    this.forward = null;
    this.data = data;
    }
    }
    public class linked_Double_Notfor
    {
    private Node head;
    private Node sec_head;
    public linked_Double_Notfor()
    {
    this.head = null;
    this.sec_head = null;
    }
    public void addList(int data)
    {
    Node newNode = new Node(data);
    if(head == null)
    {
    head = newNode;
    sec_head = newNode;
    }
    else
    {
    Node temp = head;
    while(temp.next != null)
    {
    temp = temp.next;
    }
    temp.next = newNode;
    newNode.forward = sec_head;
    sec_head = newNode;
    }
    }
    public void calculateSum()
    {
    int sum = 0;
    Node current = head;
    System.out.print("每一个值是:\n");
    int i = 0;
    while (current != null)
    {
    sum += current.data;
    i++;
    System.out.printf("%3d%c", current.data, i % 10 == 0 ? '\n' : 32);
    current = current.next;
    }
    System.out.println("\n总和是:" + sum); // 打印总和
    }
    public static void main(String[] args)
    {
    linked_Double_Notfor text = new linked_Double_Notfor();
    for(int i = 1 ; i <= 100 ; i++)
    {
    text.addList(i);
    }
    text.calculateSum();
    }

    }

    这个是向后插入方法

    package Linked_Double_Forward_Not_Nohead;



    //创建节点类
    class Node
    {
    //定义所需要的属性
    Node before;
    int data;
    Node next;
    //初始化节点
    public Node(int data)
    {
    this.before = null;
    this.data = data;
    this.next = null;
    }
    }
    //创建链表类
    public class LinkedList
    {
    //属性创建
    Node head;
    Node tail;
    //链表类的初始化
    public LinkedList()
    {
    this.head = null;
    this.tail = null;
    }
    //添加节点
    public void add_Node(int data)
    {
    //创建新节点
    Node newNode = new Node(data);
    //验证链表是否存在,同时链接节点
    if(head == null)
    {
    //如果不存在,我们就将新节点作为第一个节点
    head = newNode;
    tail = newNode;
    }
    else
    {
    //如果存在,就需要找到相应的位置进行链接
    Node temp = head;
    newNode.next = temp;
    head = newNode;
    temp.before = newNode;
    }
    }
    //测试程序
    public void calculateSum()
    {
    int sum = 0;
    Node current = tail;
    System.out.print("每一个值是:\n");
    int i = 0;
    while (current != null)
    {
    sum += current.data;
    i++;
    System.out.printf("%3d%c", current.data, i % 10 == 0 ? '\n' : 32);
    current = current.before;
    }
    System.out.println("\n总和是:" + sum); // 打印总和
    }
    public static void main(String[] args)
    {
    LinkedList text = new LinkedList();
    for(int i = 1 ; i <= 100 ; i++)
    {
    text.add_Node(i);
    }
    text.calculateSum();
    }
    }

    这个是向前插入方式。

    这两者之间其实差别挺大的。总体来说,这两种插入方式不适合单独来讲,每种方式里面都有另外一种方式的影子。最好还是用画图的方式理解,这是我的感悟,在做一件事情之前,必须把思路理清楚确保他是相对合理的,然后再按照这个思路去做。我就是太急忙了,这两个东西搞了近两小时,实在不应该。图一点更要清楚,图一定要清楚,图一定要清楚。

    第二件事就是读了一篇随笔,蒙田的《论我们的行为变化多端》。这篇随笔里,蒙田所认为的人应该是行为的集合体,而不是个体的多重方面。这样看也不是不无道理,因为构成我们的是行为,所以即使做错了事也不会神化它,错了就必须改正,这样我们不会片面地看待一个人。蒙田说人的变化多端是人性中最普遍也是最劣质的特性,我们不能仅仅依据生活和外表简单的评判一个人。人的种种行为是在多方面的影响下由个人做出的应激性判断,这里可以是金钱、名利、理想等等。要评价一个人必须要探究其行动背后的能量来源,由一个人的本质延伸到评价。我是很赞同的,我个人的经历也是告诉我这样,行动是多变的,只有我们自己的内心是唯一的,我们内心的能量供给行为才是始终不变的,比如一些动物本能——饥饿,困倦或者是各种情绪。尽管在类似情境中,这些都会变化,但他们却总是存在而不是说有一天会消失,当然,这里考虑的是生前的事情。变得始终如一,所有的行动都是由内心世界自助供能而不是外界供能,像性恶论一样,通过内心的建设,净化令人厌恶的一切,最终死的时候是洁白一身。

    第三件事就是体育课的突发奇想。昨天说过,大部分学科之间都是有联系的,这之中,体育是身体,是几乎所有行动的本源。所以上心还是有必要的。

    各位,晚安。




    分享至
    成為作者繼續創作的動力吧!
    © 2024 vocus All rights reserved.