回头看数据结构,写了个链表,...
此链表是带头节点单向链表,增删查均按位置关系来定(仿我现在看的书)
代码清单:
===================
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);
}
}