通讯录功能实现/c++链表

***********************************************************
person.h

#ifndef PERSON_H
#define PERSON_H
#include<iostream>
#include<string>
using namespace std;
class Person
{
//    friend ostream &operator<<(ostream &out,Person &person);
public:
    string name;
    string phone;
    Person &operator=(Person &person);
    bool operator==(Person &person);

};
#endif // PERSON_H
***************************************************************************

person.cpp
#include"Person.h"
Person &Person::operator=(Person &person)
{
    this->name=person.name;
    this->phone=person.phone;
    return *this;
}
bool Person::operator==(Person &person)
{
    if(this->name==person.name||this->phone==person.phone)
    {
        return true;
    }
    return false;
}
//ostream &operator<<(ostream &out,Person &person)
//{
//    out<<person.name<<","<<person.phone;
//    return out;
//}

*******************************************
Node.h

#ifndef NODE_H
#define NODE_H
#include"Person.h"
#include<iostream>
class Node
{
  public:
      Person data;
      Node *next;
      void PrintNode();
};

#endif // NODE_H
**************************************************************

Node.cpp
#include"Node.h"
#include<iostream>
using namespace std;
void Node::PrintNode()
{
    cout<<data.name<<","<<data.phone<<endl;
    //cout<<data<<endl;
}
************************************************************************

List.h
#define LIST_H
#include<iostream>
#include"Node.h"
class List
{
public:
    List();//链表不需要一开始就确定大小
    ~List();//所有的节点都释放掉
    void ClearList();//只保留头节点 其他的节点释放掉
    bool ListInsert(int i,Node *pNode);
    bool ListDelete(int i,Node *pNode);
    int ListLocate(Node *pNode);
    bool GetElem(int i,Node *pNode);
    bool ListPrior(Node *current,Node *prior);
    bool ListNext(Node *current,Node *next);
    int ListLength();
    bool ListEmpty();
    void ListTraverse();
    bool ListInsertHead(Node *pNode);
    bool ListInsertTail(Node *pNode);
private:
    Node *m_pList;
    int m_iLength;
};
#endif // LIST_H
*********************************************************************

List.cpp
#include<iostream>
#include"List.h"
using namespace std;
List::List()
{
    m_pList=new Node;
    //m_pList->data=0;
    m_pList->next=NULL;
    m_iLength=0;
}
List::~List()
{
  ClearList();
  delete m_pList;
  m_pList=NULL;
}
bool List::ListInsertHead(Node *pNode)
{
    Node *temp=m_pList->next;
    Node *newNode=new Node;
    if(newNode==NULL)
        {
            return false;
        }
    newNode->data=pNode->data;
    m_pList->next=newNode;
    newNode->next=temp;
    m_iLength++;
    return true;
}

bool List::ListInsertTail(Node *pNode)
{
    Node *currentNode=m_pList;
    while(currentNode->next!=NULL)
    {
        currentNode=currentNode->next;
    }
    Node *newNode=new Node;
    if(newNode==NULL)
    {
        return false;
    }
    newNode->data=pNode->data;
    newNode->next=NULL;
    currentNode->next=newNode;
    m_iLength++;
    return true;
}

void List::ClearList()
{
    Node *currentNode=m_pList->next;
    while(currentNode!=NULL)
    {
        Node *temp=currentNode->next;
        delete currentNode;
        currentNode=temp;
    }
    m_pList->next=NULL;
    m_iLength=0;
}
bool List::ListInsert(int i,Node *pNode)
{
    if(i<0||i>m_iLength)
    {
        return false;
    }
    Node *currentNode=m_pList;
    for(int j=0;j<i;j++)
    {
        currentNode=currentNode->next;
    }
    Node *newNode=new Node;
    if(newNode==NULL)
    {
        return false;
    }
    newNode->data=pNode->data;
    newNode->next=currentNode->next;
    currentNode->next=newNode;
    m_iLength++;
    return true;
}
bool List::ListDelete(int i,Node *pNode)
{
    if(i<0||i>=m_iLength)
    {
        return false;
    }
    Node *currentNode=m_pList;
    Node *currentNodeBefore=NULL;
    for(int j=0;j<=i;j++)
        {
          currentNodeBefore=currentNode;
          currentNode=currentNode->next;
        }
    currentNodeBefore->next=currentNode->next;
    pNode->data=currentNode->data;
    delete currentNode;delete currentNodeBefore; currentNode=NULL;
    m_iLength--;
//    for(int j=1;j<i;j++)
//    {
//        currentNode=currentNode->next;
//    }
//    currentNode->next=currentNode->next->next;
//    pNode->data=currentNode->next->data;
//    delete currentNode;
//    currentNode=NULL;
//    m_iLength--;
    return true;
}
int List::ListLocate(Node *pNode)
{
    Node *currentNode=m_pList;
    for(int i=0;i<m_iLength;i++)
    {
        if(currentNode->data==pNode->data)
        {
            return i;
        }
        currentNode=currentNode->next;
    }
    return -1;
}
bool List::GetElem(int i,Node *pNode)
{
    if(i<0||i>=m_iLength)
    {
        return false;
    }
    Node *currentNode=m_pList;
    for(int j=0;j<=i;j++)
    {
        currentNode=currentNode->next;
    }
    pNode->data=currentNode->data;
    return true;
}
bool List::ListPrior(Node *current,Node *prior)
{
    Node *currentNode=m_pList;
    Node *currentNodeBefore=NULL;
    for(int i=0;i<m_iLength;i++)
    {
        currentNodeBefore=currentNode;
        currentNode=currentNode->next;
        if(current->data==currentNode->data)
        {
            if(currentNodeBefore!=m_pList)
            {
                return false;
            }
            prior->data=currentNodeBefore->data;
            return true;
        }
    }
    return false;
}
bool List::ListNext(Node *current,Node *next)
{
    Node *currentNode=m_pList;
    Node *currentNodeAfter=NULL;
    for(int i=0;i<m_iLength;i++)
    {
        currentNodeAfter=currentNode->next;
        if(currentNode->data==current->data&&i!=m_iLength-1)
        {
            next->data=currentNodeAfter->data;
            return true;
        }
        currentNode=currentNode->next;
    }
    return false;
}
int List::ListLength()
{
    return m_iLength;
}
bool List::ListEmpty()
{
    if(m_iLength==0)
        return true;
    return false;
}
void List::ListTraverse()
{
    Node *currentNode=m_pList;
    for(int i=0;i<m_iLength;i++)
    {
        currentNode=currentNode->next;
        currentNode->PrintNode();
    }
}
****************************************************

