Skip to content

Commit ee72e8a

Browse files
committed
顺序表BUG修正
1 parent eac86a5 commit ee72e8a

File tree

3 files changed

+139
-194
lines changed

3 files changed

+139
-194
lines changed

sources/go/list/ArrayList.go

Lines changed: 0 additions & 181 deletions
This file was deleted.

sources/go/list/SequenList.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* 顺序表
3+
*/
4+
5+
package list
6+
7+
import (
8+
"errors"
9+
"fmt"
10+
)
11+
12+
// 线性表结构体对象 由于Go没有泛型,这里很难实现:调用者传递一个数组参数的方式New一个SequenList,只能定死如下一个整型数组
13+
type SequenList struct {
14+
size int // 该线性表最大容量
15+
length int // 该线性表最大长度
16+
data [10]int // 线性表内数据,这里为了演示默认设置为10长度的int数组,所有元素默认为0(Go的0值机制)
17+
}
18+
19+
// 创建线性表实例 按笔者认为这里应该传入一个泛型数组,通过泛型数组来更高抽象顺序表
20+
func NewSequenList() *SequenList {
21+
22+
var arr [10]int
23+
24+
return &SequenList{
25+
size: 10,
26+
length: 0,
27+
data: arr,
28+
}
29+
}
30+
31+
// 打印线性表
32+
func (sl *SequenList)Show() {
33+
fmt.Println(sl)
34+
}
35+
36+
// 插入元素:从末尾append一个数据
37+
func (sl *SequenList)Append(data int) error{
38+
39+
// 判断空间是否已满
40+
if sl.IsFull() {
41+
return errors.New("SequenList overflow")
42+
}
43+
44+
sl.data[sl.length] = data
45+
sl.length++
46+
47+
return nil
48+
}
49+
50+
// 插入元素:任意位置插入元素
51+
func (sl *SequenList)Insert(index int, data int) error {
52+
53+
if sl.IsFull() {
54+
return errors.New("SequenList overflow")
55+
}
56+
57+
if index < 0 || index > sl.length {
58+
return errors.New("index overflow")
59+
}
60+
61+
// 这里如果按照正序循环则书写极其麻烦,从最后一位开始往后移动很简便
62+
for i := sl.length; i >= index; i-- {
63+
64+
if i == sl.length { // 如果是在末尾插入 时间复杂度为O(1)
65+
sl.data[i] = data
66+
break
67+
}
68+
69+
sl.data[i] = sl.data[i - 1]
70+
}
71+
72+
sl.length++
73+
return nil
74+
}
75+
76+
// 删除元素:从末尾pop一个数据
77+
func (sl *SequenList)Pop() (int, error) {
78+
79+
if sl.IsEmpty() {
80+
return 0, errors.New("SequenList is empty")
81+
}
82+
83+
e := sl.data[sl.length - 1]
84+
sl.data[sl.length - 1] = 0
85+
sl.length --
86+
return e, nil
87+
}
88+
89+
// 获取顺序表长度
90+
func (sl *SequenList)Length() int{
91+
return sl.length
92+
}
93+
94+
// 判断顺序表是否已满
95+
func (sl *SequenList)IsFull() bool {
96+
if sl.length == sl.size {
97+
return true
98+
}
99+
return false
100+
}
101+
102+
// 判断顺序表是否为空方法
103+
func (sl *SequenList)IsEmpty() bool {
104+
if sl.length == 0 {
105+
return true
106+
}
107+
return false
108+
}
109+
110+
// 获取顺序表容量
111+
func (sl *SequenList)Size() int{
112+
return sl.size
113+
}

sources/go/main.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,37 @@ import (
55
"fmt"
66
)
77

8+
func testSequenList() {
89

9-
func main() {
10+
sl := list.NewSequenList()
11+
sl.Show()
12+
13+
sl.Append(7)
14+
sl.Append(9)
15+
sl.Show()
16+
17+
sl.Insert(1,6)
18+
sl.Show()
19+
20+
sl.Pop()
21+
sl.Show()
1022

11-
al := list.NewArrayList()
12-
al.Show()
23+
// err := al.Insert(1,15)
24+
// fmt.Println(err)
25+
// al.Show()
1326

14-
al.Append(10)
15-
al.Append(12)
16-
al.Show()
27+
// err = al.Insert(2,13)
28+
// fmt.Println(err)
29+
// al.Show()
30+
// fmt.Println(al.Length())
31+
32+
}
33+
34+
func main() {
1735

18-
err := al.Insert(1,15)
19-
fmt.Println(err)
20-
al.Show()
36+
fmt.Println("start run...")
2137

22-
err = al.Insert(2,13)
23-
fmt.Println(err)
24-
al.Show()
25-
fmt.Println(al.Length())
38+
testSequenList() // 测试顺序表
2639

2740

2841

0 commit comments

Comments
 (0)