2022-7-20-----C++------用链表实现通讯录功能

#include <iostream>
using namespace std;

/* 声明  */
struct PersionInfo;
bool is_number(string str);
void showTip();
PersionInfo* endP(PersionInfo* head);
void add_persionInfo(PersionInfo* head);
void show_persionInfo(PersionInfo* head);

/*定义结构体*/
struct PersionInfo
{
    int num;
    string name;
    PersionInfo* next;
};

void showTip() {
    cout << "*******************" << endl;
    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 << "*******************" << endl;
    cout << "*******************" << endl;
}



bool is_number(string str)
{
    if (str.c_str()[0] != 45)
    {
        for (int i = 0; i < str.length(); i++)
        {
            if (str.c_str()[i] < '0' || str.c_str()[i] > '9')
            {
                return false;
            }
        }
        return true;
    }
    else
    {
        for (int i = 1; i < str.length(); i++)
        {
            if (str.c_str()[i] < '0' || str.c_str()[i] > '9')
            {
                return false;
            }
        }
        return true;
    }
}


void show_persionInfo(PersionInfo* head) {
    PersionInfo* q = head->next;
    PersionInfo* p;
    if (q == NULL) {
        cout <<"无联系人" << endl;
    }
    while (q != NULL)
    {
        cout << q->name <<"--------->"<<q->num << endl;
        p = q;
        q = q->next;
        if (q == NULL) {
            cout<<"尾节点为:"<<p->name << endl;
        }
    }
}

/*获取链表的尾结点*/
PersionInfo* endP(PersionInfo* head) {
    PersionInfo* q = head->next;
    if (q == NULL) {
        return head;
    }
    PersionInfo* p;
    while (q != NULL)
    {
        p = q;
        q = q->next;
        if (q == NULL) {
            cout <<"获取尾节点!!!" << endl;
            return p;
        }
    }
}

void add_persionInfo(PersionInfo* head) {
    PersionInfo* p = NULL;
    PersionInfo* q;

    int num = 0;
    string name = "xxx";
    string input;
    q = endP(head);
   
    cout << "请输入号码" << endl;
    cin >> input;

    while (input != "-1")
    {
        if (is_number(input)) {
            num = atoi(input.c_str());
            cout << "请输入姓名" << endl;
            cin >> name;
            p = new PersionInfo;
            p->num = num;
            p->name = name;
            p->next = NULL;

            q->next = p;
            q = q->next;
     
        }
        else {
            cout << "号码请输入数字!" << endl;
        }
        cout << "请输入号码" << endl;
        cin >> input;
    }
}

void  find_PersionInfo(PersionInfo* head, int findnum) {
    PersionInfo* q = head->next;
    while ( q != NULL )
    {
        if (q->num == findnum) {
            cout<<"查询到:" << endl;
            cout << q->name << "--------->" << q->num << endl;
            break;
        }
        else {
            q = q->next;
        }
    }
};

void  changNum_PersionInfo
(PersionInfo* head, int findnum) {
    PersionInfo* q = head->next;
    while (q != NULL)
    {
        if (q->num == findnum) {
            cout << "请输入想要修改的数据:" << endl;
            cin >> q->num;
            cin >> q->name;
            cout << q->name << "--------->" << q->num << endl;
            break;
        }
        else {
            q = q->next;
        }
    }
};
int main()
{
    showTip();
    PersionInfo* head = NULL;
    head = new PersionInfo;
    head->next = NULL;
    int sel = 0;
    cin >> sel;
    while ( sel != 7 ) {
        if (sel == 1) {
            add_persionInfo( head );
        }
        else if (sel == 2) {
            show_persionInfo(head);
        }
        else if (sel == 3) {

        }
        else if (sel == 4) {
            string findnum = "";
            cin >> findnum;
            if (is_number(findnum)) {
                find_PersionInfo(head, atoi(findnum.c_str()));
            }
            else
            {
                cout << "号码请输入数字!" << endl;
            }
            
        }
        else if (sel == 5) {
            string changNum = "";
            cin >> changNum;
            if (is_number(changNum)) {
                changNum_PersionInfo(head, atoi(changNum.c_str()));
            }
            else
            {
                cout << "号码请输入数字!" << endl;
            }
        }
        else if (sel == 6) {
            head = NULL;
            head = new PersionInfo;
            head->next = NULL;
        }
        else if (sel == 2) {

        }
        showTip();
        cin >> sel;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值