Guest User

Untitled

a guest
Jan 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. public class IntQueue {
  2.  
  3. private int[] items_;
  4. private int top_;
  5. private int capacity_;
  6. private int iter_;
  7.  
  8. public IntQueue(int capacity)
  9. {
  10. if(capacity <=0) capacity = 10;
  11.  
  12. capacity_ = capacity;
  13. top_=0;
  14. count_ = 0;
  15. iter_=0;
  16.  
  17. items_= new int[capacity_];
  18. }
  19.  
  20. public void push_back(int value)
  21. {
  22. if(top_>= capacity_)
  23. overflow();
  24.  
  25. items_[top_++]=value;
  26. count_++;
  27. }
  28.  
  29. public int front()
  30. {
  31. if(top_<=0)
  32. return 0;
  33.  
  34. int temp=0;
  35. temp=items_[iter_];
  36.  
  37. count_--;
  38. iter_++;
  39. return temp;
  40.  
  41. }
  42.  
  43. public IntQueue clone()
  44. {
  45. IntQueue result = new IntQueue(capacity_);
  46.  
  47. for(int i=0 ; i<top_; ++i)
  48. {
  49. result.push_back(items_[i]);
  50.  
  51. }
  52.  
  53. /*for(int i=0 ; i<top_ ; ++i)
  54. {
  55.  
  56. result.items_[i] = items_[i];
  57. }*/
  58.  
  59. return result;
  60.  
  61. }
  62.  
  63. public void log()
  64. {
  65.  
  66. for(int i=0 ; i <top_; ++i)
  67. {
  68. System.out.print(items_[i]);
  69. if(i<top_ -1)
  70. System.out.print(", ");
  71. }
  72. System.out.println();
  73. }
  74.  
  75. }
  76.  
  77. private void overflow()
  78. {
  79. int[] newItem = new int[capacity_*2];
  80.  
  81. for(int i=0 ; i <top_; ++i)
  82. newItem[i] = items_[i];
  83. items_=newItem;
  84. capacity_ *=2;
  85.  
  86. }
  87.  
  88. public static void main(String args[])
  89. {
  90. IntQueue queue = new IntQueue(2);
  91.  
  92. System.out.println("queue push 3: "); queue.push_back(3);
  93. System.out.println("queue push 2: "); queue.push_back(2);
  94. System.out.println("queue push 1: "); queue.push_back(1);
  95.  
  96. System.out.print("queue log: "); queue.log();
  97.  
  98. System.out.println("front " + queue.front());
  99. System.out.println("front " + queue.front());
  100.  
  101. System.out.print("queue log: "); queue.log();
  102.  
  103. System.out.println("queue push 12: "); queue.push_back(12);
  104. System.out.println("queue push 11: "); queue.push_back(11);
  105. System.out.println("queue push 21: "); queue.push_back(21);
  106. System.out.println("queue push 31: "); queue.push_back(31);
  107.  
  108. System.out.print("queue log: "); queue.log();
  109.  
  110. System.out.println("front " + queue.front());
  111. System.out.println("front " + queue.front());
  112.  
  113. System.out.print("clone queue log: "); queue.clone().log();
  114.  
  115.  
  116. }
  117.  
  118. }
  119.  
  120. void removeAt(int i) {
  121. final E[] items = this.items;
  122. // if removing front item, just advance
  123. if (i == takeIndex) {
  124. items[takeIndex] = null;
  125. takeIndex = inc(takeIndex);
  126. } else {
  127. // slide over all others up through putIndex.
  128. for (;;) {
  129. int nexti = inc(i);
  130. if (nexti != putIndex) {
  131. items[i] = items[nexti];
  132. i = nexti;
  133. } else {
  134. items[i] = null;
  135. putIndex = i;
  136. break;
  137. }
  138. }
  139. }
  140. --count;
  141. notFull.signal();
  142. }
  143.  
  144. import java.util.Iterator;
  145. import java.util.LinkedList;
  146. import java.util.Queue;
  147.  
  148. public class QueueExample {
  149.  
  150. public static void main(String[] args) {
  151.  
  152. Queue<String> qe=new LinkedList<String>();
  153.  
  154. qe.add("b");
  155. qe.add("a");
  156. qe.add("c");
  157. qe.add("e");
  158. qe.add("d");
  159.  
  160. Iterator it=qe.iterator();
  161.  
  162. System.out.println("Initial Size of Queue :"+qe.size());
  163.  
  164. while(it.hasNext())
  165. {
  166. String iteratorValue=(String)it.next();
  167. System.out.println("Queue Next Value :"+iteratorValue);
  168. }
  169.  
  170. // get value and does not remove element from queue
  171. System.out.println("Queue peek :"+qe.peek());
  172.  
  173. // get first value and remove that object from queue
  174. System.out.println("Queue poll :"+qe.poll());
  175.  
  176. System.out.println("Final Size of Queue :"+qe.size());
  177. }
  178. }
  179.  
  180. int queue[SIZE];
  181. int first = 0;
  182. int last = 0;
  183.  
  184. void enque(int i) {
  185. if(last == SIZE)
  186. throw new RuntimeExeption("Queue is full");
  187. queue[last++] = i;
  188. }
  189.  
  190. int deque() {
  191. if(first == last)
  192. throw new RuntimeExeption("Queue is empty");
  193. return queue[first++];
  194. }
  195.  
  196. import java.io.*;
  197. import java.lang.*;
  198. class clrqueue
  199. {
  200. DataInputStream get=new DataInputStream(System.in);
  201. int a[];
  202. int i,front=0,rear=0,n,item,count=0;
  203. void getdata()
  204. {
  205. try
  206. {
  207. System.out.println("Enter the limit");
  208. n=Integer.parseInt(get.readLine());
  209. a=new int[n];
  210. }
  211. catch(Exception e)
  212. {
  213. System.out.println(e.getMessage());
  214. }
  215. }
  216. void enqueue()
  217. {
  218. try
  219. {
  220. if(count<n)
  221. {
  222. System.out.println("Enter the element to be added:");
  223. item=Integer.parseInt(get.readLine());
  224. a[rear]=item;
  225. rear++;
  226. count++;
  227. }
  228. else
  229. System.out.println("QUEUE IS FULL");
  230. }
  231. catch(Exception e)
  232. {
  233. System.out.println(e.getMessage());
  234. }
  235. }
  236. void dequeue()
  237. {
  238. if(count!=0)
  239. {
  240. System.out.println("The item deleted is:"+a[front]);
  241. front++;
  242. count--;
  243. }
  244. else
  245. System.out.println("QUEUE IS EMPTY");
  246. if(rear==n)
  247. rear=0;
  248. }
  249. void display()
  250. {
  251. int m=0;
  252. if(count==0)
  253. System.out.println("QUEUE IS EMPTY");
  254. else
  255. {
  256. for(i=front;m<count;i++,m++)
  257. System.out.println(" "+a[i]);
  258. }
  259. }
  260. }
  261. class Myqueue
  262. {
  263. public static void main(String arg[])
  264. {
  265. DataInputStream get=new DataInputStream(System.in);
  266. int ch;
  267. clrqueue obj=new clrqueue();
  268. obj.getdata();
  269. try
  270. {
  271. do
  272. {
  273. System.out.println(" 1.Enqueue 2.Dequeue 3.Display 4.Exit");
  274. System.out.println("Enter the choice");
  275. ch=Integer.parseInt(get.readLine());
  276. switch (ch)
  277. {
  278. case 1:
  279. obj.enqueue();
  280. break;
  281. case 2:
  282. obj.dequeue();
  283. break;
  284. case 3:
  285. obj.display();
  286. break;
  287. }
  288. }
  289. while(ch!=4);
  290. }
  291. catch(Exception e)
  292. {
  293. System.out.println(e.getMessage());
  294. }
  295. }
  296. }
Add Comment
Please, Sign In to add comment