链表的基本算法的实现:
Node.h
#ifndef NODE_H
#define NODE_H
class Node
{
public:
int data;
Node *next;
void printNode();
};
#endif
Node.cpp
#include"Node.h"
#include<iostream>
using namespace std;
void Node::printNode()
{
cout<<data<<endl;
}
List.h
#include"Node.h"
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 *pPretNode);//查找指定节点的直接前驱
bool NextElem(Node *pCurrentNode,Node *pNextNode);//查找指定节点的直接后继
bool ListInsert(int i,Node *pNode) ; //插入节点
bool ListDelete(int i,Node *pNode) ; //删除节点
void ListTraverse(); //遍历链表,输出所有节点
bool ListInsetHead(Node *pNode); //插入到起始节点前一个位置
bool ListInsetTail(Node *pNode); //插入到末尾节点后一个位置
private:
Node *m_pList; // 头指针
int m_iLength; //链表长度
};
List.cpp
#include"List.h"
#include"Node.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() //析构函数
{
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; //保存头节点
for(int k=0;k<=i;k++) //查找第i个节点
{
currentNode=currentNode->next; //循环结束代表currentNode就是第i节点
}
pNode->data=currentNode->data; //得到第i个节点的数据域
return true;
}
int List::LocateElem(Node *pNode)
{
Node *currentNode=m_pList;
int count=0;
while(currentNode->next!=NULL) //遍历节点
{
currentNode=currentNode->next; //经过while循环后的currentNode就是尾节点
if(currentNode->data==pNode->data) //匹配到要找的节点
{
return count ; //返回指定节点的位置
}
count++;
}
return -1;
}
bool List::PriorElem(Node *pCurrentNode,Node *pPretNode)
{
Node *currentNode=m_pList;
Node *tempNode=NULL;
while(currentNode->next!=NULL) //遍历节点
{
tempNode=currentNode;
currentNode=currentNode->next; //经过while循环后的currentNode就是尾节点
if(currentNode->data==pCurrentNode->data) //匹配到节点
{
if(tempNode==m_pList)
{
return false ;
}
pPretNode->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; //经过while循环后的currentNode就是尾节点
if(currentNode->data==pCurrentNode->data)
{
if(currentNode->next==NULL)
{
return false ;
}
pNextNode->data= currentNode->next->data ;
return true;
}
}
return false;
}
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-1个节点
{
currentNode=currentNode->next; //循环结束代表currentNode就是第i-1节点
}
Node *newNode=new Node; //从堆中申请内存
if(newNode==NULL) //内存申请失败,返回false
return false;
newNode->data=pNode->data; //
newNode->next=currentNode->next; //第i-1个节点的指针域赋给新节点的指针域
currentNode->next=newNode; //第i-1个节点指针域指向第i个节点
m_iLength++; //节点的长度+1
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; //找到第i-1个节点
currentNode=currentNode->next; //循环结束代表currentNode就是第i节点
}
currentNodeBefore->next=currentNode->next;//第i个节点指针域赋给第i-1节点指针域
pNode->data=currentNode->data; //删掉节点的数据域赋给pNode输出
delete currentNode; //释放删除节点的内存
currentNode=NULL;
m_iLength--; //节点长度减1
return true;
}
void List::ListTraverse()
{
Node *currentNode=m_pList;
while(currentNode->next!=NULL) //遍历所有节点
{
currentNode=currentNode->next;
currentNode->printNode(); //遍历输出
}
}
bool List::ListInsetHead(Node *pNode)
{
Node *temp=m_pList->next; //保存原起始节点的地址
Node *newNode=new Node; //从堆中申请内存
if(newNode==NULL) //内存申请失败,返回false
return false;
newNode->data=pNode->data; //只拿数据域
m_pList->next=newNode; //头指针指向新起始节点
newNode->next=temp; //原起始节点的地址放到新起始节点的指针域中
m_iLength++;
return true ;
}
bool List::ListInsetTail(Node *pNode)
{
Node *currentNode=m_pList;
while(currentNode->next!=NULL) //遍历节点
{
currentNode=currentNode->next; //经过while循环后的currentNode就是尾节点
}
Node *newNode=new Node; //从堆中申请内存
if(newNode==NULL) //内存申请失败,返回false
{
return false;
}
newNode->data=pNode->data; //将要插入的节点的数据保存到新节点中
newNode->next=NULL; //因为新节点是要作为尾节点,所以指针域置空
currentNode->next=newNode; //原来的尾节点指向新尾节点
m_iLength++;
return true;
}
mian.cpp
#include"List.h"
#include"Node.h"
#include<iostream>
using namespace std;
void main()
{
Node temp;
Node node1,node2,node3,node4,node5;
node1.data=3;
node2.data=1;
node3.data=4;
node4.data=2;
node5.data=5;
List *pList=new List();
pList->ListInsetTail(&node1);
pList->ListInsetTail(&node2);
pList->ListInsetTail(&node3);
pList->ListInsetTail(&node4);
pList->ListInsetTail(&node5);
cout<<"顺序输出所有节点为: ";
pList->ListTraverse();
cout<<endl;
cout<<"链表的长度为:"<<pList->ListLength()<<endl;
cout<<"node2是第几个节点:"<<pList->LocateElem(&node2)<<endl;
pList->GetElem(3,&temp);
cout<<"第三个节点的数据是:"<<temp.data<<endl;
pList->PriorElem(&node2,&temp);
cout<<"node2节点的前一个节点的数据是:"<<temp.data<<endl;
pList->ListDelete(1,&temp);
cout<<"删除的节点的数据为:"<<temp.data<<endl;
cout<<"删除第二个节点后所有节点为:"<<endl;
pList->ListTraverse();
delete pList;
pList=NULL;
system("pause");
}