二、線性表
2.1 線性表的定義和特點
2.2 線性表的順序表示和實現(xiàn)
圖書表的順序存儲結(jié)構(gòu)類型定義:
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1
#define MAXSIZE 10000 //能夠存儲的最大圖書數(shù)量
//1、圖書表的順序存儲結(jié)構(gòu)類型定義
//1.1、圖書的存儲結(jié)構(gòu)
typedef struct{
char isbn[20];//圖書的isbn編號
char name[50];//圖書的名稱
double price;//圖書的價格
}Book;
typedef Book ElemType;//元素數(shù)據(jù)的類型
//1.2、圖書表的順序存儲結(jié)構(gòu)
typedef struct{
ElemType* books;//存儲圖書數(shù)組結(jié)構(gòu)的基地址
int length;//當前有多少本圖書
}BookList;
2.3 類C語言有關(guān)操作補充
在調(diào)用函數(shù)的過程中,當形參為引用類型時,實參和形參占用了相同的空間
2.4 線性表基本操作的實現(xiàn)
2.4.1 線性表的基本操作:
2.4.2 線性表L的初始化
// 順序表的存儲結(jié)構(gòu)
typedef struct {
ElemType* elems;//存儲基本元素的數(shù)組基地址
int length;//當前基本元素的個數(shù)
}SqList;
typedef int Statu;
//1、順序表L的初始化
Statu initSqList(SqList &L) {
L.elems = new ElemType[MAXSIZE];//為存儲數(shù)據(jù)的數(shù)組開辟空間
if (!L.elems) exit(OVERFLOW);//空間開辟失敗
L.length = 0;//空表長度置為0
return OK;
}
2.4.3 銷毀和清空線性表L
//2、銷毀和清空線性表L
//2.1 銷毀順序表L
void destroySqList(SqList &L) {
if (L.elems) delete L.elems;
L.length = 0;
}
//2.2 清空線性表L
void clearSqList(SqList &L) {
L.length = 0;
}
2.4.4 求線性表L的長度以及判斷線性表L是否為空
//3、求線性表L的長度以及判斷線性表L是否為空
//3.1 求線性表L的長度
int getLength(SqList L) {
return L.length;
}
//3.2 判斷線性表L是否為空
Statu isEmpty(SqList L) {
if (L.length == 0) return TRUE;
return FALSE;
}
2.4.5 順序表的取值(根據(jù)位置i獲取相應位置數(shù)據(jù)元素的內(nèi)容)
//4、順序表的取值(獲取第i個位置數(shù)據(jù)元素的內(nèi)容)
Statu getElem(int i, SqList L,ElemType &e) {
if (i<1 || i>L.length) return ERROR;//數(shù)組越界
e=L.elems[i - 1];
return OK;
}
2.4.6 順序表的查找(在線性表L中查找與指定值e相同的數(shù)據(jù)元素的位置)
//5、判斷兩個數(shù)據(jù)元素是否相等
Statu isEqual(ElemType a, ElemType b) {
if (strcmp(a.isbn,b.isbn)==0) return TRUE;
return FALSE;
}
//6、順序表的查找(在線性表L中查找與指定值e相同的數(shù)據(jù)元素的位置)
int LocateElem(ElemType e, SqList L) {
for (int i = 0; i < L.length; i++) {
if (isEqual(L.elems[i],e)) return i+1;
}
return 0;
}
順序表的查找算法分析
2.4.7 順序表的插入(在第i個位置插入指定的元素)
Statu insertSqList(SqList &L, int i,ElemType e) {
if (i<1 || i>L.length + 1) return ERROR;//插入位置不合法
if (L.length == MAXSIZE) return ERROR;//元素已滿,無法插入
for (int j = L.length-1; j >=i-1; j--) {
L.elems[j+1] = L.elems[j];
}
L.elems[i - 1] = e;
L.length++;
return OK;
}
2.4.8 順序表的刪除(刪除第i個位置的元素)
Statu deleteSqList(SqList& L, int i) {
if (i<1 || i>L.length) return ERROR;//刪除位置不合法
for (int j = i; j < L.length; j++) {
L.elems[j - 1] = L.elems[j];
}
L.length--;
return OK;
}
2.5 順序表(線性表的順序存儲結(jié)構(gòu))的特點
-
(1)利用數(shù)據(jù)元素的存儲位置表示線性表中相鄰數(shù)據(jù)元素之間的前后關(guān)系,即線性表的邏輯結(jié)構(gòu)與存儲結(jié)構(gòu)一致
-
(2)在訪問線性表時,可以快速地計算出任何一個數(shù)據(jù)元素的存儲地址。因此可以粗略地認為,訪問每個元素所花時間相等
-
這種存儲元素的方法被稱為隨機存取法
2.6 C++實現(xiàn)代碼
main.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"Sqlist.h"
int main() {
test();
return 0;
}
Sqlist.h
文章來源:http://www.zghlxwxcb.cn/news/detail-425619.html
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1
#define MAXSIZE 10000 //能夠存儲的最大圖書數(shù)量
//1、圖書表的順序存儲結(jié)構(gòu)類型定義
//1.1、圖書的存儲結(jié)構(gòu)
typedef struct{
char isbn[20];//圖書的isbn編號
char name[50];//圖書的名稱
double price;//圖書的價格
}Book;
typedef Book ElemType;//元素數(shù)據(jù)的類型
//1.2、圖書表的順序存儲結(jié)構(gòu)
typedef struct{
ElemType* books;//存儲圖書數(shù)組結(jié)構(gòu)的基地址
int length;//當前有多少本圖書
}BookList;
// 順序表的存儲結(jié)構(gòu)
typedef struct {
ElemType* elems;//存儲基本元素的數(shù)組基地址
int length;//當前基本元素的個數(shù)
}SqList;
typedef int Statu;
//函數(shù)聲明===========================================
//2、順序表L的初始化
Statu initSqList(SqList &L);
//3、銷毀線性表L
void destroySqList(SqList &L);
//4、清空線性表L
void clearSqList(SqList& L);
//5、求線性表L的長度
int getLength(SqList L);
//6、判斷線性表L是否為空
Statu isEmpty(SqList L);
//7、順序表的取值(根據(jù)位置i獲取相應位置數(shù)據(jù)元素的內(nèi)容)
Statu getElem(int i, SqList L, ElemType& e);
//8、順序表的查找(在線性表L中查找與指定值e相同的數(shù)據(jù)元素的位置)
//8.1 //判斷兩本圖書是否相等
Statu isEqual(ElemType a, ElemType b);
//8.2 順序表的查找(在線性表L中查找與指定值e相同的數(shù)據(jù)元素的位置)
int LocateElem(ElemType e, SqList L);
//9、順序表的插入(在第i個位置插入指定的元素)
Statu insertSqList(SqList& L, int i, ElemType e);
//10、順序表的刪除(刪除第i個位置的元素)
Statu deleteSqList(SqList& L, int i);
//11、打印單個元素
void displayElem(ElemType e);
//12、打印整個線性表
void displaySqList(SqList L);
//13、從txt文件中讀取數(shù)據(jù)元素數(shù)據(jù)
Statu readDataFromTxt(SqList& L, const char* filename);
//================================================
//2、順序表L的初始化
Statu initSqList(SqList& L) {
L.elems = new ElemType[MAXSIZE];//為存儲數(shù)據(jù)的數(shù)組開辟空間
if (!L.elems) exit(OVERFLOW);//空間開辟失敗
L.length = 0;//空表長度置為0
return OK;
}
//3、銷毀線性表L
void destroySqList(SqList& L) {
if (L.elems) delete L.elems;
L.length = 0;
}
//4、清空線性表L
void clearSqList(SqList& L) {
L.length = 0;
}
//5、求線性表L的長度
int getLength(SqList L) {
return L.length;
}
//6、判斷線性表L是否為空
Statu isEmpty(SqList L) {
if (L.length == 0) return TRUE;
return FALSE;
}
//7、順序表的取值(根據(jù)位置i獲取相應位置數(shù)據(jù)元素的內(nèi)容)
Statu getElem(int i, SqList L,ElemType &e) {
if (i<1 || i>L.length) return ERROR;//數(shù)組越界
e=L.elems[i - 1];
return OK;
}
//8、順序表的查找(在線性表L中查找與指定值e相同的數(shù)據(jù)元素的位置)
//8.1 判斷兩本圖書是否相等
Statu isEqual(ElemType a, ElemType b) {
if (strcmp(a.isbn,b.isbn)==0) return TRUE;
return FALSE;
}
//8.2 順序表的查找(在線性表L中查找與指定值e相同的數(shù)據(jù)元素的位置)
int LocateElem(ElemType e, SqList L) {
for (int i = 0; i < L.length; i++) {
if (isEqual(L.elems[i], e)) return i + 1;
}
return 0;
}
//9、順序表的插入(在第i個位置插入指定的元素)
Statu insertSqList(SqList &L, int i,ElemType e) {
if (i<1 || i>L.length + 1) return ERROR;//插入位置不合法
if (L.length == MAXSIZE) return ERROR;//元素已滿,無法插入
for (int j = L.length-1; j >=i-1; j--) {
L.elems[j+1] = L.elems[j];
}
L.elems[i - 1] = e;
L.length++;
return OK;
}
//10、順序表的刪除(刪除第i個位置的元素)
Statu deleteSqList(SqList& L, int i) {
if (i<1 || i>L.length) return ERROR;//刪除位置不合法
for (int j = i; j < L.length; j++) {
L.elems[j - 1] = L.elems[j];
}
L.length--;
return OK;
}
//11、打印單個元素
void displayElem(ElemType e) {
printf("isbn編號是:%s\n", e.isbn);
printf("圖書的名稱是:%s\n", e.name);
printf("圖書的價格是:%lf\n", e.price);
}
//12、打印整個線性表
void displaySqList(SqList L) {
for (int i = 0; i < L.length; i++) {
displayElem(L.elems[i]);
}
}
//13、從txt文件中讀取數(shù)據(jù)元素數(shù)據(jù)
Statu readDataFromTxt(SqList &L, const char* filename) {
FILE* file = freopen(filename, "r", stdin);
if (!file) {
printf("文件讀取失敗\n");
return ERROR;
}
ElemType e;
int i = 0;
while (scanf("%s%s%lf", e.isbn, e.name, &e.price)!=EOF&&i<MAXSIZE) {
L.elems[i++] = e;
L.length++;
}
return OK;
}
//14、測試函數(shù)
void test() {
SqList L;//定義一個線性表
initSqList(L);//初始化線性表
//判斷線性表是否為空
if (isEmpty(L)) printf("線性表是空的\n");
else{
printf("線性表的長度是: %d\n", L.length);
}
printf("======1、從文件中讀取圖書表數(shù)據(jù)========\n");
//從文件中讀取圖書表數(shù)據(jù)
readDataFromTxt(L, "data.in.txt");
displaySqList(L);
//往線性表中插入元素
printf("======2、往線性表中插入元素========\n");
ElemType e1;
strcpy(e1.isbn, "00X");
strcpy(e1.name, "安徒生童話");
e1.price = 88.8;
insertSqList(L, 3, e1);
//判斷線性表是否為空
if (isEmpty(L)) printf("線性表是空的\n");
else {
printf("線性表的長度是: %d\n", L.length);
}
printf("=======3、取第三個元素=======\n");
//取第三個元素
ElemType e;
getElem(3, L, e);
displayElem(e);
printf("========4、在第三個位置插入元素e========\n");
//在第三個位置插入元素e
insertSqList(L, 3, e);
//打印線性表
displaySqList(L);
printf("=========5、刪除第三個位置的元素========\n");
//刪除第三個位置的元素
deleteSqList(L, 3);
displaySqList(L);
printf("=========6、查看元素e在線性表的哪個位置=======\n");
//查看元素e在線性表的哪個位置
int index=LocateElem(e, L);
printf("元素e在線性表中的位置: %d\n", index);
}
data.in.txt
文章來源地址http://www.zghlxwxcb.cn/news/detail-425619.html
001 平凡的世界 22.2
002 活著 66.5
003 天之下 33.2
到了這里,關(guān)于【數(shù)據(jù)結(jié)構(gòu)與算法】二、線性表的順序表示【硬核】的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!