单向链表

来源:互联网 发布:拦截导弹算法pascal 编辑:程序博客网 时间:2024/05/26 12:03

单向链表:数据类型可以是任意类型

/*

 * 节点类,包括数据域和指针域

 */

public class ListNode {

public ListNode Next; //指向下一节点的指针与域

public int dataValue;        //数据域

public ListNode(int NewValue)

{

    dataValue=NewValue;

}

}

/*

 *  链表类,实现添加、插入、删除、打印

 */

public class Clist {

private ListNode Head;          //头指针

private ListNode Tail;  //尾指针

private ListNode Current;  //当前指针

private int ListCountValue;      //链表数据的个数

public Clist()     //构造函数

{

    ListCountValue = 0;  //初始化

    Head = null; //头指针为空

    Tail = null; //尾指针为空

}

//添加一个节点

public void Append(int DataValue)

{

    ListNode NewNode = new ListNode(DataValue);         //初始化节点

    if (ListCountValue==0) //如果链表为空

    {

        //头节点和尾节点都是该值

        Head = NewNode;

        Tail = NewNode;

    }

    else

   {

        Tail.Next = NewNode; //尾指针指向添加的数据

        Tail = NewNode;     //尾节点为新添加的数据

    }

    Current = NewNode;     //当前节点为尾节点

    ListCountValue += 1;  //链表数据个数加1

}

//插入一个节点

public void Insert(int DataValue)

{

    ListNode NewNode = new ListNode(DataValue);

    if (ListCountValue==0) //如果链表为空

    {

        Append(DataValue);  //为空表,则添加

        return; //跳出程序

    }

    if (Current==Head) //如果当前节点为头结点

    {

         //为头部插入

         NewNode.Next = Head; //新结点的指针指向原来的头节点

         Head = NewNode; //新节点为头结点

         Current = Head; //当前节点为头节点

         ListCountValue += 1; //节点数加1

         return; //跳出程序

     }

    if (Current==Tail) //若当前节点为尾节点

        {

            Tail.Next = NewNode;     //尾节点就为当前节点指向的前一个节点

            Tail=NewNode;

            Current = Tail;     //当前节点为尾节点

            ListCountValue += 1; //节点数加1

           

        }

     //中间插入

    if((Current!=Head)&&(Current!=Tail))

    {

     NewNode.Next = Current.Next; //将当前节点的指针赋值给新节点的指针

     Current.Next=NewNode;

     Current=NewNode;

     ListCountValue+=1;

    }

}

//删除节点  

public void Delete()

{       

if (ListCountValue!=0)     //若链表不为空

    {     

         if (Current==Head) //若当前节点为头结点

         {

             Head = Current.Next; //头结点为当前节点指定的下个节点

             Current = Head; //当前节点为头结点

             ListCountValue -= 1; //节点数减1

             return; //跳出程序

         }else

         {

         Current = Current.Next;          //若从链表中间删除数据

         

         ListCountValue -= 1;                   //节点数减1

         }

      }

 }

/*

 *打印所有节点

 */

public void printAllListNode(){

if(ListCountValue!=0){

    System.out.println("输出链表中的所有数据:");

    Current = Head;         //当前节点为头结点

    for(int i=0;i<ListCountValue;i++){

     System.out.println(Current.dataValue); //输出当前节点值

     Current = Current.Next;     //当前节点为下一个节点

    }

 }

}

}

/*

 * 测试类

 */

public class Test {

public static void main(String[] args) {

Clist clist = new Clist();

clist.Append(12); //添加尾节点

clist.Append(10); //添加尾节点

clist.Insert(66); //插入新节点

clist.Insert(33);                           //插入新节点

clist.Delete();                             //删除当前节点的下一节点

clist.printAllListNode(); //输出链表中所有的数据

}

}

原创粉丝点击