为了账号安全,请及时绑定邮箱和手机立即绑定

具体实现代码@数据结构探险——线性表应用之通讯录(二)

标签:
C++
file:LinkedList.hpp

#ifndef LinkedList_hpp
#define LinkedList_hpp

#include <stdio.h>
#include "Node.hpp"

class LinkedList{
public:
    LinkedList();
    ~LinkedList();
    void ClearList();
    bool ListEmpty();
    int ListLength();
    bool getElem(int index,Node *pNode);
    int locateElem(Node *pNode);
    bool preElem(Node *pCurrentNode,Node *pPreNode);
    bool nextElem(Node *pCurrentNode,Node *pNextNode);
    void ListTraverse();
    bool ListInsert(int index,Node *pNode);
    bool ListDelete(int index,Node *pNode);
    bool ListInsertHead(Node *pNode);
    bool ListInsertTail(Node *pNode);
private:
    int m_iLength;
    Node *m_pList;
};

#endif /* LinkedList_hpp */
file:LinkedList.cpp

#include "LinkedList.hpp"
#include <iostream>

using namespace std;

LinkedList::LinkedList(){
    m_pList = new Node;
    m_pList->next = NULL;
    m_iLength = 0;
}

bool LinkedList::ListEmpty(){
    return m_iLength==0?true:false;
}

int LinkedList::ListLength(){
    return m_iLength;
}

void LinkedList::ClearList(){
    Node *CurrentNode = m_pList->next;
    while(CurrentNode != NULL){
        Node *temp = CurrentNode->next;
        delete CurrentNode;
        CurrentNode = temp;
    }
    m_pList->next = NULL;
    m_iLength = 0;
}

LinkedList::~LinkedList(){
    ClearList();
    delete m_pList;
    m_pList = NULL;
}

bool LinkedList::ListInsertHead(Node *pNode){
    Node *temp = m_pList->next;
    Node *newNode = new Node;
    if(newNode == NULL){
        return false;
    }
    else{
        m_pList->next = newNode;
        newNode->data = pNode->data;        
        newNode->next = temp;

        m_iLength++;
        return true;
    }
}

bool LinkedList::ListInsertTail(Node *pNode){
    Node *CurrentNode = m_pList;
    while(CurrentNode->next != NULL){
        CurrentNode = CurrentNode->next;
    }
    Node *newNode = new Node;
    if(newNode == NULL){
        return false;
    }
    else{
        CurrentNode->next = newNode;
        newNode->data = pNode->data;
        newNode->next = NULL;

        m_iLength++;
        return true;
    }
}

bool LinkedList::ListInsert(int index, Node *pNode){
    if(index < 0 || index > m_iLength){
        return false;
    }
    else{
        Node *CurrentNode = m_pList;
        for(int k = 0;k < index;k++){
            CurrentNode = CurrentNode->next;
        }
        Node *newNode = new Node;
        if(newNode == NULL){
            return false;
        }
        else{
            newNode->data = pNode->data;
            newNode->next = CurrentNode->next;
            CurrentNode->next = newNode;

            m_iLength++;
            return true;
        }
    }
}

bool LinkedList::ListDelete(int index, Node *pNode){
    if(index < 0 || index > m_iLength){
        return false;
    }
    else{
        Node *CurrentNode = m_pList;
        Node *CurrentNodePre = NULL;
        for(int k = 0;k < index;k++){
            CurrentNodePre = CurrentNode;
            CurrentNode = CurrentNode->next;
        }
        CurrentNodePre->next = CurrentNode->next;
        pNode->data = CurrentNode->data;
        delete CurrentNode;
        CurrentNode = NULL;
        m_iLength--;
        return true;
    }
}

bool LinkedList::getElem(int index,Node *pNode){
    if(index < 0 || index > m_iLength){
        return false;
    }
    else{
        Node *CurrentNode = m_pList;
        Node *CurrentNodePre = NULL;
        for(int k = 0;k < index;k++){
            CurrentNodePre = CurrentNode;
            CurrentNode = CurrentNode->next;
        }
        pNode->data = CurrentNode->data;
        return true;
    }
}

