- 通讯录的元素是人,所以需要新建一个Person类作为链表每个Node的元素。并且为了正常输出,Person还需要虫重载一些运算符,包括输出<<, 判等==
- main函数里也要写一些辅助函数来获取信息。
- 链表本身实现部分不需要修改太多,把原来的的int型元素改为Person类型即可
- 代码如下
#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:
};
#include"Person.h"
Person::Person(string na, int num):name(na),phone(num)
{}
Person::Person()
{}
Person::~Person()
{}
#pragma once
using namespace std;
#include<iostream>
#include"Person.h"
class Node
{
public:
Node(Person a);
Node();
~Node();
Person person;
Node* next;
private:
};
#include"Node.h"
Node::Node(Person a):person(a)
{}
Node::Node()
{}
Node::~Node()
{}
#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;
};
#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++)
{
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;
}
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;
}
#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;
}
Higher you climb, more view you will see.