From e3988296ddd82b511b42b09f4450fa909b19355a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=88=B4=E6=9E=97=E5=B3=B0?= Date: Sat, 24 Dec 2022 15:39:38 +0800 Subject: [PATCH] fix print --- .../linked_list_test.go | 10 ++-------- .../go/chapter_array_and_linkedlist/print_utils.go | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 8 deletions(-) create mode 100644 codes/go/chapter_array_and_linkedlist/print_utils.go diff --git a/codes/go/chapter_array_and_linkedlist/linked_list_test.go b/codes/go/chapter_array_and_linkedlist/linked_list_test.go index 31c2ec043..1c0107cca 100644 --- a/codes/go/chapter_array_and_linkedlist/linked_list_test.go +++ b/codes/go/chapter_array_and_linkedlist/linked_list_test.go @@ -3,10 +3,7 @@ // Author: dlfld (2441086385@qq.com) package chapter_array_and_linkedlist -import ( - "fmt" - "testing" -) +import "testing" /* Driver Code */ func TestLinkedList(t *testing.T) { @@ -23,8 +20,5 @@ func TestLinkedList(t *testing.T) { n2.Next = n3 n3.Next = n4 - for n0 != nil{ - fmt.Printf("%v ",n0.Val) - n0 = n0.Next - } + PrintLinkedList(n0) } \ No newline at end of file diff --git a/codes/go/chapter_array_and_linkedlist/print_utils.go b/codes/go/chapter_array_and_linkedlist/print_utils.go new file mode 100644 index 000000000..693bdfa3f --- /dev/null +++ b/codes/go/chapter_array_and_linkedlist/print_utils.go @@ -0,0 +1,14 @@ +// File: print_utils.go +// Created Time: 2022-12-24 +// Author: dlfld (2441086385@qq.com) + +package chapter_array_and_linkedlist + +import "fmt" + +func PrintLinkedList(head *ListNode){ + for head != nil{ + fmt.Printf("%v ",head.Val) + head = head.Next + } +} \ No newline at end of file