链表的实现
来源:互联网 发布:java接口特点是什么 编辑:程序博客网 时间:2024/06/01 03:58
//接口
package myLinkedlist;
package myLinkedlist;
import java.util.LinkedList;
public interface Mylist<E> {
public boolean add(E e);
public boolean add(int index,E data);
/**判断是否包含
*
* @author l
* @param o
* @return
*/
boolean contains(E e);
/**移除元素
*
* @author l
* @param o
* @return
*/
public E remove(int index);
public boolean removeAll(E data);
/** 判断表是否为空
*
* @author l
* @return
*/
boolean isEmpty();
int length();
/**清楚表
*
* @author l
* @return
*/
boolean clear();
/**
* 表的大小
* @author l
* @return
*/
}
public boolean add(E e);
public boolean add(int index,E data);
/**判断是否包含
*
* @author l
* @param o
* @return
*/
boolean contains(E e);
/**移除元素
*
* @author l
* @param o
* @return
*/
public E remove(int index);
public boolean removeAll(E data);
/** 判断表是否为空
*
* @author l
* @return
*/
boolean isEmpty();
int length();
/**清楚表
*
* @author l
* @return
*/
boolean clear();
/**
* 表的大小
* @author l
* @return
*/
}
//********************************************************//节点类
package myLinkedlist;
/**
* 节点类
* @author lu
*
* @param <T>
*/
public class Node<T> {
public T data;
public Node<T> next;
public Node(T data){
this.data=data;
}
public Node(T data, Node<T> next){
this.data=data;
this.next=next;
}
}
package myLinkedlist;
/**
* 节点类
* @author lu
*
* @param <T>
*/
public class Node<T> {
public T data;
public Node<T> next;
public Node(T data){
this.data=data;
}
public Node(T data, Node<T> next){
this.data=data;
this.next=next;
}
}
//********************************************
//实现代码
//去年考研把数据结构理论算是学完了,有一定理解,但是不动手敲一下总是感觉缺少点什么,下面直接代码实现了,在有理论基础下可以很容易看懂,没有看理论的画可能比
//实现代码
//去年考研把数据结构理论算是学完了,有一定理解,但是不动手敲一下总是感觉缺少点什么,下面直接代码实现了,在有理论基础下可以很容易看懂,没有看理论的画可能比
//较苦难,主是比较少见谅,以后有时间还会整理。一起加油。
package myLinkedlist;
package myLinkedlist;
public class MyLinkedList<E> implements Mylist<E> {
protected Node<E> headNode;
protected Node<E> rear;
/**
* 初始化头结点
* 构造方法
*/
public MyLinkedList(){
headNode=new Node<E>(null);
}
/**
* 构造方法
* 初始化头结点
*/
public MyLinkedList(Node<E> head){
this();
this.headNode.next=this.rear.next=head;
rear=rear.next;
}
/**
* 构造方法
* 参数是数组
*/
public MyLinkedList(E[] a){
this();
if (a!=null&&a.length>0) {
this.headNode.next=new Node<E>(a[0]);
rear=this.headNode.next;
int i=1;
while (i<a.length) {
rear.next=new Node<E>(a[i++]);
rear=rear.next;
}
}
}
/**
* 构造方法
* 参数为链表
*/
public MyLinkedList(MyLinkedList<E> list) {
this();
if (list!=null&&list.headNode.next!=null) {
this.headNode.next=new Node<E>(list.headNode.next.data) ;
Node<E> p=list.headNode.next;
rear=list.headNode.next;
while (p!=null) {
rear.next=new Node<E>(p.next.data) ;
rear=rear.next;
p=p.next;
protected Node<E> headNode;
protected Node<E> rear;
/**
* 初始化头结点
* 构造方法
*/
public MyLinkedList(){
headNode=new Node<E>(null);
}
/**
* 构造方法
* 初始化头结点
*/
public MyLinkedList(Node<E> head){
this();
this.headNode.next=this.rear.next=head;
rear=rear.next;
}
/**
* 构造方法
* 参数是数组
*/
public MyLinkedList(E[] a){
this();
if (a!=null&&a.length>0) {
this.headNode.next=new Node<E>(a[0]);
rear=this.headNode.next;
int i=1;
while (i<a.length) {
rear.next=new Node<E>(a[i++]);
rear=rear.next;
}
}
}
/**
* 构造方法
* 参数为链表
*/
public MyLinkedList(MyLinkedList<E> list) {
this();
if (list!=null&&list.headNode.next!=null) {
this.headNode.next=new Node<E>(list.headNode.next.data) ;
Node<E> p=list.headNode.next;
rear=list.headNode.next;
while (p!=null) {
rear.next=new Node<E>(p.next.data) ;
rear=rear.next;
p=p.next;
}
}
}
/**
*
*/
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return this.headNode==null;
}
/**
*
*/
@Override
public int length() {
// TODO Auto-generated method stub
int count=0;
Node<E> p=this.headNode.next;
while (p!=null) {
count++;
p=p.next;
}
return count;
}
/**
* 获取指定位子数据
*/
public E get(int index){
if (index>0) {
}
}
/**
*
*/
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return this.headNode==null;
}
/**
*
*/
@Override
public int length() {
// TODO Auto-generated method stub
int count=0;
Node<E> p=this.headNode.next;
while (p!=null) {
count++;
p=p.next;
}
return count;
}
/**
* 获取指定位子数据
*/
public E get(int index){
if (index>0) {
int count=0;
Node<E> p=headNode.next;
while (p!=null&&count<index) {
count++;
p=p.next;
}
if (p!=null) {
return p.data;
}
}
return null;
}
/**
*设置指定位子的数据
*改变,不是增加
*/
public E set (int index,E data){
if (index>0&&data!=null) {
Node<E> p=headNode.next;
int count=0;
while (p!=null&&count<index) {
count++;
p=p.next;
}
if (p!=null) {
E oldData=p.data;
p.data=data;
return oldData;
}
}
return null;
return null;
}
/**
*设置指定位子的数据
*改变,不是增加
*/
public E set (int index,E data){
if (index>0&&data!=null) {
Node<E> p=headNode.next;
int count=0;
while (p!=null&&count<index) {
count++;
p=p.next;
}
if (p!=null) {
E oldData=p.data;
p.data=data;
return oldData;
}
}
return null;
}
/**
* 在末尾结点添加数据
*/
@Override
public boolean add(E data) {
if (data!=null) {
rear.next=new Node<E>(data);
rear=rear.next;
return true;
}
return false;
}
/**
* 在指定位置添加数据。
* 添加,不会覆盖原有数据
*/
@Override
public boolean add(int index, E data) {
if (index>0&&data!=null) {
int count=1;
Node<E> p=headNode.next;
while (p!=null&&count<index) {
count++;
p=p.next;
}
if (p!=null) {
Node<E> aNode=p.next;
p.next=new Node<E>(data, aNode);
if (p.next==this.rear) {
this.rear=p.next;
}
return true;
}
}
return false;
}
/**
* 在末尾结点添加数据
*/
@Override
public boolean add(E data) {
if (data!=null) {
rear.next=new Node<E>(data);
rear=rear.next;
return true;
}
return false;
}
/**
* 在指定位置添加数据。
* 添加,不会覆盖原有数据
*/
@Override
public boolean add(int index, E data) {
if (index>0&&data!=null) {
int count=1;
Node<E> p=headNode.next;
while (p!=null&&count<index) {
count++;
p=p.next;
}
if (p!=null) {
Node<E> aNode=p.next;
p.next=new Node<E>(data, aNode);
if (p.next==this.rear) {
this.rear=p.next;
}
return true;
}
}
return false;
}
/**
*移除指定位子的数据
*/
@Override
public E remove(int index) {
E oldData=null;
if (index>0&&headNode!=null) {
int count=0;
Node<E> p=headNode.next;
while (p!=null&&count<index) {
count++;
p=p.next;
}
Node<E> a=p.next;
if (a!=null) {
oldData=a.data;
if (this.rear==a) {
this.rear=p;
p.next=null;
}
p.next=a.next;
}
*移除指定位子的数据
*/
@Override
public E remove(int index) {
E oldData=null;
if (index>0&&headNode!=null) {
int count=0;
Node<E> p=headNode.next;
while (p!=null&&count<index) {
count++;
p=p.next;
}
Node<E> a=p.next;
if (a!=null) {
oldData=a.data;
if (this.rear==a) {
this.rear=p;
p.next=null;
}
p.next=a.next;
}
}
return oldData;
}
/**
* 移除链表中一个值
*/
@Override
public boolean removeAll(E data) {
boolean isRemoveAll=false;
Node<E> front=this.headNode;
Node<E> pre=front.next;
while (pre!=null) {
if (pre.data.equals(data)) {
if (pre==this.rear) {
rear=front;
rear.next=null;
}
front.next=pre.next;
pre=pre.next;
isRemoveAll=true;
} else {
front=pre;
pre=pre.next;
}
}
return isRemoveAll;
}
/**
* 清空链表
*/
@Override
public boolean clear() {
this.headNode.next=null;
this.rear=this.headNode;
return true;
}
/**
* 是否包含
*/
@Override
public boolean contains(E data) {
if (data!=null) {
Node<E> p=headNode.next;
while (p!=null) {
if (p.data.equals(data)) {
return true;
}
p=p.next;
}
}
return false;
}
/**
* 拼接链表
*/
public void concat(MyLinkedList<E> list){
if (this.headNode==null) {
this.headNode.next=list.headNode.next;
this.rear=list.rear;
}else {
this.rear=list.headNode.next;
this.rear=list.rear;
}
}
/**
* 转化成String类型
*/
public String toString(){
Node<E> p=headNode.next;
String string="";
while(p!=null) {
string=string+ p.data;
p=p.next;
if (p!=null) {
string+=",";
}
}
return string;
}
}
阅读全文
0 0
- 链表的实现
- 链表的实现
- 链表的 实现
- 链表的实现
- 链表的实现
- 链表的实现
- 链表的实现
- 链表的实现
- 链表的实现
- 链表的实现
- 链表的实现
- 链表的实现
- 链表的实现
- 链表的实现
- 链表的实现
- 链表的实现
- 链表的实现
- 链表的实现
- Hadoop回顾:(二)搭建5节点hadoop集群环境(CDH5)
- ubuntu安装python3.5
- Android移动开发者必须知道的Deep Linking技术
- Unity3D-接入谷歌的Firebase(iOS,Mac端)
- rosdep update出错:error loading sources list:the read operation timed out
- 链表的实现
- jquery中ajax使用error调试错误的方法
- 可动态绘制PATH的VIEW,会动态显示整个绘制过程
- HTTPS笔记
- 详细介绍VO和PO的区别
- C# 泛型的协变和逆变
- 获取指定(当前)月的天数。时间工具类 DateUtil
- CSS3 box-sizing 属性
- LeetCode 21.Merge Two Sorted Lists