单链表-数据结构-java实现
2012-10-06 17:34
603 查看
单链表-数据结构-java实现
目录
抽象表:1
单链表实现:1
单链表输出测试:3
输出结果:4
publicinterface List
{
publicvoid insert(int i ,Object obj) throws Exception; //插入
public Object delete(int i ) throws Exception; //删除
public Object getData(int i ) throws Exception; //获取i元素
publicint size(); //表数据总数
publicboolean isEmpty(); //是否为空
}
public class Node
{
Object element;
Node next;
Node(Node nextval)
{
next = nextval;
}
public Node(Object obj,Node nextval)
{
element = obj;
next = nextval;
}
public Node getNext()
{
return next;
}
public void setNext(Node nextval)
{
next = nextval;
}
public Object getElement()
{
return element;
}
public void setElement(Object obj)
{
element = obj;
}
public String toString()
{
return element.toString();
}
}
package edu.cquptzx.List;
publicclass SingleLinkList implements List
{
Node head;
Node current;
intsize;
SingleLinkList()
{
head = current = new Node(null);
size =0;
}
/**
* 定位成员函数index(int i)的实现
* 循环从头开始查找,循环的条件是:1.定位完成j==i;2.链表查找结束了.
* @param i
* @throws Exception 当参数i错误时,抛出异常.
*/
publicvoid index(int i )throws Exception
{
if(i<-1 || i >size-1)
{
thrownew Exception("i error in INDEX.");
}
if(i == -1) return;
current = head.next;
int j = 0;
while(current!=null && j<i)
{
current = current.next;
j++;
}
}
/**
* 插入节点算法:
* 1.调用index(i-1),让成员变量current指向第i-1个节点.
* 2.以obj,current.next为参数创建新的节点.
* 3.更改current指向,改为下一个节点.
* 4.表元素总数加1.
*/
publicvoid insert(int i, Object obj) throws Exception {
if(i<0 || i>size)
{
thrownew Exception ("i error in INSERT.");
}
index(i-1);
current.setNext(new Node(obj,current.next));
size++;
}
/**
* 删除节点算法:
* 1.调用index(i-1),让成员变量current指向第i-1个节点.
* 2.把第i个节点脱链:让第i-1个节点的next域等于第i个节点的next域.
* 3.数据元素总数size减1.
*/
public Object delete(int i) throws Exception {
if(size == 0)
{
thrownew Exception ("Link Blank in DELETE.");
}
if(i<0 || i>size-1)
{
thrownew Exception ("i error in DELETE.");
}
index(i-1);
Object obj = current.next.getElement();
current.setNext(current.next.next);
size--;
return obj;
}
/**
* 获取指定的元素
* 1.调用index(i),让成员变量current指向第i个节点.
* 2.返回该节点的数据域的值.
*/
@Override
public Object getData(int i) throws Exception {
// TODO Auto-generated method stub
if(i<-1 || i>size-1)
{
thrownew Exception ("i error in getData.");
}
index(i);
returncurrent.getElement();
}
@Override
publicint size() {
// TODO Auto-generated method stub
returnsize;
}
@Override
publicboolean isEmpty() {
// TODO Auto-generated method stub
returnsize ==0;
}
}
publicclass SingleLinkListTest
{
publicstaticvoid main(String agrs[])
{
SingleLinkList linkList = new SingleLinkList();
int n = 10;
try
{
for(int i = 0;i<n;i++)
{
linkList.insert(i, new Integer(i+1));
}
linkList.delete(4);
for(int i = 0;i<linkList.size;i++)
{
System.out.print(linkList.getData(i)+" --> ");
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
目录
抽象表:1
单链表实现:1
单链表输出测试:3
输出结果:4
抽象表:
package edu.cquptzx.List;publicinterface List
{
publicvoid insert(int i ,Object obj) throws Exception; //插入
public Object delete(int i ) throws Exception; //删除
public Object getData(int i ) throws Exception; //获取i元素
publicint size(); //表数据总数
publicboolean isEmpty(); //是否为空
}
单链表实现:
package edu.cquptzx.List;public class Node
{
Object element;
Node next;
Node(Node nextval)
{
next = nextval;
}
public Node(Object obj,Node nextval)
{
element = obj;
next = nextval;
}
public Node getNext()
{
return next;
}
public void setNext(Node nextval)
{
next = nextval;
}
public Object getElement()
{
return element;
}
public void setElement(Object obj)
{
element = obj;
}
public String toString()
{
return element.toString();
}
}
package edu.cquptzx.List;
publicclass SingleLinkList implements List
{
Node head;
Node current;
intsize;
SingleLinkList()
{
head = current = new Node(null);
size =0;
}
/**
* 定位成员函数index(int i)的实现
* 循环从头开始查找,循环的条件是:1.定位完成j==i;2.链表查找结束了.
* @param i
* @throws Exception 当参数i错误时,抛出异常.
*/
publicvoid index(int i )throws Exception
{
if(i<-1 || i >size-1)
{
thrownew Exception("i error in INDEX.");
}
if(i == -1) return;
current = head.next;
int j = 0;
while(current!=null && j<i)
{
current = current.next;
j++;
}
}
/**
* 插入节点算法:
* 1.调用index(i-1),让成员变量current指向第i-1个节点.
* 2.以obj,current.next为参数创建新的节点.
* 3.更改current指向,改为下一个节点.
* 4.表元素总数加1.
*/
publicvoid insert(int i, Object obj) throws Exception {
if(i<0 || i>size)
{
thrownew Exception ("i error in INSERT.");
}
index(i-1);
current.setNext(new Node(obj,current.next));
size++;
}
/**
* 删除节点算法:
* 1.调用index(i-1),让成员变量current指向第i-1个节点.
* 2.把第i个节点脱链:让第i-1个节点的next域等于第i个节点的next域.
* 3.数据元素总数size减1.
*/
public Object delete(int i) throws Exception {
if(size == 0)
{
thrownew Exception ("Link Blank in DELETE.");
}
if(i<0 || i>size-1)
{
thrownew Exception ("i error in DELETE.");
}
index(i-1);
Object obj = current.next.getElement();
current.setNext(current.next.next);
size--;
return obj;
}
/**
* 获取指定的元素
* 1.调用index(i),让成员变量current指向第i个节点.
* 2.返回该节点的数据域的值.
*/
@Override
public Object getData(int i) throws Exception {
// TODO Auto-generated method stub
if(i<-1 || i>size-1)
{
thrownew Exception ("i error in getData.");
}
index(i);
returncurrent.getElement();
}
@Override
publicint size() {
// TODO Auto-generated method stub
returnsize;
}
@Override
publicboolean isEmpty() {
// TODO Auto-generated method stub
returnsize ==0;
}
}
单链表输出测试:
package edu.cquptzx.List;publicclass SingleLinkListTest
{
publicstaticvoid main(String agrs[])
{
SingleLinkList linkList = new SingleLinkList();
int n = 10;
try
{
for(int i = 0;i<n;i++)
{
linkList.insert(i, new Integer(i+1));
}
linkList.delete(4);
for(int i = 0;i<linkList.size;i++)
{
System.out.print(linkList.getData(i)+" --> ");
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
输出结果:
相关文章推荐
- 用Java实现单链表数据结构
- 数据结构专题——线性表之单链表及其Java实现
- 数据结构:线性表的链式存储(单向链表)--Java实现
- 数据结构-Java实现链表
- 数据结构Java实现 ----循环链表、仿真链表
- 探究数据结构之链表Java代码实现(二)
- java 链表数据结构实现
- 数据结构 链表 Java实现
- java 实现双向链表(数据结构)
- (转)java实现基本数据结构(堆,栈,链表)——推荐后面的链表扩展部分
- java与数据结构(4)---java实现双向循环链表
- 数据结构Java实现03----单向链表的插入和删除
- 【数据结构】链表的原理及java实现
- 数据结构专题——线性表之双链表及其Java实现
- Java数据结构----栈(Stack)源码分析和用链表简单实现
- Java 数据结构链表操作实现代码
- java数据结构-链表的实现
- 数据结构(三)——单向循环链表的java实现
- Java实现链表数据结构
- 数据结构:二叉树的二叉链表存储--Java实现