最近开始在学习数据结构,今天重新写了一次链表的,感觉又有些收获,先写下来啦。
这一次首先是写了个接口,再用一个类来实现对链表的操作。
节点的
public class Node {
private Node head;
private Node next;
private String data;
private Node newEntry;
/**
* 重写构造函数,在添加节点时用
* @param newEntry
*/
public Node(Node newEntry){
this.newEntry=newEntry;
}
/**
* 系统默认的构造函数
*/
public Node(){ }
/**
* 存储当前节点的方法
* @param next
*/
public void setNext(Node next){
this.next=next;
}
/**
* 获取当前节点的方法
* @return Next
*/
public Node getNext(){
return next;
}
/**
* 存储节点的数据的方法
* @param data节点的数据
*/
public void setData(String data){
this.data=data;
}
/**
* 获取节点数据的方法
* @return data节点的数据
*/
public String getData(){
return data;
}
接口的代码~
/**
* 定义一个接口,用来规范链表的操作方法
* @author Lucia 2013.4.7
*
*/
public interface Operation {
/**
* Task:在链表中添加元素,默认添加在最后一个
* @param newEntry新加入的元素
*/
public void add(Node newEntry);
/**
* Task:在链表的任意位置加入元素
* @param newEntry加入元素
* @param position加入元素要插入的位置
*/
public void add(Node newEntry,int position);
/**
* Task:删除链表中任意位置的元素
* @param position传入指定位置
* @return Node返回删除的节点
*/
public Node remove(int position);
/**
* Task:查找表中temp节点的位置
* @param 要查找的节点temp
* @return int temp节点的位置
*/
public String select(Node temp);
/**
* 清除所有的元素
*/
public void clear();
}
队尾添加
/**
* 实现在链尾添加元素的方法
*/
public void add(Node newEntry) {
Node node1 = new Node(newEntry);
if(head!=null){
last.setNext(node1);
last=last.getNext();
}
else{
head=newEntry;
last=head;
}
count++;
}
任意添加(此处有些问题,正在改进中,不是每次添加都是对的)
/**
* 实现在任意位置添加元素的方法
*/
public void add(Node newEntry, int position) {
Node newNode=new Node(newEntry);
Node temp=new Node();
if(count>=position&&position!=0){
temp=head;
//找到所找节点的上一个节点
for(int i=0;i<position-1;i++){
temp=temp.getNext();
}
newNode.setNext(temp.getNext());
temp.setNext(null);
temp.setNext(newEntry);
count++;
}
else if(position==0){
temp.setNext(head.getNext());
newNode.setNext(temp);
}
else if(count<position){
System.out.println("您所输入的序号超出链长,输入错误!");
}
}
删除元素的方法,一开始是没有把删除头节点的特殊情况单独例出来,就有的情况不对,后来单独写了头节点的情况后,就OK啦~
/**
* 实现删除元素的方法
*/
public Node remove(int position) {
//分三种情况,删除中间元素
if(count>=position&&position!=0){
Node temp=new Node();
temp=head;
//找到所找节点的上一个节点
for(int i=0;i<position-1;i++){
temp=temp.getNext();
}
System.out.println("您所删除的元素是"+temp.getNext().getData());
temp.setNext(temp.getNext().getNext());
}
//删除第一个元素
else if(position==0){
System.out.println("您所删除的元素是"+head.getData());
head=head.getNext();
}
//删除超出范围
else if(count<position){
System.out.println("您所输入的序号超出链长,输入错误!");
}
return null;
}
查找元素的方法
/**
* 实现查找元素的方法
*/
public String select(Node temp) {
Node node = new Node();
node=head;
String data = null;
for(int i=0;i<count-1;i++){
if(node.getData().equals(temp.getData())){
data="要查找的数据是:"+node.getData()+",数据的序号是"+(i+1);
System.out.println(data);
break;
}
node=node.getNext();
}
return data;
}
打印链表的方法
/**
* 打印链表的方法
*/
public void print(){
if(head!=null){
Node temp = new Node();
temp=head;
for(int i=0;i<count;i++){
System.out.println("数据是"+temp.getData());
temp=temp.getNext();
}
}
else{
System.out.println("链表不存在!!!");
}
}
最终的输出是
这次的链表实现是没有专门设置为空的头节点的,打算之后再实现一下~