]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/LinkedList.java
Pjirc 2.2.1 as available on the net, reformatted and made it compile.
[irc/pjirc-ng.git] / src / main / java / irc / LinkedList.java
1 /*****************************************************/\r
2 /*          This java file is a part of the          */\r
3 /*                                                   */\r
4 /*           -  Plouf's Java IRC Client  -           */\r
5 /*                                                   */\r
6 /*   Copyright (C)  2002 - 2004 Philippe Detournay   */\r
7 /*                                                   */\r
8 /*         All contacts : theplouf@yahoo.com         */\r
9 /*                                                   */\r
10 /*  PJIRC is free software; you can redistribute     */\r
11 /*  it and/or modify it under the terms of the GNU   */\r
12 /*  General Public License as published by the       */\r
13 /*  Free Software Foundation; version 2 or later of  */\r
14 /*  the License.                                     */\r
15 /*                                                   */\r
16 /*  PJIRC is distributed in the hope that it will    */\r
17 /*  be useful, but WITHOUT ANY WARRANTY; without     */\r
18 /*  even the implied warranty of MERCHANTABILITY or  */\r
19 /*  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   */\r
20 /*  General Public License for more details.         */\r
21 /*                                                   */\r
22 /*  You should have received a copy of the GNU       */\r
23 /*  General Public License along with PJIRC; if      */\r
24 /*  not, write to the Free Software Foundation,      */\r
25 /*  Inc., 59 Temple Place, Suite 330, Boston,        */\r
26 /*  MA  02111-1307  USA                              */\r
27 /*                                                   */\r
28 /*****************************************************/\r
29 \r
30 package irc;\r
31 \r
32 /**\r
33  * LinkedListNode.\r
34  */\r
35 class LinkedListNode {\r
36         /**\r
37          * Next node.\r
38          */\r
39         public LinkedListNode next = null;\r
40         /**\r
41          * Previous node.\r
42          */\r
43         public LinkedListNode prev = null;\r
44         /**\r
45          * Node content.\r
46          */\r
47         public Object item = null;\r
48 }\r
49 \r
50 /**\r
51  * A very simple linked list.\r
52  */\r
53 public class LinkedList {\r
54         private int _size;\r
55         private LinkedListNode _head;\r
56         private LinkedListNode _tail;\r
57 \r
58         /**\r
59          * Create a new, empty linked list.\r
60          */\r
61         public LinkedList() {\r
62                 _head = new LinkedListNode();\r
63                 _tail = new LinkedListNode();\r
64                 _head.next = _tail;\r
65                 _tail.prev = _head;\r
66                 _size = 0;\r
67         }\r
68 \r
69         /**\r
70          * Get the size of the list.\r
71          * \r
72          * @return list size.\r
73          */\r
74         public int size() {\r
75                 return _size;\r
76         }\r
77 \r
78         /**\r
79          * Add the given object at the end of the list.\r
80          * \r
81          * @param obj\r
82          *          object to add.\r
83          */\r
84         public void addLast(Object obj) {\r
85                 LinkedListNode node = new LinkedListNode();\r
86                 node.item = obj;\r
87                 node.next = _tail;\r
88                 node.prev = _tail.prev;\r
89                 node.prev.next = node;\r
90                 node.next.prev = node;\r
91                 _size++;\r
92         }\r
93 \r
94         /**\r
95          * Remove and return the first object of the list.\r
96          * \r
97          * @return removed object.\r
98          * @throws RuntimeException\r
99          *           if list is empty.\r
100          */\r
101         public Object removeFirst() {\r
102                 if (_size == 0)\r
103                         throw new RuntimeException("List empty");\r
104                 LinkedListNode node = _head.next;\r
105 \r
106                 node.next.prev = _head;\r
107                 _head.next = node.next;\r
108                 _size--;\r
109                 return node.item;\r
110         }\r
111 \r
112 }\r