Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

could you use the above code to modify? will Exercise #1: Edit, compile and run the following code in NetBeans. SinglyLinkedList.java 1. class Node {

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
could you use the above code to modify?
will Exercise #1: Edit, compile and run the following code in NetBeans. SinglyLinkedList.java 1. class Node { 11/ Implementation of Singly Linked List 2. public int info; 3. public String name; 4. public double cgpa; 5. public Node next; 6. 7. public Node() { } 8. public Node(int i, String nm, double g) { 9. info i; name=nm; cgpa=g; 10. 11. } // 1/////// 12. class mySLList { 13. protected Node head, tail; 14. 15. public mySLList() { head = tail = null; } 16. 17. public boolean isEmpty() { return head == null; } 18. 19. public void addToHead(int el, String nm, double g) { 20. Node newNode = new Node (el, nm, g); 21. if (head == null) // empty 22. tail = head = newNode; 23. else { 24. newNode. next = head; 25. head = newNode; 26. 27. } 1/7 28. 29. public void addToTail(int el, String nm, double g) { 30. Node newNode = new Node (el, nm, g); 31. newNode. next = null; 32. if (head == null) // empty 33. head = tail = newNode; 34. else { 35. tail.next = newNode; 36. tail newNode; 37. mm 38. 17/11/ 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. public boolean addBefore(int el, String nm, double 8, int target) { if (head. info == target) { //target is 1st node, add before it Node newNode - new Node(el, nm, g); newNode.next = head; head = newNode; else { // find the predecessor of target Node tmp = head; while (tmp.next != null && tmp.next.info != target) tmp = tmp.next; Il check next node if (tmp.next == null) { 7/ not present System.out.println("Target not found"); return false; } else { //tmp is before target Node newNode = new Node(el, nm, e); newNode.next - tmp.next; tmp.next = newNode; } } return true; 1111111 public int deleteFromHead() { // delete head and return info; if(head = null) // empty return -1; int el - head. info; if (head == tail) if only one node on the list; head - tail = null; else head - head.next; return el; 11/17 public int deleteFromTail() { // delete tail and return info; if(head == null) // empty return -1; int el - tail.info; if (head -- tail) 1 if only one node on the list; head = tail = null; else { // if many nodes, find predecessor of tail Node tmp = head; while (tmp.next != tail) tmp = tmp.next; tail - tmp; 17 predecessor of tail becomes tail tail.next = null; } return el; public boolean delete(int el) { // delete target node with el; if (head == null) 1/empty return false; if (head == tail && el == head.info) // one node, target head tail = null; else if (el == head.info) // if more nodes, target is head head head.next; else { // if many nodes in the list, el not head Node prev = head, tmp = head.next; while tmp != null && tmp.info != el) { 69. 70. 71. 72 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 114 prev = prev next; tmp = tmp.next; 98. 99. (tmp != null) { // if el was found; 100 prev.next = tmp.next; 101 if (tmp -- tail) // if el is in last node; 102 tail = prev; 103 104 else else 105 return false; 106 } 107 return true; 108 } 11111 109 public void printAll() { 110 for (Node tmp = head; tmp != null; tmp = tmp.next) 111 System.out.print(tmp.info + " + tmp.name + 112 tmp.cgpa + -> "); 113 System.out.println("null"); } linii Writ 115 public boolean isInList(int el) { 116 Node tmp = head; 117 while (tmp != null && tmp.info != el) 118 tmp = tmp.next; 119 return tmp != null; 120 121) 122 123/11 124 public class SinglyLinkedList { 125 public static void main(String[] args) { 126 127 mySlList L1 = new myslList(); 128 129 System.out.println("Empty ? + L1.isEmpty()); 130 131 L1.addToTail(2019, "Zain", 2.50); 132 L1.addToHead (2020, "Ahmed, 2.75); 133 L1.addToTail 2022, "Majid", 2.95); 134 L1.addToHead (2021, "Khalid", 2.90); 135 L1.printAll(); 136 137 int x=2020; 138 System.out.println("Found "+ x + " + L1.isInList(x)); 139 140 System.out.println("addBefore " + x); 141 L.addBefore(2023, "Hamid", 2.0e, x); 142 L1.printAll(); 143 144 System.out.println("Delete from tail"); 145 L.deleteFromTail(); 146 L1.printAll(); 147 148 System.out.println("Delete " + x); 149 L1.delete(x); 150 L1.printail(); 151 } 152) Task-1 : Modify the above program to implement a QUEUE in a bank as depicted by the following sample output: i. ii. Provide the menu as shown below. Run the program in a loop; stop when user opts 4. Add a customer should add an int at the end using the method addToTail(). Remove a customer should delete the int at front using the method delete From Head (). Show the queue should display the queue using the method printAll (). iv. will Exercise #1: Edit, compile and run the following code in NetBeans. SinglyLinkedList.java 1. class Node { 11/ Implementation of Singly Linked List 2. public int info; 3. public String name; 4. public double cgpa; 5. public Node next; 6. 7. public Node() { } 8. public Node(int i, String nm, double g) { 9. info i; name=nm; cgpa=g; 10. 11. } // 1/////// 12. class mySLList { 13. protected Node head, tail; 14. 15. public mySLList() { head = tail = null; } 16. 17. public boolean isEmpty() { return head == null; } 18. 19. public void addToHead(int el, String nm, double g) { 20. Node newNode = new Node (el, nm, g); 21. if (head == null) // empty 22. tail = head = newNode; 23. else { 24. newNode. next = head; 25. head = newNode; 26. 27. } 1/7 28. 29. public void addToTail(int el, String nm, double g) { 30. Node newNode = new Node (el, nm, g); 31. newNode. next = null; 32. if (head == null) // empty 33. head = tail = newNode; 34. else { 35. tail.next = newNode; 36. tail newNode; 37. mm 38. 17/11/ 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. public boolean addBefore(int el, String nm, double 8, int target) { if (head. info == target) { //target is 1st node, add before it Node newNode - new Node(el, nm, g); newNode.next = head; head = newNode; else { // find the predecessor of target Node tmp = head; while (tmp.next != null && tmp.next.info != target) tmp = tmp.next; Il check next node if (tmp.next == null) { 7/ not present System.out.println("Target not found"); return false; } else { //tmp is before target Node newNode = new Node(el, nm, e); newNode.next - tmp.next; tmp.next = newNode; } } return true; 1111111 public int deleteFromHead() { // delete head and return info; if(head = null) // empty return -1; int el - head. info; if (head == tail) if only one node on the list; head - tail = null; else head - head.next; return el; 11/17 public int deleteFromTail() { // delete tail and return info; if(head == null) // empty return -1; int el - tail.info; if (head -- tail) 1 if only one node on the list; head = tail = null; else { // if many nodes, find predecessor of tail Node tmp = head; while (tmp.next != tail) tmp = tmp.next; tail - tmp; 17 predecessor of tail becomes tail tail.next = null; } return el; public boolean delete(int el) { // delete target node with el; if (head == null) 1/empty return false; if (head == tail && el == head.info) // one node, target head tail = null; else if (el == head.info) // if more nodes, target is head head head.next; else { // if many nodes in the list, el not head Node prev = head, tmp = head.next; while tmp != null && tmp.info != el) { 69. 70. 71. 72 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 114 prev = prev next; tmp = tmp.next; 98. 99. (tmp != null) { // if el was found; 100 prev.next = tmp.next; 101 if (tmp -- tail) // if el is in last node; 102 tail = prev; 103 104 else else 105 return false; 106 } 107 return true; 108 } 11111 109 public void printAll() { 110 for (Node tmp = head; tmp != null; tmp = tmp.next) 111 System.out.print(tmp.info + " + tmp.name + 112 tmp.cgpa + -> "); 113 System.out.println("null"); } linii Writ 115 public boolean isInList(int el) { 116 Node tmp = head; 117 while (tmp != null && tmp.info != el) 118 tmp = tmp.next; 119 return tmp != null; 120 121) 122 123/11 124 public class SinglyLinkedList { 125 public static void main(String[] args) { 126 127 mySlList L1 = new myslList(); 128 129 System.out.println("Empty ? + L1.isEmpty()); 130 131 L1.addToTail(2019, "Zain", 2.50); 132 L1.addToHead (2020, "Ahmed, 2.75); 133 L1.addToTail 2022, "Majid", 2.95); 134 L1.addToHead (2021, "Khalid", 2.90); 135 L1.printAll(); 136 137 int x=2020; 138 System.out.println("Found "+ x + " + L1.isInList(x)); 139 140 System.out.println("addBefore " + x); 141 L.addBefore(2023, "Hamid", 2.0e, x); 142 L1.printAll(); 143 144 System.out.println("Delete from tail"); 145 L.deleteFromTail(); 146 L1.printAll(); 147 148 System.out.println("Delete " + x); 149 L1.delete(x); 150 L1.printail(); 151 } 152) Task-1 : Modify the above program to implement a QUEUE in a bank as depicted by the following sample output: i. ii. Provide the menu as shown below. Run the program in a loop; stop when user opts 4. Add a customer should add an int at the end using the method addToTail(). Remove a customer should delete the int at front using the method delete From Head (). Show the queue should display the queue using the method printAll (). iv

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions

Question

Describe the steps used to make location decisions.

Answered: 1 week ago