int LinkedList::locateElem(Node *pNode){
    Node *CurrentNode = new Node;
    int count = 0;
    while(CurrentNode->next != NULL){
        CurrentNode = CurrentNode->next;
        if(CurrentNode->data == pNode->data){
            return count;
        }
        else{
            count ++;
        }
    }
    return -1;
}

bool LinkedList::preElem(Node *pCurrentNode,Node *pPreNode){
    Node *CurrentNode = m_pList;
    Node *tempNode = NULL;
    while(CurrentNode->next != NULL){
        tempNode = CurrentNode;
        CurrentNode = CurrentNode->next;
        if(CurrentNode->data == pCurrentNode->data){
            if(tempNode == m_pList){
                return false;
            }
            else{
                pPreNode->data = tempNode->data;
                return true;
            }
        }
    }
    return false;
}

bool LinkedList::nextElem(Node *pCurrentNode,Node *pNextNode){
    Node *CurrentNode = m_pList;
    while(CurrentNode->next != NULL){
        CurrentNode = CurrentNode->next;
        if(CurrentNode->data == pCurrentNode->data){
            if(CurrentNode->next == NULL ){
                return false;
            }
            else{
                pNextNode->data = CurrentNode->next->data;
                return true;
            }
        }
    }
    return false;
}

void LinkedList::ListTraverse(){
    Node *CurrentNode = m_pList;
    int count = 1;
    while(CurrentNode->next != NULL){
        CurrentNode = CurrentNode->next;
        cout << count << ".";
        CurrentNode->printData();
        count ++;
    }
    cout << endl;
}
file:Node.hpp

#ifndef Node_hpp
#define Node_hpp

#include "Person.hpp"

class Node{
public: 
    void printData();

public:
    Person data;
    Node *next;
    Node *prev;
};

#endif /* Node_hpp */
file:Node.cpp

#include "Node.hpp"
#include <iostream>

using namespace std;

void Node::printData(){
    cout << data << endl;
}
file:Person.hpp

#ifndef Person_hpp
#define Person_hpp

#include <stdio.h>
#include <string>

using namespace std;

class Person{
public:
    friend ostream &operator<<(ostream &out,Person &person);
    string name;
    string phone;
    Person &operator=(Person &person);
    bool operator==(Person &person);

};

#endif /* Person_hpp */
file:Person.cpp

#include "Person.hpp"
#include <ostream>

ostream &operator<<(ostream &out,Person &person){
    out << person.name << "------------" << person.phone << endl;
    return out;
}

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;
    }
    else{
        return false;
    }
}
file:demo.cpp

#include <iostream>
#include <string>
#include "LinkedList.hpp"

using namespace std;

int Menu(){
    //显示通讯录功能菜单
    cout << "功能菜单" << endl;
    cout << "1.新建联系人" << endl;
    cout << "2.删除联系人" << endl;
    cout << "3.浏览通讯录" << endl;
    cout << "4.退出通讯录" << endl;

    cout << "请输入:" << endl;

    int order = 0;
    cin >> order;

    return order;
}

void createPerson(LinkedList *pList){
    Node node;
    Person person;
    cout << "请输入姓名:" << endl;
    cin >> person.name;
    cout << "请输入电话:" << endl;
    cin >> person.phone;
    node.data = person;
    pList->ListInsertTail(&node);
}

void deletePerson(LinkedList *pList){
    Node node;

    int index = 0;
    cout << "请输入你需要删除的联系人序号:" << endl;
    cin >> index;

    pList->ListDelete(index, &node);
    cout << "已删除该联系人" << endl;
}

int main(void){
    LinkedList *pList = new LinkedList;

    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;
}

图片描述

点击查看更多内容
2人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消