c++链表出错

来源:百度知道 编辑:UC知道 时间:2024/06/10 05:21:14
#include "stdafx.h"
#include<iostream>
using namespace std;

class list
{typedef struct node
{int Date;
node *next;
}Node;
Node *head;
public:
list(){head=NULL;}
void charu(int aDate,int bDate);
void shanchu(int aDate);
void shuchu();
Node *Gethead(){return head;}
};
void list::shuchu(){
Node *p=head;
while(p->Date!=NULL)
{cout<<p->Date<<" "<<endl;
p=p->next;
}
}
void list::shanchu(int aDate){
Node *p,*q;
p=head;
if(p==NULL)
return;
if(p->Date==aDate)
{head=p->next;delete p;
}
else
{while(p->Date!=aDate&&p->next!=NULL)
{q=p;p=p->next;}
if(p->Date==aDate)
{q->next=p->next;delete p;}
}
}
void list::charu(int aDate,int bDate){
Node *p,*q,*b;
b=new(list::Node);
p=head;<

就少了个else

void list::charu(int aDate,int bDate){
Node *p,*q,*b;
b=new(list::Node);
p=head;
b->Date=bDate;
if(head==NULL)
{head=b;b->next=NULL;} //此处执行后应退出函数

else if(p->Date==b->Date) //前面加了个else
{b->next=p;head=b;}
else
{
while(p->Date!=aDate&&p->next!=NULL)
{q=p;p=p->next;}
if(p->Date==aDate)
{q->next=b;b->next=p;}
else
{p->next=b;b->next=NULL;}
}
}