您的位置:首页 > 其它

单链表的初步实现---(转置,删除,替换,查找,添加)

2016-08-22 23:39 525 查看
class Link{                                     //定义链表
private class Node{                         //定义保持的节点
private Object data;                    //要保持的数据,可以用泛型替代
private Node next;                      //保持下一个节点信息
public Node(Object data){
this.data = data;
}
public void addNode(Node newNode) {
if(this.next == null){              //如果下一个节点为空,则保持节点,如果不为空,则找到空为止
this.next = newNode;
}else{
this.next.addNode(newNode);
}
}
public Object getNode(int index) {
if(Link.this.flag ++ == index){     //从第一个下标数据开始查询,如果和查询的索引为止一样,则返回当前数据,找不到则继续往后查询
return this.data;
}else{
return this.next.getNode(index);
}
}
public void setNode(int index,Object data) {
if(Link.this.flag ++ == index){     //  和get方法是一样的,找到和索引相等位置的数据则修改数据
this.data = data;
}else{
this.next.setNode(index,data);
}
}
public boolean containsNode(Object data) {
if(this.data.equals(data)){         //  从第一个数据开始比对,如果比对到,则返回数据,如果下一个节点还存在则继续比对,如果下个节点不存在则返回false,找到返回true
return true;
}else{
if(this.next!=null){
return this.next.containsNode(data);
}
}
return false;

}
public void removeNode(Node node, Object data) {
if(this.data.equals(data)){         //找到要删除的数据的节点,比如  1-->2-->3    2 节点找到,则把指向改为1-->3
node.next = this.next;
}else{
this.next.removeNode(this, data);   //没找到数据则继续往后查询,this表示当前对象  第一次:this.root.next 第二次: this.root.next.next ...
}
}
public void toArrayNode() {
Link.this.retArray[Link.this.flag++] = this.data ;  //数组保持数据
if(this.next !=null){
this.next.toArrayNode();
}
}
public Node transposeNode(Node node) {      //链表转置   将1-->2  2--->3  变更为 2-->1  3--->2
Node first =node;
Node second = node.next;                //为转置做准备
Node third = null;
while(second!=null){
third = second.next ;               //保持2-->3的节点信息
second.next = first;                //将1--->2 变成2--->3
first = second;                     //为下一次替换做准备
second = third;
}
node.next = null;
node = first;
return node;
}
}
private Node root = null;                       //定义根节点
private int count = 0 ;                         //统计数量
private int flag = 0;                           //定义下标
private Object[]  retArray;                     //保持数据
public void add(Object data){                   //添加数据
if(data == null){
return ;
}
Node newNode = new Node(data);
if(this.root == null){
this.root = newNode;
}else{
this.root.addNode(newNode);
}
this.count++;
}
public boolean isEmpty(){                       //判断是否空
return this.count==0 && this.root==null;
}
public int size(){                              //取得链表长度
return this.count;
}
public Object get(int index){                   //取得指定索引的数据
if(this.isEmpty()){
System.out.println("链表为空,无法取值。");
return null;
}
if(index >= this.count){
System.out.println("输入的索引位置超过链表长度,请重新输入。");
return null;
}
this.flag = 0;
return this.root.getNode(index);
}
public void set(int index,Object data){         //将指定索引的数据变更
if(this.isEmpty()){
System.out.println("链表为空,无法取值。");
return ;
}
if(index >= this.count){
System.out.println("输入的索引位置超过链表长度,请重新输入。");
return ;
}
this.flag = 0;
this.root.setNode(index,data);
}
public boolean contains(Object data){           //查找数据是否存在
if(data == null){
System.out.println("输入的数据为空,无法查找。");
return false;
}
if(this.isEmpty()){
System.out.println("链表为空。");
return false;
}
return this.root.containsNode(data);
}
public boolean remove(Object data){             //删除数据
if(!(this.contains(data))){
System.out.println("要删除的数据不存在。");//删除前先确认下数据是否存在
return false;
}
if(this.root.data.equals(data)){
this.root = this.root.next;
}else{
this.root.next.removeNode(this.root,data);
}
this.count--;
return true;
}
public Object[] toArray(){                      //保存数据
if(this.isEmpty()){
System.out.println("链表为空。");
return null;
}
this.retArray = new Object[this.count];
this.flag = 0;
this.root.toArrayNode();
return retArray;
}
public void transpose(){                        //链表转置
if(this.isEmpty()){
System.out.println("链表为空。");
return ;
}
this.root = (this.root.transposeNode(this.root));
}
}
public class Test{                              //定义测试类
public static void main(String args[]){
Link all = new Link();
all.get(1);
all.add("数据A");
all.add("数据B");
all.add("数据C");
all.add("数据D");
all.add("数据E");
System.out.println("链表是否为空:"+all.isEmpty()+"链表的长度:"+all.size());
System.out.println("第三个数据是:"+all.get(2));
all.set(2, "数据修改");
System.out.println("修改后的数据是:"+all.get(2));
all.remove("数据修改");
System.out.println("查询数据是否存在:"+all.contains("数据修改"));
Object[] obj = all.toArray();
for (int i=0; i<obj.length;i++){
String str = (String)obj[i];
System.out.println(str);
}
all.transpose();
System.out.println("链表是否为空:"+all.isEmpty()+"链表的长度:"+all.size());
Object[] obj1 = all.toArray();
for (int i=0; i<obj1.length;i++){
String str = (String)obj1[i];
System.out.println(str);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  链表 单链表
相关文章推荐