通讯录设计-链表实现
功能如下:
1添加学生信息
2删除学生信息
3显示学生信息
4查询学生信息
5学生信息排序
6清空屏幕信息
7清空文档信息
8退出管理系统
上代码!
#include <iostream>
#include <algorithm>
#include <string>
#include <fstream>
using namespace std;
struct ElementType;
struct Node;
struct Queue;
typedef struct Queue* MyQueue;
struct ElementType {
int id;
string name;
int num;
};
struct Node {
ElementType data;
Node* next;
};
struct Queue {
Node* front;
Node* rear;
};
MyQueue Init(MyQueue& q);
bool IsEmpty(MyQueue q);
bool Insert(ElementType x, MyQueue q);
bool Delete(const int message, MyQueue q);
void Print(const Node* q);
void PrintAll(const MyQueue q);
bool FindByName(const string massage, const MyQueue q);
void Input(MyQueue q);
void Write(MyQueue q);
MyQueue Read();
MyQueue ReadOrClear(MyQueue& q);
void Swap(ElementType& x, ElementType& y);
MyQueue BubbleSort(MyQueue q);
void Menu(MyQueue q);
MyQueue Init(MyQueue& q) {
q = new Queue();
if (q == NULL) return NULL;
q->front = NULL;
q->rear = NULL;
return q;
}
bool IsEmpty(MyQueue q) {
return q->front == NULL;
}
bool Insert(ElementType x, MyQueue q) {
Node* temp = new Node();
if (temp == NULL) return false;
temp->data = x;
temp->next = NULL;
if (IsEmpty(q)) {
q->front = temp;
q->rear = temp;
return true;
}
else {
q->rear->next = temp;
q->rear = temp;
return true;
}
}
bool Delete(const int message, MyQueue q) {
Node* temp = new Node();
if (temp == NULL) return false;
bool pd = 0;
if (q->front->data.id == message) {
temp = q->front;
q->front = q->front->next;
delete temp;
temp = NULL;
pd = 1;
}
else if (q->rear->data.id == message) {
temp = q->front;
while (temp->next->data.id != message) temp = temp->next;
q->rear = temp;
q->rear->next = NULL;
pd = 1;
}
else {
temp = q->front;
while (temp->next != NULL && temp->next->data.id != message) temp = temp->next;
if (temp->next == NULL) return false;
Node* mp = new Node();
mp = temp->next;
temp->next = temp->next->next;
delete mp;
mp = NULL;
pd = 1;
}
if (pd == 1) {
Write(q);
cout << "已成功删除该学生信息!" << endl;
return true;
}
}
bool FindByName(const string massage, const MyQueue q) {
Node* temp = new Node();
bool pd = 0;
if (q->front->data.name == massage) {
temp = q->front;
Print(temp);
return true;
}
else {
temp = q->front;
while (temp->next != NULL && temp->next->data.name != massage) temp = temp->next;
if (temp->next == NULL) return false;
Print(temp->next);
return true;
}
}
void Print(const Node* q) {
cout << "该学生的信息为:" << endl;
cout << "学号: " << q->data.id << " 姓名:" << q->data.name << " 电话号码:" << q->data.num << endl;
}
void PrintAll(const MyQueue q) {
cout << "学号";
for (int i = 0; i < 10; i++) {
cout << "-";
}
cout << "姓名";
for (int i = 0; i < 10; i++) {
cout << "-";
}
cout << "电话号码" << endl;
Node* temp;
temp = q->front;
while (temp != NULL) {
cout << " " <<temp->data.id << " " << temp->data.name << " " << temp->data.num << endl;
temp = temp->next;
}
}
void Swap(ElementType& x, ElementType& y) {
ElementType temp;
temp = x;
x = y;
y = temp;
}
MyQueue BubbleSort(MyQueue q) {
if (q->front == NULL || q->front->next == NULL) return NULL;
for (Node* i = q->front; i->next != NULL; i = i->next) {
for (Node* j = q->front; j->next != NULL; j = j->next) {
if (j->data.id > j->next->data.id) {
Swap(j->data, j->next->data);
}
}
}
return q;
}
void Write(MyQueue q) {
q=BubbleSort(q);
ofstream writeIt;
writeIt.open("data.txt");
if (writeIt.fail()) {
cout << "该文件没有找到!" << endl;
cout << "程序已退出!" << endl;
exit(1);
}
Node* temp = new Node();
if (q!= NULL) {
temp= q->front;
while (temp != NULL) {
writeIt << temp->data.id << " " << temp->data.name << " " << temp->data.num << endl;;
temp = temp->next;
}
}
writeIt.close();
}
MyQueue Read() {
ifstream readIt("data.txt");
if (readIt.fail()) {
cout << "该文件没有找到!" << endl;
cout << "程序已退出!" << endl;
exit(1);
}
int id1;
string name1;
int num1;
MyQueue q=new Queue();
ElementType x;
while (!readIt.eof()) {
readIt >> id1 >> name1 >> num1;
if (readIt.eof()) break;
x.id = id1;
x.name = name1;
x.num = num1;
Insert(x, q);
}
readIt.close();
return q;
}
MyQueue ReadOrClear(MyQueue& q) {
q=Read();
return q;
}
void MakeEmpty(MyQueue& q) {
while (q->front != NULL) {
Node* temp = new Node();
temp = q->front;
q->front = q->front->next;
delete temp;
}
}
void Menu(MyQueue q) {
q=ReadOrClear(q);
while (1) {
cout << endl;
cout << "|--------------------学生通讯录系统---------------------|" << endl;
cout << "|--------------------1 添加学生信息---------------------|" << endl;
cout << "|--------------------2 删除学生信息---------------------|" << endl;
cout << "|--------------------3 显示学生信息---------------------|" << endl;
cout << "|--------------------4 查询学生信息---------------------|" << endl;
cout << "|--------------------5 学生信息排序---------------------|" << endl;
cout << "|--------------------6 清空屏幕信息---------------------|" << endl;
cout << "|--------------------7 清空文档信息---------------------|" << endl;
cout << "|--------------------8 退出管理系统---------------------|" << endl;
cout << "|-------------------------------------------------------|" << endl;
int n;
cout << "输入您的选择:" << endl;
cin >> n;
switch (n) {
case 1: {
ElementType x;
cout << "请输入该学生的信息:学号 姓名 电话号码" << endl;
cin >> x.id >> x.name >> x.num;
Insert(x, q);
Write(q);
cout << "已成功添加该学生信息!" << endl;
break;
}
case 2: {
cout << "请输入该学生的学号:" << endl;
int num1;
cin >> num1;
if (!Delete(num1, q)) {
cout << "该系统中不存在该学生!" << endl;
};
break;
}
case 3: {
cout << "正在打印全部学生信息中.......请稍等!" << endl;
cout << "全部学生的信息为:" << endl;
PrintAll(q);
break;
}
case 4: {
cout << "请输入该学生的姓名:" << endl;
string name1;
cin >> name1;
if (!FindByName(name1, q)) {
cout << "该系统中不存在该学生!" << endl;
}
break;
}
case 5: {
cout << "正在根据学生的学号对学生进行排序....." << endl;
cout << "排完序后,结果为:" << endl;
BubbleSort(q);
PrintAll(q);
break;
}
case 6: {
system("cls");
break;
}
case 7: {
cout << "请您在三确认是否要清空文档中的全部学生信息!清空请输入“yes”,不清空请输入“no”。" << endl;
string s;
cin >> s;
if (s == "yes") {
MakeEmpty(q);
q = Init(q);
Write(q);
cout << "已经成功清空文档中的全部学生信息!" << endl;
}
break;
}
case 8: {
cout << "退出成功!" << endl;
exit(0);
}
default:
cout << "输入的选项序号有误,请重新输入!" << endl;
}
}
}
int main() {
MyQueue q;
q = Init(q);
Menu(q);
return 0;
}