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),这种三个变量的交换还真不敢随便用,而且这三个变量都 ...
随机推荐
- 微软企业库3.1DIY编译使用(数据库连接符写在企业库DLL里)
1.在winform项目app.config文件中去掉"PublicKeyToken=b03f5f7f11d50a3a"(不然无法加载使用新编译的企业库DLL文件) 2.在企业库所 ...
- android:contentDescription的作用是什么
在写Android的XML布局文件时,在ImageView或ImageButton中经常会碰到一个提示: Missing contentDescription attribute on image. ...
- PAT (Advanced Level) 1110. Complete Binary Tree (25)
判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...
- 第11章 类的高级特性--final
1.final变量 (1)final关键字可用于变量声明,一旦该变量被设定,就不可以再改变该变量的值.通常,由final定义的变量为常量.例如:final double PI=3.14; final关 ...
- 剑指offer 栈的压入弹出 顺序
判断: 如果下一个弹出的数字刚好是栈顶元素,那么直接弹出 如果下一个弹出的数字不在栈顶,我们要把压栈序列中,还没有入栈的数字压入辅助栈,知道把下一个需要弹出的数字压入栈顶 如果所有的数字都入栈,但是仍 ...
- JSP内置对象--out对象(了解即可)
out对象是javax.servlet.jsp.JspWriter类的实例化对象,主要功能就是完成页面的输出操作,使用println()或print()输出.但是使用纪律很少,都会使用表达式完成输出的 ...
- Integer自动装箱拆箱bug,创建对象在-128到127
1 public class Demo3 { public static void main(String[] args) { Integer a = 1; Integer b = 2; Intege ...
- C#入门经典第六章函数-2-委托
委托:
- Android PopupWindow的使用技巧(转)
Android PopupWindow的使用技巧 PopupWindow是Android上自定义弹出窗口,使用起来很方便. PopupWindow的构造函数为 public PopupWindow(V ...
- tinkphp5.0 traits 的引入
Traits引入 ThinkPHP 5.0开始采用trait功能(PHP5.4+)来作为一种扩展机制,可以方便的实现一个类库的多继承问题. trait是一种为类似 PHP 的单继承语言而准备的代码复用 ...