Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 2. 링크드리스트
- function LinkedList() {
- this.length = 0;
- this.head = null;
- }
- // 노드
- function Node(v) {
- this.v = v;
- this.next = null;
- }
- // 추가
- LinkedList.prototype.append = function(v) {
- var node = new Node(v);
- var current;
- if(this.head) {
- current = this.head;
- while(current.next) {
- current = current.next;
- }
- current.next = node;
- } else {
- this.head = node;
- }
- this.length++;
- }
- LinkedList.prototype.removeAt = function(p) {
- var current = this.head;
- var prev, idx = 0;
- if (p >= 0 && p < this.length) {
- while(idx < this.length) {
- if(idx === p) {
- prev.next = current.next;
- this.length--;
- return current.v;
- } else {
- prev = current;
- current = current.next;
- idx++;
- }
- }
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement