1+ /**
2+ * Author: Juntaran
3+ * Email: Jacinthmail@gmail.com
4+ * Date: 2017/4/13 16:36
5+ */
6+
7+ package Link_List
8+
9+ import (
10+ "sync"
11+ "errors"
12+ "fmt"
13+ )
14+
15+ type ListNode struct { // 链表元素结构
16+ value interface {}
17+ next * ListNode
18+ prev * ListNode
19+ }
20+
21+ type List struct { // 链表结构
22+ head * ListNode
23+ tail * ListNode
24+ length int
25+ lock * sync.Mutex
26+ }
27+
28+ // 创建链表
29+ func New () * List {
30+ newList := new (List )
31+ newList .length = 0
32+ newList .lock = new (sync.Mutex )
33+ return newList
34+ }
35+
36+ // 在链表头插入
37+ func (list * List ) Prepend (value interface {}) {
38+ list .lock .Lock ()
39+ defer list .lock .Unlock ()
40+ {
41+ node := new (ListNode )
42+ node .value = value
43+ if list .length == 0 {
44+ list .head = node
45+ list .tail = node
46+ } else {
47+ list .head .prev = node
48+ node .next = list .head
49+ list .head = node
50+ }
51+ list .length ++
52+ }
53+ }
54+
55+ // 在链表尾插入
56+ func (list * List ) Append (value interface {}) {
57+ list .lock .Lock ()
58+ defer list .lock .Unlock ()
59+ {
60+ node := new (ListNode )
61+ node .value = value
62+
63+ if list .length == 0 {
64+ list .head = node
65+ list .tail = node
66+ } else {
67+ list .tail .next = node
68+ node .prev = list .tail
69+ node .next = nil
70+ list .tail = node
71+ }
72+ list .length ++
73+ }
74+ }
75+
76+ // 在index处插入链表
77+ func (list * List ) Insert (value interface {}, index int ) error {
78+ if index <= 0 || index > list .length {
79+ return errors .New ("Error: Insert Index Out of Range" )
80+ }
81+ list .lock .Lock ()
82+ defer list .lock .Unlock ()
83+ {
84+ node := new (ListNode )
85+ node .value = value
86+ if list .length == 0 {
87+ list .head = node
88+ list .tail = node
89+ //node.prev = nil
90+ //node.next = nil
91+ } else if index == 1 {
92+ node .next = list .head
93+ list .head .prev = node
94+ list .head = node
95+ } else {
96+ temp := list .head
97+ for i := 1 ; i < index ; i ++ {
98+ temp = temp .next
99+ }
100+ temp .prev .next = node
101+ node .prev = temp .prev
102+ node .next = temp
103+ temp .prev = node
104+ }
105+ list .length ++
106+ }
107+ return nil
108+ }
109+
110+ // 删除index位置的元素
111+ func (list * List ) DeleteIndex (index int ) error {
112+ list .lock .Lock ()
113+ defer list .lock .Unlock ()
114+ {
115+ if list .length <= 0 {
116+ return errors .New ("Error: List is Empty" )
117+ }
118+ if index <= 0 || index > list .length {
119+ return errors .New ("Error: Delete Index Out of Range" )
120+ }
121+ temp := list .head
122+ for i := 1 ; i < index ; i ++ {
123+ temp = temp .next
124+ }
125+ temp .prev .next = temp .next
126+ temp .next .prev = temp .prev
127+ list .length --
128+ return nil
129+ }
130+ }
131+
132+ // 删除所有值为value的元素
133+ func (list * List ) DeleteValue (value interface {}) (int , error ) {
134+ list .lock .Lock ()
135+ defer list .lock .Unlock ()
136+ {
137+ count := 0
138+ Here:
139+ if count == 0 && list .length <= 0 {
140+ return 0 , errors .New ("Error: Empty List" )
141+ } else if list .length <= 0 {
142+ // 第一个元素为value,且只有一个元素
143+ return count , nil
144+ }
145+ // 链表头的值恰好为value
146+ if list .head .value == value {
147+ list .head = list .head .next
148+ list .length --
149+ count ++
150+ goto Here
151+ }
152+ for node := list .head ; node .next != nil ; node = node .next {
153+ if node .value == value {
154+ node .prev .next = node .next
155+ node .next .prev = node .prev
156+ list .length --
157+ count ++
158+ }
159+ }
160+ if list .tail .value == value {
161+ count ++
162+ list .tail .prev .next = nil
163+ list .length --
164+ }
165+ return count , nil
166+ }
167+ }
168+
169+ // 获取链表长度
170+ func (list * List ) LenList () int {
171+ list .lock .Lock ()
172+ defer list .lock .Unlock ()
173+ return list .length
174+ }
175+
176+ // 判断链表是否为空
177+ func (list * List ) IsEmpty () bool {
178+ list .lock .Lock ()
179+ defer list .lock .Unlock ()
180+ return list .length == 0
181+ }
182+
183+ // 清空链表
184+ func (list * List ) Clear (){
185+ list .lock .Lock ()
186+ defer list .lock .Unlock ()
187+ {
188+ list .length = 0
189+ list .head = nil
190+ list .tail = nil
191+ }
192+ }
193+
194+ // 附加链表k
195+ func (list * List ) Connect (k * List ) {
196+ list .lock .Lock ()
197+ defer list .lock .Unlock ()
198+ {
199+ if list .length <= 0 {
200+ list = k
201+ list .length = k .length
202+ return
203+ }
204+ if k .length <= 0 {
205+ list = list
206+ return
207+ }
208+ list .tail .next , k .head .prev = k .head , list .tail
209+ list .tail = k .tail
210+ list .length += k .length
211+ }
212+ }
213+
214+ // 获取index位置的元素值
215+ func (list * List ) GetIndex (index int ) (interface {}, error ) {
216+ list .lock .Lock ()
217+ defer list .lock .Unlock ()
218+ {
219+ if index <= 0 || index > list .length {
220+ return nil , errors .New ("Error: GetIndex Index Out of Range" )
221+ }
222+ node := list .head
223+ for i := 1 ; i < index ; i ++ {
224+ node = node .next
225+ }
226+ return node .value , nil
227+ }
228+ }
229+
230+ // 寻找链表中值为value元素的位置,返回一个切片和总数
231+ func (list * List ) FindIndex (value interface {}) ([]int , int ) {
232+ list .lock .Lock ()
233+ defer list .lock .Unlock ()
234+ {
235+ if list .length <= 0 {
236+ return nil , 0
237+ }
238+ var result []int
239+ count := 0
240+ index := 1
241+ for node := list .head ; node != nil ; node = node .next {
242+ if node .value == value {
243+ count ++
244+ result = append (result , index )
245+ }
246+ index ++
247+ }
248+ return result , count
249+ }
250+ }
251+
252+ // 替换index位置的元素
253+ func (list * List ) ChangeList (index int , value interface {}) error {
254+ if index <= 0 || index > list .length {
255+ return errors .New ("Error: ChangeList Index Out of Range" )
256+ }
257+ list .DeleteIndex (index )
258+ list .Insert (value , index )
259+ return nil
260+ }
261+
262+ // 遍历输出切片
263+ func (list * List ) PrintList () {
264+ list .lock .Lock ()
265+ defer list .lock .Unlock ()
266+ {
267+ if list .length == 0 {
268+ fmt .Println ("List is Empty" )
269+ return
270+ }
271+ for node := list .head ; node != nil ; node = node .next {
272+ fmt .Printf ("%v " , node .value )
273+ }
274+ fmt .Println ()
275+ }
276+ }
0 commit comments