簡單理解就是陣列;
優點:缺點:
使用場景:
順序表結構,一般有:表體、描述(如length);
const maxsize int
=100
type seqlist struct
插入:把插入位置i之後的所有元素往後移再插入;
//insertlist 把value插入第i位置,i-1為index;i從1開始
func
(s *seqlist)
insertlist
(value, i int
)error
for j := s.length; j >= i-
1; j--
s.data[i-1]
= value
s.length++
return
nil}
刪除:把刪除位置i之後的所有元素往前移,覆蓋掉原來第i個;
//deletelist 刪除第i個
func
(s *seqlist)
deletelist
(i int
)error
for j := i; j < s.length; j++
s.length--
return
nil}
package main
import
("errors"
"fmt"
)const maxsize int
=100
type seqlist struct
//newseqlist 宣告並初始化順序表,為順序表分配記憶體空間;
func
newseqlist()
*seqlist
return
&seqlist
}//createlist 給表裡新增初始元素
func
(s *seqlist)
createlist
(data [
]int
, n int
)error
for i, value :=
range data
s.length = n
return
nil}
//insertlist 把value插入第i位置,i-1為index;i從1開始
func
(s *seqlist)
insertlist
(value, i int
)error
for j := s.length; j >= i-
1; j--
s.data[i-1]
= value
s.length++
return
nil}
//deletelist 刪除第i個
func
(s *seqlist)
deletelist
(i int
)error
for j := i; j < s.length; j++
s.length--
return
nil}
func
main()
err := seqlist.
createlist
(initdate,
len(initdate)
)if err !=
nil seqlist.
insertlist(10
,2) seqlist.
seqlistprint()
seqlist.
deletelist(2
) seqlist.
seqlistprint()
}//seqlistprint 答應順序表
func
(s *seqlist)
seqlistprint()
fmt.
println()
fmt.
println
("length: "
, s.length)
}
go語言實現鍊錶
宣告結構體 宣告全域性變數,儲存頭結點 var head node var curr node 宣告節點型別 type node struct 建立頭結點 func createheadnode data string node 新增新節點 func addnode data string node...
Go語言 實現鍊錶
鍊錶是乙個結點指向下乙個結點的儲存結構,每乙個結點有兩個元素,乙個是存放資料本身,另乙個資料指向下乙個結點,由這些結點組成乙個鍊錶 package main import fmt type node struct type nodelist struct func this nodelist add...
C語言實現順序表
標頭檔案部分 include include include typedef int datatype define maxsize 10 typedef struct seqlist seqlist 列印順序表的內容 void printseqlist seqlist seq 初始化順序表 voi...