C语言 用链表实现电话本的功能


简介:

用链表实现电话本的功能(C语言)
电话本具有如下4个功能:
1.创建一个电话本,电话本里面包含名字和电话号码
2.在指定位置插入一个名字和电话号码
3.在指定位置删除一个名字和电话号码
4.打印电话本

//其中那个color函数是我为了美观加上去的,如果感觉不需要的话可以将代码中所有有关color的都删掉即可


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <windows.h>
using namespace std;
const int N = 1000+10;
int n;
struct List 
{
    struct List *next;
    char name[100];
    char number[20];
    List()
    {
        next = NULL;
        memset(name,0,sizeof(name));
        memset(number,0,sizeof(number));
    }
};
struct List *root = new struct List;


void Creat();
void Delet();
void Add();
void Print();
void color(const unsigned short  color1);

int main()
{
    color(10);
    printf("\n\n\n\n        --------------- WSM's phonetxt---------------\n\n");
    printf("            You could chose these ops:            \n");
    printf("                1.Creat the phonetxt               \n");
    printf("                2.Delet the member in the phonetxt \n");
    printf("                3.Add the member in the phonetxt   \n");
    printf("                4.Print the phonetxt               \n");
    printf("                0.Exit WSM's phonetxt              \n\n");
    printf("        --------------------------------------------\n\n");
    color(14);
    printf("\n\n\n\n     Now,you can enter an optiton:");

    int op;
    bool flag = false;
    while(scanf("%d",&op)!=EOF)
    {
        if(op==1) Creat(),flag = true;
        else if(flag&&op==2) Delet();
        else if(flag&&op==3) Add();
        else if(flag&&op==4) Print();
        else if(flag&&op==0) return 0;
        else 
        {
            color(4);
            printf("     You input is invalid,reinput please:)\n");
            color(14);
        }
        printf("\n     Now,you can enter an optiton:");
    }
    return 0;
}


void Creat()
{
    cout<<"     how many numbers do you want to built:";
    scanf("%d",&n);
    cout<<"     please input their informations:"<<endl;
    char NAME[N][100],NUMBER[N][20];
    for(int i=1;i<=n;i++)
    {
         printf("       input the %d person name:",i);
         scanf(" %s",NAME[i]);
         printf("       input the %d person phonenumber:",i);
         scanf(" %s",NUMBER[i]);
    }
    struct List *cur = root;
    for(int i=1;i<=n;i++)
    {
        struct List *newlist = new struct List;
        strcpy(newlist->name,NAME[i]);
        strcpy(newlist->number,NUMBER[i]);
        cur->next = newlist; 
        cur = cur->next;
    }
    color(9);
    cout<<"     well done,the phonetxt has been created!!!"<<endl;
    color(14);
    return;
}

void Delet()
{
    cout<<"     please enter the number you want to delet:";
    heredelet:
    int x;
    scanf("%d",&x);
    if( x<0 || x>n) 
    {
        color(4);
        cout<<"     sorry,your input is invalid,please input again:";
        color(14);
    goto heredelet;
    }

    struct List *cur = root;
    if(x==1)
    {
        cur = cur ->next;
        root->next = cur->next; 
    }
    else
    {
        int cnt = 0;
        while(cnt<x-1) cur = cur->next,cnt++;
        if(x==n) cur->next =NULL;
        else
        {
            struct List *tmp = cur;
            cur = cur ->next;
            tmp ->next = cur -> next;
        }
    }
    n--;
    color(9);
    cout<<"     well done,the member has been deleted!!!"<<endl;
    color(14);
    return;
}
void Add()
{
    cout<<"     please enter the number you want to add:";
    hereadd:
    int x;
    scanf("%d",&x);
    if( x<0 || x>n+1) 
    {
        color(4);
        cout<<"     sorry,your input is invalid,please input again:";
        color(14);
        goto hereadd;
    }
    char NAME[100],NUMBER[20];
    printf("       input the person name:");
    scanf(" %s",NAME);
    printf("       input the person phonenumber:");
    scanf(" %s",NUMBER);

    struct List *cur = root;
    if(x==1)
    {
        cur = cur->next;
        struct List *tmp = new struct List;
        strcpy(tmp->name,NAME);
        strcpy(tmp->number,NUMBER);
        root ->next = tmp;
        tmp -> next = cur;     
    }
    else
    {
        int cnt = 0;
        while(cnt<x-1) cur = cur->next,cnt++;
        struct List *tmp = new struct List;
        strcpy(tmp->name,NAME);
        strcpy(tmp->number,NUMBER);
        if(x==n+1) 
        {
            cur->next = tmp;
        }
        else
        {
           struct List *tmp1 = cur;
           tmp1 = tmp1->next;
           cur->next = tmp;
           tmp->next = tmp1;
        } 
    }
    n++;
    color(9);
    cout<<"     well done,the member has been added!!!"<<endl;
    color(14);
    return;
}
void Print()
{
    struct List *cur = root;
    color(8);
    printf("        Name------phonenumber");
    while(cur->next!=NULL)
    {
        printf("        %s      %s\n",cur->name,cur->number);
        cur = cur->next;
    }
    printf("        %s      %s\n",cur->name,cur->number);
    color(14);
    color(9);
    cout<<"     well done,the phonetxt is above!!!"<<endl;
    color(14);
    return;
}

void color(const unsigned short color1)
{        
    if(color1>=0&&color1<=15)
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color1);
    else
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
    return;
}


  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值