linked list


SUBMITTED BY: Guest

DATE: Jan. 14, 2014, 7:20 p.m.

FORMAT: Text only

SIZE: 2.8 kB

HITS: 18046

  1. Single_Linked_List := Object clone do(
  2. head ::= nil;
  3. // constructor
  4. init := method(
  5. head = nil
  6. );
  7. // methods
  8. isEmpty := method(
  9. self head == nil
  10. );
  11. empty := method(
  12. self head = nil
  13. );
  14. insert_at_tail := method(n,
  15. if(self head == nil,
  16. self setHead(Linked_List_Node clone)
  17. );
  18. self head insert_at_tail(n);
  19. );
  20. insert_at_head := method(n,
  21. newnode := Linked_List_Node clone;
  22. if(self head == nil,
  23. self setHead(newnode),
  24. newnode link = self head;
  25. self setHead(newnode)
  26. );
  27. self head insert_at_head(n);
  28. );
  29. remove_at_head := method(
  30. if(self head == nil, return nil);
  31. if(self head link == nil, self head value println; self setHead(nil), self setHead(self head link));
  32. );
  33. )
  34. )
  35. Linked_List_Node := Object clone do(
  36. value ::= nil;
  37. link ::= nil;
  38. // constructor
  39. init := method(
  40. value = nil;
  41. link = nil
  42. );
  43. // methods
  44. insert_at_tail := method(n,
  45. if(self value == nil,
  46. self setValue(n),
  47. if (self link == nil,
  48. self setLink(Linked_List_Node clone)
  49. );
  50. self link insert_at_tail(n)
  51. );
  52. );
  53. insert_at_head := method(n,
  54. self setValue(n));
  55. push := method(n,
  56. self setValue(n);
  57. );
  58. enqueue := method(n,
  59. self setValue(n));
  60. )
  61. Stack_2 := Object Single_Linked_List clone do(
  62. head ::= nil;
  63. // constructor
  64. init := method(
  65. head = nil
  66. );
  67. // methods
  68. isEmpty := method(
  69. self head == nil
  70. );
  71. push := method(n,
  72. newhead := Linked_List_Node clone;
  73. if(self head == nil, newhead link = nil, newhead link = self head
  74. );
  75. self setHead(newhead);
  76. self head push(n);
  77. );
  78. pop := method(
  79. if(self head == nil, return nil);
  80. if(self head link == nil, self head value println, self head value println; setHead(self head link)
  81. );
  82. )
  83. )
  84. Queue_2 := Single_Linked_List clone do(
  85. head ::= nil;
  86. tail ::= nil;
  87. // constructor
  88. init := method(
  89. head = nil;
  90. tail = nil
  91. );
  92. // methods
  93. enqueue := method(n,
  94. newnode := Linked_List_Node clone;
  95. if(self tail == nil, self setHead(newnode); self setTail(newnode); newnode link = nil, self tail link = newnode; self setTail(newnode)
  96. );
  97. self tail enqueue(n);
  98. );
  99. dequeue := method(
  100. if(self head == nil, return nil, self head value println; setHead(self head link));
  101. )
  102. )

comments powered by Disqus