Advertisement
snowden_web

Untitled

May 20th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. value = [None]  # значения value
  3. right = [None]  # i next
  4.  
  5. # после iтого элемента добавляет val(новый элемент)
  6.                          
  7. def add_after(i, val):
  8.     global value, right
  9.     value += [val]
  10.     right += [right[i]]
  11.     right[i] = len(value) - 1
  12.     #i    x      k
  13.     #*    *  ->  *
  14.     #|__________^
  15.    
  16.    
  17. def delete_after(i):
  18.     right[i] = right[right[i]]
  19.     #i     X    K
  20.     #*  -> * -> *
  21.     #|__________^
  22.    
  23.  
  24. def print_linked_list():
  25.     i = 0
  26.     while right[i] is not None:
  27.         i = right[i]
  28.         print(value[i], end = ' -> ')
  29.    
  30.    
  31.    
  32.      #0 1 2 3  4
  33.    #v *       **
  34.    #r 4       none
  35.  
  36.  
  37. add_after(0, 25)
  38. add_after(0, 13)
  39. add_after(1, 9)
  40. add_after(2, 7)
  41. add_after(2,100)
  42. delete_after(0)
  43.  
  44.  
  45. print_linked_list()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement