#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<windows.h>
using namespace std;
typedef struct student
{
int num;
char name[20];
char tel[15];
struct student*pnext;
}STU;
STU* GreatList();//创建头节点
bool creat_list(STU*p);//初始化链表
bool print_list(STU*p);//打印链表
bool delete_list(STU*p);//删除节点
bool insert_list(STU*p);//插入节点
int length_list(STU*p);//计算节点个数
bool sort_list(STU*p, int len);//排序
bool inversion_list(STU*p);//逆置
void reserve_list(STU*p);//保存文件
void welcome();
int main()
{
STU*Phead = GreatList();
/*creat_list(Phead);
print_list(Phead);
reserve_list(Phead);*/
//delete_list(Phead);
//print_list(Phead);
//insert_list(Phead);
//print_list(Phead);
//int a = length_list(Phead);
//sort_list(Phead, a);
//print_list(Phead);
/*inversion_list(Phead);
print_list(Phead);*/
int elect;
///*cout << "选择要执行的功能" << endl;*/
//cin >> elect;
do{
welcome();
cin >> elect;
switch (elect)
{
case 1:
creat_list(Phead);
break;
case 2:
insert_list(Phead);
break;
case 3:
delete_list(Phead);
break;
case 4:
print_list(Phead);
break;
case 5:
sort_list(Phead, length_list(Phead));
break;
case 6:
inversion_list(Phead);
break;
case 7:
reserve_list(Phead);
}
} while (elect != 7);
}
STU* GreatList()
{
STU*pHead = (STU*)malloc(sizeof(STU));
pHead->pnext = NULL;
return pHead;
}
bool creat_list(STU*p)
{
system("cls");
system("color 5f");
STU*ptemp = p;
int i = 0, num;
cout << "输入要创建的节点数" << endl;
cin >> num;
for (i = 0; i < num; i++)
{
STU*pnew = (STU*)malloc(sizeof(STU));
cout << "学号为:";
scanf("%d", &pnew->num);
cout << "姓名为: ";
scanf("%s", pnew->name);
cout << "手机号为:";
scanf("%s", pnew->tel);
pnew->pnext = NULL;
p->pnext = pnew;
p = p->pnext;
}
return true;
}
bool print_list(STU*p)
{
system("cls");
system("color 9a");
STU*ptemp = p->pnext;
cout << "------------------------------------" << endl;
while (ptemp != NULL)
{
cout << "学号为: " << ptemp->num << endl;
cout << "姓名为: " << ptemp->name << endl;
cout << "手机号为: " << ptemp->tel << endl;
ptemp = ptemp->pnext;
cout << "************************************" << endl;
}
system("pause");
return true;
}
bool delete_list(STU*p)
{
system("cls");
system("color 6f");
int num;
cout << "输入你要删除的节点的位置:";
cin >> num;
STU*ptemp = p;
int i = 0;
while (ptemp != NULL && i < num - 1)
{
ptemp = ptemp->pnext;
i++;
}
if (i>num - 1 || ptemp == NULL)
{
return false;
}
STU*q = NULL;
q = ptemp->pnext;
ptemp->pnext = q->pnext;
free(q);
getchar();
return true;
}
bool insert_list(STU*p)
{
system("cls");
system("color 4c");
int num, num1;
cout << "输入你要插入的节点的位置:";
cin >> num;
cout << "输入你要插入的节点的个数:";
cin >> num1;
STU*ptemp = p;
STU*ptemp1 = ptemp->pnext;
STU*ptemp2 = NULL;
STU*ptemp3 = NULL;
STU*ptemp4 = (STU*)malloc(sizeof(STU));
ptemp4->pnext = NULL;
int i = 0;
while (ptemp != NULL && i < num - 1)
{
ptemp = ptemp->pnext;
i++;
}
if (i>num - 1 || ptemp == NULL)
{
return false;
}
int j = 0;
for (j = 0; j < num1; j++)
{
STU*pnew = (STU*)malloc(sizeof(STU));
cout << "学号为:";
scanf("%d", &pnew->num);
while (ptemp1 != NULL)
{
if (ptemp1->num == pnew->num)
{
cout << "学号重复,请重新输入" << endl;
int a; cout << "新的学号为:";
cin >> a;
pnew->num = a;
ptemp1 = ptemp1->pnext;
}
else
ptemp1 = ptemp1->pnext;
}
cout << "姓名为: ";
scanf("%s", pnew->name);
cout << "手机号为:";
scanf("%s", pnew->tel);
if (j == 0)
{
ptemp2 = pnew;
}
if (j == num1 - 1)
{
ptemp3 = pnew;
}
pnew->pnext = NULL;
ptemp4->pnext = pnew;
ptemp4 = ptemp4->pnext;
}
ptemp3->pnext = ptemp->pnext;
ptemp->pnext = ptemp2;
getchar();
return true;
}
int length_list(STU*p)
{
system("cls");
system("color 3f");
STU*ptemp = p->pnext;
int i = 0;
while (ptemp != NULL)
{
ptemp = ptemp->pnext;
i++;
}
cout << "长度为" << i << endl;
getchar();
return i;
}
bool sort_list(STU*p, int len)
{
system("cls");
system("color 6a");
STU*ptemp = p->pnext;
STU*ptemp1 = NULL;
int length = len;
int i, j, t;
for (i = 0, ptemp; i<length - 1; i++, ptemp = ptemp->pnext)
{
for (j = i + 1, ptemp1 = ptemp->pnext; j < length; j++, ptemp1 = ptemp1->pnext)
{
if (ptemp->num > ptemp1->num)
{
t = ptemp->num;
ptemp->num = ptemp1->num;
ptemp1->num = t;
}
}
}
getchar();
return true;
}
bool inversion_list(STU*p)
{
system("cls");
system("color 8f");
STU *ptemp, *ptemp1, *ptemp2;
ptemp = p->pnext;
ptemp1 = NULL;
p->pnext = NULL;//将头节点与首节点断开
while (ptemp)
{
ptemp2 = ptemp->pnext;
ptemp->pnext = ptemp1;
ptemp1 = ptemp;
ptemp = ptemp2;
}
p->pnext = ptemp1;//将最后一个节点连接到头节点上
getchar();
return true;
}
void reserve_list(STU*p)
{
system("cls");
system("color 5f");
STU*ptemp = p->pnext;
FILE*pf;
char ch[20] = "a.txt";
pf = fopen(ch, "w");
if (pf == NULL)
{
cout << "打开失败" << endl;
}
else
{
cout << "保存成功" << endl;
while (ptemp != NULL)
{
fprintf(pf, "%d\t%s\t%s\n", ptemp->num, ptemp->name, ptemp->tel);
ptemp = ptemp->pnext;
}
}
getchar();
fclose(pf);
}
void welcome()
{
system("cls");
system("color 8f");
cout << "***********************************************************" << endl;
cout << "\t选择要执行的功能" << endl;
cout << "\t1\t录入数据" << endl;
cout << "\t2\t录入数据" << endl;
cout << "\t3\t再次追加录入数据" << endl;
cout << "\t4\t打印数据" << endl;
cout << "\t5\t对数据排序" << endl;
cout << "\t6\t对数据逆置" << endl;
cout << "\t7\t退出系统" << endl;
cout << "**************************************************************" << endl;
}
通讯录管理系统(C语言编写)
猜你喜欢
转载自blog.csdn.net/m0_49019274/article/details/114906814
今日推荐
周排行