Advertisement
Guest User

Set class

a guest
Jan 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. public class Set
  2. {
  3.     private int[] s;
  4.     private int tot, curr;
  5.    
  6.     public Set(int max)
  7.     {
  8.         tot = max;
  9.         s = new int[tot];
  10.         curr = 0;
  11.     }
  12.     public boolean add(int n)
  13.     {
  14.         for(int i = 0; i < s.length; i++)
  15.             if(s[i] == n)
  16.                 return false;
  17.         for(int j = curr; j < s.length; j++)
  18.         {
  19.             if(curr < s.length && s[j] != n)
  20.             {
  21.                 curr++;
  22.                 s[j] = n;
  23.                 return true;
  24.             }
  25.             else if(j >= 1 && curr < s.length && n != s[j - 1])
  26.             {
  27.                 curr++;
  28.                 s[j] = n;
  29.                 return true;
  30.             }
  31.         }
  32.         return false;
  33.     }
  34.    
  35.     public boolean isMember(int n)
  36.     {
  37.         for(int i = 0; i < s.length; i++)
  38.             if(s[i] == n)
  39.                 return true;
  40.         return false;
  41.     }
  42.     public void show()
  43.     {
  44.             if(curr == 0)
  45.             {
  46.                 System.out.print("()");
  47.             }
  48.             else if(curr == 1)
  49.             {
  50.                 System.out.print("(" + s[0] + ")");
  51.             }
  52.             else
  53.             {
  54.                 System.out.print("(");
  55.                 for(int j = 0; j < curr; j++)
  56.                 {
  57.                     if(j == curr - 1)
  58.                         System.out.print(s[j]);
  59.                     else
  60.                         System.out.print(s[j] + ",");
  61.                 }
  62.                 System.out.print(")");
  63.             }
  64.             System.out.println();
  65.         }
  66.     public boolean isEmpty()
  67.     {
  68.         return (curr == 0);
  69.     }
  70.    
  71.     public boolean isFull()
  72.     {
  73.         return (curr == s.length);
  74.     }
  75.    
  76.     public boolean remove(int n)
  77.     {
  78.         int c = 0;
  79.         for(int i = 0; i < s.length - 1; i++)
  80.         {
  81.             if(s[i] == n && curr != 0)
  82.             {
  83.                 c = i;
  84.                 for(int j = c; j < s.length - 1; j++)
  85.                 {
  86.                     s[j]  = s[j + 1];
  87.                 }
  88.                 curr--;
  89.                 return true;
  90.             }
  91.         }
  92.         return false;
  93.     }
  94.    
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement