顺序表:
代码如下:
1 #include
2 #include
3 #include
4 #include
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 > choose;
93 switch (choose)
94 {
95 case 1:
96 if (InitList_Sq(L))
97 cout > head_2 >> head_3;
114 while (!file.eof()) {
115 file >> L.elem[i].id >> L.elem[i].name >> L.elem[i].price;
116 i++;
117 }
118 cout > i;
126 temp = GetElem(L, i, e);
127 if (temp != 0) {
128 cout price;
139 temp = LocateElem_Sq(L, price);
140 if (temp != 0) {
141 cout > a;
149 cin >> e.id >> e.name >> e.price;
150 if (ListInsert_Sq(L, a, e))
151 cout > c;
158 if (ListDelete_Sq(L, c))
159 cout next=p->next;
72 p->next=s;
73 return OK;
74 }
75
76 Status ListDelete_L(LinkList &L, int i)
77 {
78 LinkList p, q;
79 int j;
80 p = L;
81 j = 0;
82 while((p->next)&&(j < i - 1))
83 {
84 p=p->next;
85 ++j;
86 }
87 if (!(p->next)||(j>i-1))
88 return ERROR;
89 q=p->next;
90 p->next=q->next;
91 delete q;
92 return OK;
93 }
94
95
96
97 int main() {
98 int a,n,choose;
99 double price;
100 Book e;
101 LinkList L,p;
102 string head_1, head_2, head_3;
103 cout>choose;
117 switch (choose)
118 {
119 case 1:
120 if (InitList_L(L))
121 coutnext = NULL;
127 int length = 0;
128 fstream file;
129 file.open("book.txt");
130 if (!file)
131 {
132 cout > head_2 >> head_3;
136 while (!file.eof())
137 {
138 p = new LNode;
139 file >> p->data.id >> p->data.name >> p->data.price;
140 p->next = L->next;
141 L->next = p;
142 length++;
143 }
144 file.close();
145 }
146 break;
147 case 3:
148 cout>a;
150 if(GetElem_L(L,a,e))
151 {
152 coute.price;
177 if (ListInsert_L(L,a,e))
178 cout>a;
185 if (ListDelete_L(L, a))
186 coutnext;
193 while(p)
194 {
195 cout |