C++数据结构___线性表

线性表

数据结构探险之线性表篇

线性表是n个数据元素的有限序列
C++数据结构___线性表_第1张图片

顺序表

List.h

#pragma once
#include
using namespace std;
#include "Coordinate.h"

class List
{
public:
	List(int size);
	~List();
	void ClearList();
	bool ListEmpty();
	int 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;
};

List.cpp

#include"List.h"
List::List(int size)
{
	m_iSize = size;
	m_pList = new Coordinate[m_iSize];
	m_iLength = 0;
}

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

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

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

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

bool List::GetElem(int i, Coordinate *e)
{
	if (i < 0 || i >= m_iSize)
	{
		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 (temp == -1 || temp == 0)
	{
		return false;
	}
	else
	{
		*preElem = m_pList[temp - 1];
	}
}

bool List::NextElem(Coordinate *currentElem, Coordinate *nextElem)
{
	int temp = LocateElem(currentElem);
	if (temp == -1 || temp == m_iLength - 1)
	{
		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;
		// 		m_pList[i].printCoordinate();
	}
}

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

Coordinate.h

#pragma once
#include
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;
};

Coordinate.cpp

#include "Coordinate.h"
#include 
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;
}

demo.cpp

#include 
#include "List.h"

int main()
{
	Coordinate e1(1, 2);
	Coordinate e2(3, 4);
	Coordinate e3(5, 6);

	List *list = new List(10);
	list->ListInsert(0, &e1);
	list->ListInsert(1, &e2);
	list->ListInsert(2, &e3);

	list->ListTraverse();

	printf("长度:%d\n", list->ListLength());

	return 0;
}

链表

node.h

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

node.cpp

#include "Node.h"
#include 
using namespace std;

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

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);
	bool PriorElem(Node *pCurrentNode, Node *pPreNode);
	bool NextElem(Node *pCurrentNode, Node *pNextNode);
	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;
};

List.cpp

//线性表--顺序表

#include "List.h"
#include 
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;
}

bool List::ListInsertHead(Node *pNode)
{
	Node *newNode = new Node;
	if (newNode == NULL)
	{
		return false;
	}
	newNode->data = pNode->data;
	newNode->next = m_pList->next;
	m_pList->next = newNode;
	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;
}
void List::ClearList()
{
	Node *currentNode = m_pList->next;
	while (currentNode != NULL)
	{
		Node *temp = currentNode->next;
		delete currentNode;
		currentNode = temp;
	}
	m_pList->next = NULL;
	m_iLength = 0;
}

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

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

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

demo.cpp

#include 
#include "List.h"
#include 
using namespace std;

int main()
{
	List *pList = new List();
	Node node1;
	node1.data = 1;
	Node node2;
	node2.data = 2;
	Node node3;
	node3.data = 3;
	Node node4;
	node4.data = 4;
	Node node5;
	node5.data = 5;

	// 	pList->ListInsertHead(&node1);
	// 	pList->ListInsertHead(&node2);
	// 	pList->ListInsertHead(&node3);
	// 	pList->ListInsertHead(&node4);

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

	pList->ListInsert(1, &node5);

	// 	Node temp;
	// 	pList->ListDelete(1, &temp);
	// 	cout << "temp = " << temp.data << endl;

	pList->ListTraverse();

	delete pList;
	pList = NULL;

	return 0;
}

链表应用之通讯录

Person.h

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

Person.cpp

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

Node.h

#include "Person.h"

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

Node.cpp

#include "Node.h"
#include 
using namespace std;

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

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);
	bool PriorElem(Node *pCurrentNode, Node *pPreNode);
	bool NextElem(Node *pCurrentNode, Node *pNextNode);
	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;
};

List.cpp

//线性表--顺序表

#include "List.h"
#include 
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;
}

bool List::ListInsertHead(Node *pNode)
{
	Node *newNode = new Node;
	if (newNode == NULL)
	{
		return false;
	}
	newNode->data = pNode->data;
	newNode->next = m_pList->next;
	m_pList->next = newNode;
	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;
}
void List::ClearList()
{
	Node *currentNode = m_pList->next;
	while (currentNode != NULL)
	{
		Node *temp = currentNode->next;
		delete currentNode;
		currentNode = temp;
	}
	m_pList->next = NULL;
	m_iLength = 0;
}

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

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

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

demo.cpp

#include 
#include "List.h"
#include 
using namespace std;

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;
	cout << "请输入姓名:" << endl;
	cin >> node.data.name;
	cout << "请输入电话:" << endl;
	cin >> node.data.phone;
	pList->ListInsertTail(&node);
}

void deletePerson(List *pList)
{
	Node node;
	cout << "请输入姓名:" << endl;
	cin >> node.data.name;
	cout << "请输入电话:" << endl;
	cin >> node.data.phone;
	int num = 0;
	num = pList->LocateElem(&node);
	pList->ListDelete(num, &node);
}

int main()
{
	List *pList = new List();

	int userOrder = 0;
	while (userOrder != 4)
	{
		userOrder = menu();
		switch (userOrder)
		{
		case 1:
			cout << "用户指令--->新建联系人:" << endl;
			createPerson(pList);
			break;
		case 2:
			cout << "用户指令--->删除联系人:" << endl;
			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;
}

你可能感兴趣的:(C++)