一、什么是順序表?
順序表是用一段物理地址連續(xù)的存儲單元依次存儲數(shù)據(jù)元素的線性結構,一般情況下采用數(shù)組存儲。在數(shù)組上完成數(shù)據(jù)的增刪查改。
順序表:可動態(tài)增長的數(shù)組,要求數(shù)據(jù)是連續(xù)存儲的
特點:隨機訪問
二、創(chuàng)建順序表
順序既可以靜態(tài)分配,也可以動態(tài)分配。在靜態(tài)分配時,由于數(shù)組的大小和空間已經(jīng)事先固定,一旦空間占滿,再加入新的數(shù)據(jù)就會產(chǎn)生溢出,進而導致程序崩潰。
而在動態(tài)分配時,存儲數(shù)組的空間是在程序執(zhí)行過程中通過動態(tài)存儲分配語句分配的,一旦數(shù)據(jù)空間占滿,就另外開辟一塊更大的空間,用來替換原來的空間,從而達到擴充存儲數(shù)組空間的目的,而不是一次性的劃分。
typedef struct{
int *data; //指示動態(tài)分配的指針
int length, size; //分別表示總空間大小以及長度
}sqlist;
三、順序表的一些基本操作
(一)初始化操作
bool InitList(sqlist &L){
L.data = new int[Maxsize];
if (!L.data)
return false; //如果順序表不為空,則返回false
L.length = 0;
L.size = Maxsize;
return true;
}
(二)返回鏈表長度
int Length(sqlist L){
return L.length;
}
(三)添加元素
在順序表的末尾添加元素:
bool AppendElem(sqlist &L, int e){
if (L.length >= Maxsize)
return false;
L.data[L.length] = e;
L.length++;
return true;
}
(四)查找元素
如果該元素存在與順序表中,返回該元素的位置i,不存在則返回0
int LocatedElem(sqlist L, int e){
int i;
for (int i = 0; i < L.length; i++){
if (L.data[i] == e)
return i + 1;
}
return 0; //如果沒有找到,返回0
}
(五)查找指定位置的元素
需要先判斷該下標i是否合法,不合法返回-1;否則返回該位置元素的值;
int GetElem(sqlist L, int i){
if (i<1 || i>L.length)
return -1; //如果輸入的下標不合理,返回-1
return L.data[i - 1];
}
(六)插入元素
刪除和插入元素圖片解釋:

在表L的第i個位置插入指定元素e
bool InsertElem(sqlist &L, int i, int e){
if (i<1 || i>L.length + 1)
return false; //檢查插入的位置是否合理,不合理返回false
if (L.length >= Maxsize)
return false;
for (int j = L.length; j >= i; j--){
L.data[j ] = L.data[j-1];
}
L.data[i - 1] = e;
L.length++;
}
(七)刪除元素
刪除表L第i個位置的元素,并用e返回刪除位置元素的值文章來源:http://www.zghlxwxcb.cn/news/detail-732507.html
bool DeleteElem(sqlist &L, int i, int &e){
if (i<1 || i>L.length)
return false;
e = L.data[i - 1];
for (int j = i; j < L.length; j++){
L.data[j - 1] = L.data[j];
}
L.length--;
return true;
}
(八)打印表中所有元素
void PrintList(sqlist L){
cout << "順序表的總空間大小為:" << L.size << ",已占用空間為:" << L.length << endl;
for (int i = 0; i < L.length; i++){
cout << L.data[i] << " ";
}
cout << endl;
}
(十)判斷表是否為空
bool Empty(sqlist L){
if (L.data)
return true;
else
return false;
}
四、銷毀表
void Destroy(sqlist &L){
if (L.data)
delete[] L.data;
L.length = 0;
L.size = 0;
}
五、完整代碼示例
#include<iostream>
using namespace std;
#define Maxsize 100
typedef struct{
int *data;
int length, size; //分別表示總空間大小以及長度
}sqlist;
bool InitList(sqlist &L){
L.data = new int[Maxsize];
if (!L.data)
return false; //如果順序表不為空,則返回false
L.length = 0;
L.size = Maxsize;
return true;
}
int Length(sqlist L){
return L.length;
}
bool AppendElem(sqlist &L, int e){
if (L.length >= Maxsize)
return false;
L.data[L.length] = e;
L.length++;
return true;
}
int LocatedElem(sqlist L, int e){
int i;
for (int i = 0; i < L.length; i++){
if (L.data[i] == e)
return i + 1;
}
return 0; //如果沒有找到,返回0
}
int GetElem(sqlist L, int i){
if (i<1 || i>L.length)
return -1; //如果輸入的下標不合理,返回-1
return L.data[i - 1];
}
bool InsertElem(sqlist &L, int i, int e){
if (i<1 || i>L.length + 1)
return false; //檢查插入的位置是否合理,不合理返回false
if (L.length >= Maxsize)
return false;
for (int j = L.length; j >= i; j--){
L.data[j ] = L.data[j-1];
}
L.data[i - 1] = e;
L.length++;
return true;
}
bool DeleteElem(sqlist &L, int i, int &e){
if (i<1 || i>L.length)
return false;
e = L.data[i - 1];
for (int j = i; j < L.length; j++){
L.data[j - 1] = L.data[j];
}
L.length--;
return true;
}
void PrintList(sqlist L){
cout << "順序表的總空間大小為:" << L.size << ",已占用空間為:" << L.length << endl;
for (int i = 0; i < L.length; i++){
cout << L.data[i] << " ";
}
cout << endl;
}
bool Empty(sqlist L){
if (L.data)
return true;
else
return false;
}
void Destroy(sqlist &L){
if (L.data)
delete[] L.data;
L.length = 0;
L.size = 0;
}
int main(){
sqlist list;
int e, index;
cout << "順序表初始化..." << endl;
//1.初始化
if (InitList(list)) {
cout << "順序表初始化成功!" << endl;
}
else cout << "順序表初始化失??!" << endl;
PrintList(list);
//2.添加元素
int count;
cout << "請輸入要添加元素的個數(shù):" ;
cin >> count;
for (int i = 0; i < count; i++){
cout << "請輸入要添加元素e的值:";
cin >> e;
if (AppendElem(list, e))
cout << "添加成功!" << endl;
else
cout << "添加失??!" << endl;
}
PrintList(list);
//3.查找元素,存在則返回該元素的位置
cout << "請輸入要查找的元素e的值:";
cin >> e;
index = LocatedElem(list, e);
if (index)
cout << "該元素" << e << "在表中的第" << index << "個位置" << endl;
else
cout << "表中不存在該元素" << endl;
PrintList(list);
//4.返回第i個元素的值
cout << "請輸入要查找元素的位置:";
cin >> index;
if (GetElem(list, index) != -1)
cout << "第" << index << "個位置的值為:" << GetElem(list, index) << endl;
else
cout << "輸入的下標不合理!" << endl;
PrintList(list);
//5.插入元素
cout << "請輸入要插入的元素位置";
cin >> index;
cout << "請輸入要插入的元素的值";
cin >> e;
if (InsertElem(list, index, e)) {
cout << "插入成功!" << endl;
}
else {
cout << "插入失敗!" << endl;
}
PrintList(list);
//6.刪除元素
cout << "請輸入要刪除的元素的位置" << endl;
cin >> index;
if (DeleteElem(list, index,e)) {
cout << "刪除成功!" << endl;
}
else {
cout << "刪除失敗!" << endl;
}
cout << "刪除的元素的值為:" << e << endl;
PrintList(list);
//7.銷毀
Destroy(list);
system("pause");
return 0;
}
六、總結
以上就是線性表中順序表的講解以及C++代碼示例,希望大家都能熟練掌握順序表這個知識點,如果有什么問題,歡迎大家來找我交流。最后,希望小伙伴們能點贊收藏加關注,十分感謝。文章來源地址http://www.zghlxwxcb.cn/news/detail-732507.html
到了這里,關于數(shù)據(jù)結構-線性表的順序表基本操作代碼實現(xiàn)(超級詳細清晰 C++實現(xiàn))的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!