大神帮我看看简单的链表哪里错了
#include <iostream>#include "windows.h"
using namespace std;
struct Node
{
int data;
Node* next;
};
class List
{
public:
Node* head;
public:
List(){head=NULL;};
void insertList(int aData,int bData);
void deleteList(int aData);
void outputList();
Node* getHead(){return head;}
};
void List::insertList(int aData, int bData)
{
Node *p,*q,*s;
s=(Node*)new(Node);
s->data=bData;
p=head;
q=head;
if(head==NULL)
{
head=s;
s->next=NULL;
}
else
if(p->data==aData)
{
p->next=s;
head =p;
cout<<"dd"<<endl;
}
else
{
///////////////以下为错误出现的地方,我是想在某个节点后面插入一个新的节点,但是老出现错误
结果我会贴在后面/////////////////////////
while(p->data!=aData&&p->next!=NULL)
{
p=p->next;
}
if(p->data==aData)
{
s->next=p->next;
p->next=s;
}
else
{
return;
}
}
}
void List::deleteList(int aData)
{
Node *p,*q,*s;
/*s=(Node*)new(Node);
s->data=bData;*/
p=head;
if(head==NULL)
return;
else
if(p->data==aData)
{
head=p->next;
delete p;
cout<<"aa"<<endl;
}
else
while(p->data!=aData&&p->next!=NULL)
{
q=p;
p=p->next;
}
if(p->data==aData)
{
q->next=p->next;
delete p;
}
}
void List::outputList()
{
Node* current=head;
while(current!=NULL)
{
cout<<current->data<<" ";
current=current->next;
}
cout<<endl;
};
/////////////以下为主函数////////////////
#include <iostream>
#include "List.h"
using namespace std;
void main()
{
List A,B;
int data[10]={1,2,3,4,5,6,7,8,9,10};
A.insertList(0,data[0]);
A.insertList(data[0],data[1]);
A.insertList(data[1],data[2]);
A.insertList(data[2],data[3]);
A.insertList(data[3],data[4]);
cout<<"aaa"<<endl;
A.deleteList(data[2]);
cout<<"ddd"<<endl;
A.outputList();
}
下面是运行结果