Object修改链表
以前学习过链表的时候由于类型的接收不同,每次要重写链表
下面修改可用链表
- class Link{
- private class Node{
- private Object data ;
- private Node next ;
- public Node (Object data){
- this.data = data ;
- }
- public void add(Node newNode){
- if(this.next == null)
- this.next = newNode;
- else{
- this.next.add(newNode) ;
- }
- }
- public void print(){
- System.out.println(this.data) ;
- if(this.next == null)
- return ;
- else {
- this.next.print() ;
- }
- }
- public boolean containsNode(Object data){
- if(data.equals(this.data))
- return true;
- else {
- if(this.next != null)
- return this.next.containsNode(data);
- else
- return false ;
- }
- }
- public Object getNode(int index){
- if(Link.this.foot ++ == index){
- return this.data ;
- } else {
- return this.next.getNode(index) ;
- }
- }
- public void setNode(int index ,Object data){
- if(Link.this.foot ++ == index){
- this.data = data ;
- }
- else{
- this.next.setNode(index,data) ;
- }
- }
- public void removeNode(Node previous ,Object data) {
- if(data.equals(this.data)){
- previous.next = this.next ;
- } else {
- this.next.removeNode(this,data) ;
- }
- }
- public void toArrayNode(){
- Link.this.retArray[Link.this.foot ++ ] = this.data ;
- if(this.next != null)
- this.next.toArrayNode() ;
- }
- }
- private Node root ;
- private int count = 0 ;
- private int foot = 0 ;
- private Object [] retArray ;
- public void add(Object data){
- if(data == null)
- return ;
- Node newNode = new Node(data) ;
- if (root == null)
- root = newNode;
- else {
- this.root.add(newNode) ;
- }
- this.count ++ ;
- }
- public void print(){
- this.foot = 0 ;
- if(root == null)
- return ;
- else{
- root.print() ;
- }
- }
- public int size() {
- return this.count ;
- }
- public boolean isEmpty(){
- return this.count == 0 ;
- }
- public boolean contains(Object data) {
- if(this.root == null || data == null)
- return false ;
- else {
- return this.root.containsNode(data) ;
- }
- }
- public Object get(int index){
- if(index > this.count)
- return null ;
- else{
- return this.root.getNode(index) ;
- }
- }
- public void set(int index , Object data) {
- if(index > this.count)
- return ;
- else{
- this.foot = 0 ;
- this.root.setNode(index,data) ;
- }
- }
- public void remove(Object data){
- if(this.contains(data)){
- if(data.equals(this.root.data))
- this.root = this.root.next ;
- else{
- this.root.next.removeNode(root,data) ;
- }
- }
- }
- public Object [] toArray(){
- if(this.root == null){
- return null ;
- } else {
- this.foot = 0 ;
- this.retArray = new Object[this.count] ;
- this.root.toArrayNode() ;
- return this.retArray ;
- }
- }
- }
- public class Link1{
- public static void main(String args[]){
- Link all = new Link() ;
- all.add("A") ; //String转为Object
- all.add("B") ;
- all.add("C") ;
- all.remove("A") ;//String已经覆写了equals()方法
- Object [] data = all.toArray();
- for(int x = 0 ; x < data.length ; x ++){
- String str = (String)data[x] ; //每一个对象向下转型 Object变为String
- System.out.println(str) ;
- }
- }
- }
总结:
Object类对象可以接受一切数据类型,解决了数据统一问题
Object修改链表的更多相关文章
- CLRS10.2-4练习 - 修改链表查询方法
要求: As written, each loop iteration in the LIST-SEARCH' procedure requires two tests:one for x ≠ L.n ...
- JAVA 链表操作:循环链表
主要分析示例: 一.循环链表简述 二.单链表循环链表 三.双链表循环链表 一.循环链表简述 循环链表即链表形成了一个循环的结构,尾节点不再指向NULL,而是指向头节点HEAD,此时判定链表的结束是尾节 ...
- JAVA 链表操作:单链表和双链表
主要讲述几点: 一.链表的简介 二.链表实现原理和必要性 三.单链表示例 四.双链表示例 一.链表的简介 链表是一种比较常用的数据结构,链表虽然保存比较复杂,但是在查询时候比较便捷,在多种计算机语言都 ...
- 单链表的python实现
首先说下线性表,线性表是一种最基本,最简单的数据结构,通俗点讲就是一维的存储数据的结构. 线性表分为顺序表和链接表: 顺序表示指的是用一组地址连续的存储单元依次存储线性表的数据元素,称为线性表的顺序存 ...
- Java链表讲解
主要讲述几点: 一.链表的简介 二.链表实现原理和必要性 三.单链表示例 四.双链表示例 一.链表的简介 链表是一种比较常用的数据结构,链表虽然保存比较复杂,但是在查询时候比较便捷,在多种计算机语言都 ...
- Python3玩转单链表——逆转单向链表pythonic版
[本文出自天外归云的博客园] 链表是由节点构成的,一个指针代表一个方向,如果一个构成链表的节点都只包含一个指针,那么这个链表就是单向链表. 单向链表中的节点不光有代表方向的指针变量,也有值变量.所以我 ...
- java对单向单向链表的操作
概述:众所周知,数据对于数据的存储时连续的,也就是说在计算机的内存中是一个整体的.连续的.不间断的ADT数据结构.伴随的问题也会随之出现,这样其实对于内存的动态分配是不灵活的.而链表具备这个优点.因此 ...
- Java链表设计
链表 1,链表的实现 在实际开发之中对象数组是一项非常实用的技术,并且利用其可以描述出“多”方的概念,例如:一个人有多本书,则在人的类里面一定要提供有一个对象数组保存书的信息,但是传统的对象数组依赖于 ...
- 由反转链表想到python链式交换变量
这两天在刷题,看到链表的反转,在翻解体思路时看到有位同学写出循环中一句搞定三个变量的交换时觉得挺6的,一般用的时候都是两个变量交换(a,b=b,a),这种三个变量的交换还真不敢随便用,而且这三个变量都 ...
随机推荐
- C++:预处理指令
Preprocessor directives 预处理器指令 预处理器指令是指那些包含在我们代码中的预处理器语句行,这些预处理器语句不是真正的代码语句,但是他们指导程序如何进行编译.这些语句总是以 ‘ ...
- Android --- 读取系统资源函数getResources()小结
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1201/655.html 编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用, ...
- BootStrap详解之(一)
一.BootStrap简介 BootStrap是一个用来构建网站前段框架的一个插件.无论你是想构建应用程序.博客还是CMS网站,Bootstrap都特别的使用,只要你想得到,它就能行.Bootstra ...
- 使用curl获取乱码问题
今天通过curl获取百度地图接口数据,获取到居然是乱码,于是我查看是不是编码问题,发现返回的编码和自己的编码都是utf-8, 继续找原因,发现header报文中 Content-encoding 为 ...
- java的property
System.currentTimeMillis() 返回以毫秒为单位的当前时间.System.gc() 垃圾回收System.getProperties().返回当前的系统属性System.getP ...
- Socket在手机上的应用
usb读取:pid vid --可以唯一的确定设备获取手机驱动socket固定端口通信 wifipc机在局域网内,udp的数据包(整个网段) 蓝牙配对 bluetoothsocket 如果放大:可以分 ...
- Binary Watch
Binary Watch 描述 Consider a binary watch with 5 binary digits to display hours (00 - 23) and 6 binary ...
- PAT (Advanced Level) 1073. Scientific Notation (20)
简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- codeforces 665C Simple Strings
相同的一段字母变一下就可以. #include<cstdio> #include<cstring> #include<cmath> #include<vect ...
- mysql 本地操作
先把数据库传到根目录 再直接导入 没有50M限制root@b101 [/home/user/www]# mysql -uusername_007li -ppasswd -D dbname_li12d ...