此链表是带头节点单向链表,增删查均按位置关系来定(仿我现在看的书)代码清单:
===================
package pk;import java.io.*;class MyList
{
private MyNode head; //链表头
private int length; //链表长度
public MyList()
{
head=new MyNode(null); //初始化头结点
length=0;
}
public int length() //返回链表长度
{
return length;
}
public Object find(int index)
{
if(index>length||index<=0) //没有这么多结点
return null;
MyNode p=head;
for(int i=0;i<index;i++) //找到第index个结点
{
p=p.next;
}
return p.obj; //返回找到的对象
}
public boolean insert(Object o,int index)
{
if(index>length+1||index<0) //没有此位置
return false;
MyNode q,p=head;
for(int i=0;i<index-1;i++) //找到插入点的前驱
{
p=p.next;
}
q=new MyNode(o); //根据o分配新结点q
q.next=p.next; //新结点q连入链表
p.next=q;
length++; //链表长度加一
return true;
}
public boolean delete(int index)
{
if(index>length||index<=0) //没有此位置
return false;
MyNode q,p=head;
for(int i=0;i<index-1;i++) //找到插入点的前驱
{
p=p.next;
}
q=p.next; //q是要删除的结点
p.next=q.next;
q.next=null;
length--; //链表长度减一
return true;
}
public void showList()
{
if(length==0) //空链表的情况
{
System.out.print("the List is null!!");
return;
}
MyNode p=head.next;
System.out.print("the List:\nhead:");
while(p!=null)//逐个输出链表里的数据:需要实现toString方法
{
System.out.print(" ->"+p.obj);
p=p.next;
}
System.out.println();
}
class MyNode //链表节点类
{
Object obj;
MyNode next;
protected MyNode(Object o)
{
obj=o;
}
}
}public class Seqlist //测试链表
{ static public void main(String args[])
{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
int iCommand=0,iPos=0;
String temp=null;
MyList ml=new MyList(); //实例化一个链表
ml.insert("hello", 0);//并插入条数据:以String为例
ml.insert("world", 0);
ml.insert("good", 0);
ml.insert("bye", 0);
ml.showList();//显示链表
do{
System.out.println("\nselect an action:");
System.out.println("1-add an item;");
System.out.println("2-delete an item:");
System.out.println("3-find an item");
System.out.println("4-show the list");
System.out.println("0-exit");
try {
temp=bf.readLine();
iCommand=Integer.parseInt(temp);
} catch (IOException e) {
e.printStackTrace();
}
switch(iCommand)
{
case 0:break;
case 1:
try {
System.out.print("input the position to insert:");
temp=bf.readLine();
iPos=Integer.parseInt(temp);
System.out.print("and the object.");
temp=bf.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if(ml.insert(temp, iPos))//插入
System.out.println("insert succeed!");
else System.out.println("the position doesn't exist!");
break;
case 2:
try {
System.out.print("input the position to delete:");
temp=bf.readLine();
iPos=Integer.parseInt(temp);
} catch (IOException e) {
e.printStackTrace();
}
if(ml.delete(iPos))//删除
System.out.println("delete succeed!");
else System.out.println("the position doesn't exist!");
break;
case 3:
try {
System.out.print("input the position to find:");
temp=bf.readLine();
iPos=Integer.parseInt(temp);
} catch (IOException e) {
e.printStackTrace();
}
if(ml.find(iPos)!=null)//查找
System.out.println("the "+iPos+"'s object is "+ml.find(iPos));
else System.out.println("the position doesn't exist!");
break;
case 4:
ml.showList();//显示链表
System.out.println("\nthe length of List:"+ml.length());
break;
default:
System.out.println("bad command!!");
break;
}
}while(iCommand!=0);
}
}
===================
package pk;import java.io.*;class MyList
{
private MyNode head; //链表头
private int length; //链表长度
public MyList()
{
head=new MyNode(null); //初始化头结点
length=0;
}
public int length() //返回链表长度
{
return length;
}
public Object find(int index)
{
if(index>length||index<=0) //没有这么多结点
return null;
MyNode p=head;
for(int i=0;i<index;i++) //找到第index个结点
{
p=p.next;
}
return p.obj; //返回找到的对象
}
public boolean insert(Object o,int index)
{
if(index>length+1||index<0) //没有此位置
return false;
MyNode q,p=head;
for(int i=0;i<index-1;i++) //找到插入点的前驱
{
p=p.next;
}
q=new MyNode(o); //根据o分配新结点q
q.next=p.next; //新结点q连入链表
p.next=q;
length++; //链表长度加一
return true;
}
public boolean delete(int index)
{
if(index>length||index<=0) //没有此位置
return false;
MyNode q,p=head;
for(int i=0;i<index-1;i++) //找到插入点的前驱
{
p=p.next;
}
q=p.next; //q是要删除的结点
p.next=q.next;
q.next=null;
length--; //链表长度减一
return true;
}
public void showList()
{
if(length==0) //空链表的情况
{
System.out.print("the List is null!!");
return;
}
MyNode p=head.next;
System.out.print("the List:\nhead:");
while(p!=null)//逐个输出链表里的数据:需要实现toString方法
{
System.out.print(" ->"+p.obj);
p=p.next;
}
System.out.println();
}
class MyNode //链表节点类
{
Object obj;
MyNode next;
protected MyNode(Object o)
{
obj=o;
}
}
}public class Seqlist //测试链表
{ static public void main(String args[])
{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
int iCommand=0,iPos=0;
String temp=null;
MyList ml=new MyList(); //实例化一个链表
ml.insert("hello", 0);//并插入条数据:以String为例
ml.insert("world", 0);
ml.insert("good", 0);
ml.insert("bye", 0);
ml.showList();//显示链表
do{
System.out.println("\nselect an action:");
System.out.println("1-add an item;");
System.out.println("2-delete an item:");
System.out.println("3-find an item");
System.out.println("4-show the list");
System.out.println("0-exit");
try {
temp=bf.readLine();
iCommand=Integer.parseInt(temp);
} catch (IOException e) {
e.printStackTrace();
}
switch(iCommand)
{
case 0:break;
case 1:
try {
System.out.print("input the position to insert:");
temp=bf.readLine();
iPos=Integer.parseInt(temp);
System.out.print("and the object.");
temp=bf.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if(ml.insert(temp, iPos))//插入
System.out.println("insert succeed!");
else System.out.println("the position doesn't exist!");
break;
case 2:
try {
System.out.print("input the position to delete:");
temp=bf.readLine();
iPos=Integer.parseInt(temp);
} catch (IOException e) {
e.printStackTrace();
}
if(ml.delete(iPos))//删除
System.out.println("delete succeed!");
else System.out.println("the position doesn't exist!");
break;
case 3:
try {
System.out.print("input the position to find:");
temp=bf.readLine();
iPos=Integer.parseInt(temp);
} catch (IOException e) {
e.printStackTrace();
}
if(ml.find(iPos)!=null)//查找
System.out.println("the "+iPos+"'s object is "+ml.find(iPos));
else System.out.println("the position doesn't exist!");
break;
case 4:
ml.showList();//显示链表
System.out.println("\nthe length of List:"+ml.length());
break;
default:
System.out.println("bad command!!");
break;
}
}while(iCommand!=0);
}
}
解决方案 »
- 父类引用子类对象 转换及与多态的关系
- 请问soap接口调用时的报文消息超时重发机制该如何实现?
- 下列哪些会导致线程暂停执行
- JAVA初学者请求帮助!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- 关于socket read()的一个问题!请帮忙 谢谢
- 文件安全小问题,急!!!
- java的类和接口继承问题
- 在JAVA中,定义类时有public和没有public有什么区别?
- 一直很郁闷阿,
- 在tomcat中报如下错误是什么意思???
- 怎样将DES加密算法代码转换成为加密->解密?
- 大哥大姐们,小弟请问一个字符串比较的问题???
人家是说回头复习了下。
我没怎么学过JAVA,边用边学,没人教,自己看书多~接触JAVA有两三年了吧(在学校),不过用的C/C++比较多
现在自己编的代码还是不多的慢慢学习中
(曾经以为自己有点基础的:()