Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. // 2. 링크드리스트
  2. function LinkedList() {
  3. this.length = 0;
  4. this.head = null;
  5. }
  6.  
  7. // 노드
  8. function Node(v) {
  9. this.v = v;
  10. this.next = null;
  11. }
  12.  
  13. // 추가
  14. LinkedList.prototype.append = function(v) {
  15. var node = new Node(v);
  16. var current;
  17.  
  18. if(this.head) {
  19. current = this.head;
  20.  
  21. while(current.next) {
  22. current = current.next;
  23. }
  24.  
  25. current.next = node;
  26. } else {
  27. this.head = node;
  28. }
  29.  
  30. this.length++;
  31. }
  32.  
  33. LinkedList.prototype.removeAt = function(p) {
  34. var current = this.head;
  35. var prev, idx = 0;
  36.  
  37. if (p >= 0 && p < this.length) {
  38. while(idx < this.length) {
  39. if(idx === p) {
  40. prev.next = current.next;
  41. this.length--;
  42. return current.v;
  43. } else {
  44. prev = current;
  45. current = current.next;
  46. idx++;
  47. }
  48. }
  49. }
  50.  
  51. return null;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement