eternalmeg

list

Jun 12th, 2024
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. class List {
  2. constructor() {
  3. this.elements = [];
  4. this.size = 0;
  5. }
  6.  
  7. add(element) {
  8. this.elements.push(element);
  9. this.elements.sort((a, b) => a - b);
  10. this.size = this.elements.length;
  11. }
  12.  
  13. remove(index) {
  14. if (index < 0 || index >= this.elements.length) {
  15. throw new Error('Index out of range');
  16. }
  17. this.elements.splice(index, 1);
  18. this.size = this.elements.length;
  19. }
  20.  
  21. get(index) {
  22. if (index < 0 || index >= this.elements.length) {
  23. throw new Error('Index out of range');
  24. }
  25. return this.elements[index];
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment