Advertisement
Guest User

linked list example

a guest
Apr 16th, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. from List import *
  2.  
  3. def main():
  4.    arr = eval(input("Enter an array of integers: "))
  5.    strings = ArrayToList(arr)
  6.    integers = ListMap(int, strings)
  7.  
  8.    print("The list of integers:")
  9.    printList(integers)
  10.  
  11.    smallest, index = minimumList(integers)
  12.    print("The smallest value in the list:", smallest, "and it exists at:", index)
  13.  
  14.    subList = getSubList(integers, index)
  15.    print("The sublist:")
  16.    printList(subList)
  17.  
  18.    s = sumList(subList)
  19.    print("Sum of sublist:", s)
  20.  
  21. def sumList(lst):
  22.    s = 0
  23.    spot = lst
  24.    while (spot != None):
  25.       s += head(spot)
  26.       spot = tail(spot)
  27.    return s
  28.  
  29. def getSubList(lst, index):
  30.    spot = lst
  31.    for i in range(0, index, 1):
  32.       spot = tail(spot)
  33.    return tail(spot)
  34.  
  35. def minimumList(lst):
  36.    count = 0
  37.    index = 0
  38.    smallest = head(lst)
  39.    spot = tail(lst)
  40.    while (spot != None):
  41.       if (head(spot) < smallest):
  42.          smallest = head(spot)
  43.          index = count
  44.       count += 1
  45.       spot = tail(spot)
  46.    return smallest,index+1
  47.  
  48. def printList(lst):
  49.    spot = lst
  50.    while (spot != None):
  51.       print(head(spot))
  52.       spot = tail(spot)
  53.  
  54. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement