Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 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.         for(int j = 0; j < s.length; j++)
  17.         {
  18.             if(curr < s.length && s[j] != n)
  19.             {
  20.                 curr++;
  21.                 s[j] = n;
  22.                 return true;
  23.             }
  24.             else if(j >= 1 && curr < s.length && n != s[j - 1])
  25.             {
  26.                 curr++;
  27.                 s[j] = n;
  28.                 return true;
  29.             }
  30.         }
  31.         return false;
  32.     }
  33.    
  34.     public boolean isMember(int n)
  35.     {
  36.         for(int i = 0; i < s.length; i++)
  37.             if(s[i] == n)
  38.                 return true;
  39.         return false;
  40.     }
  41.     public void show()
  42.     {
  43.         for(int i = 0; i < curr; i++)
  44.         {
  45.             if(curr == 0)
  46.             {
  47.                 System.out.print("()");
  48.                 break;
  49.             }
  50.             if(curr != 0 && i == 0)
  51.                 System.out.print("(" + s[i] + ")" );
  52.             else if(curr != 0 && i > 0)
  53.                 System.out.println("(" + s[i] + ")");
  54.         }
  55.         System.out.println();
  56.     }
  57.     public boolean isEmpty()
  58.     {
  59.         return (curr == 0);
  60.     }
  61.    
  62.     public boolean isFull()
  63.     {
  64.         return (curr == s.length);
  65.     }
  66.    
  67.     public boolean remove(int n)
  68.     {
  69.         int c = 0;
  70.         for(int i = 0; i < s.length - 1; i++)
  71.         {
  72.             if(s[i] == n && curr != 0)
  73.             {
  74.                 c = i;
  75.                 for(int j = c; j < s.length - 1; j++)
  76.                     s[j]  = s[j + 1];
  77.                 return true;
  78.             }
  79.         }
  80.         return false;
  81.     }
  82.    
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement