#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
typedef struct page
{
int TatilPage;
int TailInfo;
int OnePageCount;
int CurrentPage;
}Page;
typedef struct node
{
int id;
char *name;
char *tel;
struct node *pNext;
}List;
char get_Didplay_Menu;
char get_Didplay_Return;
int GetId();
List *GetNode();
char *GetName();
char *GetTel();
void AddNode(List **pHead, List *pNode);
void InitList(List **pHead);
void Trverse(List *pHead);
Page *GetPage();
void DisPlay(List *pHead,int nNum);
void ShowMenu();
void ShowPage(Page *pPage, List *pHead);
char GetKey();
void SelectLike(List *pHead);
char *GetString();
void DeleteNode(List **pHead);
void UpdateNode(List *pHead);
char *GetString_n();
void AddNodeInfo(List **pHead);
int GetId()
{
static int id = 1;
return id++;
}
char *GetName()
{
int i;
char *pName = (char *)malloc(sizeof(char)*6);
for(i=0; i<5; i++){
pName[i] = rand()%26 +'a';
}
pName[5] = '\0';
return pName;
}
char *GetTel()
{
int nNum = rand()%4;
int i;
char *pTel = (char *)malloc(sizeof(char)*12);
switch(nNum){
case 0:
strcpy_s(pTel, 12, "155");
break;
case 1:
strcpy_s(pTel, 12, "188");
break;
case 2:
strcpy_s(pTel, 12, "177");
break;
case 3:
strcpy_s(pTel, 12, "166");
}
for(i=3; i<11; i++){
pTel[i] = rand()%10 +'0';
}
pTel[11] = '\0';
return pTel;
}
List *GetNode()
{
List *pTemp = (List *)malloc(sizeof(List));
pTemp->id = GetId();
pTemp->name = GetName();
pTemp->tel = GetTel();
pTemp->pNext = NULL;
return pTemp;
}
void AddNode(List **pHead, List *pNode)
{
if(pHead == NULL || pNode == NULL)return;
if(*pHead == NULL){
*pHead = pNode;
}else{
List *pTemp = *pHead;
while(pTemp->pNext){
pTemp = pTemp->pNext;
}
pTemp->pNext = pNode;
}
}
void InitList(List **pHead)
{
int i;
srand((unsigned int)time(NULL));
for(i=0; i<100; i++){
AddNode(pHead,GetNode());
}
}
Page *GetPage()
{
Page *pPage = (Page *)malloc(sizeof(Page));
pPage->OnePageCount = 0;
pPage->TatilPage = 0;
pPage->TailInfo = 0;
pPage->CurrentPage = 0;
return pPage;
}
void DisPlay(List *pHead,int nNum)
{
Page *pPage = GetPage();
List *pNode = pHead;
char ch = 's';
if(pHead == NULL){
get_Didplay_Menu ^= 10;
ShowMenu();
get_Didplay_Menu ^= 10;
ch = '\0';
}
while(pNode){
pPage->TailInfo++;
pNode = pNode->pNext;
}
pPage->OnePageCount = nNum;
pPage->TatilPage = pPage->TailInfo % pPage->OnePageCount == 0 ?
pPage->TailInfo/pPage->OnePageCount : pPage->TailInfo/pPage->OnePageCount + 1;
while(1){
switch(ch){
case 'w':
if(pPage->CurrentPage <= 1){
printf("已经第一页\n");break;
}
pPage->CurrentPage--;
ShowPage(pPage,pHead);
ShowMenu(pPage);
break;
case 's':
if(pPage->CurrentPage >= pPage->TatilPage){
printf("已经最后一页\n");break;
}
pPage->CurrentPage++;
ShowPage(pPage,pHead);
ShowMenu(pPage);
break;
case 'b':
get_Didplay_Return = ch;
return;
break;
case 'c':
get_Didplay_Return = ch;
return;
break;
case 'd':
get_Didplay_Return = ch;
return;
break;
case 'u':
get_Didplay_Return = ch;
return;
break;
case '\0':
break;
default:
printf("按错了\n");
break;
}
ch = GetKey();
}
}
void ShowPage(Page *pPage, List *pHead)
{
//1~10
//11~20
int nBegin = (pPage->CurrentPage-1) * pPage->OnePageCount + 1;
int nEnd = pPage->CurrentPage * pPage->OnePageCount;
int nCount = 1;
if(pPage == NULL || pHead == NULL)return;
while(pHead && nCount < nBegin){
pHead = pHead->pNext;
nCount++;
}
while(pHead && nCount <= nEnd){
printf("%d\t%s\t%s\n",pHead->id, pHead->name, pHead->tel);
pHead = pHead->pNext;
nCount++;
}
}
void ShowMenu(Page *pPage)
{
switch(get_Didplay_Menu){
case 1:
printf("当前第%d页,一共%d页,总过%d条,按w上一页,按s下一页,按b退出\n", pPage->CurrentPage,pPage->TatilPage,pPage->TailInfo);break;
case 2:
printf("当前第%d页,一共%d页,总过%d条,按w上一页,按s下一页,按b退出,按c继续查询\n", pPage->CurrentPage,pPage->TatilPage,pPage->TailInfo);break;
case 3:
printf("当前第%d页,一共%d页,总过%d条,按w上一页,按s下一页,按b退出,按d删除\n", pPage->CurrentPage,pPage->TatilPage,pPage->TailInfo);break;
case 4:
printf("当前第%d页,一共%d页,总过%d条,按w上一页,按s下一页,按b退出,按u更新\n", pPage->CurrentPage,pPage->TatilPage,pPage->TailInfo);break;
default:
printf("没有数据,按c继续查询,按d退出\n");break;
}
}
char GetKey()
{
char ch,flag = '\0';
while((ch = getchar())=='\n');
while(ch !='\n'){
flag = ch;
break;
}
return flag;
}
void SelectLike(List *pHead)
{
char *str;
List *pTemp = NULL;
List *pNewList = NULL;
if(pHead == NULL)return;
while(1){
int nCount = 1;
printf("输入字符进行搜索\n");
str = GetString();
pTemp = pHead;
while(pTemp){
pTemp->id = nCount++;
if(strncmp(str, pTemp->name, strlen(str)) == 0 ||
strncmp(str, pTemp->tel, strlen(str)) == 0) {
List *pNode = (List *)malloc(sizeof(List));
pNode->id = pTemp->id;
pNode->name = pTemp->name;
pNode->tel = pTemp->tel;
pNode->pNext = NULL;
AddNode(&pNewList,pNode);
}
pTemp = pTemp->pNext;
}
DisPlay(pNewList,10);
while(pNewList){
List *pNode = pNewList;
pNewList = pNewList->pNext;
free(pNode);
pNode = NULL;
}
free(str);
str = NULL;
if(get_Didplay_Return != 'c')return;
}
}
char *GetString()
{
int size=5, nCount=0;
char *str = (char *)malloc(sizeof(char)*size),ch;
while((ch=getchar()) == '\n');
while(ch != '\n'){
str[nCount++] = ch;
if(nCount == size-1){
char *pNewStr = NULL;
str[nCount] = '\0';
size +=5;
pNewStr = (char *)malloc(sizeof(char)*size);
strcpy_s(pNewStr, size, str);
free(str);
str = NULL;
str = pNewStr;
}
ch = getchar();
}
str[nCount] = '\0';
return str;
}
char *GetString_n()
{
int size=5, nCount=0;
char *str = (char *)malloc(sizeof(char)*size),ch;
while((ch=getchar()) != '\n'){
str[nCount++] = ch;
if(nCount == size-1){
char *pNewStr = NULL;
str[nCount] = '\0';
size +=5;
pNewStr = (char *)malloc(sizeof(char)*size);
strcpy_s(pNewStr, size, str);
free(str);
str = NULL;
str = pNewStr;
}
}
str[nCount] = '\0';
return str;
}
void DeleteNode(List **pHead)
{
while(1){
int id;
char ch;
char *str;
List *pTemp = *pHead;
if(*pHead == NULL)return;
SelectLike(*pHead);
if(get_Didplay_Return != 'd')return;
printf("输入要删除数据的id\n");
str = GetString();
id = atoi(str);
if(pTemp->id == id){
*pHead = (*pHead)->pNext;
free(pTemp->name);
free(pTemp->tel);
free(pTemp);
pTemp = NULL;
}else{
while(pTemp->pNext){
if(pTemp->pNext->id == id){
List *pDel = pTemp->pNext;
pTemp->pNext = pTemp->pNext->pNext;
free(pDel->name);
free(pDel->tel);
free(pDel);
pDel = NULL;
break;
}
pTemp = pTemp->pNext;
}
}
free(str);
str = NULL;
printf("继续删除y,按任意键退出\n");
ch = GetKey();
if(ch != 'y' && ch != 'Y')return;
}
}
void UpdateNode(List *pHead)
{
char *str = NULL, ch;
int id = 0;
List *pNode = NULL;
if(pHead == NULL)return;
while(1){
SelectLike(pHead);
if(get_Didplay_Return != 'u')return;
printf("输入修改的id\n");
str = GetString();
id = atoi(str);
pNode = pHead;
while(pNode){
if(pNode->id == id){
char *pStr = NULL;
printf("修改姓名,按回车跳过\n");
pStr = GetString_n();
if(*pStr != '\0'){
free(pNode->name);
pNode->name = pStr;
}
printf("修改电话,按回车跳过\n");
pStr = GetString_n();
if(*pStr != '\0'){
free(pNode->tel);
pNode->tel = pStr;
}
break;
}
pNode = pNode->pNext;
}
free(str);
str = NULL;
printf("继续修改按y,其他键退出\n");
ch = GetKey();
if(ch!='y' && ch!='Y')return;
}
}
void AddNodeInfo(List **pHead)
{
List *pTemp = *pHead;
List *pNode = (List *)malloc(sizeof(List));
if(pHead == NULL)return;
pNode->id = GetId();
printf("输入姓名\n");
pNode->name = GetString();
printf("输入电话\n");
pNode->tel = GetString();
pNode->pNext = NULL;
if(*pHead == NULL){
*pHead = pNode;
}else{
while(pTemp->pNext){
pTemp = pTemp->pNext;
}
pTemp->pNext = pNode;
}
}
int main()
{
List *pHead = NULL;
InitList(&pHead);
while(1){
char ch = NULL;
printf("1.浏览数据\n");
printf("2.查询数据\n");
printf("3.添加数据\n");
printf("4.修改数据\n");
printf("5.删除数据\n");
printf("q.退出\n");
ch = GetKey();
switch (ch)
{
case '1':
get_Didplay_Menu = 1;
DisPlay(pHead,10);
break;
case '2':
get_Didplay_Menu = 2;
SelectLike(pHead);
break;
case '3':
AddNodeInfo(&pHead);
break;
case '4':
get_Didplay_Menu = 4;
UpdateNode(pHead);
break;
case '5':
get_Didplay_Menu = 3;
DeleteNode(&pHead);
break;
case 'q':
return 0;
break;
default:
if(ch != '\0'){
printf("按错了\n");
}
break;
}
}
}
链表小项目优化
最新推荐文章于 2022-08-08 22:02:37 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)