C++ 链表面向对象实现简单通讯录

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

 

    原文作者:算法
    原文地址: https://www.twblogs.net/a/5bd39e682b717778ac209817
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