Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3.  
  4. /*
  5. * To change this license header, choose License Headers in Project Properties.
  6. * To change this template file, choose Tools | Templates
  7. * and open the template in the editor.
  8. */
  9.  
  10. /**
  11. *
  12. * @author stillmen
  13. */
  14. public class Tester{
  15. public static void main(String[]args){
  16. Scanner sc=new Scanner(System.in);
  17. QueueLL q=new QueueLL();
  18. int masukan=sc.nextInt();
  19. while(masukan!=-1){
  20. System.out.println(q.poll());
  21. System.out.println(q.peek());
  22. q.offer(masukan);
  23. masukan=sc.nextInt();
  24. }
  25. }
  26. }
  27.  
  28. class QueueLL {
  29. protected MyLinkedList linkedList;
  30.  
  31. public QueueLL(){
  32. linkedList=new MyLinkedList();
  33. }
  34.  
  35. public boolean isEmpty(){
  36. if(linkedList.size()==0){
  37. return true;
  38. }
  39. else{
  40. return false;
  41. }
  42. }
  43.  
  44. public boolean offer(Object newObj){
  45. linkedList.addLast(newObj);
  46. return true;
  47. }
  48.  
  49. public Object poll(){
  50. if(!this.isEmpty()){
  51. return linkedList.removeFirst();
  52. }
  53. else{
  54. return null;
  55. }
  56. }
  57.  
  58. public Object peek(){
  59. if(!this.isEmpty()){
  60. return linkedList.get(0);
  61. }
  62. else{
  63. return null;
  64. }
  65. }
  66.  
  67. public int size(){
  68. return linkedList.size();
  69. }
  70. }
  71.  
  72. class MyLinkedList{
  73. Node head;
  74. Node tail;
  75. int count;
  76.  
  77. MyLinkedList(Node head){
  78. this.head=head;
  79. count++;
  80. }
  81.  
  82. MyLinkedList(){
  83. this.head=null;
  84. }
  85.  
  86. boolean empty(){
  87. if(head==null){
  88. return true;
  89. }
  90. else{
  91. return false;
  92. }
  93. }
  94.  
  95. void addLast(Object info){
  96. Node newNode=new Node(info);
  97. if(head==null){
  98. this.head=newNode;
  99. this.tail=newNode;
  100. }
  101. else{
  102. this.tail.setNext(newNode);
  103. this.tail=newNode;
  104. }
  105. count++;
  106. }
  107.  
  108. Object removeFirst(){
  109. if(head==null){
  110. return null;
  111. }
  112. else{
  113. count++;
  114. Node removedNode=head;
  115. head=head.getNext();
  116. if(head==null){
  117. tail=null;
  118. }
  119. return removedNode.getInfo();
  120. }
  121. }
  122.  
  123. Object peek(){
  124. if(head==null){
  125. return "Stack is empty";
  126. }
  127. else{
  128. return head;
  129. }
  130. }
  131.  
  132. int size(){
  133. return this.count;
  134. }
  135. }
  136.  
  137. class Node{
  138. Object info;
  139. Node next;
  140.  
  141. Node(Object info){
  142. this.info=info;
  143. }
  144.  
  145. Object getInfo(){
  146. return this.info;
  147. }
  148.  
  149. void setInfo(Object info){
  150. this.info=info;
  151. }
  152.  
  153. Node getNext(){
  154. return this.next;
  155. }
  156.  
  157. void setNext(Node next){
  158. this.next=next;
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement