Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. Program1 1 – Generic Linked List
  2. public class GenericProject {
  3. public static void main(String[] args) {
  4.  
  5.  
  6. }
  7. }
  8.  
  9. abstract class GenericList<I> {
  10.  
  11. Node<I> head;
  12. private int length;
  13.  
  14. // abstract prototye that will be overridden by the GenericQueue and GenericStack class
  15. public abstract void add(I data);
  16.  
  17. // prints list items, one per line
  18. public void print() {
  19. if (head == null) {
  20. System.out.println("The list is empty.");
  21. return;
  22. }
  23.  
  24. Node<I> n = head;
  25. while (n!=null) {
  26. System.out.println(n.data);
  27. n = n.next;
  28. }
  29. }
  30.  
  31. // returns the first value in the list and deletes the node
  32. public I delete() {
  33. if (head == null) { // if the list is empty, nothing needs to be deleted
  34. return null;
  35. }
  36.  
  37. I contents = head.data; // save the contents of the first node
  38. head = head.next; // move the head, deleting the node
  39. setLength(getLength()-1);
  40. return contents;
  41. }
  42.  
  43. // gets the current length of the list
  44. public int getLength() { return length; }
  45.  
  46. // sets the current length of the list
  47. public void setLength(int len) { length = len; }
  48.  
  49.  
  50. // node class for individual list items
  51. class Node<T> {
  52. T data;
  53. Node next;
  54.  
  55. Node(T data) {
  56. this.data = data;
  57. setLength(getLength()+1);
  58. }
  59. }
  60.  
  61. }
  62.  
  63.  
  64. class GenericQueue<I> extends GenericList<I> { // FIFO
  65.  
  66. // constructor that takes a parameter to initialize the head
  67. GenericQueue(I data) {
  68. head = new Node<I>(data);
  69. }
  70.  
  71. // overrides add() in GenericList. the enqueue operation is implemented
  72. public void add(I data) {
  73. Node<I> n = new Node(data); // create a new node
  74. if (head == null) { // empty list case
  75. head = n;
  76. }
  77. else {
  78. Node<I> t = head;
  79. while (t.next != null) {
  80. t = t.next;
  81. }
  82. t.next = n;
  83. }
  84. }
  85.  
  86. // calls add() with enqueue implemented
  87. public void enqueue(I data) {
  88. add(data);
  89. }
  90.  
  91. // calls delete() with dequeue implemented
  92. public I dequeue() {
  93. return delete();
  94. }
  95. }
  96.  
  97.  
  98. class GenericStack<I> extends GenericList<I> { // LIFO
  99.  
  100. // constructor that takes a parameter to initialize the head
  101. GenericStack(I data) {
  102. head = new Node(data);
  103. }
  104.  
  105. // overrides add() in GenericList. the push operation is implemented
  106. public void add(I data) {
  107. Node<I> t = head;
  108. head = new Node(data);
  109. head.next = t;
  110. }
  111.  
  112. // calls add() with push implemented
  113. public void push(I data) {
  114. add(data);
  115. }
  116.  
  117. // calls delete with pop implemented
  118. public I pop() {
  119. return delete();
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement