美文网首页
Linked List

Linked List

作者: 綿綿_ | 来源:发表于2019-03-29 13:23 被阅读0次

    Create a linked list

    class DATA2
        {
            String key;
            String name;
            int age;
        }
        class CLType
        {
            DATA2 nodeData = new DATA2();
            CLType nextnode;
        }
    

    add an element at the end of list

    CLType CLAddEnd (CLType head, DATA2 nodeData)
            {
                CLType node, htemp;
                if ((node =new CLType())==null)
                {
                    System.out.print("memory error");
                    return null;
                }else
                {
                    node.nodeData=nodeData; //save data
                    node.nextnode=null; //list tail
                    if(head == null)
                    {
                        head=node;
                        return head;
                    }
                    htemp=head;
                    while(htemp!=null)
                    {
                        htemp=htemp.nextnode;
                    }
                    htemp.nextnode=node;
                    return head;
                }
            }
    

    Add an element at the first of list

    CLType CLAddFirst(CLType head,DATA2 nodeData)
            {
                CLType node;
                if((node = new CLType())==null)
                {
                    System.out.print("memory error");
                    return null;
                }else
                {
                    node.nodeData=nodeData;
                    node.nextnode=head;
                    head=node;
                    return head;
                }
            }
        
    

    Search for node with keyword

    CLType CLFindNode (CLType head,String key)
            {
                CLType htemp;
                htemp=head;
                while(htemp!=null)
                {
                    if(htemp.nodeData.key.compareTo(key)==0)
                    {
                        return htemp;
                    }
                    htemp=htemp.nextnode;
                }
                return null;
            }
    

    Insert an element

    CLType CLInsertNode(CLType head,String findkey,DATA2 nodeData)
            {
                CLType node,nodetemp;
                if ((node = new CLType())==null)
                {
                    System.out.print("memory error");
                    return null;
                }
                node.nodeData=nodeData;
                nodetemp=CLFindNode(head,findkey);
                if(nodetemp!=null)
                {
                    node.nextnode=nodetemp.nextnode;
                    nodetemp.nextnode=node;
                }
                else
                {
                    System.out.println("position not found");
                }
                return head;
                
            }
    

    Delete an element

    int  CLDeleteNode(CLType head,String key)
            {
                CLType node,htemp;
                htemp=head;
                node=head;
                while(htemp!=null)
                {
                    if  (htemp.nodeData.key.compareTo(key)==0)
                    {
                        node.nextnode=htemp.nextnode;
                        htemp=null; //release memory
                        return 1 ;
                    }
                    else
                    {
                        node = htemp;
                        htemp=htemp.nextnode;
                    }
                }
                return 0;
            }   
    

    Calculate the length of linked list

    int CLLength(CLType head)
            {
                CLType htemp;
                int len=0;
                htemp=head;
                while(htemp!=null)
                {
                    len++;
                    htemp=htemp.nextnode;
                }
                return len;
            }
    

    相关文章

      网友评论

          本文标题:Linked List

          本文链接:https://www.haomeiwen.com/subject/eobhbqtx.html