初学编程C++之单链表作通讯录

代码示例:

#ifndef PERSON_H
#define PERSON_H
#include<string>
#include<ostream>
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);
};
#endif


#include"Person.h"

ostream &operator<<(ostream &out, Person &person)
{
	out<<person.name<<"————"<<person.phone<<endl;
	return out;
}

Person &Person::operator=(Person &person)
{
	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;
}


#ifndef NODE_H
#define NODE_H
#include"Person.h"

class Node
{
public:
	Person data;//数据域
	Node*next;//指针域
	void printNode();
};
#endif


#include"Node.h"
#include<iostream>
using namespace std;

void Node::printNode()
{
	cout<<data<<endl;
}


#ifndef LIST_H
#define LIST_H
#include"Node.h"
class List
{
public:
	List();//创建线性表
	~List();//销毁线性表
	void ClearList();//清空线性表
	bool ListEmpty();//判断线性表是否为空
    int ListLength();//获取线性表的长度
    bool GetElem(int i,Node*pNode);//获取指定元素
    int LocateElem(Node*pNode);//寻找第一个满足e的数据元素的位序
    bool PriorElem(Node*pCurrentNode,Node*pPreNode);//获取指定元素的前驱
    bool NextElem(Node*pCurrentElem,Node*pNextElem);//获取指定元素的后驱
	void ListTraverse();//遍历线性表
    bool ListInsert(int i,Node*pNode);//在第i个位置插入元素
    bool ListDelete(int i,Node*pNode);//在第i个位置删除元素
	bool ListInsertHead(Node*pNode);//从起点插入节点
	bool ListInsertTail(Node*pNode);//从末尾插入节点
    
private:
	Node *m_pList;
	int m_iLength;
};
#endif


#include"List.h"
#include<iostream>
using namespace std;

List::List ()
{
	m_pList=new Node;
	//m_pList->data=0;
	m_pList->next=NULL;
	m_iLength=0;
}
bool List::ListEmpty()
{
	return m_iLength==0? true:false;
}
int List::ListLength()
{
	return m_iLength;
}
void List::ClearList()
{
	Node*currentNode=m_pList->next;
	while(currentNode!=NULL)//逐一清除
	{
		Node*temp=currentNode->next;
		delete currentNode;
		currentNode=temp;
	}
	m_pList->next=NULL;
}
List::~List()
{
	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;
}
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;
}
bool List::ListInsert(int i,Node*pNode)
{
	if(i<0||i>m_iLength)
	{
		return false;
	}
	Node*currentNode=m_pList;
	for(int k=0;k<i;k++)
	{
		currentNode=currentNode->next;
	}
	Node*newNode=new Node;
	if(newNode==NULL)
	{
		return false;
	}
	newNode->data=pNode->data;
	newNode->next=currentNode->next;
	currentNode->next=newNode;
	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 k=0;k<=i;k++)
	{
		currentNodeBefore=currentNode;
		currentNode=currentNode->next;
	}
	currentNodeBefore->next=currentNode->next;
	pNode->data=currentNode->data;
	delete currentNode;
	currentNode=NULL;
	m_iLength--;
	return true;
}
bool List::GetElem(int i,Node*pNode)
{
	if(i<0||i>=m_iLength)
	{
		return false;
	}
	Node*currentNode=m_pList;
	Node*currentNodeBefore=NULL;
	for(int k=0;k<=i;k++)
	{
		currentNodeBefore=currentNode;
		currentNode=currentNode->next;
	}
	pNode->data=currentNode->data;
	return true;
}
int List::LocateElem(Node*pNode)
{
	Node*currentNode=m_pList;
	int count=0;
	while(currentNode->next!=NULL)
	{
		currentNode=currentNode->next;
		if(currentNode->data==pNode->data)
		{
			return count;
		}
		count++;
	}
	return -1;
}
bool List::PriorElem(Node*pCurrentNode,Node*pPreNode)
{
	Node*currentNode=m_pList;
	Node*tempNode=NULL;
	while(currentNode->next!=NULL)
	{
		tempNode=currentNode;
		currentNode=currentNode->next;
		if(currentNode->data==pCurrentNode->data)
		{
			if(tempNode==m_pList)
			{
				return false;
			}
			pPreNode->data=tempNode->data;
			return true;
		}
	
	}
	return false;

}
bool List::NextElem(Node*pCurrentNode,Node*pNextNode)
{
	Node*currentNode=m_pList;
	while(currentNode->next!=NULL)
	{
		currentNode=currentNode->next;
		if(currentNode->data==pCurrentNode->data)
		{
			if(currentNode->next==NULL)
			{
				return false;
			}
			pNextNode->data=currentNode->next->data;
			return true;
		}
	}
	return false;
}
void List::ListTraverse()
{
	Node*currentNode=m_pList;
	while(currentNode->next!=NULL)
	{
		currentNode=currentNode->next;
		currentNode->printNode();
	}
}



#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include"Node.h"
#include"List.h"
using namespace std;
/*
  线性表——通讯录
  功能菜单:
      1、新建联系人
	  2、删除联系人
	  3、浏览通讯录
	  4、退出通讯录
	请输入:

  BOOL InitList(List**list);//创建线性表
  void DestroyList(List*list);//销毁线性表
  void ClearList(List*list);//清空线性表
  BOOL ListEmpty(List*list);//判断线性表是否为空
  int ListLength(List*list);//获取线性表的长度
  BOOL GetElem(List*list,int i,Elem*e);//获取指定元素
  int LocateElem(List*list,Elem*e);//寻找第一个满足e的数据元素的位序
  BOOL PriorElem(List*list,Elem*currentElem,Elem*preElem);//获取指定元素的前驱
  BOOL NextElem(List*list,Elem*currentElem,Elem*NextElem);//获取指定元素的后驱
  BOOL ListInsert(List*list,int i,Elem*e);//在第i个位置插入元素
  BOOL LIstDelete(LIst*list,int i,Elem*e);//在第i个位置删除元素
  void ListTraverse(LIst*list);//遍历线性表
*/
int menu()
{
	//显示通讯录功能菜单
	cout<<" 功能菜单:"<<endl;
	cout<<"1、新建联系人"<<endl;
	cout<<"2、删除联系人"<<endl;
	cout<<"3、浏览通讯录"<<endl;
	cout<<"4、退出通讯录"<<endl;
	cout<<"请输入:"<<endl;
	int order=0;
	cin>>order;
	return order;
}
void createPerson(List*pList)
{
	Node node;
	Person person;
	cout<<"请输入姓名:" ;
	cin>>person.name;
	cout<<"请输入电话:" ;
	cin>>person.phone;
	node.data=person;
	pList->ListInsertTail(&node);
}
void deletePerson(List*pList)
{
	/*Node node;
	Person person;
	cout<<"请输入想要删除的姓名:" ;
	cin>>person.name;
	node.data=person;
	int temp=pList->LocateElem(&node);
	pList->ListDelete( temp,&node);*/
}

int main(void)
{


	int userOrder=0;
	List*pList=new List();

	while(userOrder!=4)
	{
		int 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;
	    }
	}
	delete pList;
	pList=NULL;
	
	/*Node node1;
	node1.data.name="tester1";
	node1.data.phone="123456";

	Node node2;
	node2.data.name="tester2";
	node2.data.phone="234567";
	List*pList=new List();

	pList->ListInsertHead(&node1);
	pList->ListInsertHead(&node2);
	pList->ListInsertTail(&node1);
	pList->ListInsertTail(&node2);

	pList->ListTraverse();

	delete pList;
	pList=NULL;*/
	
	system("pause");
	return 0;
}

打印结果:
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值