Advertisement
Felix0301

Linked List Example

Oct 29th, 2014
2,641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Eiffel 3.65 KB | None | 0 0
  1. -- Class APPLICATION
  2. note
  3.     description : "project application root class"
  4.     date        : "$Date$"
  5.     revision    : "$Revision$"
  6.  
  7. class
  8.     APPLICATION
  9.  
  10. inherit
  11.     ARGUMENTS
  12.  
  13. create
  14.     make
  15.  
  16. feature {NONE} -- Initialization
  17.  
  18.     make
  19.         local
  20.             list:MY_LINKED_LIST
  21.         do
  22.             create list.make_with (5)
  23.             list.add (3)
  24.             list.add (7)
  25.             list.add (19)
  26.             list.add (1)
  27.             list.add (563)
  28.             io.put_integer (list.ith (3))
  29.             io.new_line
  30.             list.remove_ith (3)
  31.             io.put_integer (list.ith (3))
  32.             io.new_line
  33.             list.add_ith (3, 99)
  34.             io.put_integer (list.ith (3))
  35.             io.new_line
  36.             list.add_ith (1, 88)
  37.             io.put_integer (list.ith (1))
  38.             io.new_line
  39.         end
  40.  
  41. end
  42.  
  43.  
  44. -- Class MY_LINKED_LIST
  45.  
  46. note
  47.     description: "Summary description for {NEW_1}."
  48.     author: ""
  49.     date: "$Date$"
  50.     revision: "$Revision$"
  51.  
  52. class
  53.     MY_LINKED_LIST
  54.  
  55. create
  56.     make_with
  57.  
  58. feature {NONE} -- Initialization
  59.  
  60.     make_with (i:INTEGER)
  61.             -- Initialization for `Current'.
  62.         do
  63.             create first.set_value(i)
  64.             last := first
  65.         end
  66.  
  67. feature
  68.  
  69.     add (i:INTEGER)
  70.             -- add integer at end of list
  71.         local
  72.             new:MY_LINKABLE
  73.         do
  74.             create  new.set_value(i)
  75.             last.set_next(new)
  76.             last := new
  77.         ensure
  78.             length_correct:old length + 1= length
  79.         end
  80.  
  81.     length: INTEGER
  82.             -- returns lenght of current list
  83.         local
  84.             item:MY_LINKABLE
  85.             i:INTEGER
  86.         do
  87.             from
  88.                 item := first
  89.                 i := 1
  90.             until
  91.                 item.next = void
  92.             loop
  93.                 item := item.next
  94.                 i := i + 1
  95.             end
  96.  
  97.             result := i
  98.         end
  99.  
  100.     remove_ith(i:INTEGER)
  101.             -- removes ith item of the list
  102.         require
  103.             in_range:i > 0 and i <= length
  104.  
  105.         local
  106.             len:INTEGER
  107.  
  108.         do
  109.             len := current.length
  110.             if
  111.                 i = 1
  112.             then
  113.                 first := first.next
  114.  
  115.             else
  116.                 if
  117.                     i = len
  118.                 then
  119.                     ith_intern(i-1).set_next(void)
  120.                     last := ith_intern(i-1)
  121.                 else
  122.                     ith_intern(i-1).set_next(ith_intern(i+1))
  123.                 end
  124.             end
  125.  
  126.         ensure
  127.             length_correct:old length - 1 = length
  128.             first_adapted: i = 1 implies old ith_intern(2) = first
  129.             last_adapted: i = length implies old ith_intern(length - 1) = last
  130.         end
  131.  
  132.     ith(i:INTEGER):INTEGER
  133.             -- return value of ith item
  134.         require
  135.             in_range:i > 0 and i <= length
  136.         do
  137.             result := ith_intern(i).value
  138.         end
  139.  
  140.     add_ith(i,value:INTEGER)
  141.             -- add new linkable at ith position
  142.         require
  143.             in_range: i>0 and i<=length
  144.  
  145.         local
  146.             new: MY_LINKABLE
  147.  
  148.         do
  149.             create new.set_value (value)
  150.             if
  151.                 i = 1
  152.             then
  153.                 new.set_next (first)
  154.                 first := new
  155.             else
  156.                 new.set_next (ith_intern(i))
  157.                 ith_intern(i-1).set_next(new)
  158.             end
  159.  
  160.         ensure
  161.             length_correct:old length + 1= length
  162.  
  163.         end
  164.  
  165.  
  166.     first, last:MY_LINKABLE
  167.  
  168. feature {NONE}
  169.     ith_intern(i:INTEGER):MY_LINKABLE
  170.             -- returns value at ith position
  171.             require
  172.                 in_range:i > 0 and i <= length
  173.             local
  174.                 item:MY_LINKABLE
  175.                 count:INTEGER
  176.             do
  177.                 from
  178.                     count := 1
  179.                     item := first
  180.                 until
  181.                     count >= i
  182.                 loop
  183.                     item := item.next
  184.                     count := count + 1
  185.                 variant
  186.                     i - count
  187.                 end
  188.                 result := item
  189.             end
  190.  
  191. invariant
  192.     first_equals_last: length = 1 implies first = last
  193.     first_unequals_last: length /= 1 implies first /= last
  194. end
  195.  
  196.  
  197. -- Class MY_LINKABLE
  198.  
  199. note
  200.     description: "Summary description for {MY_LINKABLE}."
  201.     author: ""
  202.     date: "$Date$"
  203.     revision: "$Revision$"
  204.  
  205. class
  206.     MY_LINKABLE
  207.  
  208. create
  209.     set_value
  210.  
  211. feature -- Initialization
  212.  
  213.     set_value(val:INTEGER)
  214.             -- Initialization for `Current'.
  215.         do
  216.             value := val
  217.         end
  218.  
  219.     set_next(nxt:MY_LINKABLE)
  220.             -- change referenz to next object in list
  221.         do
  222.             next := nxt
  223.         end
  224.  
  225.     value:INTEGER
  226.  
  227.     next:MY_LINKABLE
  228. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement