数据结构探险之线性表下篇:链表
链表实现
顺序表的优缺点:
优点:遍历寻址很方便,基于数组效率高
缺点:插入时元素挪位。删除时元素挪位。
什么是链表?
单向性:节点(数据域|指针域)指针域指向下一个节点。直到最后一个指针域指向NULL;
循环链表:
特点:最后一个节点的指针域又指向头结点。
双向链表:
每个节点由三部分组成。指针域与指针域之间还有数据域。
一个正向指针域,一个反向指针域。既能从头找到尾,又能从尾找到头。
静态链表:通过数组
数组的天然编号。加自定义的指向指针域
链表编码说明
单链表
ListInsertHead是在链表的前边插入一个节点,紧跟在头节点后面,而不是在头节点的前面插入一个节点来替换掉原本的头节点!!!
#ifndef LIST_H
#define LIST_H
#include "Node.h"
class List
{
public:
List(int size);//相对于线性表,先放了头结点
~List();
void CleanList();//得一个一个清空
bool ListEmpty();
int ListLength();
bool GetElem(int i, Node *pNode);
int LocateElem(Node *pNode);//给定节点的坐标
bool PriorElem(Node *pCurrentElem, Node *pPreElem);//根据currentnode找到相应节点。将前驱或后继赋值给其他变量
bool NextElem(Node *pCurrentElem, Node *pNextElem);
void ListTraverse();//拿着头结点。
bool ListInsert(int i, Node *pNode);//找到i-1个节点。指向该节点。该节点指向原来第i个。
bool ListDelete(int i, Node *pNode);//找到第i个节点。上一个节点指向下一个节点
bool ListInsertHead(Node *pNode);//从头开始插入。头结点指针域指向该节点。该节点指针域指向原来头结点指向的结点。
bool ListInsertTail(Node *pNode);//从尾开始插入。只需要找到最后一个节点。该节点指针域为NulL。让插入节点指向null。
private:
int *m_pList;
int m_iSize;
int m_iLength;
};
#endif
链表编码实战(一)
链表相对于顺序表没有size。
析构与清空的区别:析构会连第一个构造函数创建的头结点也删除,而清空会保留这个申请的头结点内存。
#ifndef LIST_H
#define LIST_H
#include "Node.h"
class List
{
public:
List();//相对于线性表,先放了头结点
~List();
void CleanList();//得一个一个清空
bool ListEmpty();
int ListLength();
bool GetElem(int i, Node *pNode);
int LocateElem(Node *pNode);//给定节点的坐标
bool PriorElem(Node *pCurrentElem, Node *pPreElem);//根据currentnode找到相应节点。将前驱或后继赋值给其他变量
bool NextElem(Node *pCurrentElem, Node *pNextElem);
void ListTraverse();//拿着头结点。
bool ListInsert(int i, Node *pNode);//找到i-1个节点。指向该节点。该节点指向原来第i个。
bool ListDelete(int i, Node *pNode);//找到第i个节点。上一个节点指向下一个节点
bool ListInsertHead(Node *pNode);//从头开始插入。头结点指针域指向该节点。该节点指针域指向原来头结点指向的结点。
bool ListInsertTail(Node *pNode);//从尾开始插入。只需要找到最后一个节点。该节点指针域为NulL。让插入节点指向null。
private:
Node *m_pList;
int m_iLength;
};
#endif
#include "Link.h"
#include <iostream>
using namespace std;
List::List()
{
m_pList = new Node;
m_pList->data = 0;
m_pList->next = NULL;
m_iLength = 0;
}
List::~List()
{
CleanList();
delete m_pList;
m_pList = NULL;
}
void List::CleanList()
//顺藤摸瓜。
{
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::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;
}
currentNode->next = newNode;
newNode->data = pNode->data;
newNode->next = NULL;
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的下一个节点变成了newnode的下一个节点
currentNode->next = newNode;
//newnode变成了currentnode的下一个节点
//带入两个极端情况来验证
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;
}
//找第i个节点
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++;
//第一次拿到的head是头结点并不算第0号元素。
}
return -1;
}
bool List::PriorElem(Node *pCurrentElem, Node *pPreElem)
{
Node *currentNode = m_pList;
Node *tempNode = NULL;
while (currentNode->next != NULL)
{
tempNode = currentNode;
currentNode = currentNode->next;
if (currentNode->data == pCurrentElem->data)
{
if (tempNode == m_pList)//如果是头结点
{
return false;
}
pPreElem->data = tempNode->data;
return true;
}
}
return false;
}
bool List::NextElem(Node *pCurrentElem, Node *pNextElem)
{
Node *currentNode = m_pList;
while (currentNode->next != NULL)
{
currentNode = currentNode->next;
if (currentNode->data == pCurrentElem->data)
{
if (currentNode->next == NULL)//如果是尾节点
{
return false;
}
pNextElem->data = currentNode->next->data;
return true;
}
}
return false;
}
void List::ListTraverse()
{
Node *currentNode = m_pList;
while (currentNode->next != NULL)
{
currentNode = currentNode->next;
currentNode->printNode();
}
}
node.h & node.cpp:
#ifndef NODE_H
#define NODE_H
class Node
{
public:
int data;//数据域
Node *next;//指针域指向下一个地点
void printNode();
};
#endif
#include "Node.h"
#include <iostream>
using namespace std;
void Node::printNode()
{
cout << data << endl;
}
main.cpp:
#include <stdlib.h>
#include "Link.h"
#include <iostream>
using namespace std;
//bool InitList(List **list); //创建线性表
//void DestoryList(List *list); //销毁线性表
//void ClearList(List *list); //清空线性表
//bool ListEmpty(List *list); //判断线性表是否为空
//bool GetElem(List *list, int i, Elem *e);//
int main()
{
Node node1;
node1.data = 3;
Node node2;
node2.data = 4;
Node node3;
node3.data = 5;
Node node4;
node4.data = 6;
Node node5;
node5.data = 7;
Node temp;
List *pList = new List();
/*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);
//pList->ListDelete(1, &temp);
//pList->GetElem(1, &temp);
//pList->PriorElem(&node5, &temp);
pList->NextElem(&node5, &temp);
pList->ListTraverse();
cout <<"temp:" <<temp.data << endl;
delete pList;
pList = NULL;
system("pause");
return 0;
}
运行结果:
链表应用之通讯录
要求:
person.h & person.cpp:
#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"
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;
}
ostream &operator<<(ostream &out, Person &person)
{
out << person.name <<"---------"<< person.phone << endl;
return out;
}
node.h & node.cpp:
#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;
}
link.h & link.cpp:
#ifndef LIST_H
#define LIST_H
#include "Node.h"
class List
{
public:
List();//相对于线性表,先放了头结点
~List();
void CleanList();//得一个一个清空
bool ListEmpty();
int ListLength();
bool GetElem(int i, Node *pNode);
int LocateElem(Node *pNode);//给定节点的坐标
bool PriorElem(Node *pCurrentElem, Node *pPreElem);//根据currentnode找到相应节点。将前驱或后继赋值给其他变量
bool NextElem(Node *pCurrentElem, Node *pNextElem);
void ListTraverse();//拿着头结点。
bool ListInsert(int i, Node *pNode);//找到i-1个节点。指向该节点。该节点指向原来第i个。
bool ListDelete(int i, Node *pNode);//找到第i个节点。上一个节点指向下一个节点
bool ListInsertHead(Node *pNode);//从头开始插入。头结点指针域指向该节点。该节点指针域指向原来头结点指向的结点。
bool ListInsertTail(Node *pNode);//从尾开始插入。只需要找到最后一个节点。该节点指针域为NulL。让插入节点指向null。
private:
Node *m_pList;
int m_iLength;
};
#endif
#include "Link.h"
#include <iostream>
using namespace std;
List::List()
{
m_pList = new Node;
//m_pList->data = 0;
m_pList->next = NULL;
m_iLength = 0;
}
List::~List()
{
CleanList();
delete m_pList;
m_pList = NULL;
}
void List::CleanList()
//顺藤摸瓜。
{
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::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;
}
currentNode->next = newNode;
newNode->data = pNode->data;
newNode->next = NULL;
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的下一个节点变成了newnode的下一个节点
currentNode->next = newNode;
//newnode变成了currentnode的下一个节点
//带入两个极端情况来验证
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;
}
//找第i个节点
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++;
//第一次拿到的head是头结点并不算第0号元素。
}
return -1;
}
bool List::PriorElem(Node *pCurrentElem, Node *pPreElem)
{
Node *currentNode = m_pList;
Node *tempNode = NULL;
while (currentNode->next != NULL)
{
tempNode = currentNode;
currentNode = currentNode->next;
if (currentNode->data == pCurrentElem->data)
{
if (tempNode == m_pList)//如果是头结点
{
return false;
}
pPreElem->data = tempNode->data;
return true;
}
}
return false;
}
bool List::NextElem(Node *pCurrentElem, Node *pNextElem)
{
Node *currentNode = m_pList;
while (currentNode->next != NULL)
{
currentNode = currentNode->next;
if (currentNode->data == pCurrentElem->data)
{
if (currentNode->next == NULL)//如果是尾节点
{
return false;
}
pNextElem->data = currentNode->next->data;
return true;
}
}
return false;
}
void List::ListTraverse()
{
Node *currentNode = m_pList;
while (currentNode->next != NULL)
{
currentNode = currentNode->next;
currentNode->printNode();
}
}
main.cpp:
#include <stdlib.h>
#include "Link.h"
#include <iostream>
using namespace std;
//bool InitList(List **list); //创建线性表
//void DestoryList(List *list); //销毁线性表
//void ClearList(List *list); //清空线性表
//bool ListEmpty(List *list); //判断线性表是否为空
//bool GetElem(List *list, int i, Elem *e);//
int main()
{
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->ListInsertTail(&node1);
pList->ListInsertTail(&node2);*/
pList->ListInsertHead(&node1);
pList->ListInsertHead(&node2);
pList->ListTraverse();
delete pList;
pList = NULL;
system("pause");
return 0;
}
运行
通讯录加壳版
main.cpp:
#include <iostream>
#include <stdlib.h>
#include "Link.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 creatperson(List *pList)
{
Node node;
Person person;
cout << "输入用户姓名:";
cin >> person.name;
cout << "输入电话号码:";
cin >> person.phone;
node.data = person;
pList->ListInsertTail(&node);
}
int main()
{
int userorder = 0;
List *pList = new List();
while (userorder != 4)
{
userorder = menu();
switch (userorder)
{
case 1:
cout << "用户指令-------》新建联系人" << endl;
creatperson(pList);
break;
case 2:
cout << "用户指令-------》删除联系人" << endl;
break;
case 3:
cout << "用户指令-------》浏览通讯录" << endl;
pList->ListTraverse();
break;
case 4:
cout << "用户指令-------》退出通讯录" << endl;
break;
}
}
delete pList;
pList = NULL;
system("pause");
return 0;
}