線性表的順序儲存是指在記憶體中用位址連續的一塊儲存空間順序存放線性表的各元素,用這種儲存形式儲存的線性表稱為順序表。
順序表實現的標頭檔案seqlist.h:
1#ifndef seqlist_h
2#define seqlist_h
34 #include 5
6using
namespace
std;
78 template
9class
seqlist10;
2526 template
27 seqlist::seqlist(int
size)
2833
34 template
35 seqlist::~seqlist()
3640
41 template
42int seqlist::getslsize()
4346
47 template
48int seqlist::getcurrentsize()
4952
53 template
54bool seqlist::isempty()
5559
60 template
61 t seqlist::get(int
index)
6268
69return
pdata[index];70}
7172
//element t is at index(start from 0) after inserting
73 template
74bool seqlist::slinsert(int
index, t t)
7581
82if(index < 0 || index > last + 1)83
8788
//if the table is empty
89if(last == -1)90
9596
//if the location is at last
97if(index == last + 1)98
103104
//for common situation
105for(int j = last; j >= index; j--)
106109 pdata[index] =t;
110 last++;
111return
true
;112
}113
114 template
115bool seqlist::sldelete(int
index)
116122
123if(last == -1
)124
128129
//if the location is at last
130if(index ==last)
131135
136//
for common situation
137for(int j = index; j < last; j++)
138141 last--;
142return
true
;143
}144
145#endif
順序表的測試檔案tseqlist.cpp:
1 #include 2 #include 3 #include 45using
namespace
std;67
struct
student8;
1213
classa14
20void setv(int v = 1
);21
intf;
22};
2324
//void a::setv(int v = 3)
//compile error
25void a::setv(int
v)26
2930
void
objecttest()31,
35 ,
36 ,
37};
3839 cout << endl << "
----object test----
"<40 seqlist*pslist = new seqlist();
41 cout << "
size =
"<< pslist->getslsize() <4243 cout << "
check location:
"<44 pslist->slinsert(-1, stu[0
]);45
46 cout << "
insert when table is empty
"<47 pslist->slinsert(0, stu[0
]);48
49 cout << "
insert at last
"<50 pslist->slinsert(1, stu[1
]);51
52 cout << "
insert at first
"<53 pslist->slinsert(0, stu[2
]);54
55student p;
56int cusize = pslist->getcurrentsize();
57for(int i = 0; i <= cusize; i++)
5862
63 cout << "
check overflow:
"<64 pslist->slinsert(0, stu[0
]);65
66 cout << "
check deleting location:
"<67 pslist->sldelete(3
);68
69 cout << "
delete the first one:
"<70 pslist->sldelete(0
);71 cusize = pslist->getcurrentsize();
72for(int i = 0; i <= cusize; i++)
7377
78 cout << "
check deleting the last one
"<79 pslist->sldelete(1
);80 cusize = pslist->getcurrentsize();
81for(int i = 0; i <= cusize; i++)
8286
87int k = 0;88
while(!pslist->isempty())
8993 cout << "
after deleting size =
"<< k <9495
a a;
96a.setv();
97//
a.setv(6);
98 cout << "
f =
"<< a.f <99100
delete
pslist;
101return
;102
}103
104int
main()
105
輸出結果:
1 ----object test----2 size = 3
3check location:
4 location error!
5 insert when table is
empty
6insert at last
7insert at first
8 pslist[0] = jack, 13
9 pslist[1] = sky, 11
10 pslist[2] = rose, 12
11check overflow:
12 table is full!
13check deleting location:
14 location error!
15delete the first one:
16 pslist[0] = sky, 11
17 pslist[1] = rose, 12
18check deleting the last one
19 pslist[0] = sky, 11
20 after deleting, size = 1
21 f = 1
順序表插入操作注意事項:
1)判斷表是否滿,若滿了不作插入;
2)檢驗插入位置的有效性;
3)若為空表,直接插入首位置;
4)若在表末端插入,無需移動元素。
順序表刪除操作注意事項:
1)判斷表是否為空;
2)檢驗刪除位置的有效性;
3)若刪除最後乙個元素,則無需移動其它元素。
資料結構 順序表
順序表的特徵 1由唯一的表名標識 2佔據一塊連續的儲存空間 3資料順序存放,元素之間有先後關係 定義動態的順序表 define maxsize 100 typedef struct sqlist 這個結構體型別存放的是順序表的資訊和順序表的資料 初始化順序表 void initsqlist sqli...
資料結構 順序表
順序表示最簡單的乙個資料結構,直接貼 吧,因為比較簡單。include include typedef struct sqlist sqlist void initlist sqlist l l length 0 void getelem sqlist l 初始化 l length j printf...
資料結構順序表
include include include include include include include include include include include include include include using namespace std define maxn 100000...