Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace CK_Assessment3
  8. {
  9.  
  10.  
  11. ///<summary>
  12. /// anarray based list for integers
  13. ///</summary>
  14. class IntArrayList
  15. {
  16.  
  17.  
  18.  
  19. private int count;
  20.  
  21.  
  22. private int[] values;
  23.  
  24.  
  25. //Default Constructor setting maximum size of list to 20
  26. public IntArrayList()
  27. {
  28. values = new int[20];
  29. count= 0;
  30. }
  31.  
  32. //Overloaded Constructor
  33. public IntArrayList(int capacity)
  34. {
  35. values = new int[capacity];
  36. count = 0;
  37. }
  38.  
  39.  
  40.  
  41. //Check to see if list is empty
  42. public bool isEmpty()
  43. {
  44. return(count==0); //
  45. }
  46.  
  47. //Checks to see if list is full
  48. public bool isFull()
  49. {
  50. return values.Length==count; //Return full if no more space
  51.  
  52. }
  53.  
  54. //Adds value to first postition of array list
  55. public void addFirst(int value)
  56. {
  57. if(isFull())
  58. {
  59. throw new Exception("list full"); //Error message display if list is full
  60. }
  61. if(isEmpty())
  62. {
  63. addLast(value); //Adds value to first postion
  64. }
  65. else
  66. {
  67. for(int pos =count;pos>0;pos--)
  68. {
  69. values[pos]=values[pos-1]; ////Loops through list, decrementing position in list by 1
  70. }
  71. values[0]=value; //Sets first value in list to new value entered
  72. count++; //Count increased by 1
  73. }
  74. }
  75.  
  76.  
  77. //Adds value to last position
  78. public void addLast(int value)
  79. {
  80. if(isFull())
  81. {
  82. throw new Exception("list full"); //Checks if list is full, displays message if full
  83. }
  84. values[count]=value; //Sets last value in list to new value entered
  85. count++;
  86.  
  87. }
  88.  
  89. //Removing last value
  90. public int removeLast()
  91. {
  92.  
  93. if(isEmpty())
  94. {
  95. throw new Exception("list Empty"); //Checks if list is empty, displays message if empty
  96. }
  97.  
  98. int popValue=values[count-1]; //Removes last value from list
  99. count--;
  100. return popValue; //Returns value that is removed
  101.  
  102. }
  103.  
  104. //Returns size of list
  105. public int size()
  106. {
  107. return count;
  108. }
  109.  
  110. //consoleUI for testing
  111. public void display()
  112. {
  113. if(count==0)
  114. Console.WriteLine("list is empty");
  115. else
  116. {
  117. Console.WriteLine("list has"+ count+"items");
  118. for(int i = count-1;i>=0;i--) Console.WriteLine("value:"+ values[i]);
  119. }
  120. }
  121.  
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement