?? linklist.java
字號:
package member;
public class LinkList implements List
{
public Node head;
private int length;
public LinkList()
{
head=new Node(null);
length=0;
head.data=null;
}
public void clear() //置空表
{
head.next=null;
length=0;
}
public int getLength() //求表長
{
return length;
}
public Object getEntry(int i) //取出第i個元素
{
if(length<i)
{
return null;
}
Node n=new Node();
n=head;
for(int index=0;index<i;index++)
{
n=n.next;
}
return n.data;
}
public boolean isEmpty() //判表空運算
{
return (length==0);
}
public boolean add(int i, Object newEntry) //將數據元素插入線性表中第//i個元素之后
{
if(i>length)
{
return false;
}
if(i==length)
{
Node p=head;
while(p.next!=null)
{
p=p.next;
}
p.next=new Node(newEntry);
length++;
return true;
}
Node n=new Node();
n=head;
for(int index=0;index<i;index++)
{
n=n.next;
}
Node newNode=new Node(newEntry);
newNode.next=n.next;
n.next=newNode;
length++;
return true;
}
public Object remove(int i) //刪除第i個元素,并將被刪除的元素返回
{
if(i>length)
{
return false;
}
Node b=new Node();
b=head;
for(int index=0;index<i-1;index++)
{
b=b.next;
}
Node a=new Node();
Node result=new Node();
result=b.next;
a=result.next;
b.next=a;
length--;
return result.data;
}
public boolean add(Object newEntry) //將數據元素插入線性表的表頭
{
if(this.isEmpty())
{
this.head.next=new Node(newEntry);
length++;
return true;
}
Node p=new Node(newEntry);
p.next=head.next;
head.next=p;
length++;
return true;
}
/* public static void main(String [] args)
{
LinkList L=new LinkList();
Node node=new Node();
node=L.head;
System.out.println("生成以下鏈表");
for(int i=0;i<6;i++)
{
node.next=new Node((Object)i);
node=node.next;
System.out.print(node.tostring()+" ");
}
System.out.println();
Node p=L.head.next;
L.head.next=null;
for(int i=0;i<6;i++)
{
L.add(p.data);
if(i<5)
p=p.next;
}
System.out.println("反轉后:");
p=L.head;
for(int i=0;i<6;i++)
{
p=p.next;
System.out.print(p.tostring()+" ");
}
System.out.println();
System.out.println("刪除第5個結點:"+L.remove(5)+" ");
System.out.println("再插入第5個結點:11");
L.add(5,11) ;
p=L.head;
for(int i=0;i<6;i++)
{
p=p.next;
System.out.print(p.tostring()+" ");
}
System.out.println();
}*/
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -