代码示例:
#ifndef NODE_H
#define NODE_H
class Node
{
public:
int data;//数据域
Node*next;//指针域
void printNode();
};
#endif
#include"Node.h"
#include<iostream>
using namespace std;
void Node::printNode()
{
cout<<data<<endl;
}
#ifndef LIST_H
#define 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);//寻找第一个满足e的数据元素的位序
bool PriorElem(Node*pCurrentNode,Node*pPreNode);//获取指定元素的前驱
bool NextElem(Node*pCurrentElem,Node*pNextElem);//获取指定元素的后驱
void ListTraverse();//遍历线性表
bool ListInsert(int i,Node*pNode);//在第i个位置插入元素
bool ListDelete(int i,Node*pNode);//在第i个位置删除元素
bool ListInsertHead(Node*pNode);//从起点插入节点
bool ListInsertTail(Node*pNode);//从末尾插入节点
private:
Node *m_pList;
int m_iLength;
};
#endif
#include"List.h"
#include<iostream>
using namespace std;
List::List ()
{
m_pList=new Node;
m_pList->data=0;
m_pList->next=NULL;
m_iLength=0;
}
bool List::ListEmpty()
{
return m_iLength==0? true:false;
}
int List::ListLength()
{
return m_iLength;
}
void List::ClearList()
{
Node*currentNode=m_pList->next;
while(currentNode!=NULL)//逐一清除
{
Node*temp=currentNode->next;
delete currentNode;
currentNode=temp;
}
m_pList->next=NULL;
}
List::~List()
{
ClearList();
delete m_pList;
m_pList=NULL;
}
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++)
{
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;
}
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;
}
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;
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;
}
void List::ListTraverse()
{
Node*currentNode=m_pList;
while(currentNode->next!=NULL)
{
currentNode=currentNode->next;
currentNode->printNode();
}
}
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include"Node.h"
#include"List.h"
using namespace std;
/*
线性表——单链表
BOOL InitList(List**list);//创建线性表
void DestroyList(List*list);//销毁线性表
void ClearList(List*list);//清空线性表
BOOL ListEmpty(List*list);//判断线性表是否为空
int ListLength(List*list);//获取线性表的长度
BOOL GetElem(List*list,int i,Elem*e);//获取指定元素
int LocateElem(List*list,Elem*e);//寻找第一个满足e的数据元素的位序
BOOL PriorElem(List*list,Elem*currentElem,Elem*preElem);//获取指定元素的前驱
BOOL NextElem(List*list,Elem*currentElem,Elem*NextElem);//获取指定元素的后驱
BOOL ListInsert(List*list,int i,Elem*e);//在第i个位置插入元素
BOOL LIstDelete(LIst*list,int i,Elem*e);//在第i个位置删除元素
void ListTraverse(LIst*list);//遍历线性表
*/
int main(void)
{
Node node1;
node1.data=3;
Node node2;
node2.data=2;
Node node3;
node3.data=9;
Node node4;
node4.data=7;
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->ListTraverse();
cout<<endl;
/*pList->ListInsert(1,&node5);
pList->ListTraverse();
cout<<endl;*/
/*pList->ListDelete(1,&temp);
pList->ListTraverse();
cout<<"Delete temp="<<temp.data<<endl;
cout<<endl;*/
/*pList->GetElem(3,&temp);
cout<<"get temp="<<temp.data<<endl;
cout<<endl;*/
pList->PriorElem(&node3,&temp);
cout<<"prior temp="<<temp.data<<endl;
cout<<endl;
pList->NextElem(&node3,&temp);
cout<<"next temp="<<temp.data<<endl;
delete pList;
pList=NULL;
system("pause");
return 0;
}
打印结果: