第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

具體實(shí)現(xiàn)代碼@數(shù)據(jù)結(jié)構(gòu)探險(xiǎn)——線(xiàn)性表應(yīng)用之通訊錄(二)

標(biāo)簽:
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;
}

图片描述

點(diǎn)擊查看更多內(nèi)容
2人點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶(hù)
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消