Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace TASK02_QUEUES
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. char addMore, removeMore, remove;
  13. int[] Array = new int[10];
  14. Array[0] = 3;
  15. Array[1] = 5;
  16. Array[2] = 4;
  17. Queues queuearray = new Queues(Array);
  18. queuearray.Display();
  19. do
  20. {
  21. Console.WriteLine("ENTER VALVE");
  22. int value = Convert.ToInt32(Console.ReadLine());
  23.  
  24. Console.WriteLine("ENTER {0}'s Priority in Number", value);
  25. int Priority = Convert.ToInt32(Console.ReadLine());
  26.  
  27. queuearray.EnQueue(value, Priority);
  28. queuearray.Display();
  29.  
  30. Console.WriteLine("DO YOU WANT TO ADD MORE?(Y/N)");
  31. addMore = Convert.ToChar(Console.ReadLine());
  32. }
  33. while (addMore == 'y' || addMore == 'Y');
  34. Console.Clear();
  35. Console.WriteLine("DO YOU WANT TO REMOVE A VALUE?(Y/N)");
  36. remove = Convert.ToChar(Console.ReadLine());
  37. if (remove == 'y' || remove == 'Y')
  38. {
  39. do
  40. {
  41. queuearray.Display();
  42. queuearray.DeQueue();
  43. queuearray.Display();
  44.  
  45. Console.WriteLine("DO YOU WANT TO REMOVE MORE?(Y/N)");
  46. removeMore = Convert.ToChar(Console.ReadLine());
  47. } while (removeMore == 'y' || removeMore == 'Y');
  48. }
  49. }
  50. }
  51. class Queues
  52. {
  53. private int[] queueArray;
  54. private int upBound, queueElelment;
  55. public Queues(int[] queueArray)
  56. {
  57. this.queueArray = queueArray;
  58. upBound = queueArray.Length;
  59. }
  60. public void Display()
  61. {
  62. Console.Write("QUEUE: ");
  63. for (int i = 0; i < queueArray.Length; i++)
  64. {
  65. Console.Write("{0} ", queueArray[i]);
  66. }
  67. Console.WriteLine();
  68. }
  69. public void EnQueue(int value, int Priority)
  70. {
  71. Priority = Priority - 1;
  72. if (queueArray[Priority] > 0)
  73. {
  74. Console.WriteLine("Element with same priority already existed.\nPlease Add the element again with different priority.");
  75. }
  76. else
  77. {
  78. queueArray[Priority] = value;
  79. }
  80. }
  81. public bool DeQueue()
  82. {
  83. if (queueElelment < upBound)
  84. {
  85. for (int i = 0; i < upBound - 1; i++)
  86. {
  87. if (queueArray[i] != 0)
  88. {
  89. queueArray[i] = queueArray[i + 1];
  90. queueElelment = i + 1;
  91. }
  92. }
  93. queueArray[queueElelment] = 0;
  94. return true;
  95. }
  96. else
  97. {
  98. return false;
  99. }
  100. }
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement