#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#define OK 1
#define ERROR 0
typedef int Status;
typedef string ElemType;
struct Node {
ElemType data;
Node* next;
};
typedef Node* LinkList;
int length = 0;
Status InitList_L(LinkList &L) {
L = new Node;
L->next = nullptr;
return OK;
}
Status GetElem_L(LinkList L, int i, ElemType &e) {
Node* p = L->next;
int j = 1;
while (j < i && p) {
p = p->next;
++j;
}
if (!p || j > i) return ERROR;
e = p->data;
return OK;
}
Node* LocateElem_L(LinkList L, const ElemType &e) {
Node* p = L->next;
while (p && p->data != e) p = p->next;
return p;
}
Status ListInsert_L(LinkList &L, int i, const ElemType &e) {
Node* p = L;
int j = 0;
while (p && j < i - 1) {
p = p->next;
++j;
}
if (!p || j > i - 1) return ERROR;
Node* s = new Node{e, p->next};
p->next = s;
++length;
return OK;
}
Status ListDelete_L(LinkList &L, int i) {
Node* p = L;
int j = 0;
while (p->next && j < i - 1) {
p = p->next;
++j;
}
if (!(p->next) || j > i - 1) return ERROR;
Node* q = p->next;
p->next = q->next;
delete q;
--length;
return OK;
}
void ClearList(LinkList &L) {
while (L) {
Node* temp = L;
L = L->next;
delete temp;
}
}
void InitSolarTerms(LinkList &L) {
string terms[] = {
"立春", "雨水", "惊蛰", "春分", "清明", "谷雨",
"立夏", "小满", "芒种", "夏至", "小暑", "大暑",
"立秋", "处暑", "白露", "秋分", "寒露", "霜降",
"立冬", "小雪", "大雪", "冬至", "小寒", "大寒"
};
for (int i = 0; i < 24; ++i) {
ListInsert_L(L, i + 1, terms[i]);
}
length = 24;
}
int main() {
LinkList list;
InitList_L(list);
InitSolarTerms(list);
int choose;
while (true) {
cout << "\n===== 二十四节气管理系统 =====\n";
cout << "1. 插入节气(位置从" << length + 1 << "开始)\n";
cout << "2. 删除节气\n";
cout << "3. 按位置查询(1-" << length << ")\n";
cout << "4. 按名称查询\n";
cout << "5. 显示所有节气\n";
cout << "0. 退出系统\n";
cout << "请输入操作码:";
cin >> choose;
cin.ignore();
switch (choose) {
case 1: {
string term;
cout << "输入节气名称(不超过10字):";
getline(cin, term);
if (term.empty()) {
cout << "输入无效!\n";
continue;
}
ListInsert_L(list, length + 1, term);
cout << "插入成功!当前节气总数:" << ++length << endl;
break;
}
case 2: {
int pos;
cout << "输入要删除的位置(1-" << length << "):";
cin >> pos;
if (ListDelete_L(list, pos)) {
cout << "删除成功!当前节气总数:" << --length << endl;
} else {
cout << "删除失败!位置无效或列表为空\n";
}
break;
}
case 3: {
int pos;
cout << "输入查询位置(1-" << length << "):";
cin >> pos;
ElemType term;
if (GetElem_L(list, pos, term)) {
cout << "位置" << pos << "的节气是:" << term << endl;
} else {
cout << "查询失败!位置超出范围\n";
}
break;
}
case 4: {
string term;
cout << "输入要查询的节气名称:";
getline(cin, term);
Node* node = LocateElem_L(list, term);
if (node) {
cout << "找到节气:" << node->data
<< "(位置:" << node->data << ")\n";
} else {
cout << "未找到该节气!\n";
}
break;
}
case 5: {
cout << "\n当前节气列表:\n";
Node* p = list->next;
int count = 0;
while (p) {
cout << setw(4) << ++count << ": "
<< setw(10) << p->data << endl;
p = p->next;
}
cout << "共计 " << length << " 个节气\n";
break;
}
case 0: {
ClearList(list);
cout << "\n系统已退出,感谢使用!\n";
return 0;
}
default: {
cout << "无效操作码,请重新输入!\n";
}
}
}
return 0;
}