C ++ simple address book list

Contacts brief

Contacts this thing, I believe we will see in daily life. We can think about, since we want to achieve contacts, address book then we would find out what's inside. And there are a contact name, contact number (these are the attributes), there are some simple functions, such as add contacts, delete contacts, find contacts, when you find the number wrong, you need to modify the contact number of (we generally find a contact by name to find), which are the four basic functions to achieve the above three operations, we can also browse contacts and see what contacts there, is the last exit the address book.
Speaking of contacts, and there are attributes, functional, we need to create a Person class, specifically to write these things. For those functions, the add, delete, modify, find what, you need to use the list to achieve, so it, before that we need to write a class list for our use.
Chain into a single chain, double chain, a circular linked list. For the contacts we use a single linked list it. I briefly explain the chain bar. Chain is composed of nodes and pointers, nodes, and pointer data field is divided into fields, the data field is used to store data, and a pointer field is used to point to other nodes, a single linked list node pointer field only a double linked list there are two, they last a pointer field pointing to an empty node, but a circular linked list node pointer field pointing to the last head node.

Analysis and list implementation

First we need to define a node class, and class nodes Well there are data fields and pointer field in the address book inside our data node is the human contact.

class Node
{
	public:
		Person data;             //数据域,把他们放在public里面是为了方便后面的赋值 
		Node *next;             //指针域
		void printNode()
		{
			cout<<data<<endl;
		} 
};

And then for the list, we need two properties, one is node type, is a linked list length.

private:
		Node *m_list;
		int length;

The list also requires some performance function, is certainly the most basic constructor and destructor, the contacts inside it, we need to meet to add, delete, search by name, so we can define these functions:
constructors and destructors :

List()                           //构造函数
		{
		  m_list=new Node;       //构造函数中定义头结点 ,头结点的数据域无意义,不算在length里面
		 // 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;
		}

Find a place among the nodes in a linked list by name:

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

Gets the list among the nodes of a location information:

bool GetElem(int i,Node *pNode)          //需要找到头结点 
		{                
			if(i<0||i>=length)
			{
				return false;
			}
			Node *currentNode=m_list;
			for(int k=0;k<=i;k++)
			{
				currentNode=currentNode->next;
			}
			pNode->data=currentNode->data;                 //把相应位置结点的数据赋值给定义的pNode
			return true;
		}

Add and delete nodes list:

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

Since we need to browse contacts, then we need to list the data output of each node on the line
list traversal output:

void ListTraverse()
		{
			Node *currentNode=m_list;
			while(currentNode->next!=NULL)
			{
				currentNode=currentNode->next;
				currentNode->printNode();
			}
		}

Note that, among the data nodes are we human contact, the contact among human beings, assignment, output, input, need to be determined equal operator overloading.

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

In order to address book look look better, we can achieve with the menu, you can define a menu function:

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

Here Insert Picture Description
For add a contact, we will be able to enter new contact information is assigned to the new node, and then use the list to add function to add:

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

For delete contacts, we can find the location of the node in the list which by name, then use the list to delete function on it:

void deleteperson(List *list,string pname)
{
	 int m=list->LocateElem(pname);
	 Node node;
	 list->ListDelete(m,&node);
}

To find a contact, you can also find the location of nodes by name, output on the line.

void pfind(List *list,string pname)
{
	int m=list->LocateElem(pname);
	Node node;
	list->GetElem(m,&node);
	node.printNode();
}

Here Insert Picture Description
Through the contacts on the use of traversal function on it:

list->ListTraverse();

Modify Contact: first find the location of the name of the corresponding node, and then modify the information in the definition of a new node, delete the original node, you can insert a new 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);
}

If readers are interested to see the source code, then you can click on the following link
https://blog.csdn.net/weixin_44346470/article/details/98640585

Published 22 original articles · won praise 27 · views 2855

Guess you like

Origin blog.csdn.net/weixin_44346470/article/details/98632997