顺序表:
代码如下:
1 #include<iostream> 2 #include<fstream> 3 #include<string> 4 #include<iomanip> 5 using namespace std; 6 #define OK 1 7 #define ERROR 0 8 #define OVERFLOW -2 9 typedef int Status; 10 typedef int ElemType; 11 12 #define MAXSIZE 100 13 struct Book 14 { 15 string id; 16 string name; 17 double price; 18 }; 19 typedef struct 20 { 21 Book *elem; 22 int length; 23 }SqList; 24 25 Status InitList_Sq(SqList &L) 26 { 27 28 L.elem=new Book[MAXSIZE]; 29 if (!L.elem) 30 exit(OVERFLOW); 31 L.length = 0; 32 return OK; 33 } 34 35 Status GetElem(SqList L, int i, Book &e) 36 { 37 if (i < 1 || i > L.length) 38 return ERROR; 39 e=L.elem[i-1]; 40 return OK; 41 } 42 43 int LocateElem_Sq(SqList L, double e) 44 { 45 for (int i = 0; i < L.length; i++) 46 if (L.elem[i].price == e) 47 return i + 1; 48 return 0; 49 } 50 51 Status ListInsert_Sq(SqList &L, int i, Book e) 52 { 53 if ((i < 1) || (i > L.length + 1)) 54 return ERROR; 55 if (L.length == MAXSIZE) 56 return ERROR; 57 for(int j=L.length-1;j>=i-1;j--) 58 L.elem[j+1]=L.elem[j]; 59 L.elem[i-1]=e; 60 ++L.length; 61 return OK; 62 } 63 64 Status ListDelete_Sq(SqList &L, int i) 65 { 66 if ((i < 1) || (i > L.length)) 67 return ERROR; 68 for (int j = i; j < L.length; j++) 69 L.elem[j - 1] = L.elem[j]; 70 --L.length; 71 return OK; 72 } 73 74 int main() { 75 SqList L; 76 int i = 0, temp, a, c, choose; 77 double price; 78 Book e; 79 string head_1, head_2, head_3; 80 cout << "1. 建立\n"; 81 cout << "2. 输入\n"; 82 cout << "3. 取值\n"; 83 cout << "4. 查找\n"; 84 cout << "5. 插入\n"; 85 cout << "6. 删除\n"; 86 cout << "7. 输出\n"; 87 cout <<