Guest User

Untitled

a guest
Jan 16th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. package list;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class DemoList
  6. {
  7. int[] list = new int[10];
  8.  
  9. public static void main(String[] args)
  10. {
  11. DemoList list1 = new DemoList();
  12.  
  13. list1.insertItem(42);
  14. list1.insertItem(43);
  15. list1.insertItem(44);
  16. list1.insertItem(45);
  17. list1.insertItem(46);
  18.  
  19. System.out.println("before " + Arrays.toString(list1.list));
  20.  
  21. list1.removeItem(2);
  22.  
  23. System.out.println("after " + Arrays.toString(list1.list));
  24.  
  25. }
  26.  
  27. void insertItem(int value)
  28. {
  29. for (int i = 0; i < list.length; i++)
  30. {
  31. if (list[i] == 0)
  32. {
  33. list[i] = value;
  34. break;
  35. }
  36. }
  37. }
  38.  
  39. void removeItem(int index)
  40. {
  41. int i = 0;
  42.  
  43. for (i = index ; i < list.length - 1 ; i++)
  44. {
  45. // System.out.print(list[i] + " <-- " + list[i+1] + "\n");
  46. list[i] = list[i + 1];
  47. }
  48.  
  49. list[list.length - 1] = 0;
  50. // set the last element as 0
  51. }
  52.  
  53. int isEmpty()
  54. {
  55. if (list[0] == 0)
  56. {
  57. return 1;
  58. }
  59. else
  60. {
  61. return 0;
  62. }
  63. }
  64.  
  65.  
  66. int isFull()
  67. {
  68. if (list[list.length - 1] != 0)
  69. {
  70. return 1;
  71. }
  72. else
  73. {
  74. return 0;
  75. }
  76. }
  77.  
  78. }
Add Comment
Please, Sign In to add comment