zoukankan      html  css  js  c++  java
  • 数据结构-链表(二)-通讯录-C++实现

    • 通讯录的元素是人,所以需要新建一个Person类作为链表每个Node的元素。并且为了正常输出,Person还需要虫重载一些运算符,包括输出<<, 判等==
    • main函数里也要写一些辅助函数来获取信息。
    • 链表本身实现部分不需要修改太多,把原来的的int型元素改为Person类型即可
    • 代码如下
    //Person.h
    #pragma once
    #include<iostream>
    #include<string>
    using namespace std;
    class Person
    {
    	friend ostream& operator<<(ostream& out,Person &p)
    	{
    		out << "Name: "<<p.name<<"---" <<"Pnone number: "<< p.phone << endl;
    		return out;
    	}
    public:
    	Person(string na, int num);
    	Person();
    	~Person();
    	bool operator ==(Person &p)
    	{
    		if ((this->name == p.name) && (this->phone == p.phone))
    		{
    			return true;
    		}
    		else
    		{
    			return false;
    		}
    	}
    	string name="张三";
    	int phone=0;
    private:
    
    };
    //Person.cpp
    #include"Person.h"
    Person::Person(string na, int num):name(na),phone(num)
    {}
    Person::Person()
    {}
    
    Person::~Person()
    {}
    
    //Node.h
    #pragma once
    using namespace std;
    #include<iostream>
    #include"Person.h"
    class Node
    {
    public:
    	Node(Person a);
    	Node();
    	~Node();
    	Person person;
    	Node* next;
    private:
    
    };
    
    //Node.cpp
    #include"Node.h"
    Node::Node(Person a):person(a)
    {}
    Node::Node()
    {}
    Node::~Node()
    {}
    
    //List.h
    #pragma once
    #include<iostream>
    using namespace std;
    #include"Node.h"
    #define __debug__
    #ifdef __debug__
    #endif
    class List
    {
    public:
    	List();//
    	~List();//
    	bool get_add_head(Node* e);//在头节点后面插入
    	bool get_add_tail(Node* e);//在尾节点后面插入
    	void get_traverse();//遍历
    	int get_length();//获取长度
    	bool get_empty();//判空
    	void get_clear();//清空线性表
    	int get_ele_num(Node* p);//获取与传入参数值相同的链表中元素的序号
    	bool get_i_ele(int e, Node* value);//获指定位序的元素
    	void get_delete(int location);//删除某位置元素	
    	bool get_add(int location, Node* e);//添加在某位置元素
    	bool get_pre(Node* e, Node* pre);//获取前驱
    	bool get_post(Node* e, Node* pos);//获取后继
    private:
    	Node* node;//头节点
    	int length;
    };
    //List.cpp
    #include"List.h"
    List::List()
    {
    	length = 0;
    	node = new Node;
    	node->person.name="zhangsan";
    	node->person.phone = 0;
    	node->next = NULL;
    }
    List::~List()
    {
    	get_clear();
    	delete node;
    	node = NULL;
    }
    bool List::get_add_head(Node* e)
    {
    	Node* c_node = new Node;
    	c_node=node->next;
    	Node* new_node = new Node;
    	if (new_node == NULL)
    	{
    		return false;
    	}
    	new_node->person = e->person;
    	new_node->next = c_node;
    	node->next = new_node;
    	length++;
    #ifdef __debug__
    	cout << node->next->person << endl;
    #endif
    	return true;
    }
    bool List::get_add_tail(Node* e)
    {
    	Node* c_node = new Node;
    	c_node = node;
    	while (c_node->next!=NULL)
    	{
    		c_node = c_node->next;
    	}
    	Node* new_node = new Node;
    	if (new_node == NULL)
    	{
    		return false;
    	}
    	new_node->person = e->person;
    	new_node->next = NULL;
    	c_node->next = new_node;
    	length++;
    #ifdef __debug__
    	cout << c_node->next->person << endl;
    #endif
    	return true;
    }
    void List::get_traverse()
    {
    	Node* c_node = new Node;
    	c_node = node->next;
    	while (c_node != NULL)
    	{
    		cout << c_node->person << endl;
    		c_node = c_node->next;
    	}
    }//遍历
    int List::get_length()
    {
    	return length;
    }//获取长度
    bool List::get_empty()
    {
    	return length == 0 ? true : false;
    }
    void List::get_clear()
    {
    	Node* c_node = new Node;
    	c_node = node;
    	while(c_node->next != NULL)
    	{
    		Node* new_node = new Node;
    		new_node=c_node->next;
    		delete c_node;//清除指向的内容
    		c_node = new_node;
    	}	
    	length = 0;
    	node->next = NULL;//易错,勿忘!
    }//清空线性表
    int List::get_ele_num(Node* p)//获取元素的位序
    {
    	Node* c_node = new Node;
    	c_node=node;
    	int count = 1;
    	while (c_node->next != NULL)
    	{
    		c_node = c_node->next;
    		if (c_node->person == p->person)
    		{
    			return count;
    		}
    		else
    		{	
    			count++;
    		}
    	}
    	return -1;
    }
    
    bool List::get_i_ele(int e, Node* value)
    {
    	if (e <1 || e > length)
    	{
    		cout << "Invalid num!" << endl;
    		return false;
    	}
    	Node* c_node = new Node;
    	c_node = node;
    	for (int i = 0; i <e; i++)
    	{
    		c_node = c_node->next;
    	}
    	value->person = c_node->person;
    #ifdef __debug__
    	cout << c_node->person << endl;
    #endif
    	return true;
    }//获指定位序元素的值
    //
    void List::get_delete(int location)
    {
    	if (location<1 || location>length )
    	{
    		cout << "invalid num!!" << endl;
    	}
    	else
    	{
    		Node* c_node = new Node;
    		c_node = node;
    		Node* c_node_before = new Node;
    		for (int i = 0; i < location; i++)//遍历到了location对应的位置,与增加元素有所不同,需要注意!
    		{
    			c_node_before = c_node;
    			c_node = c_node->next;
    		}
    		c_node_before->next = c_node->next;
    		length--;
    	}	
    }//某位置删除元素
    //
    bool List::get_add(int location, Node* e)
    {
    	if (location<1 || location>length)
    	{
    		cout << "Invalid num!" << endl;
    		return false;
    	}
    	else
    	{
    		Node* c_node = new Node;
    		c_node=node;
    		for (int i = 0; i < (location-1); i++)
    		{
    			c_node = c_node->next;
    		}//此时是遍历到了(location-1)那个元素的位置,然后在其后面插入新元素则是第location个元素。
    		Node* new_node = new Node;
    		if (new_node == NULL)
    		{
    			return false;
    		}
    		new_node->person= e->person;
    		new_node->next = c_node->next;
    		c_node->next = new_node;
    		length++;
    		return true;
    	}
    }//在某位置插入元素
    
    bool  List::get_pre(Node* e, Node* pre)
    {
    	Node* c_node = new Node;
    	c_node=node;
    	Node* pre_node = new Node;
    	while (c_node->next != NULL)
    	{
    		pre_node = c_node;
    		c_node = c_node->next;
    		if (c_node->person== e->person)
    		{	
    			if (pre_node==node)
    			{
    				cout << "No pre!" << endl;
    				return false;
    			}
    			else
    			{
    				pre->person = pre_node->person;
    #ifdef __debug__
    				cout << pre->person << endl;
    
    #endif
    				return true;
    			}	
    		}
    	}
    	cout << "No this value!" << endl;
    	return false;
    }//获取前驱
    bool  List::get_post(Node* e, Node* pos)
    {
    	Node* c_node = new Node;
    	c_node=node;
    	Node* pos_node = new Node;
    	while (c_node->next != NULL)
    	{
    		c_node = c_node->next;
    		if (c_node->person == e->person)
    		{
    			if (c_node->next== NULL)
    			{
    				
    				return false;
    			}
    			else
    			{
    				pos->person = c_node->next->person;
    #ifdef __debug__
    				cout << pos->person << endl;
    
    #endif
    				return true;
    			}
    		}
    	}
    	return false;
    }//获取后继
    
    //main.cpp
    #include"List.h"
    int menu()
    {
    	cout << "----请输入你的选择----" << endl;
    	cout << "1.新建联系人" << endl;
    	cout << "2.删除联系人" << endl;
    	cout << "3.输出通讯录" << endl;
    	cout << "4.退出通讯录" << endl;
    	int order = 0;
    	cin >> order;
    	return order;
    }
    void creat_person(List* p)
    {
    	Node m;
    	cout << "姓名" << endl;
    	cin>>m.person.name;
    	cout << "电话" << endl;
    	cin >> m.person.phone;
    	p->get_add_tail(&m);
    }
    int main(int *argc, char* argv[])
    {
    	List* p_list = new List;
    	int order =0;
    	while (order != 4)
    	{
    		order = menu();
    		switch (order)
    		{
    		case 1:
    		{
    			cout << "你选择新建联系人" << endl;
    			creat_person(p_list);
    			break;
    		}
    		case 2:
    		{
    			cout << "你选择删除联系人" << endl;
    			cout << "你要删除第几个?" << endl;
    			int mm;
    			cin >> mm;
    			p_list->get_delete(mm);
    			break;
    		}
    		case 3:
    		{
    			cout << "你选择输出联系人" << endl;
    			p_list->get_traverse();
    			break;
    		}
    		case 4:
    		{
    			cout << "你选择退出" << endl;
    			break;
    		}
    		default:
    		{
    			cout << "What do you mean?" << endl;
    			break;
    		}		
    		}	
    	}
    	cout << "已退出" << endl;
    	delete p_list;
    	p_list = NULL;
    	return 0;
    }
    
    /*运行结果
    ----请输入你的选择----
    1.新建联系人
    2.删除联系人
    3.输出通讯录
    4.退出通讯录
    1
    你选择新建联系人
    姓名
    张三
    电话
    1235
    Name: 张三---Pnone number: 1235
    
    ----请输入你的选择----
    1.新建联系人
    2.删除联系人
    3.输出通讯录
    4.退出通讯录
    3
    你选择输出联系人
    Name: 张三---Pnone number: 1235
    
    ----请输入你的选择----
    1.新建联系人
    2.删除联系人
    3.输出通讯录
    4.退出通讯录
    2
    你选择删除联系人
    你要删除第几个?
    1
    ----请输入你的选择----
    1.新建联系人
    2.删除联系人
    3.输出通讯录
    4.退出通讯录
    4
    你选择退出
    已退出
    
    
    
    
    */
    
    Higher you climb, more view you will see.
  • 相关阅读:
    利用正则表达式,完成参数的替换
    使用python读取yaml文件
    python+unittet在linux与windows使用的区别
    python发送requests请求时,使用登录的token值,作为下一个接口的请求头信息
    jmeter线程组之间传参
    requests:json请求中中文乱码处理
    ddt源码修改:HtmlTestRunner报告依据接口名显示用例名字
    使用openpyxl的styles,实现写入值时加背景色
    批量数据请求接口
    locust参数化
  • 原文地址:https://www.cnblogs.com/yyfighting/p/12500613.html
Copyright © 2011-2022 走看看