Source Code Cross Referenced for LinkedList.java in  » Net » SkunkDAV » HTTPClient » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » SkunkDAV » HTTPClient 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)LinkedList.java					0.3-2 18/06/1999
003:         *
004:         *  This file is part of the HTTPClient package
005:         *  Copyright (C) 1996-1999  Ronald Tschalär
006:         *
007:         *  This library is free software; you can redistribute it and/or
008:         *  modify it under the terms of the GNU Lesser General Public
009:         *  License as published by the Free Software Foundation; either
010:         *  version 2 of the License, or (at your option) any later version.
011:         *
012:         *  This library is distributed in the hope that it will be useful,
013:         *  but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015:         *  Lesser General Public License for more details.
016:         *
017:         *  You should have received a copy of the GNU Lesser General Public
018:         *  License along with this library; if not, write to the Free
019:         *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
020:         *  MA 02111-1307, USA
021:         *
022:         *  For questions, suggestions, bug-reports, enhancement-requests etc.
023:         *  I may be contacted at:
024:         *
025:         *  ronald@innovation.ch
026:         *
027:         */
028:
029:        package HTTPClient;
030:
031:        /**
032:         * This class implements a singly linked list.
033:         *
034:         * @version	0.3-2  18/06/1999
035:         * @author	Ronald Tschalär
036:         */
037:
038:        class LinkedList {
039:            /** head of list */
040:            private LinkElement head = null;
041:
042:            /** tail of list (for faster adding) */
043:            private LinkElement tail = null;
044:
045:            /**
046:             * Add the specified element to the head of the list.
047:             *
048:             * @param elem the object to add to the list.
049:             */
050:            public synchronized void addToHead(Object elem) {
051:                head = new LinkElement(elem, head);
052:
053:                if (head.next == null)
054:                    tail = head;
055:            }
056:
057:            /**
058:             * Add the specified element to the end of the list.
059:             *
060:             * @param elem the object to add to the list.
061:             */
062:            public synchronized void addToEnd(Object elem) {
063:                if (head == null)
064:                    head = tail = new LinkElement(elem, null);
065:                else
066:                    tail = (tail.next = new LinkElement(elem, null));
067:            }
068:
069:            /**
070:             * Remove the specified element from the list. Does nothing if the element
071:             * is not in the list.
072:             *
073:             * @param elem the object to remove from the list.
074:             */
075:            public synchronized void remove(Object elem) {
076:                if (head == null)
077:                    return;
078:
079:                if (head.element == elem) {
080:                    head = head.next;
081:                    return;
082:                }
083:
084:                LinkElement curr = head;
085:                while (curr.next != null) {
086:                    if (curr.next.element == elem) {
087:                        if (curr.next == tail)
088:                            tail = curr;
089:                        curr.next = curr.next.next;
090:                        return;
091:                    }
092:                    curr = curr.next;
093:                }
094:            }
095:
096:            /**
097:             * Return the first element in the list. The list is not modified in any
098:             * way.
099:             *
100:             * @return the first element
101:             */
102:            public synchronized Object getFirst() {
103:                if (head == null)
104:                    return null;
105:                return head.element;
106:            }
107:
108:            private LinkElement next_enum = null;
109:
110:            /**
111:             * Starts an enumeration of all the elements in this list. Note that only
112:             * one enumeration can be active at any time.
113:             *
114:             * @return the first element, or null if the list is empty
115:             */
116:            public synchronized Object enumerate() {
117:                if (head == null)
118:                    return null;
119:
120:                next_enum = head.next;
121:                return head.element;
122:            }
123:
124:            /**
125:             * Gets the next element in the enumeration. The enumeration must have
126:             * been first initalized with a call to <code>enumerate()</code>.
127:             *
128:             * @return the next element, or null if none left
129:             * @see #enumerate()
130:             */
131:            public synchronized Object next() {
132:                if (next_enum == null)
133:                    return null;
134:
135:                Object elem = next_enum.element;
136:                next_enum = next_enum.next;
137:
138:                return elem;
139:            }
140:
141:            public static void main(String args[]) throws Exception {
142:                // LinkedList Test Suite
143:
144:                System.err.println("\n*** Linked List Tests ...");
145:
146:                LinkedList list = new LinkedList();
147:                list.addToHead("One");
148:                list.addToEnd("Last");
149:                if (!list.getFirst().equals("One"))
150:                    throw new Exception("First element wrong");
151:                if (!list.enumerate().equals("One"))
152:                    throw new Exception("First element wrong");
153:                if (!list.next().equals("Last"))
154:                    throw new Exception("Last element wrong");
155:                if (list.next() != null)
156:                    throw new Exception("End of list wrong");
157:                list.remove("One");
158:                if (!list.getFirst().equals("Last"))
159:                    throw new Exception("First element wrong");
160:                list.remove("Last");
161:                if (list.getFirst() != null)
162:                    throw new Exception("End of list wrong");
163:
164:                list = new LinkedList();
165:                list.addToEnd("Last");
166:                list.addToHead("One");
167:                if (!list.getFirst().equals("One"))
168:                    throw new Exception("First element wrong");
169:                if (!list.enumerate().equals("One"))
170:                    throw new Exception("First element wrong");
171:                if (!list.next().equals("Last"))
172:                    throw new Exception("Last element wrong");
173:                if (list.next() != null)
174:                    throw new Exception("End of list wrong");
175:                if (!list.enumerate().equals("One"))
176:                    throw new Exception("First element wrong");
177:                list.remove("One");
178:                if (!list.next().equals("Last"))
179:                    throw new Exception("Last element wrong");
180:                list.remove("Last");
181:                if (list.next() != null)
182:                    throw new Exception("End of list wrong");
183:
184:                list = new LinkedList();
185:                list.addToEnd("Last");
186:                list.addToHead("Two");
187:                list.addToHead("One");
188:                if (!list.getFirst().equals("One"))
189:                    throw new Exception("First element wrong");
190:                if (!list.enumerate().equals("One"))
191:                    throw new Exception("First element wrong");
192:                if (!list.next().equals("Two"))
193:                    throw new Exception("Second element wrong");
194:                if (!list.next().equals("Last"))
195:                    throw new Exception("Last element wrong");
196:                if (list.next() != null)
197:                    throw new Exception("End of list wrong");
198:                list.remove("Last");
199:                list.remove("Two");
200:                list.remove("One");
201:                if (list.getFirst() != null)
202:                    throw new Exception("Empty list wrong");
203:
204:                System.err.println("\n*** Tests finished successfuly");
205:            }
206:        }
207:
208:        /**
209:         * The represents a single element in the linked list.
210:         */
211:        class LinkElement {
212:            Object element;
213:            LinkElement next;
214:
215:            LinkElement(Object elem, LinkElement next) {
216:                this.element = elem;
217:                this.next = next;
218:            }
219:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.