新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

查看: 892|回复: 0

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

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

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

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

x
题目:反向输出一个链表。
1 q' A# Q% J0 F程序分析:无。2 G6 Z% J/ S2 m, g: S& U( E( Q
  1. class Node:8 V" f. `  G2 B$ e5 o4 O2 y

  2. . d8 H& ?2 {0 |% q. c
  3.     def __init__(self, data):
    3 r) g5 s7 I1 a6 k
  4.         self.data = data' B* |; [( w2 a6 O( i% J" ^
  5.         self.next = None
    , J: e( P- I$ `( p5 \

  6. # n' C& Q) k: i0 H# J* c" ^
  7.     def get_data(self):5 E" x8 f" ~. N4 G$ x  o
  8.         return self.data; s4 z1 R$ H3 R# n# C

  9. 1 }3 O: r' m" G( k; l4 D& s
  10. class List:5 e, T1 X  I6 o2 a

  11. ) d" x$ m1 m  m4 M! r: \
  12.     def __init__(self, head):
    & Y' P5 k3 c; a" L) f+ U( |
  13.         self.head = head
    / S/ T8 @! Q7 u: W- `; Y- G, _
  14. ! o  p) Q* U7 `9 k
  15.     def is_empty(self):
    2 D  x: b# G" {" Q; o
  16.         return self.get_len() == 0
    9 J$ j$ g8 j0 ~) s; b

  17. # L+ d9 N: b, X3 q9 q9 c
  18.     def get_len(self):  6 `6 ]% z8 h1 h! M! w" p$ X
  19.         length = 08 J* @, I" m" X& q! M; p
  20.         temp = self.head
      j+ ]+ W+ Y  U0 ]7 c8 }. l
  21.         while temp is not None:) I; S/ K; `/ `
  22.             length += 1- \5 Y* b% A. `' w7 x$ N% d
  23.             temp = temp.next6 m3 a% {) A8 n
  24.         return length
    8 X) {9 Q, k) H. v+ D7 s
  25. - i6 e( U  K' X& g
  26.     def append(self, node):
    9 `! U$ ?0 A4 V+ @& l9 M
  27.         temp = self.head
    & Z- V# N: o( y4 q, Z, [
  28.         while temp.next is not None:
    , X# Z2 f9 \& K+ Y) g4 c
  29.             temp = temp.next
    0 z+ e8 t6 y9 y
  30.         temp.next = node
    3 r/ h" i4 ]7 T! U; j, j7 a
  31. 8 t- }! s3 Z  F, c& R& J5 m
  32.     def delete(self, index):
    ' w: n9 a+ k( R4 x0 u1 D) N" l
  33.         if index < 1 or index > self.get_len():
    ; P- J2 [& l9 w+ {* ?( u
  34.             print("给定位置不合理")" N  J, w3 p" [. D* r) h7 x7 G% Q
  35.             return
    3 Z1 q9 j8 w1 z. @* U' a! ?! B* Q
  36.         if index == 1:
    " M7 n7 p; D2 k7 Y' S; q, [0 @, Q
  37.             self.head = self.head.next
    0 D( f# a6 |+ w3 Z* r
  38.             return+ Y. R! h4 S8 [/ N3 g
  39.         temp = self.head( L) E- _; \5 o& D3 r- x
  40.         cur_pos = 00 j9 ]9 a+ w1 U5 x# a
  41.         while temp is not None:( L) R0 ~* Z4 M. R# \- L
  42.             cur_pos += 1+ E1 _8 I" s6 q7 G" C
  43.             if cur_pos == index-1:. w5 I0 V. s& i/ J6 [* X1 R4 K
  44.                 temp.next = temp.next.next
    8 x+ @2 b, F! {8 B* q5 ]
  45.             temp = temp.next
    6 i/ I; G" @( b% w. C
  46. 9 k! j& f) _; z# |+ y
  47.     def insert(self, pos, node):- Q5 O( P0 _% U+ O# i# P; g
  48.         if pos < 1 or pos > self.get_len():
    * m8 n  p( K& j6 g
  49.             print("插入结点位置不合理")3 T( H  r: T% ]8 |! Y$ p0 \! j
  50.             return/ p* F2 f7 X; _( A; A, ^7 Q3 K: r
  51.         temp = self.head" B8 _7 f) \4 s( d* F( I
  52.         cur_pos = 0
    6 h: U  r3 b1 l; A9 m0 C
  53.         while temp is not Node:5 q$ @) v0 ~# c4 p/ S/ @. C
  54.             cur_pos += 16 O( y0 G8 m- U& a$ u
  55.             if cur_pos == pos-1:5 N8 W  ?" N6 [" ?# G6 F
  56.                 node.next = temp.next0 y' r! K$ s5 j6 m! z( Y
  57.                 temp.next =node3 p& F4 ]# T. G5 A
  58.                 break
    2 K0 o- ~- s2 k% G4 k
  59.             temp = temp.next
    8 n1 b; U# E* f0 X7 S; D

  60. 5 K( t; i( Y# C- E& [1 G2 B8 L% R
  61.     def reverse(self, head):' x* \* }4 S. S3 `* s* h  v
  62.         if head is None and head.next is None:
    * F3 b7 d% X4 ^- q* b$ f' _
  63.             return head
    / }: D0 _: s' r1 B- O. F) ?4 u
  64.         pre = head
    2 F6 \  D9 T; _$ l7 b2 ^- A
  65.         cur = head.next$ y* h& H& H1 X, @
  66.         while cur is not None:
    $ }3 H1 J+ u7 s& M  v  a0 J
  67.             temp = cur.next/ O) q9 f: O# ], ~! U( Y* r
  68.             cur.next = pre& I! C9 T; ]1 v$ q3 e* q
  69.             pre = cur
    ! u! C. x. z9 D6 b: B- M3 E
  70.             cur = temp: Z' q; g4 ]; q% z" |( C
  71.         head.next = None
    . X! e! \) q1 H$ ~6 _' [
  72.         return pre
    , F: n$ B$ a) c5 `  k" |

  73. 4 @# H9 v/ g5 @9 R: f: M, h
  74.     def print_list(self, head):( T6 Q9 t7 T5 k. d+ k
  75.         init_data = []( d" o, i0 _* T/ o' l8 ~$ s
  76.         while head is not None:- L) b% W, x4 c( E
  77.             init_data.append(head.get_data())
    + r" _$ P/ m3 F  t7 u% G( r9 P
  78.             head = head.next
    * [; B3 ?0 s0 ?/ d% F% J& R. V
  79.         return init_data( W5 T( b3 J  \( I  v! {$ c* K

  80. " f; b- u3 k  F# Y6 P3 k
  81. if __name__=='__main__':
    ; t$ r; o) E7 G3 N" w; B& ]0 A0 W9 s3 D
  82.     head=Node('head')
    , b) M- O9 k. W8 d. Y
  83.     link=List(head)# i, D) W1 @" w
  84.     for i in range(10):( W5 v- O' O# e! h+ Q
  85.         node=Node(i)- \1 P% _0 C) }' N4 c
  86.         link.append(node)0 Z0 G" M' w$ ~) C
  87.     print(link.print_list(head))
    1 o5 Y4 @. F1 c! p
  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-2-4 09:42 , Processed in 0.085507 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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