#include"List.h"
#include
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::ListInsert(int i,Node *pNode)
{
if(im_iLength){
return false;
}
Node *currentNode=m_pList;
for(int k=0;k<i;k++)
{
currentNode=currentNode->next;
}
Node *newNode=new Node;
newNode->data=pNode->data;
newNode->next=currentNode->next;
currentNode->next=newNode;
m_iLength++;
return true;
}
bool List::ListDelete(int i,Node *pNode)
{
if(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::ListInsertHead(Node *pNode){
Node *temp=m_pList->next;
Node *newNode=new Node;
newNode->data=pNode->data;
m_pList->next=newNode;
newNode->next=temp;
return true;
}
void List::Tranverse()
{
Node*currentNode=m_pList;
while(currentNode->next!=NULL)
{
currentNode=currentNode->next;
cout<data<<endl;
}
}
int main()
{
List *pList=new List();
Node nodel;
nodel.data=3;
pList->ListInsert(0,&nodel);
Node node5;
node5.data=7;
pList->ListInsert(1,&node5);
cout<<"未删除节点前"<<endl;
pList->Tranverse();
Node temp;
pList->ListDelete(0,&temp);
cout<<"删除节点后"<<endl;
pList->Tranverse();
cout<<"被删除的节点是"<<&temp<<endl;
system("pause");
}