回头看数据结构,写了个链表,...

malligator 2006-11-29 03:23:46
此链表是带头节点单向链表,增删查均按位置关系来定(仿我现在看的书)

代码清单:
===================
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);
}
}
...全文
149 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
malligator 2006-11-29
  • 打赏
  • 举报
回复
奇怪的是弄了一段时间的JAVA,C++什么的好象差不多都忘光了
(曾经以为自己有点基础的:()
malligator 2006-11-29
  • 打赏
  • 举报
回复
呵呵
我没怎么学过JAVA,边用边学,没人教,自己看书多~

接触JAVA有两三年了吧(在学校),不过用的C/C++比较多
现在自己编的代码还是不多的

慢慢学习中
wunan320 2006-11-29
  • 打赏
  • 举报
回复
LZ也没说自己是刚开始学习JAVA的啊。。。
人家是说回头复习了下。
jlhxbg 2006-11-29
  • 打赏
  • 举报
回复
这个程序是刚开始学习JAVA就能写出来的吗?我还是先吧基础学习学习。这个也能看明白点。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