#include <iostream>
using namespace std;
typedef struct lnode
{
int data;
struct lnode* nxt;
}lnode, * linklist;
class list
{
public:
list();
~list();
list(const list& li);
void init(int n);
bool insert(int index, int elem);
bool Delete(int elem);
bool remove(int elem);
void print();
void clean();
bool empty();
int size();
list operator=(const list& L);
friend ostream& operator<<(ostream& output, list L);
friend istream& operator>>(istream& input, list& L);
private:
linklist l;
int lenght;
};
list::list()
{
l = new lnode;
l->nxt = NULL;
lenght = 0;
}
list::list(const list& L)
{
l = new lnode;
l->nxt = NULL;
lenght = 0;
linklist r = l, t;
linklist p;
p = new lnode;
p = L.l->nxt;
while (p != NULL)
{
t = new lnode;
t->data = p->data;
t->nxt = NULL;
r->nxt = t;
r = t;
p = p->nxt;
++lenght;
}
r->nxt = NULL;
}
list::~list()
{
clean();
}
void list::init(int n)
{
linklist r = l;
for (int i = 0; i < n; i++)
{
linklist p = new lnode;
int temp;
cin >> temp;
p->nxt = NULL;
p->data = temp;
r->nxt = p;
r = p;
}
r->nxt = NULL;
lenght = n;
}
bool list::insert(int index, int elem)
{
int cnt = 1;
linklist p = l;
while (cnt < index)
p = p->nxt, cnt++;
linklist r = new lnode;
r->data = elem;
r->nxt = p->nxt;
p->nxt = r;
++lenght;
return true;
}
bool list::Delete(int elem)
{
linklist p = new lnode;
p = l;
while (p->nxt && p->nxt->data != elem)
p = p->nxt;
if (p->nxt)
{
linklist t = new lnode;
t = p->nxt;
p->nxt = p->nxt->nxt;
delete t;
--lenght;
return true;
}
return false;
}
bool list::remove(int index)
{
if (index > lenght)
{
cout << "字符串中无此元素" << endl;
return false;
}
linklist p = l;
int cnt = 1;
while (cnt++ < index)
p = p->nxt;
linklist t = p->nxt;
p->nxt = t->nxt;
delete t;
return true;
}
void list::print()
{
linklist p = l->nxt;
while (p)
{
cout << p->data << " ";
p = p->nxt;
}
cout << endl;
}
void list::clean()
{
if (lenght == 0)
return;
linklist p = l;
linklist t;
while (p)
{
t = p;
p = p->nxt;
delete t;
}
lenght = 0;
}
bool list::empty()
{
if (!lenght)
return true;
return false;
}
int list::size()
{
return lenght;
}
list list::operator=(const list& L)
{
list temp;
linklist r = temp.l, t;
linklist p;
p = new lnode;
p = L.l->nxt;
while (p != NULL)
{
t = new lnode;
t->data = p->data;
t->nxt = NULL;
r->nxt = t;
r = t;
p = p->nxt;
++temp.lenght;
}
r->nxt = NULL;
return temp;
}
ostream& operator<<(ostream& output, list L)
{
linklist p = L.l->nxt;
while (p)
{
output << p->data << " ";
p = p->nxt;
}
output << endl;
return output;
}
istream& operator>>(istream& input, list& L)
{
int n;
linklist r = L.l;
cout << "链表长度 : " << endl;
cin >> n;
for (int i = 0; i < n; i++)
{
linklist p = new lnode;
int temp;
input >> temp;
p->nxt = NULL;
p->data = temp;
r->nxt = p;
r = p;
}
r->nxt = NULL;
L.lenght = n;
return input;
}
int main()
{
list a;
cin >> a;
list b = a;
cout << b << endl;
return 0;
}