Advertisement
Ramiiiii

Untitled

Nov 17th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1.  
  2.  
  3. class Person{
  4. private String first;
  5. private String last;
  6. public Person(String x, String y){
  7. first=x;
  8. last=y;
  9. }
  10. public String toString(){
  11. return(first+" "+last);
  12. }
  13.  
  14.  
  15. }
  16. class LinkedListQueue {
  17. //instance variables
  18. private Node start;
  19. private class Node {
  20. private Object data;
  21. private Node next;
  22. Node(Object a){
  23. data=a;
  24. next=null;
  25. }
  26. }
  27. public String toString() {
  28. Node b = start;
  29. /*while (b != null) {
  30. b = b.next;
  31. toString();
  32. }\
  33.  
  34. */
  35. return ("Name:" + b.data.toString() + "\n");
  36. }
  37. public void add(Object element) {
  38. Node second = new Node(element);
  39. if (start == null) {
  40. start = new Node(element);
  41. return;
  42. }
  43. second.next = null;
  44. Node last = start;
  45. while (last.next !=null){
  46. last=last.next;
  47. }
  48. last.next=second;
  49. return;
  50. }
  51.  
  52. public int queueSize(){
  53. int s = 0;
  54. Node n = start;
  55. while(n != null){
  56. s++;
  57. n = n.next;
  58. }
  59. return s;
  60. }
  61.  
  62. //remove from the front of queue, return null if empty
  63. public String remove(){
  64. Node temp=start;
  65. start=start.next;
  66. return temp.data.toString();
  67. }
  68.  
  69. //returns the first item without removing it, null if empty
  70. public String peek(){
  71. if(start==null)
  72. return null;
  73.  
  74. return start.data.toString()+"\n";
  75. }
  76.  
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement