Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class List {
- constructor() {
- this.elements = [];
- this.size = 0;
- }
- add(element) {
- this.elements.push(element);
- this.elements.sort((a, b) => a - b);
- this.size = this.elements.length;
- }
- remove(index) {
- if (index < 0 || index >= this.elements.length) {
- throw new Error('Index out of range');
- }
- this.elements.splice(index, 1);
- this.size = this.elements.length;
- }
- get(index) {
- if (index < 0 || index >= this.elements.length) {
- throw new Error('Index out of range');
- }
- return this.elements[index];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment