编译时没问题,运行时就有问题了。求高手解答。 单链表.exe 中的 0x00ad1fc0 处有未经处理的异常: 0xC0000005: 读取位置 0x00000000 时发生访问冲突
demo.cpp
#include "list.h"
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(void)
{
Node node1, node2;
node1.data=3;
node2.data=4;
List *plist=new List();
// plist->ListInsertHead(&node1);
// plist->ListInsertHead(&node2);//把这两行注释结果是第一张图 ,不注释的话结果是第二张图。
plist->ListTraverse();
delete plist;
plist=NULL;
system("pause");
return 0;
}
Node.cpp
#include "Node.h"
#include<iostream>
using namespace std;
void Node::printNode()
{
cout<<data<<endl;
}
List.cpp
#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;
//cout<<"::List()"<<endl;
}
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:: GetElem(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;
}
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 *tempNode =NULL;
Node *currentNode = m_pList;
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();
}
}
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;
m_iLength++;
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::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;
}