Dear,

Just refer this source.
I modify a little bir for your request.
The orginal source is from "Java Examples In A Nutshell" 2nd edition from
O'REILLY.
I think that book will give you more idea.

Thanks
HyunChul
----------------------------------------------------------------------------
---------
public class LinkedListCount
{
    Linkable head;
    private final int MAXLINK = 1000;
    private int linkCount = 0;
    private int maxLinkCount = MAXLINK;

    public interface Linkable
    {
        public Linkable getNext();
        public void setNext(Linkable node);
    }

    public synchronized Linkable getHead()
    {
        return head;
    }

    public synchronized void setMaxLinkCount(int maxLinkCount)
    {
        this.maxLinkCount = maxLinkCount;
    }

    public synchronized boolean insertAtHead(Linkable node)
    {
        if (linkCount < maxLinkCount)
        {
            node.setNext(head);
            head = node;
            linkCount++;
            return true;
        } else
            return false;
    }

    public synchronized boolean insertAtTail(Linkable node)
    {
        if (linkCount < maxLinkCount)
        {
            if(head == null)
                head = node;
            else
            {
                Linkable p, q;
                for(p = head; (q=p.getNext())!= null;p = q);
                p.setNext(node);
            }
            linkCount++;
            return true;
        } else
            return false;
    }

    public synchronized Linkable removeFromHead()
    {
        Linkable node = head;

        if(node != null)
        {
            head = node.getNext();
            node.setNext(null);
            linkCount--;
        }
        return node;
    }

    public synchronized Linkable removeFromTail()
    {
        if(head == null)
            return null;
        Linkable p = head, q=null, next = head.getNext();
        if(next == null)
        {
            head = null;
            linkCount--;
            return p;
        }

        while((next = p.getNext()) != null)
        {
            q = p;
            p = next;
        }
        q.setNext(null);
        linkCount--;
        return p;
    }

    public synchronized boolean remove(Linkable node)
    {
        if(head == null)
            return false;
        if(node.equals(head))
        {
            head = head.getNext();
            linkCount--;
            return true;
        }

        Linkable p = head, q = null;
        while((q = p.getNext()) != null)
        {
            if(node.equals(q))
            {
                p.setNext(q.getNext());
                linkCount--;
                return true;
            }
            p = q;
        }
        return true;
    }

    public static class TestString
    {
        static class LinkableString implements Linkable
        {
            String str = new String();

            Linkable next;

            public LinkableString(String s)
            {
                this.str = s;
            }

            public Linkable getNext()
            {
                return next;
            }

            public void setNext(Linkable node)
            {
                next = node;
            }

            public String toString()
            {
                return str;
            }

            public boolean equals(Object o)
            {
                if(this == o)
                    return true;
                if(!(o instanceof LinkableString))
                    return false;
                if(((LinkableString)o).str.equals(this.str))
                    return true;
                return false;
            }
        }

        public static void main(String[] args)
        {
            LinkedListCount ll = new LinkedListCount();

            ll.setMaxLinkCount(100);
            if (ll.insertAtHead(new LinkableString("1")))
                System.out.println("1 Insert Success");
            else
                System.out.println("1 Insert Fail");
            if (ll.insertAtHead(new LinkableString("2")))
                System.out.println("2 Insert Success");
            else
                System.out.println("2 Insert Fail");
            if (ll.insertAtHead(new LinkableString("3")))
                System.out.println("3 Insert Success");
            else
                System.out.println("3 Insert Fail");
            if (ll.insertAtHead(new LinkableString("4")))
                System.out.println("4 Insert Success");
            else
                System.out.println("4 Insert Fail");
            if (ll.insertAtHead(new LinkableString("5")))
                System.out.println("5 Insert Success");
            else
                System.out.println("5 Insert Fail");
            if (ll.insertAtHead(new LinkableString("6")))
                System.out.println("6 Insert Success");
            else
                System.out.println("6 Insert Fail");
            System.out.println();
            System.out.println(ll.removeFromHead());
            System.out.println(ll.removeFromTail());
            ll.remove(new LinkableString("3"));

            System.out.println();
            for(Linkable l = ll.getHead(); l != null; l = l.getNext())
                System.out.println(l);
        }
    }
}


----- Original Message -----
From: "Shirley Chen" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, December 08, 2000 3:48 AM
Subject: LinkedList


> I am strugling so hard on this, could anybody help me? Here is my problem:
>
> I need a fixed size linkedlist (100 elements) to store string objects.
The
> reason for the fixed size is I don't know how many strings I will get, and
I
> don't want the linkedlist out grow so it will fill up the memory.  When I
> got 101th string, I will delete the first one, and make the space for the
> new one.  What is the efficient way to do that?
>
> Thanks
>
>
>
>
____________________________________________________________________________
_________
> Get more from the Web.  FREE MSN Explorer download :
http://explorer.msn.com
>
>
===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
> For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
>

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to