
注:转载请标明原文出处链接:https://xiongyiming.blog.csdn.net/article/details/100855604
3 链表的基本操作
3.1 链表简介
链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。
链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。
相比于线性表顺序结构,操作复杂。由于不必须按顺序存储,链表在插入的时候可以达到O(1)的复杂度,比另一种线性表顺序表快得多,但是查找一个节点或者访问特定编号的节点则需要O(n)的时间,而线性表和顺序表相应的时间复杂度分别是O(logn)和O(1)。
(以上均来自百度百科)

顺序表优点:遍历和寻址非常快
顺序表缺点:往顺序表中插入一个元素,顺序表中的元素都要往后移动;顺序表中删除一个元素, 顺序表中的元素都要往前移动。这样的效率很低。
因此,针对顺序表的缺点,引出链表。




3.2 代码示例
要求
线性表——单链表
- 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 *pCurrentNode, Node *pNextNode); //获取指定元素的后继
- bool ListInsert(int i, Node *pNode); //在第i个位置插入元素
- bool ListDelete( int i, Node *pNode); //删除第i个位置的元素
- void ListTraverse(); //遍历线性表
- bool ListInsertHead(Node *pNode);//从头插入节点
- bool ListInsertTail(Node *pNode);//从尾插入节点
Node.h
#pragma once
class Node
{
public:
int data;//数据域
Node *next;//指针域
void printNode();//打印节点
};
Node.cpp
#include<iostream>
#include"Node.h"
using namespace std;
//打印节点
void Node::printNode()
{
cout << data << endl;
}
List.h
#pragma once
#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 *pCurrentNode, Node *pNextNode); //获取指定元素的后继
bool ListInsert(int i, Node *pNode); //在第i个位置插入元素
bool ListDelete( int i, Node *pNode); //删除第i个位置的元素
void ListTraverse(); //遍历线性表
bool ListInsertHead(Node *pNode);//从头插入节点
bool ListInsertTail(Node *pNode);//从尾插入节点
private:
Node *m_pList;
int m_iLength;
};
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()
{
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;
}
//寻找第一个满足e的数据元素的位序
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;
int count = 0;
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;
}
//在第i个位置插入元素
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;
}
//删除第i个位置的元素
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::ListTraverse()
{
Node *currentNode = m_pList;
while (currentNode->next != NULL)
{
currentNode = currentNode->next;
currentNode->printNode();
}
}
//从头插入节点
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->data = pNode->data;
newNode->next = NULL;
currentNode->next = newNode;
m_iLength++;
return true;
}
main.cpp
#include<iostream>
#include"List.h"
using namespace std;
/**********************************************************************
线性表——单链表
List(int size);//创建线性表——构造函数
~List(); //销毁线性表——析构函数
void ClearList();//清空线性表
bool ListEmpty();//判断线性表是否为空
int ListLength(); //获取线性表长度
bool GetElem(int i, int *e); //获取指定元素
int LocateElem(int *e); //寻找第一个满足e的数据元素的位序
bool PriorElem(int *currentElem, int *preElem); //获取指定元素的前驱
bool NextElem(int *currentElem, int *nextElem); //获取指定元素的后继
bool ListInsert(int i, int *e); //在第i个位置插入元素
bool ListDelete( int i, int *e); //删除第i个位置的元素
void ListTraverse(); //遍历线性表
**********************************************************************/
int main()
{
Node node1;
node1.data = 3;
Node node2;
node2.data = 4;
Node node3;
node3.data = 5;
Node node4;
node4.data = 6;
List *pList = new List();
pList->ListInsertHead(&node1);
pList->ListInsertHead(&node2);
pList->ListInsertHead(&node3);
pList->ListInsertHead(&node4);
pList->ListTraverse();
cout << "______________" << endl;
//从中间插入元素
Node node5;
node5.data = 7;
pList->ListInsert(0, &node5);
pList->ListTraverse();
cout << "______________" << endl;
delete pList;
pList = NULL;
cin.get();
return 0;
}
运行结果

4 链表应用——通讯录
等待更新…….
参考资料
[1] 数据结构探险之线性表篇
来源:https://blog.csdn.net/zaishuiyifangxym/article/details/100855604