Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. package chow6542_a03;
  2.  
  3. public class LinkMaster<T> {
  4. private class Node<T> {
  5. private T data;
  6. private Node<T> link;
  7.  
  8. public Node() {
  9. data = null;
  10. link = null;
  11. }
  12.  
  13. public Node(T newData, Node<T> linkValue) {
  14. data = newData;
  15. link = linkValue;
  16. }
  17. }
  18.  
  19. private Node<T> head;
  20.  
  21. public LinkMaster() {
  22. head = null;
  23. }
  24.  
  25. public void addToStart(T itemData) {
  26. head = new Node<T>(itemData, head);
  27. }
  28.  
  29. public boolean deleteHeadNode() {
  30. if (head != null) {
  31. System.out.println();
  32. System.out.println("eliminate Suitor # " + head.data);
  33. head = head.link;
  34. return true;
  35. } else {
  36. return false;
  37. }
  38. }
  39.  
  40. public void removeNode(int deleteSpot){
  41. Node<T> position = head;
  42. for(int i = 1; i < deleteSpot - 1; i++){
  43. position = position.link;
  44. }
  45. System.out.println();
  46. System.out.println("eliminate Suitor # " + position.link.data);
  47. position.link = position.link.link;
  48. }
  49.  
  50. public void removeSuitors(int size){
  51. int spot = 1;
  52. while(size > 1){
  53. for(int i = 0; i < 2; i++){
  54. if(spot == size){
  55. spot = 1;
  56. } else if(spot > size){
  57. spot = 1;
  58. } else {
  59. spot++;
  60. }
  61. }
  62. if(spot == 1){
  63. deleteHeadNode();
  64. size--;
  65. } else {
  66. removeNode(spot);
  67. size--;
  68. if(spot > size){
  69. spot = 1;
  70. }
  71. }
  72. outputList();
  73. }
  74. }
  75.  
  76. public int size() {
  77. int count = 0;
  78. Node<T> position = head;
  79. while (position != null) {
  80. count++;
  81. position = position.link;
  82. }
  83. return count;
  84. }
  85.  
  86. // other methods similar to LinkedList3
  87. // print the sequence of suitor eliminations
  88. // after each elimination, the resulting list is printed
  89. public void printWinningPosition() {
  90. Node<T> position = head;
  91. System.out.println();
  92. System.out.println("The winning position is: " + position.data);
  93. }
  94.  
  95. // print the list of suitors
  96. public void outputList() {
  97. Node<T> position = head;
  98. while (position != null) {
  99. System.out.print(position.data + " ");
  100. position = position.link;
  101. }
  102. }
  103. }// end LinkedList3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement