日撸 Java 三百行day11-13

文章目录

  • 说明
  • day11-day12 顺序表
    • 1.面向过程面向对象区别
    • 2.代码
      • 2.1 面向过程
      • 2.2 面向对象
  • day13 链表
    • 1.成员内部类
    • 2.链表的插入删除
    • 3.代码

说明

闵老师的文章链接: 日撸 Java 三百行(总述)_minfanphd的博客-CSDN博客

自己也把手敲的代码放在了github上维护:https://github.com/fulisha-ok/sampledata

day11-day12 顺序表

1.面向过程面向对象区别

面向过程更关注的是过程(函数),每个函数会完成特定的功能,函数可以通过传参数来控制数据。
面向对象更关注的是对象,每一个对象都有自己的属性和方法,相应的数据和方法都封装在对象内部,一般是通过方法去访问对象内部的数据。

2.代码

正如文章所说,在面向对象的设计中,用对象来存储数据及其上的操作。我们现实生活中的所有事物都可以被看做是对象。在今天写的这个例子中,首先,对于顺序表的操作,我们采用面向过程的思想和面向对象的区别

2.1 面向过程

(参考了PTA中的数据结构与算法题目集):

  • 1.对顺序表的构造是通过结构体的形式来声明的
  • 2.创建一个空的线性表
  • 3 对顺序表的操作就调不同的方法即可
#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 5
typedef char element;
typedef struct LNode *List;
struct LNode {
    element Data[MAXSIZE];
    int Last;  /* 线性表长度*/
};

//创建了一个空的线性表
List MakeEmpty(){
    List list = (List)malloc(sizeof(struct LNode));
    list->Last= -1;
    return list;
}

//返回线性表中X的位置。若找不到则返回ERROR;
Position Find( List L, ElementType X ){
    for(int i = 0; i <= L->Last; i++){
    	if(L->Data[i] == X){
            return i;
        }
	}
    return ERROR;
}

//将X插入在位置P并返回true。若空间已满,则打印“FULL”并返回false;如果参数P指向非法位置,则打印“ILLEGAL POSITION”并返回false;
bool Insert( List L, ElementType X, Position P ){
     if(L->Last+1>= MAXSIZE){
        printf("FULL");
        return false;
    }
    
    if(P<0 || P>L->Last + 1 || P>=MAXSIZE){
        printf("ILLEGAL POSITION");
        return false;
    }
   
    for(int i = L->Last; i >= P; i--){
        L->Data[i+1] = L->Data[i];
    }
    L->Data[P]= X;
    L->Last++;
    //printf("%d-%d\n", L->Last, MAXSIZE);
    return true;
}

//将位置P的元素删除并返回true。若参数P指向非法位置,则打印“POSITION P EMPTY”(其中P是参数值)并返回false
bool Delete( List L, Position P ){
    if(P<0 || P>L->Last){
        printf("POSITION %d EMPTY", P);
        return false;
    }
    for(int i = P; i < L->Last; i++){
        L->Data[i] = L->Data[i+1];
    }
    L->Last--;
    return true;
}

int main()
{
    List L;
    ElementType X;
    Position P;
    int N;

    L = MakeEmpty();
    scanf("%d", &N);
    while ( N-- ) {
        scanf("%d", &X);
        if ( Insert(L, X, 0)==false )
            printf(" Insertion Error: %d is not in.\n", X);
    }
    scanf("%d", &N);
    while ( N-- ) {
        scanf("%d", &X);
        P = Find(L, X);
        if ( P == ERROR )
            printf("Finding Error: %d is not in.\n", X);
        else
            printf("%d is at position %d.\n", X, P);
    }
    scanf("%d", &N);
    while ( N-- ) {
        scanf("%d", &P);
        if ( Delete(L, P)==false )
            printf(" Deletion Error.\n");
        if ( Insert(L, 0, P)==false )
            printf(" Insertion Error: 0 is not in.\n");
    }
    return 0;
}


2.2 面向对象

大部分顺序表都会有一个数组和相应数组的长度,也都会初始化顺序表;我们则可以给它抽象为一个类,即顺序表类;在这个类中,包含的属性即数组和数组长度(可能根据不同的情况可能会有不同,我们可以通过继承等来扩展这个类),也应该有初始化数组的方法,以及每个数组可能会用到的方法:如将数组转化为字符串输出,插入,删除,查找以及重置顺序表等。当我们想要使用顺便表时,就可以用new实例化一个顺序表对象,而生成的实例化对象也就拥有了这个顺表类中所对应的属性已经方法等。所以面向


  • 如下面的例子 SequentialList 就是一个抽象出来的一个顺序表类

  • 构造函数作用
    构造函数名与类名一样,无返回值,可多个。如果类中没有构造函数,其实在未定义构造函数时会有一个缺省无参构造函数。构造函数最大的作用是:在创建对象时(new)初始化对象

  • 对象
    通过new来实例化对象。
    通SequentialList tempFirstList = new SequentialList(tempArray); //带参数初始化对象
    SequentialList tempFirstList = new SequentialList(); //不带参数的初始化对象

  • Object类
    Object类是所有类的超类(一般我们创建的类都是隐式继承),所有类都可以使用Object类中的成员方法和变量,也可以重写其中的方法,例如今天的代码就重写了toString.若不重写toString方法,则会返回类名+hash值

package datastructure.list;
public class SequentialList {
    /**
     * The maximal length of the list. It is a constant.
     */
    public  static final int MAX_LENGTH = 10;

    /**
     * The actual length not exceeding MAX_LENGTH. Attention: length is not only the member variable of Sequential list,
     * but also the member variable of Array. In fact, a name can be the member variable of different classes.
     */
    int length;

    /**
     * The data stored in an array.
     */
    int[] data;

    /**
     *  Construct an empty sequential list.
     */
    public SequentialList(){
        length = 0;
        data = new int[MAX_LENGTH];
    }

    /**
     * Construct a sequential list using an array.
     * @param paraArray The given array. Its length should not exceed MAX_LENGTH. For simplicity now we do not check it.
     */
    public SequentialList(int[] paraArray){
        data = new int[MAX_LENGTH];
        length = paraArray.length;
        for (int i = 0; i < paraArray.length; i++) {
            data[i] = paraArray[i];
        }
    }

    /**
     * Overrides the method claimed in Object, the superclass of any class.
     * @return
     */
    public String toString(){
        String resultString = "";

        if (length == 0){
            return "empty";
        }

        for (int i = 0; i<length-1; i++){
            resultString += data[i] + ",";
        }

        resultString += data[length - 1];

        return resultString;
    }

    /**
     * Reset to empty.
     */
    public void reset(){
        length = 0;
    }

    /**
     * Find the index of the given value. If it appears in multiple positions,simply return the first one.
     * @param paraValue The given value.
     * @return The position. -1 for not found.
     */
    public int indexOf(int paraValue){
        int tempPosition = -1;

        for (int i = 0; i<paraValue; i++){
            if (data[i] == paraValue){
                tempPosition = i;
                break;
            }
        }

        return tempPosition;
    }

    /**
     * Insert a value to a position. If the list is already full, do nothing.
     * @param paraPosition The given position.
     * @param paraValue The given value.
     * @return Success or not.
     */
    public boolean insert(int paraPosition, int paraValue){
        if (length == MAX_LENGTH){
            System.out.println("List full.");
            return false;
        }

        if (paraPosition < 0 || paraPosition > length){
            System.out.println("The position " + paraPosition + " is out of bounds.");
            return false;
        }

        for (int i = length; i > paraPosition; i--){
            data[i] = data[i-1];
        }

        data[paraPosition] = paraValue;
        length++;

        return true;
    }

    /**
     * Delete a value at a position.
     * @param paraPosition paraPosition The given position.
     * @return Success or not.
     */
    public boolean delete(int paraPosition){
        if (paraPosition < 0 || paraPosition >= length){
            System.out.println("The position " + paraPosition + " is out of bounds.");
            return false;
        }

        for (int i = paraPosition; i < length-1; i++){
            data[i] = data[i+1];
        }

        length--;
        return true;
    }

    public static void main(String args[]) {
        /*int[] tempArray = { 1, 4, 6, 9 };
        SequentialList tempFirstList = new SequentialList(tempArray);
        System.out.println("Initialized, the list is: " + tempFirstList.toString());
        System.out.println("Again, the list is: " + tempFirstList);

        tempFirstList.reset();
        System.out.println("After reset, the list is: " + tempFirstList);*/

        int[] tempArray = { 1, 4, 6, 9 };
        SequentialList tempFirstList = new SequentialList(tempArray);
        System.out.println("After initialization, the list is: " + tempFirstList.toString());
        System.out.println("Again, the list is: " + tempFirstList);

        int tempValue = 4;
        int tempPosition = tempFirstList.indexOf(tempValue);
        System.out.println("The position of " + tempValue + " is " + tempPosition);

        tempValue = 5;
        tempPosition = tempFirstList.indexOf(tempValue);
        System.out.println("The position of " + tempValue + " is " + tempPosition);

        tempPosition = 2;
        tempValue = 5;
        tempFirstList.insert(tempPosition, tempValue);
        System.out.println(
                "After inserting " + tempValue + " to position " + tempPosition + ", the list is: " + tempFirstList);

        tempPosition = 8;
        tempValue = 10;
        tempFirstList.insert(tempPosition, tempValue);
        System.out.println(
                "After inserting " + tempValue + " to position " + tempPosition + ", the list is: " + tempFirstList);

        tempPosition = 3;
        tempFirstList.delete(tempPosition);
        System.out.println("After deleting data at position " + tempPosition + ", the list is: " + tempFirstList);

        for (int i = 0; i < 8; i++) {
            tempFirstList.insert(i, i);
            System.out.println("After inserting " + i + " to position " + i + ", the list is: " + tempFirstList);
        } // Of for i

        tempFirstList.reset();
        System.out.println("After reset, the list is: " + tempFirstList);
    }
}

在这里插入图片描述
若将toString注释掉 输出的内容:
在这里插入图片描述
对操作集的输出

在这里插入图片描述

day13 链表

1.成员内部类

在c语言中,对链表的数据结构是用结构体来表示,在java中使用内部类来表示链表结构体,成员内部类是定义在另一个类的内部的类,可以访问外部类的成员和方法,包括私有成员和方法。如文章中的Node类可以访问外部类中所有的成员变量和成员方法。

2.链表的插入删除

插入

删除
在这里插入图片描述

3.代码

在c中,对链表结构的定义

typedef struct LNode *PtrToLNode;
struct LNode {
    ElementType Data;
    PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;

在java中

package datastructure.list;

import sun.applet.Main;
import java.util.UUID;
public class LinkedList {
    class Node{
        int data;
        Node next;

        /**
         * The constructor
         * @param paraValue The data.
         */
        public Node(int paraValue){
            data = paraValue;
            next = null;
        }
    }

    Node header;

    /**
     * Construct an empty linked list.
     */
    public LinkedList(){
        header = new Node(0);
    }

    /**
     * Overrides the method claimed in Object, the superclass of any class.
     * @return
     */
    public String toString(){
        String resultString = "";

        if (header.next == null){
            return "empty";
        }

        Node tempNode = header.next;
        while (tempNode != null){
            resultString += tempNode.data + ",";
            tempNode  = tempNode.next;
        }

        return resultString;
    }

    /**
     * Reset to empty. Free the space through garbage collection.
     */
    public void reset() {
        header.next = null;
    }

    /**
     * Locate the given value. If it appears in multiple positions, simply return the first one.
     * @param paraValue The given value.
     * @return The position. -1 for not found.
     */
    public int locate(int paraValue){
        int tempPosition = -1;

        Node tempNode = header.next;
        int tempCurrentPosition = 0;
        while (tempNode != null){
            if (tempNode.data == paraValue){
                tempPosition = tempCurrentPosition;
                break;
            }

            tempNode = tempNode.next;
            tempCurrentPosition++;
        }

        return  tempCurrentPosition;
    }

    /**
     * Insert a value to a position. If the list is already full, do nothing.
     * @param paraPosition The given position.
     * @param paraValue The given value.
     * @return Success or not.
     */
    public boolean insert(int paraPosition, int paraValue){
        Node tempNode = header;
        Node tempNewNode;

        //find a preNode
        for (int i = 0; i<paraPosition; i++){
            if (tempNode.next == null){
                System.out.println("The position " + paraPosition + " is illegal.");
                return false;
            }
            tempNode = tempNode.next;
        }

        tempNewNode = new Node(paraValue);

        tempNewNode.next = tempNode.next;
        tempNode.next = tempNewNode;

        return  true;
    }


    /**
     * Delete a value at a position.
     * @param paraPosition The given position.
     * @return Success or not
     */
    public boolean delete(int paraPosition){
        if (header.next == null){
            System.out.println("Cannot delete element from an empty list.");
            return false;
        }

        Node tempNode = header;

        for (int i = 0; i < paraPosition; i++){
            if (tempNode.next == null){
                System.out.println("The position " + paraPosition + " is illegal.");
                return false;
            }

            tempNode = tempNode.next;
        }

        tempNode.next = tempNode.next.next;
        return true;
    }

