Examples of smaller Java programs that do various interesting things.

ListNode.java 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * A class that represents a node to be used in a linked list.
  3. * These nodes are singly linked.
  4. *
  5. * @author Mike Scott
  6. * @version July 27, 2005
  7. */
  8. public class ListNode
  9. {
  10. // instance variables
  11. // the data to store in this node
  12. private Object myData;
  13. // the link to the next node (presumably in a list)
  14. private ListNode myNext;
  15. /**
  16. * default constructor
  17. * pre: none<br>
  18. * post: getData() = null, getNext() = null
  19. */
  20. public ListNode()
  21. { this(null, null);
  22. }
  23. /**
  24. * create a ListNode that holds the specified data and refers to the specified next element
  25. * pre: none<br>
  26. * post: getData() = item, getNext() = next
  27. * @param item the data this ListNode should hold
  28. * @param next the next node in the list
  29. */
  30. public ListNode(Object data, ListNode next)
  31. { myData = data;
  32. myNext = next;
  33. }
  34. /**
  35. * return the data in this node
  36. * pre: none<br>
  37. * @return the data this ListNode holds
  38. */
  39. public Object getData()
  40. { return myData; }
  41. /**
  42. * return the ListNode this ListNode refers to
  43. * pre: none<br>
  44. * @return the ListNode this ListNode refers to (normally the next one in a list)
  45. */
  46. public ListNode getNext()
  47. { return myNext; }
  48. /**
  49. * set the data in this node
  50. * The old data is over written.<br>
  51. * pre: none<br>
  52. * @param data the new data for this ListNode to hold
  53. */
  54. public void setData(Object data)
  55. { myData = data; }
  56. /**
  57. * set the next node this ListNode refers to
  58. * pre: none<br>
  59. * @param next the next node this ListNode should refer to
  60. */
  61. public void setNext(ListNode next)
  62. { myNext = next; }
  63. }