mian
#include<iostream>
#include<stdlib.h>
#include"List.h"
#include"Node.h"
using namespace std;
int menu();
void CreatePerson(List *pList);
void DeletePerson(List *pList);
int main(void)
{
//    List *pList=new List;
//
//    Node node1;
//    Node node2;
//    Node node3;
//    Node node4;
//    node1.data.name="car";
//    node1.data.phone="13640558897";
//    node2.data.name="ctt";
//    node2.data.phone="18580787315";
//    node2.data=4;
//    node3.data=5;
//    node4.data=6;
//
//    Node node5;
//    node5.data=7;
//
//    Node temp;
//    pList->ListInsertTail(&node1);
//    pList->ListInsertTail(&node2);
//    pList->ListInsertTail(&node3);
//    pList->ListInsertTail(&node4);
//    pList->ListInsert(1,&node5);
//
//    pList->ListDelete(1,&temp);
//      cout<<temp.data<<endl;
//    pList->ListTraverse();
//    pList->GetElem(0,&temp);
//    cout<<temp.data<<endl;
//    pList->ListPrior(&node1,&temp);
//
//    pList->ListNext(&node2,&temp);
//
//    cout<<temp.data<<endl;
//
//    if(!pList->ListEmpty())
//    {
//        cout<<"not empty"<<endl;
//    }
//
//    pList->ClearList();
//
//    if(pList->ListEmpty())
//    {
//        cout<<"empty"<<endl;
//    }
//    cout<<pList->ListLength()<<endl;
//    delete pList;
//    pList=NULL;
    List *pList=new List;
    Node temp;
    int userOrder=0;
    while(userOrder!=4)
    {
        userOrder=menu();
        switch(userOrder)
        {
        case 1:
            cout<<"用户指令----->>新建联系人"<<endl;
            CreatePerson(pList);
            break;
        case 2:
            cout<<"用户指令----->>删除联系人"<<endl;
            DeletePerson(pList);
            break;
        case 3:
            cout<<"用户指令----->>浏览通讯录"<<endl;
            pList->ListTraverse();
            break;
        case 4:
            cout<<"用户指令----->>退出通讯录"<<endl;
            break;
        default:
            break;
        }
    }


    delete pList;
    pList=NULL;
    return 0;
}
int menu()
{
    cout<<"**菜单功能**"<<endl;
    cout<<"1.新建联系人"<<endl;
    cout<<"2.删除联系人"<<endl;
    cout<<"3.浏览通讯录"<<endl;
    cout<<"4.退出通讯录"<<endl;
    cout<<"请输入:"<<endl;
    int order;
    cin>>order;
    return order;
}
void CreatePerson(List *pList)
{
    Node node;
    Person person;
    cout<<"请输入联系人姓名"<<endl;
    cin>>person.name;
    cout<<"请输入联系人电话"<<endl;
    cin>>person.phone;
    node.data=person;
    pList->ListInsertTail(&node);
}
void DeletePerson(List *pList)
{
    Node node;
    Person person;
    cout<<"请输入你想删除的联系人姓名"<<endl;
    cin>>person.name;
    node.data=person;
    int i=pList->ListLocate(&node);
    pList->ListDelete(i,&node);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值