一、线性表的概念及工作原理
线性表是N个数据元素的有限序列
二、顺序表的基本操作实战
//在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);
int LocateElem(int *e);
bool PriorElem(int *currentElem, int *preElem);
bool NextElem(int *currentElem, int *nextElem);
void ListTraverse();
bool ListInsert(int i, int *e);
bool ListDelete(int i, int*e);
private:
int *m_pList;
int m_iSize;
int m_iLength;
};
#endif // !LIST_H
//List.cpp文件中
#include "List.h"
#include <iostream>
using namespace std;
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::ListEmpty()
{
if (m_iLength == 0)
return true;
return false;
}
int List::ListLength()
{
return m_iLength;
}
bool List::GetElem(int i, int *e)
{
if (i < 0 || i < m_iSize)
{
return false;
}
*e = m_pList[i];
return true;
}
int List::LocateElem(int *e)
{
for (int i = 0; i < m_iLength; i++)
{
if (*e == m_pList[i])
{
return i;
}
}
return -1;
}
bool List::PriorElem(int *currentElem, int *preElem)
{
int temp = LocateElem(currentElem);
if (temp == -1)
{
return false;
}
else
{
if (temp == 0)
{
return false;
}
else
{
*preElem = m_pList[temp - 1];
return true;
}
}
}
bool List::NextElem(int *currentElem, int *nextElem)
{
int temp = LocateElem(currentElem);
if (temp == -1)
{
return false;
}
else
{
if (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;
}
}
bool List::ListInsert(int i, int *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, int*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;
}
三、链表的基本操作实战
//List.h文件
#ifndef LIST_H
#define LIST_H
#include"Node.h"
class LinkList
{
public:
LinkList();//构造函数
~LinkList();//析构函数
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_H
//List.cpp文件
#include"List.h"
#include<iostream>
using namespace std;
LinkList::LinkList()
{
m_pList = new Node;
m_pList->data = 0;
m_pList->next = NULL;
m_iLength = 0;
}
LinkList::~LinkList()
{
ClearList();
delete m_pList;
m_pList = NULL;
}
void LinkList::ClearList()
{
Node *currentNode = m_pList->next;
while (currentNode != NULL)
{
Node *temp = currentNode->next;
delete currentNode;
currentNode = temp;
}
m_pList->next = NULL;
}
bool LinkList::ListEmpty()
{
if (m_iLength == 0)
{
return true;
}
return false;
}
int LinkList::ListLength()
{
return m_iLength;
}
bool LinkList::ListInsertHead(Node *pNode)
{
Node *temp = m_pList->next;
Node *newNode = new Node;
if (newNode == NULL) //判断申请的结点内存是否为空
{
return false;
}
else
{
newNode->data = pNode->data;
m_pList->next = newNode;
newNode->next = temp;
m_iLength++;
return true;
}
}
bool LinkList::ListInsertTail(Node *pNode)
{
Node *currentNode = m_pList;
while (currentNode->next != NULL)
{
currentNode = currentNode->next;
}
Node *newNode = new Node;
if (newNode == NULL)
{
return false;
}
else
{
newNode->data = pNode->data;
newNode->next = NULL;
currentNode->next = newNode;
m_iLength++;
return true;
}
}
bool LinkList::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;
}
else
{
newNode->data = pNode->data;
newNode->next = currentNode->next;
currentNode->next = newNode;
return true;
}
}
bool LinkList::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 LinkList::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 LinkList::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 LinkList::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 LinkList::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 LinkList::ListTraverse()
{
Node *currentNode = m_pList;
while (currentNode->next != NULL)
{
currentNode = currentNode->next;
currentNode->printNode();
}
}
//node.cpp文件
#include"Node.h"
#include<iostream>
using namespace std;
void Node::printNode()
{
cout << data << endl;
}
//node.h文件
#ifndef NODE_H
#define NODE_H
class Node
{
public:
int data;
Node* next;
void printNode();
};
#endif // !NODE_H
//demo.cpp文件
#include"List.h"
#include "Node.h"
#include <iostream>
using namespace std;
int main(void)
{
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;
LinkList *pList = new LinkList();
//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;
return 0;
}