#include"iostream"
#include"malloc.h"
using namespace std;
class list
{
private:
int data;
list * next;
public:
LNode(){};
void InitLNode(list *&L){
L=(list *)malloc(sizeof(list));
L->next=NULL;
}
void insert(list *&,int);
bool delete1(list *&,int);
void print(list *);
void Destroylist(list *&);
~list(){};
};
void list::Destroylist(list *&L){
list* p,*s;
p=L->next;
while(p!=NULL)
{
s=p->next;
free(p);
p=s;
};
free(p);
cout << "list is delete." << endl;
}
void list::insert(list *&L,int e)
{
list* p;
p=(list *)malloc(sizeof(list));
p->data=e;
p->next=L->next;
L->next=p;
}
bool list::delete1(list *&L,int i)
{
list *p,*q;
int j=0;
p=L;
while(j<i-1&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
{
return false;
}
else
{
q=p->next;
if(q==NULL)
return false;
p->next=q->next;
free(q);
return true;
}
}
void list::print(list *L)
{
list *p=L->next;
while(p!=NULL)
{
cout << p->data << " " ;
p=p->next;
}
cout << endl;
}
void main()
{
list *L;
L->Initlist(L);
L->insert(L,5);
L->insert(L,4);
L->insert(L,3);
L->print(L);
L->delete1(L,1);
L->print(L);
L->Destroylist(L);
}