数据结构探险——线性表篇(代码实现)

imooc相关学习视频

1、线性表

1.1、什么是线性表?

线性表是n个数据元素的有限序列。
在这里插入图片描述

1.2、应用场景

a. 通讯录
b. 一元多项式
在这里插入图片描述

1.3、代码实现

线性表—顺序表

3 5 7 2 9 1 8
前驱:某个位置元素的前一个位置的节点,比如5的前驱为3
后继:某个位置元素的后一个位置的节点,比如5的后继为7

以下为C语音编写的有关顺序表的相关接口函数

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个位置插入元素,i位置后面的元素都要后移一位
BOOL ListDelete(List *list, int i, Elem *e);   //删除第i个位置上的元素,i后面的元素都要往前移一个位置
void ListTraverse(List *list);                 //遍历线性表

第一个测试代码
List.h–>定义顺序表相关功能接口和元素的头文件(存入的数据都是int类型)

#ifndef LIST_H_
#define LIST_H_

class List
{
public:
	List(int size);
	~List();
	void ClearList();
	bool ListEmpty();
	bool ListLength();
	bool GetElem(int i, int *e);
	int LocateElem(int *e);
	bool PriorElem(int *currentElem, int *preElem);
	bool NextElem(int *currentElem, int *nextElem);
	void ListTraverse();
	bool ListInsert(int i, int *e);
	bool ListDelete(int i, int *e);

private:
	int *m_pList;  //指向线性表存储的内存地址
	int m_iSize;   //表示线性表存储的大小
	int m_iLength; //表示线性表存储了多少元素
};

#endif /* LIST_H_ */

List.cpp–>实现顺序表相关功能代码

#include <iostream>
#include "List.h"

using namespace std;

List::List(int size)
{
	m_iSize= size;
	m_pList = new int[m_iSize];
	m_iLength = 0;
}

List::~List()
{
	if (!m_pList) {
		delete[] m_pList;
		m_pList = NULL;
	}
}

void List::ClearList()
{
	m_iLength = 0;
}

bool List::ListEmpty()
{
	return m_iLength == 0 ? true : false;
}

bool List::ListLength()
{
	return m_iLength;
}

bool List::GetElem(int i, int *e)
{
	//if (i < 0 || i >= m_iSize) {
	if (i < 0 || i >= m_iLength) {
		return false;
	}
	*e = m_pList[i];
	return true;
}

int List::LocateElem(int *e)
{
	for (int i = 0; i < m_iLength; i++) {
		if (m_pList[i] == *e) {
			return i;
		}
	}
	return -1;

}

bool List::PriorElem(int *currentElem, int *preElem)
{
	int temp = LocateElem(currentElem);
	if (-1 == temp) {  //表示没找到该元素
		return false;
	} else {
		if (0 == temp) {  //表示当前元素为第一个元素
			return false;
		} else {
			*preElem = m_pList[temp - 1];
			return true;
		}
	}
}

bool List::NextElem(int *currentElem, int *nextElem)
{
	int temp = LocateElem(currentElem);
	if (-1 == temp) {  //表示没找到该元素
		return false;
	} else {
		if (m_iLength == temp) {  //表示当前元素为最后一个元素
			return false;
		} else {
			*nextElem = m_pList[temp + 1];
			return true;
		}
	}
}

void List::ListTraverse()
{
	for(int i = 0; i < m_iLength; i++) {
		cout << m_pList[i] << endl;
	}
}

bool List::ListInsert(int i, int *e)
{
	if (i < 0 || i > m_iLength) {
		return false;
	}
	for (int k = m_iLength - 1; k >= i; k--) {
		m_pList[k+1] = m_pList[k];
	}
	m_pList[i] = *e;
	m_iLength++;
	return true;
}

bool List::ListDelete(int i, int *e)
{
	if (i < 0 || i >= m_iLength) {
		return false;
	}
	*e = m_pList[i];
	for (int k = i + 1; k <m_iLength; k++) {
		m_pList[k-1] = m_pList[k];
	}
	m_iLength--;
	return true;
}

ListDemo.cpp–>测试入口代码文件

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

int main() {
	//3 5 7 2 9 1 8
	List *list1 = new List(10);
	int e1 = 3;
	int e2 = 5;
	int e3 = 7;
	int e4 = 2;
	int e5 = 9;
	int e6 = 1;
	int e7 = 8;
	list1->ListInsert(0, &e1);
	list1->ListInsert(1, &e2);
	list1->ListInsert(2, &e3);
	list1->ListInsert(3, &e4);
	list1->ListInsert(4, &e5);
	list1->ListInsert(5, &e6);
	list1->ListInsert(3, &e7);  // 将e7插入第3个位置

	int temp = 0;
	list1->ListDelete(0, &temp);  //删除第0个位置的元素
	cout << "temp: " << temp << endl;


	list1->ListTraverse();



	delete list1;
	list1 = NULL;
	return 0;
}

测试结果为:

temp: 3
5
7
8
2
9
1

第二个测试代码
如果传入的数据不是int类型,而是一个Coordinate对象的代码如下:
Coordinate.h–>对象相关的头文件信息

#ifndef COORDINATE_H_
#define COORDINATE_H_

#include <ostream>
using namespace std;

class Coordinate
{
	friend ostream &operator<<(ostream &out, Coordinate &coor);
public:
	Coordinate(int x=0, int y=0);
	void printCoordinate();
	bool operator==(Coordinate &coor);

private:
	int m_iX;
	int m_iY;
};

#endif /* COORDINATE_H_ */

Coordinate.cpp–>对象相关的实现类

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

Coordinate::Coordinate(int x, int y)
{
	m_iX = x;
	m_iY = y;
}

void Coordinate::printCoordinate()
{
	cout << "(" << m_iX << ", " << m_iY << ")" << endl;
}

ostream &operator<<(ostream &out, Coordinate &coor)
{
	out << "(" << coor.m_iX << ", " << coor.m_iY << ")" << endl;
	return out;
}

bool Coordinate::operator==(Coordinate &coor)
{
	if (this->m_iX == coor.m_iX && this->m_iY == coor.m_iY) {
		return true;
	}
	return false;
}

List.h–>修改后的定义顺序表相关功能接口和元素的头文件(存入的数据都是Coordinate类型)

#ifndef LIST_H_
#define LIST_H_

#include "Coordinate.h"
class List
{
public:
	List(int size);
	~List();
	void ClearList();
	bool ListEmpty();
	bool ListLength();
	bool GetElem(int i, Coordinate *e);
	int LocateElem(Coordinate *e);
	bool PriorElem(Coordinate *currentElem, Coordinate *preElem);
	bool NextElem(Coordinate *currentElem, Coordinate *nextElem);
	void ListTraverse();
	bool ListInsert(int i, Coordinate *e);
	bool ListDelete(int i, Coordinate *e);

private:
	Coordinate *m_pList;  //指向线性表存储的内存地址
	int m_iSize;   //表示线性表存储的大小
	int m_iLength; //表示线性表存储了多少元素
};

#endif /* LIST_H_ */

List.cpp–>修改后的实现顺序表相关功能代码

#include <iostream>
#include "List.h"

using namespace std;

List::List(int size)
{
	m_iSize= size;
	m_pList = new Coordinate[m_iSize];
	m_iLength = 0;
}

List::~List()
{
	if (!m_pList) {
		delete[] m_pList;
		m_pList = NULL;
	}
}

void List::ClearList()
{
	m_iLength = 0;
}

bool List::ListEmpty()
{
	return m_iLength == 0 ? true : false;
}

bool List::ListLength()
{
	return m_iLength;
}

bool List::GetElem(int i, Coordinate *e)
{
	//if (i < 0 || i >= m_iSize) {
	if (i < 0 || i >= m_iLength) {
		return false;
	}
	*e = m_pList[i];
	return true;
}

int List::LocateElem(Coordinate *e)
{
	for (int i = 0; i < m_iLength; i++) {
		if (m_pList[i] == *e) {
			return i;
		}
	}
	return -1;

}

bool List::PriorElem(Coordinate *currentElem, Coordinate *preElem)
{
	int temp = LocateElem(currentElem);
	if (-1 == temp) {  //表示没找到该元素
		return false;
	} else {
		if (0 == temp) {  //表示当前元素为第一个元素
			return false;
		} else {
			*preElem = m_pList[temp - 1];
			return true;
		}
	}
}

bool List::NextElem(Coordinate *currentElem, Coordinate *nextElem)
{
	int temp = LocateElem(currentElem);
	if (-1 == temp) {  //表示没找到该元素
		return false;
	} else {
		if (m_iLength == temp) {  //表示当前元素为最后一个元素
			return false;
		} else {
			*nextElem = m_pList[temp + 1];
			return true;
		}
	}
}

void List::ListTraverse()
{
	for(int i = 0; i < m_iLength; i++) {
		cout << m_pList[i] << endl;
	}
}

bool List::ListInsert(int i, Coordinate *e)
{
	if (i < 0 || i > m_iLength) {
		return false;
	}
	for (int k = m_iLength - 1; k >= i; k--) {
		m_pList[k+1] = m_pList[k];
	}
	m_pList[i] = *e;
	m_iLength++;
	return true;
}

bool List::ListDelete(int i, Coordinate *e)
{
	if (i < 0 || i >= m_iLength) {
		return false;
	}
	*e = m_pList[i];
	for (int k = i + 1; k <m_iLength; k++) {
		m_pList[k-1] = m_pList[k];
	}
	m_iLength--;
	return true;
}


ListDemo.cpp–>修改后的测试入口代码文件

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

int main() {
	//3 5 7 2 9 1 8
	Coordinate c1(3, 5);
	Coordinate c2(5, 7);
	Coordinate c3(7, 9);

	List *list1 = new List(10);
	list1->ListInsert(0, &c1);
	list1->ListInsert(1, &c2);
	list1->ListInsert(2, &c3);


	list1->ListTraverse();



	delete list1;
	list1 = NULL;
	return 0;
}


测试结果:

(3, 5)

(5, 7)

(7, 9)

2、链表

2.1 什么是链表

链表分为静态链表、单链表、循环链表和双链表。
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

线性表—单链表

第一个测试代码
Node.h–>定义单链表中的节点元素相关功能接口和元素的头文件

#ifndef NODE_H_
#define NODE_H_

class Node
{
public:
	int data;
	Node *next;
	void printNode();
};

#endif /* NODE_H_ */

Node.cpp–>实现单链表中节点相关功能代码

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

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

List.h–>定义单链表相关功能接口和元素的头文件(存入的数据都是Node类型)

#ifndef LIST_H_
#define LIST_H_

#include "Node.h"

class List
{
public:
	List();
	~List();
	void ClearList();
	bool ListEmpty();
	bool ListLength();
	bool GetElem(int i, Node *pNode);
	int LocateElem(Node *pNode);
	bool PriorElem(Node *pCurrentNode, int *pPreNode);
	bool NextElem(Node *pCurrentNode, int *pNexNode);
	void ListTraverse();
	bool ListInsert(int i, Node *pNode);
	bool ListDelete(int i, Node *pNode);
	bool ListInsertHead(Node *pNode);
	bool ListInsertTail(Node *pNode);

private:
	Node *m_pList;  //链表指针
	int m_iLength; //表示线性表存储了多少元素
};




#endif /* LIST_H_ */

List.cpp–>实现单链表相关功能代码

#include <iostream>
#include "List.h"

using namespace std;

List::List()
{
	m_pList = new Node;
	m_pList->data = 0;
	m_pList->next = NULL;
	m_iLength = 0;
}

List::~List()
{
	ClearList();
	delete m_pList;
	m_pList = NULL;
}

void List::ClearList()
{
	Node *currentNode = m_pList->next;
	while (currentNode != NULL) {
		Node *temp = currentNode->next;
		delete currentNode;
		currentNode = temp;
	}
	m_pList->next = NULL;
}

bool List::ListEmpty()
{
	return m_iLength == 0 ? true : false;
}

bool List::ListLength()
{
	return m_iLength;
}

bool List::ListInsertHead(Node *pNode)
{
	Node *temp = m_pList->next;
	Node *newNode = new Node;
	if (NULL == newNode) {
		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 (NULL == newNode) {
		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 (NULL == newNode) {
		return false;
	}
	newNode->data = pNode->data;
	newNode->next = currentNode->next;
	currentNode->next = newNode;
	m_iLength++;
	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;
	for (int k = 0; k <= i; k++) {
		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 *pNexNode)
{
	Node *currentNode = m_pList;
	while (currentNode->next != NULL) {
		currentNode = currentNode->next;
		if (currentNode->data == pCurrentNode->data) {
			if (currentNode->next == NULL) {
				return false;
			}
			pNexNode->data = currentNode->next->data;
			return true;
		}
	}
	return false;
}
void List::ListTraverse()
{
	Node *currentNode = m_pList;
	while(currentNode->next != NULL) {
		currentNode =  currentNode->next;
		currentNode->printNode();
	}
}

ListDemo.cpp–>修改后的测试入口代码文件

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

int main() {
	Node node1;
	node1.data = 3;
	Node node2;
	node2.data = 4;
	Node node3;
	node3.data = 5;
	Node node4;
	node4.data = 6;

	List *list1 = new List();
	/*
	// 测试1
	list1->ListInsert(0, &node1);
	list1->ListInsert(1, &node2);
	list1->ListInsert(2, &node3);
	list1->ListInsert(3, &node4);
	*/

	/*
	// 测试2
	list1->ListInsertHead(&node1);
	list1->ListInsertHead(&node2);
	list1->ListInsertHead(&node3);
	list1->ListInsertHead(&node4);
	*/

	/*
	// 测试3
	list1->ListInsertTail(&node1);
	list1->ListInsertTail(&node2);
	list1->ListInsertTail(&node3);
	list1->ListInsertTail(&node4);
	*/

	// 测试4
	cout << list1->ListEmpty() << endl;
	cout << list1->ListLength() << endl;

	list1->ListInsertTail(&node1);
	list1->ListInsertTail(&node2);
	list1->ListInsertTail(&node3);
	list1->ListInsertTail(&node4);

	Node temp;
	list1->GetElem(1, &temp);
	cout <<"temp.data:" << temp.data << endl;

	cout << "index:" << list1->LocateElem(&temp) << endl;

	Node pPreNode;
	list1->PriorElem(&temp, &pPreNode);
	cout  << "pPreNode.data" << pPreNode.data << endl;

	Node nextNode;
	list1->NextElem(&temp, &nextNode);
	cout  << "nextNode.data:" << nextNode.data << endl;
	list1->ListTraverse();

	delete list1;
	list1 = NULL;
	return 0;
}


测试结果1:

3
4
5
6

测试结果2:

6
5
4
3

测试结果3:

3
4
5
6

测试结果4:

1
0
temp.data:4
index:1
pPreNode.data3
nextNode.data:5
3
4
5
6

3、通讯录

3.1线性表–通讯录

联系人信息:姓名 电话

第一个代码实现:
Person.h–>定义单链表中的节点存储的信息为通讯录信息(包含姓名和电话)相关功能接口和元素的头文件

#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 /* PERSON_H_ */

Person.cpp–>实现单链表中节点通信录信息相关功能代码

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

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

Node.h–>定义单链表中的节点元素相关功能接口和元素的头文件

#ifndef NODE_H_
#define NODE_H_

#include "Person.h"
class Node
{
public:
	Person data;
	Node *next;
	void printNode();
};

#endif /* NODE_H_ */

Node.cpp–>实现单链表中节点相关功能代码

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

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

List.h–>定义单链表相关功能接口和元素的头文件(存入的数据都是Node类型)

#ifndef LIST_H_
#define LIST_H_

#include "Node.h"

class List
{
public:
	List();
	~List();
	void ClearList();
	bool ListEmpty();
	bool ListLength();
	bool GetElem(int i, Node *pNode);
	int LocateElem(Node *pNode);
	bool PriorElem(Node *pCurrentNode, Node *pPreNode);
	bool NextElem(Node *pCurrentNode, Node *pNexNode);
	void ListTraverse();
	bool ListInsert(int i, Node *pNode);
	bool ListDelete(int i, Node *pNode);
	bool ListInsertHead(Node *pNode);
	bool ListInsertTail(Node *pNode);

private:
	Node *m_pList;  //链表指针
	int m_iLength; //表示线性表存储了多少元素
};


#endif /* LIST_H_ */

List.cpp–>实现单链表相关功能代码

#include <iostream>
#include "List.h"

using namespace std;

List::List()
{
	m_pList = new Node;
	// m_pList->data = 0;
	m_pList->next = NULL;
	m_iLength = 0;
}

List::~List()
{
	ClearList();
	delete m_pList;
	m_pList = NULL;
}

void List::ClearList()
{
	Node *currentNode = m_pList->next;
	while (currentNode != NULL) {
		Node *temp = currentNode->next;
		delete currentNode;
		currentNode = temp;
	}
	m_pList->next = NULL;
}

bool List::ListEmpty()
{
	return m_iLength == 0 ? true : false;
}

bool List::ListLength()
{
	return m_iLength;
}

bool List::ListInsertHead(Node *pNode)
{
	Node *temp = m_pList->next;
	Node *newNode = new Node;
	if (NULL == newNode) {
		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 (NULL == newNode) {
		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 (NULL == newNode) {
		return false;
	}
	newNode->data = pNode->data;
	newNode->next = currentNode->next;
	currentNode->next = newNode;
	m_iLength++;
	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;
	for (int k = 0; k <= i; k++) {
		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 *pNexNode)
{
	Node *currentNode = m_pList;
	while (currentNode->next != NULL) {
		currentNode = currentNode->next;
		if (currentNode->data == pCurrentNode->data) {
			if (currentNode->next == NULL) {
				return false;
			}
			pNexNode->data = currentNode->next->data;
			return true;
		}
	}
	return false;
}
void List::ListTraverse()
{
	Node *currentNode = m_pList;
	while(currentNode->next != NULL) {
		currentNode =  currentNode->next;
		currentNode->printNode();
	}
}

ListDemo.cpp–>修改后的测试入口代码文件

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

int main() {
	Node node1;
	node1.data.name = "tester1";
	node1.data.phone = "123456";
	Node node2;
	node2.data.name = "tester2";
	node2.data.phone = "234567";

	List *list = new List();
	//测试1
	cout << "从链表头开始插入" << endl;
	list->ListInsertHead(&node1);
	list->ListInsertHead(&node2);
	list->ListTraverse();

	//测试2
	cout << "从链表尾开始插入" << endl;
	list->ListInsertTail(&node1);
	list->ListInsertTail(&node2);
	list->ListTraverse();
	delete list;
	list = NULL;
	return 0;
}

测试结果:

从链表头开始插入
tester2----234567

tester1----123456

从链表尾开始插入
tester2----234567

tester1----123456

tester1----123456

tester2----234567

第二代码实现:
包含以下功能菜单:
功能菜单:
1)新建 联系人
2)删除联系人
3)浏览通讯录
4)退出通讯录
请输入:

该代码是基于第一个代码实现的基础上编写测试入口代码。
ListDemo.cpp–>修改后的测试入口代码文件

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


int menu()
{
	// 显示通讯录功能菜单
	cout << "功能菜单" << endl;
	cout << "1.新建联系人" << endl;
	cout << "2.删除联系人" << endl;
	cout << "3.浏览通讯录" << endl;
	cout << "4.退出通讯录" << endl;

	cout << "请输入:";

	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)
{
	int count;
	cout << "请输入要删除第几个信息:";
	cin >> count;
	Node temp;
	pList->ListDelete(count, &temp);
}
int main() {
	int userOrder = 0;
	List *pList = new List();
	while(userOrder != 4) {
		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;
		default:
			break;

		}
	}
	delete pList;
	pList = NULL;
	return 0;
}


测试结果:

功能菜单
1.新建联系人
2.删除联系人
3.浏览通讯录
4.退出通讯录
请输入:1
用户指令-->>新建联系人:
请输入姓名:tester1
请输入电话:11111
功能菜单
1.新建联系人
2.删除联系人
3.浏览通讯录
4.退出通讯录
请输入:1
用户指令-->>新建联系人:
请输入姓名:tester2
请输入电话:22222
功能菜单
1.新建联系人
2.删除联系人
3.浏览通讯录
4.退出通讯录
请输入:3
用户指令-->>浏览通讯录:
tester1----11111

tester2----22222

功能菜单
1.新建联系人
2.删除联系人
3.浏览通讯录
4.退出通讯录
请输入:2
用户指令-->>删除联系人:
请输入要删除第几个信息:0
功能菜单
1.新建联系人
2.删除联系人
3.浏览通讯录
4.退出通讯录
请输入:3
用户指令-->>浏览通讯录:
tester2----22222

功能菜单
1.新建联系人
2.删除联系人
3.浏览通讯录
4.退出通讯录
请输入:
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值