新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

《新大榭》- 创大榭地方网络社区先锋品牌 新大榭始终专注于地方网络社区平台的建设 关于我们- [大记事]- 留言建议- [新手报道]

发布 .新大榭软件管家(Excel版) V6.0版 财务/仓库/生产/销售/采购/行政/人事/校园 .公告 - 客户 - 打赏 - 职场 - Excel - Python.

新大榭镜像-音乐-法律-图书-高中课堂-实验 广告是为了能更好的发展 [欢迎商家支持本站互利共赢] 广告位招租.首页黄金广告位等您来!联系 13566035181

查看: 922|回复: 0

[题库] 7933 - [D]实例073:反向输出链表

[复制链接]
发表于 2021-10-21 09:15:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!

您需要 登录 才可以下载或查看,没有账号?注册

x
题目:反向输出一个链表。
- h8 L# S; F4 N5 ^7 o程序分析:无。! e" u4 r" w2 B  |6 K2 I, v, ~; K. B1 H7 F
  1. class Node:
    ( h4 Y( e- H2 \  t8 B
  2. ' T, _1 k" J2 Q1 Y! M9 E# |
  3.     def __init__(self, data):
    * J& u) Z) J8 W7 n
  4.         self.data = data) P# A1 I7 c$ m# L! {
  5.         self.next = None
    7 h  [- C" _" ~% {( e; e4 i& O

  6. $ Z8 S$ g' h) n4 _: `/ r8 Q
  7.     def get_data(self):& R6 U+ `5 c3 `% B- i5 B6 j
  8.         return self.data; N: [2 I+ q* i+ s- D: J

  9.   r: P3 V+ w5 @( U
  10. class List:6 g! A+ q1 O& ?
  11. : i4 B! T& U( ^
  12.     def __init__(self, head):' `; z- S$ {% g$ [/ S
  13.         self.head = head# ?; ]1 f$ m) p: J( T6 e7 K3 S% w- F- h
  14. $ l0 F! V+ v9 t, a3 F
  15.     def is_empty(self): / K" m! R/ q% e+ A* \
  16.         return self.get_len() == 0& @% L$ q. V# ~: J

  17. 5 L1 T2 g& \+ j0 k5 c. b& v
  18.     def get_len(self):  3 _8 U' U5 `# J5 L1 p$ }
  19.         length = 0
    * H" e. |; M) @  ^
  20.         temp = self.head
    - N5 s2 B' d3 y  T
  21.         while temp is not None:/ Z% u- d) b$ F6 [1 R! S
  22.             length += 16 }, f/ c# {* H8 @  ^8 v
  23.             temp = temp.next
    6 J- U7 B" n& G, Q; x# W
  24.         return length; S& h  D: }; S. \& H
  25. 9 v2 q" ?) W" j* U
  26.     def append(self, node):& a) ~3 O) v2 }0 `8 _7 I# f
  27.         temp = self.head! D, t3 P" [0 u3 N5 G/ f( D
  28.         while temp.next is not None:
    : z: j5 p0 ]( e! G7 S
  29.             temp = temp.next
    , K! Q7 a- w' R
  30.         temp.next = node& G( x; M, F' T+ ~. Y
  31. * q9 ]; o; t5 [' _0 V  u% s, z5 E( |, q
  32.     def delete(self, index): ! Q1 q4 j  K. u4 r2 Y2 d6 |8 R
  33.         if index < 1 or index > self.get_len():* x4 x  v! y6 [5 O; @
  34.             print("给定位置不合理")# [, D, W# x& h2 e  _2 x8 {% m
  35.             return1 I" ]. E+ w4 V; K
  36.         if index == 1:; x2 q1 T6 u# C* l" a
  37.             self.head = self.head.next
    " z' ^8 R% ]# [1 d' c! A$ ]
  38.             return; [/ T0 h$ W. P% u
  39.         temp = self.head
    ( d' F9 t# L( B, g( o
  40.         cur_pos = 01 q6 J9 Z# R4 J0 {) _
  41.         while temp is not None:
    6 e) L, \# N+ y
  42.             cur_pos += 1
    : L$ I, }/ U/ T+ E
  43.             if cur_pos == index-1:# Q1 W  p' `% k- M" F
  44.                 temp.next = temp.next.next
    4 I5 L- }# O: {" U  P
  45.             temp = temp.next
    / H8 K; ?" V2 w$ ]& n
  46. 2 B  H1 A# p6 d4 a& v
  47.     def insert(self, pos, node):; \  y% q% l3 b$ o7 f) V' N# C6 U
  48.         if pos < 1 or pos > self.get_len():; P' |+ {; n$ X4 R2 W
  49.             print("插入结点位置不合理")
    2 N# I* Y0 A! A  N# z! ~' j
  50.             return% _; A' `9 n0 m# A+ z+ I- U' n- I
  51.         temp = self.head8 o1 f, [8 j& {; r$ C
  52.         cur_pos = 0
    ( L$ T( l$ s6 n; }
  53.         while temp is not Node:
    $ B5 v, L8 X8 p3 C1 P8 X' S1 [1 t
  54.             cur_pos += 1) Q" q7 F: X- ]. `0 e/ V
  55.             if cur_pos == pos-1:
    - `! z5 k0 e, k7 G
  56.                 node.next = temp.next
    % }6 _8 J% L4 _. d8 w9 z
  57.                 temp.next =node
    8 Z+ n& T1 p( W) Y% P* A
  58.                 break
    1 h* J) A2 H" C* S& y: O
  59.             temp = temp.next1 M: s: @% F% l  z8 p1 @- ]1 G

  60.   L( U/ p" g# C  Z( m6 J
  61.     def reverse(self, head):
    1 M" Z. I% r9 l2 D. f: Q! W
  62.         if head is None and head.next is None:
    : Q1 i# i9 i& v! h: a! g1 T+ `
  63.             return head
    9 L$ }- W  H$ ]  t
  64.         pre = head2 P5 F* {# \6 F
  65.         cur = head.next
    8 G% M. q2 ]" H7 i7 K- D7 }
  66.         while cur is not None:
    7 W6 o6 A9 Z; s" q) h/ p' [" ]3 R
  67.             temp = cur.next
    , l/ T1 y/ v/ v4 R  h+ r
  68.             cur.next = pre
    $ j2 q9 Y# q7 `# z
  69.             pre = cur
    * C& D( ^# ~5 ?& ?2 j; a/ ~
  70.             cur = temp3 S2 `- ?& g" C" K, z& y, y% u
  71.         head.next = None
    0 E. F) P% I& u
  72.         return pre; K* b7 a) k3 F* ?% p1 H/ w2 _
  73. ; K9 ~( V, y- l. u  h
  74.     def print_list(self, head):
    ( N% L! a2 W0 G! N/ q
  75.         init_data = []: s+ r( N4 J6 E0 |2 `
  76.         while head is not None:+ ^  Z$ b' `/ x1 u# u
  77.             init_data.append(head.get_data())
    $ @2 U' p; x  ^+ \2 `. w
  78.             head = head.next4 I7 [8 B- E/ ]) \0 l
  79.         return init_data9 }: |4 J# P' A7 C3 x  ?: Y

  80. 5 l- a, [8 s: Z; Y: x4 b+ o
  81. if __name__=='__main__':" K7 l' |) Z+ i
  82.     head=Node('head')
    2 B8 f9 m. v# M4 ~8 ?% D
  83.     link=List(head)& `: ~! n3 @2 C+ ]/ g! B: l
  84.     for i in range(10):4 m3 C( X$ l9 c* j: b
  85.         node=Node(i)7 N+ k  O. G' b! F; P# e
  86.         link.append(node)
    0 b) V. V1 [0 d( _9 `8 Z
  87.     print(link.print_list(head))
    7 [# d6 T4 h7 h: H3 E. |  Z
  88.     print(link.print_list(link.reverse(head)))
复制代码
新大榭Python学习社区培训、Excel业务指导、办公软件定制、网站建设;新大榭探索实验室欢迎您!http://lab.daxie.net.cn/
Q群推荐 大榭本地求职招聘QQ群,欢迎转发分享本地招聘信息资讯! 官方招聘1群(已满);官方招聘2群:315816937 *
您需要登录后才可以回帖 登录 | 注册

本版积分规则

文字版|小黑屋|新大榭 ( 浙ICP备16018253号-1 )|点击这里给站长发消息|

GMT+8, 2026-5-15 11:39 , Processed in 0.086704 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表