944,073 Members | Top Members by Rank

Ad:
  • Java Code Snippet
  • Views: 12556
  • Java RSS
0

Demonstration of Linked List

by on Jun 11th, 2007
Demonstration of Linked list in java. Very simple and well commented java code for the beginners. Linked list of points are used to create a Polyline and display it

More java codes at www.mycplus.com
Java Code Snippet (Toggle Plain Text)
  1. //****************************
  2. //LinkedList.java
  3. //****************************
  4. // Modified to support backwards traversal of the list.
  5. // Additions and modifications are marked by ***.
  6.  
  7. public class LinkedList
  8. {
  9. private ListItem start; // First ListIem in the list.
  10. private ListItem end; // Last ListIem in the list.
  11. private ListItem current; // The current item for iterating.
  12.  
  13. // Constructor to create a list containing one object:
  14. public LinkedList(Object item)
  15. {
  16. start = new ListItem(item); // item is the start
  17. current = end = start; // as well as the end and current.
  18. }
  19.  
  20. // Construct a linked list from an array of objects:
  21. public LinkedList(Object[] items)
  22. {
  23. // Create a one item list:
  24. start = new ListItem(items[0]); // First item is the start
  25. end = start; // as well as the end.
  26.  
  27. // Now add the other items:
  28. for(int i = 1; i < items.length; i++)
  29. addItem(items[i]);
  30. }
  31.  
  32. // Add an item object to the list:
  33. public void addItem(Object item)
  34. {
  35. ListItem newEnd = new ListItem(item); // Create a new ListItem.
  36. end.setNext(newEnd); // Set next variable for old end as new end.
  37. newEnd.setPrevious(end); // So previous for new item. ***
  38. current = end = newEnd; // Store new item as end and current. ***
  39. }
  40.  
  41. // Get the first object in the list:
  42. public Object getFirst()
  43. {
  44. current = start;
  45. return start.getObject();
  46. }
  47.  
  48. // Additional method to get the last object in the list: ***
  49. public Object getLast()
  50. {
  51. current = end;
  52. return end.getObject();
  53. }
  54.  
  55. // Get the next object in the list:
  56. public Object getNext()
  57. {
  58. current = current.getNext(); // Get the reference to the next item.
  59. return current == null ? null : current.getObject();
  60. }
  61.  
  62. // Additional method to get the previous object in the list: ***
  63. public Object getPrevious()
  64. {
  65. current = current.getPrevious(); // Get the reference to the previous item.
  66. return current == null ? null : current.getObject();
  67. }
  68. }
  69.  
  70.  
  71. //****************************
  72. //ListItem.java
  73. //****************************
  74. // Modified to support backwards traversal of the list.
  75. // Additions and modifications are marked by ***.
  76.  
  77. public class ListItem
  78. {
  79. ListItem next; // Refers to next item in the list.
  80. ListItem previous; // Refers to the previous item. ***
  81. Object item; // The item for this ListItem.
  82.  
  83. // Constructor:
  84. public ListItem(Object item)
  85. {
  86. this.item = item; // Store the item.
  87. next = previous = null; // Set next and previous to null. ***
  88. }
  89.  
  90. // Set the pointer to the next ListItem:
  91. public void setNext(ListItem next)
  92. {
  93. this.next = next; // Store reference to the next item.
  94. }
  95.  
  96. // Additional method to set the pointer to the previous ListItem: ***
  97. public void setPrevious(ListItem previous)
  98. {
  99. this.previous = previous; // Store reference to the previous item.
  100. }
  101.  
  102. // Get the next item in the list:
  103. public ListItem getNext()
  104. {
  105. return next;
  106. }
  107.  
  108. // Additional method to get the previous item in the list: ***
  109. public ListItem getPrevious()
  110. {
  111. return previous;
  112. }
  113.  
  114. // Get the object for this item:
  115. public Object getObject()
  116. {
  117. return item;
  118. }
  119.  
  120. // Return class name & object:
  121. public String toString()
  122. {
  123. return "ListItem " + item;
  124. }
  125. }
  126.  
  127.  
  128. //****************************
  129. //Point.java
  130. //****************************
  131.  
  132. public class Point
  133. {
  134. double x;
  135. double y;
  136.  
  137. // Constructors:
  138.  
  139. public Point()
  140. {
  141. x = 0.0;
  142. y = 0.0;
  143. }
  144.  
  145. // Construct a Point from its coordinates:
  146. public Point(double x, double y)
  147. {
  148. this.x = x;
  149. this.y = y;
  150. }
  151.  
  152. // Construct a Point from another Point:
  153. public Point(Point point)
  154. {
  155. x = point.x;
  156. y = point.y;
  157. }
  158.  
  159. // Method to return a point defined relative to this
  160. // point in coordinates:
  161. public Point add(Point z)
  162. {
  163. return new Point(x+z.x,y+z.y);
  164. }
  165.  
  166. // Overrides the method inherited from Object:
  167. public String toString()
  168. {
  169. return "Point: " + x + "," + y;
  170. }
  171. }
  172.  
  173. //****************************
  174. //PolyLine.java
  175. //****************************
  176.  
  177. // MODIFIED TO OUTPUT THE POLYLINE POINTS IN REVERSE ORDER.
  178. public class PolyLine
  179. {
  180. LinkedList polyline; // The linked list of points.
  181.  
  182. // Construct a polyline from an array of coordinate pairs:
  183. public PolyLine(double[][] coords)
  184. {
  185. Point[] points = new Point[coords.length]; // Array to hold points.
  186.  
  187. // Create points from the coordinates:
  188. for(int i = 0; i < coords.length ; i++)
  189. points[i] = new Point(coords[i][0], coords[i][1]);
  190.  
  191. // Create the polyline from the array of points:
  192. polyline = new LinkedList(points);
  193. }
  194.  
  195. // Construct a polyline from an array of points:
  196. public PolyLine(Point[] points)
  197. {
  198. polyline = new LinkedList(points); // Create the polyline.
  199. }
  200.  
  201. // Add a Point object to the list:
  202. public void addPoint(Point point)
  203. {
  204. polyline.addItem(point); // Add the point to the list.
  205. }
  206.  
  207. // Add a point from a coordinate pair to the list:
  208. public void addPoint(double x, double y)
  209. {
  210. polyline.addItem(new Point(x, y)); // Add the point to the list.
  211. }
  212.  
  213. // Output the polyline in reverse order:
  214. public void show()
  215. {
  216. System.out.println("Polyline points are:");
  217.  
  218. // Set the 1st point as start:
  219. Point nextPoint = (Point)polyline.getLast(); // ***
  220.  
  221. // Output the points:
  222. while(nextPoint != null)
  223. {
  224. System.out.println(nextPoint); // Output the current point.
  225. nextPoint = (Point)polyline.getPrevious(); // Get the next point.
  226. }
  227. }
  228. }
  229.  
  230. //****************************
  231. //TryPolyLine.java
  232. //****************************
  233.  
  234. public class TryPolyLine
  235. {
  236. public static void main(String[] args)
  237. {
  238. // Create an array of coordinate pairs:
  239. double[][] coords = { {1., 1.}, {1., 2.}, { 2., 3.},
  240. {-3., 5.}, {-5., 1.}, {0., 0.} };
  241.  
  242. // Create a polyline from the coordinates and display it:
  243. PolyLine polygon = new PolyLine(coords);
  244. polygon.show();
  245.  
  246. // Add a point and display the polyline again:
  247. polygon.addPoint(10., 10.);
  248. polygon.show();
  249.  
  250. // Create Point objects from the coordinate array:
  251. Point[] points = new Point[coords.length];
  252. for(int i = 0; i < points.length; i++)
  253. points[i] = new Point(coords[i][0],coords[i][1]);
  254.  
  255. // Use the points to create a new polyline and display it:
  256. PolyLine newPoly = new PolyLine(points);
  257. newPoly.show();
  258. }
  259. }
Comments on this Code Snippet
Aug 13th, 2007
0

Re: Demonstration of Linked List

Or just use java.util.LinkedList

One should always prefer standard library classes first, existing stable 3rd party libraries second, and only implement one's own version as a last resort for production systems.
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Message:
Previous Thread in Java Forum Timeline: java code
Next Thread in Java Forum Timeline: I want learn about core JAVA....





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC