Person類,包括三個重載函數以及name和phone兩個數據域函數
Person.h
#pragma once
#include<string>
#include<ostream>
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); // 判斷相等運算符重載
};
Person.cpp
#include "Person.h"
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;
}
return false;
}
Node.h 單個節點的類
#pragma once
#include "Person.h"
class Node
{
public:
Person data;
Node *next;
void printNode();
};
Node.cpp
#include "Node.h"
#include<iostream>
void Node::printNode()
{
std::cout << data << std::endl;
}
注意,這裏直接輸出data,但是data是Person的實例,說明是一個運算符重載
List.h
#pragma once
#include"Node.h"
class List
{
private:
Node * m_pList;
int m_iLength;
public:
List();
~List();
void ClearList(); // 清空整個鏈表
bool ListEmpty(); // 判斷鏈表是否爲空
bool ListInsertHead(Node *pNode); // 頭部插入一個節點
bool ListInsertTail(Node *pNode); // 尾部插入一個節點
bool ListInsert(int i, Node *pNode); // 從任意位置插入節點
bool ListDelete(int i, Node *pNode); // 從任意位置刪除結點
bool ListGetElem(int i, Node *pNode); // 查找第i個元素
int LocateElem(Node *pNode); // 查找結點的位序
bool PriorElem(Node *pCurrentNode, Node *pPreNode); // 找指針的前驅指針
bool NextElem(Node *pCurrentNode, Node *pNextNode); // 找指針的後繼指針
void ListTraverse(); // 鏈表的遍歷
};
List.cpp 實現List.h中具體函數的操作,因爲具體實現中有賦值=和比較運算符==,所以都需要使用重載
#include "List.h"
List::List()
{
m_pList = new Node;
m_pList->next = NULL;
m_iLength = 0;
}
bool List::ListEmpty()
{
if (m_iLength == 0)
{
return true;
}
else
{
return false;
}
}
void List::ClearList()
{
Node *currentNode = m_pList->next; // 指向頭指針後第一個元素
while (currentNode != NULL)
{
Node *temp = currentNode->next;
delete currentNode;
currentNode = temp;
}
m_pList->next = NULL; // 頭指針下一個爲空
}
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;
}
bool List::ListInsert(int i, Node *pNode)
{
if (i<0 || i>m_iLength)
return false;
Node *currentNode = m_pList;
for (int k = 0; k < i; k++)
{
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 k = 0; k <= i; k++)
{
currentNodeBefore = currentNode;
currentNode = currentNode->next;
}
currentNodeBefore->next = currentNode->next;
pNode->data = currentNode->data;
delete currentNode;
currentNode = NULL;
m_iLength--;
return true;
}
bool List::ListGetElem(int i, Node *pNode)
{
if (i < 0 || i >m_iLength)
return false;
Node *currentNode = m_pList;
for (int k = 0; k <= i; k++)
{
currentNode = currentNode->next;
}
pNode->data = currentNode->data;
return true;
}
int List::LocateElem(Node *pNode)
{
Node *currentNode = m_pList;
int count = 0;
while (currentNode->next!= NULL)
{
currentNode = currentNode->next;
if (currentNode->data == pNode->data)
{
return count;
}
count++;
}
return -1;
}
bool List::PriorElem(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 List::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 List::ListTraverse()
{
Node *currentNode = m_pList;
while (currentNode->next != NULL)
{
currentNode = currentNode->next;
currentNode->printNode();
}
}
簡單測試主函數
#include "List.h"
#include<iostream>
using namespace std;
int menu()
{
// 顯示通訊錄功能菜單
cout << "功能菜單" << endl;
cout << "1.新建聯繫人" << endl;
cout << "2.刪除聯繫人" << endl;
cout << "3.瀏覽通訊錄" << endl;
cout << "4.退出通訊錄" << endl;
cout << "請輸入:";
int order;
cin >> order;
return order;
}
void createPerson(List *pList)
{
Node node;
Person person;
cout << "請輸入姓名:";
cin >> person.name;
cout << "請輸入電話:";
cin >> person.phone;
node.data = person;
pList->ListInsertTail(&node);
}
int main()
{
int userOrder = 0;
List *pList = new List();
while (userOrder != 4)
{
userOrder = menu();
switch (userOrder)
{
case 1 :
cout << "用戶指令--->>新建聯繫人:" << endl;
createPerson(pList);
break;
case 2:
cout << "用戶指令--->>刪除聯繫人:" << endl;
break;
case 3:
cout << "用戶指令--->>瀏覽通訊錄:" << endl;
pList->ListTraverse();
break;
case 4:
cout << "用戶指令--->>退出通訊錄:" << endl;
break;
default:
break;
}
}
}