前提:
链表
-
线性链式存储的特点:
动态存储分配
结点总不顺序存放(与可变长数组的差别) -
基本概念
结点(Node)
一个链表由若干个结点组成
每个结点包含两个域:数据域和指针域
数据域:存放数据
指针域:用于存放指向该链表的下一个结点的指针
单链表:
它指通过一组任意的存储单元来存储线性表中的数据元素。
头结点:
在链表的第一个结点之前附加一个结点(不算在数组索引中,数据域为空,指针域指向开始的结点)。
区分头结点和头指针:
不管带不带头结点,头指针始终指向链表的第一个结点,而头结点是带头结点链表中的第一个结点,结点内通常不存储信息。
循环链表
特点:表中最后一个结点的指针域指向头结点,整个链表形成一个环。
双向链表
双向链表的结点中有两个指针域,其一指向直接后继,另一指向直接前驱。
静态链表(无“指针”)
静态链表:第一个位置为头节点
0 1(指向的下一个元素为1号)
1 4(指向4号)
2 3(指向3号)
3 0(指向0号,当前链表走到最后)
4 2(指向2号)
链表的基本操作
插入操作
插入类型
插入操作分析
将值为x的新结点插入到单链表的第i个位置上
- 先检查插入位置的合法性
- 找到待插入位置的前驱结点,即第i-1个结点
- 插入结点
bool List::ListInsert(int i, Node *pNode)//在第i位插入结点
{
if (i<0 || i>m_iLength)//判断插入是否合法;i不能小于0,i不能大于链表长度
{
return false;
}
Node *currentNode = m_pList;//currentNode保存头结点
for (int k = 0; k < i; k++)
{
currentNode = currentNode->next;
}//直到找到第i个
Node *newNode = new Node;//声明newNode
if (newNode == NULL) //判断申请的结点内存是否为空
{
return false;
}
else
{
newNode->data = pNode->data;//pNode数据域赋值给newNode数据域
newNode->next = currentNode->next;//新结点指向原来结点的下一个结点
currentNode->next = newNode;//原来的结点指向新结点
m_iLength++;//插入链表长度+1
return true;
}
}
删除操作
删除操作分析:
bool List::ListDelete(int i, Node *pNode)
{
if (i<0 || i>=m_iLength)//判断i是否合法,注意此处的i取值
{
return false;
}
Node *currentNode = m_pList;//currentNode保存头结点
Node *currentNodeBefore = NULL;//头结点前一个结点置空
for (int k = 0; k <= i; k++)//找到<=i
{
currentNodeBefore = currentNode;//赋值
currentNode = currentNode->next;
}
currentNodeBefore->next = currentNode->next;
pNode->data = currentNode->data;//pNode数据域接收当前结点的数据域
delete currentNode;//删除结点
currentNode = NULL;//赋予null值
m_iLength--;//删除链表长度-1
return true;
}
源码:
头文件:Node.h
#include<iostream>
using namespace std;
class Node
{
public:
int data;//数据域
Node *next;//指针域
void printNode();
};
#endif
Node.cpp
#include"Node.h"
void Node::printNode()
{
cout << data << endl;
}
头文件:list.h
#include"Node.h"
#ifndef LIST_H
#define LIST_H
#include<iostream>
using namespace std;
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;
};
#endif
list.cpp
#include"List.h"
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 0 == m_iLength ? true : false;
}
int List::ListLength()
{
return m_iLength;
}
bool List::ListInsertHead(Node *pNode)//头部插入结点
{
Node *temp = m_pList->next;//保留头结点在temp
Node *newNode = new Node;//从堆申请内存保留新的结点
if (newNode == NULL) //判断申请的结点内存是否为空
{
return false;
}
else
{
newNode->data = pNode->data;
m_pList->next = newNode;//头结点的next指向新申请的结点
newNode->next = temp; //指向保留头结点的temp
m_iLength++;
return true;
}
}
bool List::ListInsertTail(Node *pNode)//尾部插入结点
{
Node *currentNode = m_pList;
while (currentNode->next != NULL)//判断当前结点next是否指向null
{
currentNode = currentNode->next;//相当于来到下一个结点
}
Node *newNode = new Node;//从堆中分配内存
if (newNode == NULL)
{
return false;
}
else
{
newNode->data = pNode->data;//赋值
newNode->next = NULL;//指针域指向null
currentNode->next = newNode;//
m_iLength++;//插入++
return true;
}
}
bool List::ListInsert(int i, Node *pNode)//在第i位插入节点
{
if (i<0 || i>m_iLength)//判断插入是否合法m_ilength每次插入都+1,每次删减都-1
{
return false;
}
Node *currentNode = m_pList;//头结点
for (int k = 0; k < i; k++)//找到第i节点
{
currentNode = currentNode->next;
}//直到找到第i个
Node *newNode = new Node;
if (newNode == NULL) //判断申请的结点内存是否为空
{
return false;
}
else
{
newNode->data = pNode->data;
newNode->next = currentNode->next;//currentnode下一个节点变成newnode的下一个节点
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++)//找到<=第i个位置
{
currentNodeBefore = currentNode;
currentNode = currentNode->next;
}
currentNodeBefore->next = currentNode->next;//将currennode分离出来
pNode->data = currentNode->data;//赋值
delete currentNode;//删除
currentNode = NULL;//赋予null值
m_iLength--;//删除节点-1
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;
int count = 0;
while (currentNode->next != NULL)
{
tempNode = currentNode;
currentNode = currentNode->next;
if (currentNode->data == pCurrentNode->data)
{
if (tempNode == m_pList) //tempnode是否是头结点
{
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;
}
}
}
void List::ListTraverse()//遍历
{
Node *currentNode = m_pList;
while (currentNode->next != NULL)
{
currentNode = currentNode->next;
currentNode->printNode();
}
}
main.cpp
#include"List.h"
#include "Node.h"
int main(void)
{
Node node1;
node1.data = 1;
Node node2;
node2.data = 2;
Node node3;
node3.data = 3;
Node node4;
node4.data = 4;
Node node5;
node5.data = 0;
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->NextElem(&node5,&temp);
pList->ListTraverse();
cout << "temp=" << temp.data << endl;
delete pList;
pList = NULL;
system("pause");
return 0;
}
资料来源:慕课网