?? chain.java
字號:
public class Chain
{
public ChainNode first;
public Chain()
{first=null;}
public int IsEmpty(){if( first==null)return 1;return 0;}
public int Length()
{ChainNode current=first;
int l=0;
while(current!=null)
{
l++;
current=current.link;
}
return l;
}
public int Find(int k,int x)
{
if(k<1)
return 0;
ChainNode p=first;
int i=0;
while(p!=null&&i<k)
{
p=p.link;
i++;
}
if(p!=null)
{
x=p.data;
return 1;
}
return 0;
}
public int Search(int x)
{
ChainNode p=first;
int i=1;
while(p!=null&&p.data!=x)
{
p=p.link;
i++;
}
if(p==null)
return 0;
else
return i;
}
public void Delete(int y,int x)
{
// 刪除指定的data值為y的元素,并存入x中
if(first.data==y)
{first=first.link;}
else
{
ChainNode t=first.link;
ChainNode p=first;
while(t.data!=y&&t!=null)
{
p=p.link;
t=p.link;
}
if(t==null)
System.out.println("not found.");
else
p.link=t.link;
}
}
public void Insert(int k,int x)
{
// 在指定位置k后插入x
ChainNode p=first;
for(int i=1;i<k&&p!=null;i++)
p=p.link;
if(p==null&&p!=first)
System.out.println("wrong.");
ChainNode y=new ChainNode();
if(k!=0)
{
y.data=x;
y.link=p.link;
p.link=y;
}
else
{
y.data=x;
y.link=first;
first=y;
}
}
public void Add(int x)
{
ChainNode y=new ChainNode();
y.data=x;
y.link=first;
first=y;
}
public void Output()
{
ChainNode current;
for(current=first;current!=null;current=current.link)
System.out.print(current.data+" ");
System.out.println();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -