数据结构——链表学习笔记
时间:2011-05-25 来源:PiGGyChee
由于跨专业找的工作,很多科班的课程只有到现在才来补学,比如很重要的《数据结构》,原来仅仅是使用,知其然而不知其所以然,现在决定花两周时间把课程自学完,关于每张学习的笔记就博客出来,留待自己将来复习。
下面是整数链表的练习程序:
public class IntNode
{
public static void main(String[] args)
{
IntNode shortList;
IntNode copy;
shortList = new IntNode(10,null);
shortList.addNodeAfter(20);
shortList.addNodeAfter(25);
shortList.addNodeAfter(30);
shortList.addNodeAfter(38);
shortList.addNodeAfter(42);
shortList.addNodeAfter(55);
// IntNode.listCopy()测试
copy = IntNode.listCopy(shortList);
System.out.println(IntNode.listLength(copy));
System.out.println(IntNode.listLength(shortList));
// IntNode.listCopyWithTail测试
IntNode[] copyWithTail = IntNode.listCopyWithTail(shortList);
System.out.println(copyWithTail[0].getData());
System.out.println(copyWithTail[1].getData());
// IntNode.listData(IntNode head)测试
for (int i = 0 ; i < IntNode.listLength(copy) ; i++ )
{
System.out.print(i + 1 + ": " + IntNode.listData(shortList)[i] + "; ");
}
// IntNode.listPart(IntNode start,IntNode end)测试
System.out.println("--------------------------");
try
{
IntNode[] listPartCopy = IntNode.listPart(shortList,new IntNode(42,null),new IntNode(25,null));
System.out.println(listPartCopy[0].getData());
System.out.println(listPartCopy[1].getData());
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
// IntNode.listPostion(IntNode head,int position)测试
System.out.println("-------------------------");
try
{
System.out.println("Position 5: " + IntNode.listPosition(shortList,5).getData());
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
}
int data;
IntNode link;
public IntNode(int data,IntNode link)
{
this.data = data;
this.link = link;
}
public int getData()
{
return this.data;
}
public void setData(int data)
{
this.data = data;
}
public IntNode getLink()
{
return this.link;
}
public void setLink(IntNode link)
{
this.link = link;
}
public void addNodeAfter(int element)
{
link = new IntNode(element,link);
}
public void removeNodeAfter()
{
link = link.link;
}
public static int listLength(IntNode head)
{
int answer = 0;
IntNode cursor;
if (head == null)
{
return answer;
}
for (cursor = head ; cursor != null ; cursor = cursor.getLink())
{
answer++;
}
return answer;
}
public static IntNode listCopy(IntNode source)
{
IntNode copyHead = null;
IntNode copyTail = null;
if (source == null)
{
return null;
}
copyHead = new IntNode(source.getData(),null);
copyTail = copyHead;
while ((source = source.getLink()) != null) //书上代码为:while(source.link != null)
{
IntNode copyTemp = new IntNode(source.getData(),copyTail.getLink());
copyTail.link = copyTemp;
copyTail = copyTemp;
// 上为自己构思的方法,这个相当于copyTail一直向后加
// 以下书上的源代码
//source = source.link;
//copyTail.addNodeAfter(source.getData());
//copyTail = copyTail.getLink();
}
return copyHead;
}
public static IntNode[] listCopyWithTail(IntNode source)
{
IntNode copyHead = null;
IntNode copyTail = null;
IntNode[] answer = new IntNode[2];
if (null == source)
{
return answer;
}
copyHead = new IntNode(source.getData(),null);
copyTail = copyHead;
answer[0] = copyHead;
System.out.println("-----------------------------");
while ((source = source.getLink()) != null)
{
System.out.println("source data:" + source.getData());
//copyTail = new IntNode(source.getData(),copyTail.getLink());
copyTail.link = new IntNode(source.getData(),copyTail.getLink());
copyTail = copyTail.link;
//copyTail.addNodeAfter(source.getData());
//copyTail = copyTail.getLink();;
System.out.println("copyed Tail data:" + copyTail.getData());
}
answer[1] = copyTail;
return answer;
}
public static IntNode[] listPart(IntNode head,IntNode start,IntNode end)
{
IntNode copyHead;
IntNode copyTail;
IntNode[] answer = new IntNode[2];
if (null == start)
{
throw new IllegalArgumentException("start is null.");
}
if (null == end)
{
throw new IllegalArgumentException("end is null.");
}
copyHead = new IntNode(start.getData(),null);
//System.out.println("start data: " + start.getData());
//System.out.println("end data: " + end.getData());
copyTail = copyHead;
start = IntNode.listNodeByData(head,start.getData());
end = IntNode.listNodeByData(head,end.getData());
while (start != end)
{
start = start.getLink();
System.out.println("start data: " + start.getData());
if (null == start)
{
throw new IllegalArgumentException("end node was not found on the list.");
}
copyTail.link = new IntNode(start.getData(),copyTail.getLink());
copyTail = copyTail.link;
}
answer[0] = copyHead;
answer[1] = copyTail;
return answer;
}
public static int[] listData(IntNode head)
{
int[] answer = new int[IntNode.listLength(head)];
for (int i = 0; head != null ; head = head.link )
{
answer[i++] = head.getData();
}
return answer;
}
public static IntNode listNodeByData(IntNode head,int data)
{
if (null == head)
{
throw new IllegalArgumentException("list is null.");
}
for (; head != null ; head = head.getLink() )
{
if (head.getData() == data )
{
return head;
}
if (null == head)
{
throw new IllegalArgumentException("node not found.");
}
}
return null;
}
public static IntNode listPosition(IntNode head,int position)
{
position--;
if (null == head)
{
throw new IllegalArgumentException("list is null.");
}
if (position <= 0)
{
throw new IllegalArgumentException("position is not positive!");
}
for (int i = 0 ; (i < position) && (head != null) ; i++ )
{
head = head.getLink();
}
return head;
}
}
相关阅读 更多 +
排行榜 更多 +