C++通讯录源代码

/*线性表项目:通讯录 
联系人信息:姓名,号码.功能菜单:添加联系人,删除联系人,浏览通讯录,查找联系人,退出通讯录1/*/
#include <iostream>
#include <cstring>
using namespace std;
class Person
{
	friend ostream &operator<<(ostream &out,Person &p)
	{
		out<<p.name<<" "<<p.number<<endl;
	}
	public:
		string name;
		string number;
		Person &operator=(Person &p)
		{
			this->name=p.name;
			this->number=p.number;
			return *this;
		}
	    bool operator==(Person &p)
	    {
	    	if(this->name==p.name&&this->number==p.number)
	    	{
	    		return true;
			}
			return false;
		}
		
}; 
class Node
{
	public:
		Person data;               //数据域,把他们放在public里面是为了方便后面的赋值 
		Node *next;             //指针域
		void printNode()
		{
			cout<<data<<endl;
		} 
};
class List                       //链表不需要容量,因为它的容量是动态的 
{
	public:
        List()                   //首先要定义一个头结点 
		{
		  m_list=new Node;       //该定义的结点不算在链表中 
		 // m_list->data=0;
		  m_list->next=NULL;
		  length=0;
		}
		
		void ClearList()//清空线性表 ,重点介绍
		{         
		    Node *currentNode=m_list->next;        
			while(currentNode!=NULL)
			{
				Node *temp=currentNode->next;
				delete currentNode;
				currentNode = temp;
			}
			m_list->next=NULL;
		}
		~List()
		{
			ClearList();
			delete m_list;
			m_list=NULL;
		}
		bool ListEmpty()
		{
			if(length==0)
			{
				return true;
			}
			else return false;
		}
		int ListLength()
		{
			return length;
		}
		bool GetElem(int i,Node *pNode)          //需要找到头结点 
		{                
			if(i<0||i>=length)
			{
				return false;
			}
			Node *currentNode=m_list;
//			Node *beforeNode=NULL;
			for(int k=0;k<=i;k++)
			{
//				beforeNode=currentNode;
				currentNode=currentNode->next;
			}
			pNode->data=currentNode->data;
			return true;
		}
		int LocateElem(string pname)             
		{
			Node *currentNode=m_list;
			int count=0;
			while(currentNode->next!=NULL)
			{
				currentNode=currentNode->next;
				if(currentNode->data.name==pname)
				{
					return count;
				}
				count++;
			}
			return -1;
		}
		bool PriorElem(Node *pCurrentNode,Node *pPreNode)
		{
			Node *currentNode=m_list;
			Node *beforeNode=new Node;
			while(currentNode->next!=NULL)
			{
				beforeNode=currentNode;
				currentNode=currentNode->next;
				
				if(currentNode->data==pCurrentNode->data)
				{
					if(beforeNode==m_list)
					{
						return false;
					}
					pPreNode->data=beforeNode->data;
					return true;
				}
			}
			return false;
		}
		bool NextElem(Node *pCurrentNode,Node *pNextNode)
		{
		    Node *currentNode=m_list;
//			Node *nextNode=new Node;
			while(currentNode->next!=NULL)
			{
//				beforeNode=currentNode;
				currentNode=currentNode->next;
				
				if(currentNode->data==pCurrentNode->data)
				{
					if(currentNode->next==NULL)
					{
						return false;
					}
					pNextNode->data=currentNode->next->data;
					return true;
				}
			}
			return false;
		}
		void ListTraverse()
		{
			Node *currentNode=m_list;
			while(currentNode->next!=NULL)
			{
				currentNode=currentNode->next;
				currentNode->printNode();
			}
		}
		bool ListInsert(int i,Node *pNode)              //从指定位置插入元素 
		{
			if(i<0||i>length)
			{
				return false;
			}
			Node *currentNode = m_list;
			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;
			length++;
			return true;
		}
		bool ListDelete(int i,Node *pNode)               //删除指定位置的元素 
		{
		    if(i<0||i>=length)
		    {
		    	return false;
			}
			Node *currentNode=m_list;
			Node *beforeNode=NULL;
			for(int k=0;k<=i;k++)
			{
				beforeNode=currentNode;
				currentNode=currentNode->next;
			}
			beforeNode->next=currentNode->next;
			pNode->data=currentNode->data;
			delete currentNode;
			currentNode=NULL;
			length--;
			return true;
		}
		bool ListInsertHead(Node *pNode)                 //插入到第一个位置 
		{
			Node *temp = m_list->next;
			Node *newNode=new Node;
			if(newNode==NULL)
			{
				return false;
			}
			newNode->data=pNode->data;
			m_list->next=newNode;
			newNode->next=temp;
			length++;
			return true;
		}
		bool ListInsertTail(Node *pNode)                 //插入到最后一个位置 
		{
			Node *currentNode = m_list;
			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;
			length++;                       //要确保插入成功才加 
		    return true;
		}
	private:
		Node *m_list;
		int length;
};
int Menu()
{
	cout<<"请选择功能:"<<endl;
	cout<<"1.添加联系人"<<endl;
	cout<<"2.删除联系人"<<endl;
	cout<<"3.浏览通讯录"<<endl;
	cout<<"4.查找联系人"<<endl; 
	cout<<"5.修改联系人"<<endl;
	cout<<"6.退出通讯录"<<endl;
	cout<<"请输入:*****************************"<<endl;
	int order=0;
	cin>>order;
	return order; 
}
void add(List *list)
{
	Node node;
	Person person;
	cout<<"请输入联系人姓名:";
	cin>>person.name;
	cout<<endl;
	cout<<"请输入联系人电话号码:";
	cin>>person.number;
	cout<<endl;
	node.data=person;
	list->ListInsertTail(&node);
}
void pfind(List *list,string pname)
{
	int m=list->LocateElem(pname);
	Node node;
	list->GetElem(m,&node);
	node.printNode();
}
void deleteperson(List *list,string pname)
{
	 int m=list->LocateElem(pname);
	 Node node;
	 list->ListDelete(m,&node);
}
void alter(List *list,string pname)
{
	int m=list->LocateElem(pname);
	Node node;
	Node temp;
	list->GetElem(m,&node);
	string pnumber;
	cout<<"请输入修改的号码:";
	cin>>pnumber;
	node.data.number=pnumber;
	list->ListDelete(m,&temp);
	list->ListInsert(m,&node);
}
int main()
{
    List *list=new List();
    int order=0;
    while(order!=6)
    {
    	order=Menu();
    	if(order==1)
    	{
    		cout<<"新建联系人:"<<endl; 
    		add(list);
		}
		else if(order==2)
		{
			cout<<"删除联系人:"<<endl;
			cout<<"请输入你想删除的联系人的名字:"<<endl;
			string pname;
			cin>>pname;
			deleteperson(list,pname);
		}
		else if(order==3)
		{
			cout<<"浏览通讯录:"<<endl;
			list->ListTraverse();
		}
		else if(order==4)
		{
			cout<<"查找联系人:"<<endl;
			cout<<"请输入想要查找的联系人的名字:"<<endl;
			string pname;
			cin>>pname;
			pfind(list,pname);
		}		
		else if(order==5)
		{
			cout<<"修改联系人:"<<endl;
			string pname;
			cout<<"请输入你想修改的联系人名:";
			cin>>pname;
			alter(list,pname);
		}
		else if(order==6)
		{
			cout<<"你已经成功退出"<<endl;
		}
		else
		{
			cout<<"输入错误,请重新输入:"<<endl; 
		}
	}
	delete list;
	list=NULL;
}
发布了22 篇原创文章 · 获赞 27 · 访问量 2856

猜你喜欢

转载自blog.csdn.net/weixin_44346470/article/details/98640585
今日推荐