客户端程序:
#include"List.h"
int menu()
{
cout << "1.新建联系人" << endl;
cout << "2.删除联系人" << endl;
cout << "3.浏览通讯录" << endl;
cout << "4.退出通讯录" << endl;
cout << "请输入:";
int input;
cin >> input;
return input;
}
void creatContact(List *pList)
{
Node node;
Person person;
cout << "请输入姓名:";
cin >> person.m_sName;
cout << "请输入号码:";
cin >> person.m_sPhone;
cout << endl;
node.data = person;
pList->ListInsertTail(&node);
}
void deleteContact(List *pList)
{
int num = 0;
cout << "请输入需要删除的联系人序号:";
cin >> num;
Node node;
pList->ListDelete(num - 1, &node);
}
int main()
{
List *pList = new List;
int userOrder = 0;
while (userOrder != 4)
{
userOrder = menu();
switch (userOrder)
{
case 1:
cout << "用户指令--->新建联系人" << endl;
creatContact(pList);
break;
case 2:
cout << "用户指令--->删除联系人" << endl;
deleteContact(pList);
break;
case 3:
cout << "用户指令--->浏览通讯录" << endl;
pList->ListTraverse();
break;
case 4:
cout << "用户指令--->退出通讯录" << endl;
break;
default:
break;
}
}
delete pList;
pList = NULL;
system("pause");
return 0;
}
--头文件
#pragma once
#ifndef LIST_H_
#define LIST_H_
#include"Node.h"
class List
{
public:
List();
~List();
void ClearList();
int ListLength();
bool isEmpty();
bool GetElem(int i, Node *pNode);
int LocateElem(Node *pNode);
bool PriorElem(Node *pcurrentNode, Node *pPreNode);
bool NextElem(Node *pcurrentNode, Node *pNextNode);
bool ListInsert(int i, Node *pNode);
bool ListDelete(int i, Node *pNode);
bool ListInsertHead(Node *pNode);
bool ListInsertTail(Node *pNode);
void ListTraverse();
private:
Node *m_pList;
int m_iLength;
};
#endif // !LIST_H_
--CPP文件:
#include "List.h"
List::List()
{
m_pList = new Node;
m_pList->data.m_sName = "";
m_pList->data.m_sPhone = "";
m_pList->next = NULL;
m_iLength = 0;
}
List::~List()
{
ClearList();
delete m_pList;
m_pList = NULL;
}
void List::ClearList()
{
Node *currentNode = m_pList->next;
while (currentNode->next != NULL)
{
Node *currentNodeNext = currentNode->next;
delete currentNode;
currentNode = currentNodeNext;
}
m_iLength = 0;
}
int List::ListLength()
{
return m_iLength;
}
bool List::isEmpty()
{
return m_iLength == 0 ? true : false;
}
bool List::GetElem(int i, Node * pNode)
{
if (i < 0 || i >= m_iLength)
{
return false;
}
Node *currentNode = m_pList->next;
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 *currentNodeBefore = NULL;
while (currentNode->next != NULL)
{
currentNodeBefore = currentNode;
currentNode = currentNode->next;
if (currentNode->data == pcurrentNode->data)
{
if (currentNodeBefore == NULL)
return false;
pPreNode->data = currentNodeBefore->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;
}
pNextNode->data = currentNode->next->data;
return true;
}
}
return false;
}
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; //currentNode的位置为i-1
}
Node *newNode = new Node;
if (newNode == NULL)
{
return false;
}
newNode->next = currentNode->next;
currentNode->next = newNode;
newNode->data = pNode->data;
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; //currentNodeBefore的位置为i-1
currentNode = currentNode->next; //currentNode的位置为i
}
currentNodeBefore->next = currentNode->next;
pNode->data = currentNode->data;
delete currentNode;
currentNode = NULL;
m_iLength--;
return true;
}
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 = *pNode;
newNode->next = NULL;
currentNode->next = newNode;
m_iLength++;
return true;
}
void List::ListTraverse()
{
Node *currentNode = m_pList;
while (currentNode->next != NULL)
{
currentNode = currentNode->next;
currentNode->printNode();
}
}
Person类:
--头文件
#pragma once
#ifndef PERSON_H_
#define PERSON_H_
#include<string>
using namespace std;
class Person
{
friend ostream& operator<<(ostream& out, Person &person);
public:
string m_sName;
string m_sPhone;
Person& operator=(Person &person);
bool operator==(Person &person);
};
#endif // !PERSON_H_
--CPP文件:
#include "Person.h"
Person & Person::operator=(Person & person)
{
this->m_sName = person.m_sName;
this->m_sPhone = person.m_sPhone;
return *this;
// TODO: 在此处插入 return 语句
}
bool Person::operator==(Person & person)
{
return this->m_sName == person.m_sName&&this->m_sPhone == person.m_sPhone ? true : false;
}
ostream & operator<<(ostream & out, Person & person)
{
out << "姓名:" << person.m_sName << endl;
out << "号码:" << person.m_sPhone << endl;
return out;
// TODO: 在此处插入 return 语句
}
Node类:
#pragma once
#ifndef NODE_H_
#define NODE_H_
#include<iostream>
#include"Person.h"
using namespace std;
class Node {
public:
Person data;
Node *next;
void printNode();
};
#endif // !NODE_H_
#include "Node.h"
void Node::printNode()
{
cout << data << endl;
}