头文件:list.h
#include<iostream>
#include <stdlib.h>
struct Node
{
int Data;
Node*next;
};
class list
{
Node*head;
public:
list(){head=NULL;}
void insertlist(int aDate,int bDate);
void Deletelist(int aDate);
void Outputlist();
Node*Gethead()
{return head;}
};
函数实现文件 list.cpp
#include "list.h"
#include<iostream>
using namespace std;
void list::Outputlist()
{
Node*current=head;
while(current!=NULL)
{
cout<<current->Data<<" ";
current=current->next;
}
cout<<endl;
}
void list::insertlist(int aDate,int bDate)
{
Node*p,*q,*s;
s=(Node*)new(Node);
s->Data=bDate;
p=head;
if(head==NULL)
{
head=s;
s->next=NULL;
}
else if(p->Data==aDate)
{
s->next=p;
head=s;
}
else
{
while(p->Data!=aDate&&p->next!=NULL)
{
q=p;
p=p->next;
}
if(p->Data==aDate)
{
q->next=s;
s->next=p;
}
else
{
p->next=s;
s->next=NULL;
}
}
}
void list::Deletelist(int aDate)
{
Node*p,*q;
p=head;
if(p==NULL)
return;
if(p->Data==aDate)
{
head=p->next;
delete p;
}
else
{
while(p->Data!=aDate&&p->next!=NULL)
{
q=p;
p=p->next;
}
if(p->Data==aDate)
{
q->next=p->next;
delete p;
}
}
}
主函数 mian.cpp
#include<iostream>
#include "list.h"
using namespace std;
int main(){
list l;
l.insertlist(1,1);
l.insertlist(1,2);
l.insertlist(1,4);
l.insertlist(1,8);
l.Outputlist();
return 0;
}