#include<iostream>
using namespace std
;
class link
{
friend class list;
link
*next
;
int x
;
public:
link():x(0)
{
}
link(int a
):x(a
)
{
}
void disp()
{
cout
<<"x="<<x
<<endl
;
}
};
class list
{
link
*first
;
link
*last
;
public:
list()
{
first
=last
=new link
;
}
~list()
{
Clear();
delete first
;
}
void Insert(link s
)
{
link
*p
=first
;
first
=new link
;
*first
=s
.x
;
first
->next
=p
;
}
void Append(link s
)
{
link
*p
=last
;
last
=new link
;
*p
=s
.x
;
p
->next
=last
;
}
void Delete()
{
if(first
==last
);
else
{
link
*p
=first
->next
;
delete first
;
first
=p
;
}
}
void Remove()
{
if(first
==last
);
else if(first
->next
==last
) Delete();
else
{
link
*p
=first
,*q
;
while(p
!=last
)
{
q
=p
;
p
=p
->next
;
}
delete p
;
q
->next
=last
;
}
}
void Clear()
{
link
*p
=first
,*q
;
while(p
!=last
)
{
q
=p
->next
;
delete p
;
p
=q
;
}
first
=last
;
}
void disp()
{
link
*p
=first
;
while(p
!=last
)
{
p
->disp();
p
=p
->next
;
}
}
};
int main()
{
list x
;
x
.Insert(link(1));
x
.Insert(link(2));
x
.Append(link(3));
x
.Insert(link(4));
x
.disp();
}
转载请注明原文地址: https://mac.8miu.com/read-501580.html