    public static void main(String args[]) {
        LinkedList tempFirstList = new LinkedList();
        System.out.println("Initialized, the list is: " + tempFirstList.toString());

        for (int i = 0; i < 5; i++) {
            tempFirstList.insert(0, i);
        }
        System.out.println("Inserted, the list is: " + tempFirstList.toString());

        tempFirstList.insert(6, 9);

        tempFirstList.delete(4);

        tempFirstList.delete(2);
        System.out.println("Deleted, the list is: " + tempFirstList.toString());

        tempFirstList.delete(0);
        System.out.println("Deleted, the list is: " + tempFirstList.toString());

        for (int i = 0; i < 5; i++) {
            tempFirstList.delete(0);
            System.out.println("Looped delete, the list is: " + tempFirstList.toString());
        }
    }
}


在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/7654.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【51单片机】:LED任务及汇编解释任务

学习目标&#xff1a; 1、用汇编或者c语言实现D1 D3 D5 D7 为一组 &#xff1b;D2 D4 D6 D8 为一组 &#xff0c;两组实现 1&#xff09;一组亮约一秒 另一组灭一秒&#xff0c;这样的互闪现象五次后 25分 2&#xff09;所有灯灭约一秒后&#xff0c; …

关于ChatGPT的一些随笔

大家好&#xff0c;我是老三&#xff0c;最近几个月关于ChatGPT的信息可以说是铺天盖地。 “王炸&#xff0c;ChatGPT……” “xxx震撼发布……” “真的要失业了&#xff0c;xxx来袭……” “普通如何利用ChatGPT……” …… 不过老三前一阵比较忙&#xff0c;对ChatGPT…

ElasticSearch简介

第一章 ElasticSearch简介 1.1 什么是ElasticSearch Elaticsearch&#xff0c;简称为es&#xff0c; es是一个开源的高扩展的分布式全文检索引擎&#xff0c;它可以近乎实时的存储、检索数据&#xff1b;本身扩展性很好&#xff0c;可以扩展到上百台服务器&#xff0c;处理PB…

【数据结构与算法】树与二叉树

目录一.树1.树的定义2.结点的分类与关系3.树的相关概念4.树的表示方法二.二叉树1.二叉树的定义2.特殊二叉树3.二叉树的性质4.二叉树的顺序结构5.二叉树的链式结构(1)链式结构的创建(2)结点的创建(3)二叉树的手动构建(4)前中后序遍历(5)二叉树结点个数(6)二叉树的高度(7)第k层的…

Docker目录迁移

介绍 在docker的使用中随着下载镜像越来越多&#xff0c;构建镜像、运行容器越来越多, 数据目录必然会逐渐增大&#xff1b;当所有docker镜像、容器对磁盘的使用达到上限时&#xff0c;就需要对数据目录进行迁移。 如何避免&#xff1a; 1.在安装前对/var/lib/docker&#x…

如何3步精读《PMBOK指南》(含PMP备考资料)

初学者学习《PMBOK指南》的确有点吃亏&#xff0c;比不得那些项目管理专业以及相关专业的毕业生&#xff0c;哪怕只稍微接触过项目的都比初学者强。 所以&#xff0c;有计划性的阅读就显得尤为重要&#xff0c;要克服的不仅是阅读上的枯燥&#xff0c;还有专业知识的理解&…

Java——JDK动态代理

1.动态代理 1.1什么是动态代理&#xff1f; 动态代理(理解) 基于反射机制 举个例子&#xff0c;生活中一般在打官司的时候都会请代理律师&#xff0c;为什么要请律师呢&#xff1f;是因为开庭的时候大部人对于打官司没有经验&#xff0c;只会说出自己案件的陈述&#xff0c;并不…

软硬皆施,WMS仓库管理系统+PDA,实现效率狂飙

人工经验Excel表格&#xff0c;是传统第三方仓储企业常用的管理模式。在这种管理模式下&#xff0c;对仓库员工的Excel操作能力、业务经验和工作素养要求极高。一旦员工的经验能力不足&#xff0c;就会导致仓库业务运行不顺畅&#xff0c;效率低下&#xff0c;而员工也会因长时…

【MySQL】基于GTID的半同步主从复制(实践)

一、GTID简介 什么是GTID? 全局事务标识符GTID的全称为Global Transaction Identifier&#xff0c;是在整个复制环境中对一个事务的唯一标识。 它是MySQL 5.6加入的一个强大特性&#xff0c;目的在于能够实现主从自动定位和切换&#xff0c;而不像以前需要指定文件和位置。 …

ArduPilot飞控之DIY-F450计划

ArduPilot飞控之DIY-F450计划1. 历史2. 源由3. 计划3.1 硬件3.2 软件4. 动手4.1 接线4.1.1 ELRS nano接收机4.1.2 BN880 GPS模块4.1.3 Radio Telemetry4.2 配置4.2.1 选择四轴机型4.2.2 电源参数调整4.2.3 校准加速度计4.2.4 校准磁力计4.2.5 遥控器校准4.2.6 电机设置4.2.7 电…

企业电子招投标采购系统——功能模块功能描述+数字化采购管理 采购招投标

​ 功能模块&#xff1a; 待办消息&#xff0c;招标公告&#xff0c;中标公告&#xff0c;信息发布 描述&#xff1a; 全过程数字化采购管理&#xff0c;打造从供应商管理到采购招投标、采购合同、采购执行的全过程数字化管理。通供应商门户具备内外协同的能力&#xff0c;为外…

Melis4.0[D1s]:4.测试笔记 - 内嵌的显示命令

文章目录1.配置将显示测试源码包含进工程&#xff08;默认是包含了&#xff09;2.不要启动melis桌面系统3.开始测试3.1 disp 命令3.1.1 disp不带参数时&#xff0c;打印显示信息&#xff1a;3.1.2 disp -c 0 8 测试4种颜色3.2 disp_layer_cfg 命令3.3 disp_mem 对显示内存写入内…

全球自动驾驶竞争力最新排行榜,4家中国企业上榜

发展至今&#xff0c;自动驾驶技术不仅是汽车行业的一个主战场&#xff0c;更是全球科技领域中备受关注和充满竞争的一个重要领域。近年来&#xff0c;各大汽车制造商和科技公司都在投入大量财力物力人力进行自动驾驶技术的研发&#xff0c;并进一步争夺市场份额。 当然&#…

人工智能前沿——「小海带」超全视觉注意力机制资源分享(附下载链接)

&#x1f4da;&#x1f4da; 人工智能 | 计算机视觉 —— 致力于目标检测领域科研Tricks改进与推荐 | 主要包括主干网络改进、轻量化网络、注意力机制、检测头部改进、空间金字塔池化、损失函数及NMS改进、ICCV/CVPR/ECCV视觉顶会创新点改进、各类数据集资源分享以及算法训练相…

智云通CRM:如何给客户创造尽可能安全的成交环境?

销售人员要想和客户顺利成交&#xff0c;给对方创造尽可能安全的成交环境尤为重要。销售人员的每一句话和每一个观点&#xff0c;应当都是客户所想的。客户是非常聪明的&#xff0c;有任何风吹草动&#xff0c;他们都会提高警惕&#xff0c;这就是客户跟销售人员之间关系的现状…

笔记:关于使用vitepress 制作静态站点并托管到gitee

笔记&#xff1a;关于使用vitepress 制作静态站点并托管到giteegiteejcLee95&#xff1a;https://blog.csdn.net/qq_28550263?spm1001.2101.3001.5343 邮箱 &#xff1a;291148484163.com 本文地址&#xff1a;https://blog.csdn.net/qq_28550263/article/details/129419979…

spring5(五):AOP操作

spring5&#xff08;五&#xff09;&#xff1a;AOP操作前言一、代理模式1、场景模拟2、代理模式2.1 概念2.2 静态代理2.3 动态代理二、AOP概述1、什么是 AOP?2、相关术语3、作用三、AOP底层原理1、AOP 底层使用动态代理2、AOP&#xff08;JDK 动态代理&#xff09;2.1 编写 J…

二分查找(二)

2.练习题 3&#xff09; 力扣https://leetcode.cn/problems/search-in-rotated-sorted-array-ii/这题需要分三种情况&#xff0c;第一种是区间有序&#xff0c;正常二分查找&#xff0c;第二种是区间 被旋转&#xff0c;左区间的值大于右区间&#xff0c;需要比较目标值和左区…

机器视觉行业公司2023今年最大的特点-老员工真香现象出现,事出反常必有妖

老员工真香现象出现&#xff0c;事出反常必有妖。-老人涨薪&#xff0c;新人不招或降薪&#xff0c;工作2-3年最值得跳槽&#xff0c;培训后好找工作。 你可能看到以上现象很反感&#xff0c;但是现在机器视觉行业环境就是这个样子的。 今年机器视觉行业最大的特点就是项目技术…

Atlassian Server用户新选择 | 迁移到数据中心版前,您需要做这些准备(2)

2024年2月&#xff0c;也就是一年不到&#xff0c;Atlassian将终止对Server产品及插件的所有支持。 此公告发布后&#xff0c;许多用户需要了解怎样的前进方向才是最适合企业的。为此&#xff0c;Atlassian不仅提供云版&#xff0c;还提供了本地部署的数据中心&#xff08;Data…