***********************************************************
person.h
#ifndef PERSON_H
#define PERSON_H
#include<iostream>
#include<string>
using namespace std;
#define PERSON_H
#include<iostream>
#include<string>
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);
{
// friend ostream &operator<<(ostream &out,Person &person);
public:
string name;
string phone;
Person &operator=(Person &person);
bool operator==(Person &person);
};
#endif // PERSON_H
#endif // PERSON_H
***************************************************************************
person.cpp
#include"Person.h"
Person &Person::operator=(Person &person)
{
this->name=person.name;
this->phone=person.phone;
return *this;
}
{
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;
}
{
if(this->name==person.name||this->phone==person.phone)
{
return true;
}
return false;
}
//ostream &operator<<(ostream &out,Person &person)
//{
// out<<person.name<<","<<person.phone;
// return out;
//}
//{
// out<<person.name<<","<<person.phone;
// return out;
//}
*******************************************
Node.h
#ifndef NODE_H
#define NODE_H
#include"Person.h"
#include<iostream>
class Node
{
public:
Person data;
Node *next;
void PrintNode();
};
#define NODE_H
#include"Person.h"
#include<iostream>
class Node
{
public:
Person data;
Node *next;
void PrintNode();
};
#endif // NODE_H
**************************************************************
Node.cpp
#include"Node.h"
#include<iostream>
using namespace std;
#include<iostream>
using namespace std;
void Node::PrintNode()
{
cout<<data.name<<","<<data.phone<<endl;
//cout<<data<<endl;
}
{
cout<<data.name<<","<<data.phone<<endl;
//cout<<data<<endl;
}
************************************************************************
List.h
#define LIST_H
#include<iostream>
#include"Node.h"
#include"Node.h"
class List
{
public:
List();//链表不需要一开始就确定大小
~List();//所有的节点都释放掉
void ClearList();//只保留头节点 其他的节点释放掉
bool ListInsert(int i,Node *pNode);
bool ListDelete(int i,Node *pNode);
int ListLocate(Node *pNode);
bool GetElem(int i,Node *pNode);
bool ListPrior(Node *current,Node *prior);
bool ListNext(Node *current,Node *next);
int ListLength();
bool ListEmpty();
void ListTraverse();
bool ListInsertHead(Node *pNode);
bool ListInsertTail(Node *pNode);
private:
Node *m_pList;
int m_iLength;
};
#endif // LIST_H
{
public:
List();//链表不需要一开始就确定大小
~List();//所有的节点都释放掉
void ClearList();//只保留头节点 其他的节点释放掉
bool ListInsert(int i,Node *pNode);
bool ListDelete(int i,Node *pNode);
int ListLocate(Node *pNode);
bool GetElem(int i,Node *pNode);
bool ListPrior(Node *current,Node *prior);
bool ListNext(Node *current,Node *next);
int ListLength();
bool ListEmpty();
void ListTraverse();
bool ListInsertHead(Node *pNode);
bool ListInsertTail(Node *pNode);
private:
Node *m_pList;
int m_iLength;
};
#endif // LIST_H
*********************************************************************
List.cpp
#include<iostream>
#include"List.h"
using namespace std;
#include"List.h"
using namespace std;
List::List()
{
m_pList=new Node;
//m_pList->data=0;
m_pList->next=NULL;
m_iLength=0;
}
{
m_pList=new Node;
//m_pList->data=0;
m_pList->next=NULL;
m_iLength=0;
}
List::~List()
{
ClearList();
delete m_pList;
m_pList=NULL;
}
{
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;
}
{
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;
}
void List::ClearList()
{
Node *currentNode=m_pList->next;
while(currentNode!=NULL)
{
Node *temp=currentNode->next;
delete currentNode;
currentNode=temp;
}
m_pList->next=NULL;
m_iLength=0;
}
bool List::ListInsert(int i,Node *pNode)
{
if(i<0||i>m_iLength)
{
return false;
}
Node *currentNode=m_pList;
for(int j=0;j<i;j++)
{
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;
}
{
if(i<0||i>m_iLength)
{
return false;
}
Node *currentNode=m_pList;
for(int j=0;j<i;j++)
{
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 j=0;j<=i;j++)
{
currentNodeBefore=currentNode;
currentNode=currentNode->next;
}
currentNodeBefore->next=currentNode->next;
pNode->data=currentNode->data;
delete currentNode;delete currentNodeBefore; currentNode=NULL;
m_iLength--;
// for(int j=1;j<i;j++)
// {
// currentNode=currentNode->next;
// }
// currentNode->next=currentNode->next->next;
// pNode->data=currentNode->next->data;
// delete currentNode;
// currentNode=NULL;
// m_iLength--;
return true;
{
if(i<0||i>=m_iLength)
{
return false;
}
Node *currentNode=m_pList;
Node *currentNodeBefore=NULL;
for(int j=0;j<=i;j++)
{
currentNodeBefore=currentNode;
currentNode=currentNode->next;
}
currentNodeBefore->next=currentNode->next;
pNode->data=currentNode->data;
delete currentNode;delete currentNodeBefore; currentNode=NULL;
m_iLength--;
// for(int j=1;j<i;j++)
// {
// currentNode=currentNode->next;
// }
// currentNode->next=currentNode->next->next;
// pNode->data=currentNode->next->data;
// delete currentNode;
// currentNode=NULL;
// m_iLength--;
return true;
}
int List::ListLocate(Node *pNode)
{
Node *currentNode=m_pList;
for(int i=0;i<m_iLength;i++)
{
if(currentNode->data==pNode->data)
{
return i;
}
currentNode=currentNode->next;
}
return -1;
}
{
Node *currentNode=m_pList;
for(int i=0;i<m_iLength;i++)
{
if(currentNode->data==pNode->data)
{
return i;
}
currentNode=currentNode->next;
}
return -1;
}
bool List::GetElem(int i,Node *pNode)
{
if(i<0||i>=m_iLength)
{
return false;
}
Node *currentNode=m_pList;
for(int j=0;j<=i;j++)
{
currentNode=currentNode->next;
}
pNode->data=currentNode->data;
return true;
}
{
if(i<0||i>=m_iLength)
{
return false;
}
Node *currentNode=m_pList;
for(int j=0;j<=i;j++)
{
currentNode=currentNode->next;
}
pNode->data=currentNode->data;
return true;
}
bool List::ListPrior(Node *current,Node *prior)
{
Node *currentNode=m_pList;
Node *currentNodeBefore=NULL;
for(int i=0;i<m_iLength;i++)
{
currentNodeBefore=currentNode;
currentNode=currentNode->next;
if(current->data==currentNode->data)
{
if(currentNodeBefore!=m_pList)
{
return false;
}
prior->data=currentNodeBefore->data;
return true;
}
}
return false;
}
{
Node *currentNode=m_pList;
Node *currentNodeBefore=NULL;
for(int i=0;i<m_iLength;i++)
{
currentNodeBefore=currentNode;
currentNode=currentNode->next;
if(current->data==currentNode->data)
{
if(currentNodeBefore!=m_pList)
{
return false;
}
prior->data=currentNodeBefore->data;
return true;
}
}
return false;
}
bool List::ListNext(Node *current,Node *next)
{
Node *currentNode=m_pList;
Node *currentNodeAfter=NULL;
for(int i=0;i<m_iLength;i++)
{
currentNodeAfter=currentNode->next;
if(currentNode->data==current->data&&i!=m_iLength-1)
{
next->data=currentNodeAfter->data;
return true;
}
currentNode=currentNode->next;
}
return false;
}
{
Node *currentNode=m_pList;
Node *currentNodeAfter=NULL;
for(int i=0;i<m_iLength;i++)
{
currentNodeAfter=currentNode->next;
if(currentNode->data==current->data&&i!=m_iLength-1)
{
next->data=currentNodeAfter->data;
return true;
}
currentNode=currentNode->next;
}
return false;
}
int List::ListLength()
{
return m_iLength;
}
{
return m_iLength;
}
bool List::ListEmpty()
{
if(m_iLength==0)
return true;
return false;
}
{
if(m_iLength==0)
return true;
return false;
}
void List::ListTraverse()
{
Node *currentNode=m_pList;
for(int i=0;i<m_iLength;i++)
{
currentNode=currentNode->next;
currentNode->PrintNode();
}
}
{
Node *currentNode=m_pList;
for(int i=0;i<m_iLength;i++)
{
currentNode=currentNode->next;
currentNode->PrintNode();
}
}
****************************************************
mian
#include<iostream>
#include<stdlib.h>
#include"List.h"
#include"Node.h"
using namespace std;
#include<stdlib.h>
#include"List.h"
#include"Node.h"
using namespace std;
int menu();
void CreatePerson(List *pList);
void DeletePerson(List *pList);
int main(void)
{
// List *pList=new List;
//
// Node node1;
// Node node2;
// Node node3;
// Node node4;
// node1.data.name="car";
// node1.data.phone="13640558897";
// node2.data.name="ctt";
// node2.data.phone="18580787315";
// node2.data=4;
// node3.data=5;
// node4.data=6;
//
// Node node5;
// node5.data=7;
//
// Node temp;
{
// List *pList=new List;
//
// Node node1;
// Node node2;
// Node node3;
// Node node4;
// node1.data.name="car";
// node1.data.phone="13640558897";
// node2.data.name="ctt";
// node2.data.phone="18580787315";
// node2.data=4;
// node3.data=5;
// node4.data=6;
//
// Node node5;
// node5.data=7;
//
// Node temp;
// pList->ListInsertTail(&node1);
// pList->ListInsertTail(&node2);
// pList->ListInsertTail(&node3);
// pList->ListInsertTail(&node4);
// pList->ListInsertTail(&node2);
// pList->ListInsertTail(&node3);
// pList->ListInsertTail(&node4);
// pList->ListInsert(1,&node5);
//
// pList->ListDelete(1,&temp);
// cout<<temp.data<<endl;
//
// pList->ListDelete(1,&temp);
// cout<<temp.data<<endl;
// pList->ListTraverse();
// pList->GetElem(0,&temp);
// cout<<temp.data<<endl;
// cout<<temp.data<<endl;
// pList->ListPrior(&node1,&temp);
//
// pList->ListNext(&node2,&temp);
//
// cout<<temp.data<<endl;
//
// if(!pList->ListEmpty())
// {
// cout<<"not empty"<<endl;
// }
//
// pList->ClearList();
//
// if(pList->ListEmpty())
// {
// cout<<"empty"<<endl;
// }
//
// pList->ListNext(&node2,&temp);
//
// cout<<temp.data<<endl;
//
// if(!pList->ListEmpty())
// {
// cout<<"not empty"<<endl;
// }
//
// pList->ClearList();
//
// if(pList->ListEmpty())
// {
// cout<<"empty"<<endl;
// }
// cout<<pList->ListLength()<<endl;
// delete pList;
// pList=NULL;
// delete pList;
// pList=NULL;
List *pList=new List;
Node temp;
Node temp;
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;
}
}
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;
return 0;
}
pList=NULL;
return 0;
}
int menu()
{
cout<<"**菜单功能**"<<endl;
cout<<"1.新建联系人"<<endl;
cout<<"2.删除联系人"<<endl;
cout<<"3.浏览通讯录"<<endl;
cout<<"4.退出通讯录"<<endl;
{
cout<<"**菜单功能**"<<endl;
cout<<"1.新建联系人"<<endl;
cout<<"2.删除联系人"<<endl;
cout<<"3.浏览通讯录"<<endl;
cout<<"4.退出通讯录"<<endl;
cout<<"请输入:"<<endl;
int order;
cin>>order;
return order;
}
int order;
cin>>order;
return order;
}
void CreatePerson(List *pList)
{
Node node;
Person person;
cout<<"请输入联系人姓名"<<endl;
cin>>person.name;
cout<<"请输入联系人电话"<<endl;
cin>>person.phone;
node.data=person;
pList->ListInsertTail(&node);
{
Node node;
Person person;
cout<<"请输入联系人姓名"<<endl;
cin>>person.name;
cout<<"请输入联系人电话"<<endl;
cin>>person.phone;
node.data=person;
pList->ListInsertTail(&node);
}
void DeletePerson(List *pList)
{
Node node;
Person person;
cout<<"请输入你想删除的联系人姓名"<<endl;
cin>>person.name;
node.data=person;
int i=pList->ListLocate(&node);
pList->ListDelete(i,&node);
}
{
Node node;
Person person;
cout<<"请输入你想删除的联系人姓名"<<endl;
cin>>person.name;
node.data=person;
int i=pList->ListLocate(&node);
pList->ListDelete(i,&node);
}