0 概述
线性表的定义:n个数据元素的有限序列。
线性表的分类:线性表可以分为顺序表(数组描述)与链表(链式描述)。
1 顺序表(数组描述)
顺序表的特点是直接前驱,直接后继,数据之间是连续存储的。
先定义一个顺序表的类,看看它需要哪些成员:
List.h
#ifndef LIST_H
#define LIST_H
class List
{
public:
List(int size); //构造函数 传入线性表长度
~List(); //析构函数
void ClearList(); //清空线性表
bool ListEmpty(); // 判断是否为空
int ListLength();
bool GetElem(int i, int *e); //获取指定元素,把下标i对应的值传回e
int LocateElem(int *e); //查找元素e所在位置
bool PriorElem( int *currentElem,int *preElem); //获取指定元素的前驱
bool NextElem(int *currentElem, int *nextElem); //获取指定元素的后继
void ListTraverse(); //遍历线性表
bool ListInsert(int i ,int *e); //在i处插入元素
bool ListDelete(int i ,int *e); //在i处删除元素
private:
int *m_pList; // 用于存储线性表的数组首地址
int m_iSize; // 线性表容量
int m_iLength;// 线性表当前长度
};
#endif
其中 int *m_pList 是用于存储线性表的数组首地址。
下面具体讨论List各成员函数如何实现:
构造函数:初始化线性表容量、为m_pList分配动态内存
List::List(int size)
{
m_iSize = size; // 容量
m_pList = new int[m_iSize];
m_iLength = 0;
}
析构函数:回收动态内存
List::~List()
{
delete []m_pList;
m_pList = NULL;
}
清空线性表:直接将线性表当前长度置零即可
void List::ClearList()
{
m_iLength = 0;
}
插入元素:
bool List::ListInsert(int i, int *e)
{
// 判断插入位置的合法性
if (i<0 || i >= m_iSize)
{
return false;
}
// 先把i及之后的先移动
// m_ilength-1是当前表中最后一个元素的下标。
for(int k = m_iLength-1; k>=i; k--)
{
m_pList[k + 1] = m_pList[k]; // 后一个位置等于前一个位置的值,即整体后移。
}
// i处元素
m_pList[i] = *e;
// 插入之后,长度变长
m_iLength++;
return true;
}
删除元素:
bool List::ListDelete(int i, int *e)
{
// i 位置的合法性判断 因为i下标,i的范围为0到m_size-1不能等于
if (i <0 || i >m_iSize)
{
return false;
}
*e = m_pList[i]; // 备份要删除的值
// i位置是要放i+1位置的元素的。
// 从i+1开始移动,不会导致被覆盖问题。
for (int k =i+1;k<m_iLength;k++)
{
m_pList[k-1] = m_pList[k];
}
m_iLength--;
return true;
}
2 链表(链式描述)
常用链表分类:
以单链表为例:
Node.h
#ifndef NODE_H
#define NODE_H
class Node
{
public:
int data;//数据域
Node *next;//指针域指向下一个节点
};
#endif
list.h
#ifndef LIST_H
#define LIST_H
#include "Node.h"
class List
{
public:
List(); //相对于线性表,先放头结点,不需要size,增加节点新申请内存。
~List();
void ClearList();//得一个一个节点清空
bool ListEmpty();
int ListLength();
bool GetElem(int i, Node *pNode);//头结点顺藤摸瓜,在第i的位置插入元素
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
list.cpp
#include "List.h"
#include <iostream>
using namespace std;
List::List()
{
m_pList = new Node; //堆中申请内存
m_pList->data = 0;
m_pList->next = NULL; //初始化时头结点指向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;
m_iLength = 0;
}
bool List::ListEmpty()
{
return m_iLength==0?true: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;
}
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++)//找到的是i位置前一个
{
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的下一个节点
// 带入两个极端情况来验证,0位置插入,for循环一次都不执行。currentNode就是头结点。然后新Node的next指向头结点原本指向的节点。
// 如果我们最后一个节点位置插入,原本最后一个节点指向NUll,赋值给新节点的Next
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;
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();
}
}