日期:2014-05-20 浏览次数:20950 次
import java.util.*;
class Node
{
static int num = 1;
double data;
Node next;
Node prep;
Node(double ddata,Node nnext,Node pprep)
{
data = ddata;
next = nnext;
prep = pprep;
}
Node(double ddata,Node k,int sign) //0为前,1为后
{
data = ddata;
if(sign == 0)
{
this.prep = k;
k.next = this;
this.next = null;
k = Listoper.MoveNodeNex(k);
}
else
{
this.next = k;
k.prep = this;
this.next = null;
k = Listoper.MoveNodePre(k);
}
}
Node(double ddata)
{
data = ddata;
next = null;
prep = null;
}
Node()
{
data = 0;
next = null;
prep = null;
}
static boolean MakeEmpty(Node Me)
{
Me.data = 0;
Me.next = null;
Me.prep = null;
Me = null;
if(Me == null)
return true;
return false;
}
void display()
{
System.out.println("节点"+num+"的值为:"+"["+data+"]");
num++;
}
}
class Listoper
{
static void InsertNode(Node ListLoc,Node WorkNode) //将节点WorkNode 插入在节点ListLoc前面。
{
WorkNode = ListLoc.prep.next;
WorkNode.prep = ListLoc.prep;
ListLoc.prep = WorkNode;
WorkNode.next = ListLoc;
}
static boolean DeleteNode(Node ListHead,int ddata) //将和ddata相同数据项的节点在链表中删除
{
while(ListHead.data != ddata && ListHead != null)
{
ListHead = ListHead.next;
}
if(ListHead == null)
{
return false;
}
Node p = new Node();
p = ListHead;
p.prep.next = p.next;
p.next.prep = p.prep;
if(Node.MakeEmpty(p) == true)
System.gc();
else
{
System.out.println("memory error!");
}
return true;
}
static void DisplayList(Node Llist) //打印出链表中各个节点
{
while(Llist != null)
{
Llist.display();
Llist = Llist.next;
}
}
static Node MoveNodePre(Node p) //对链表中当前节点进行前后移位的操作
{
return p.prep;
}
static Node MoveNodeNex(Node p)
{
return p.next;
}
}
public class ABC
{
public static void main(String[] args)
{
int i;
Random Rdata = new Random();
Node head = new Node(Rdata.nextDouble()*100);
Node p = head;
for(i = 2; i < 10; i++)
{
p = new Node(Rdata.nextDouble()*10000,p,0);
}
Listoper.DisplayList(head);
}
}
------解决方案--------------------
链表和数组有相同地方也有不同的地方
相同之处:都是为了存储数据 那有了数组为什么还要链表呢
因为 链表在存贮空间中不需要空间的存贮位置是连续的
那它怎么才能保证空间不连续的彼此相连呢
因为链表包含两个区域 一个是数据区,一个是指针区,指针区就是来保证链表中个每个单位彼此之间的
联系的
------解决方案--------------------
链表是数据结构的一种
链表的节点包括数据域和指针域
------解决方案--------------------
/**
* @(#)MyLinkedList.java
*
*自定义的LinkedList