2021-10-21 16:13:46 +00:00
|
|
|
#include "list.h"
|
|
|
|
|
|
|
|
List::List()
|
|
|
|
{
|
|
|
|
_size = 0;
|
|
|
|
|
|
|
|
header->data = 0;
|
|
|
|
header->succ = trailer;
|
|
|
|
header->pred = NULL;
|
|
|
|
|
|
|
|
trailer->succ = NULL;
|
|
|
|
trailer->pred = header;
|
|
|
|
}
|
|
|
|
|
2021-10-22 11:25:46 +00:00
|
|
|
List::~List()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
delete trailer;
|
|
|
|
delete header;
|
|
|
|
}
|
|
|
|
|
|
|
|
void List::clear()
|
|
|
|
{
|
|
|
|
while (0 < _size)
|
|
|
|
remove(0);
|
|
|
|
}
|
|
|
|
|
2021-10-21 16:13:46 +00:00
|
|
|
Node List::predInsert(Node Old, int New)
|
|
|
|
{
|
|
|
|
Node T = new node;
|
|
|
|
T->data = New;
|
|
|
|
T->succ = Old;
|
|
|
|
T->pred = Old->pred;
|
|
|
|
Old->pred = T;
|
|
|
|
_size++;
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
Node List::succInsert(Node Old, int New)
|
|
|
|
{
|
|
|
|
Node T = new node;
|
|
|
|
T->data = New;
|
|
|
|
T->pred = Old;
|
|
|
|
T->succ = Old->succ;
|
|
|
|
Old->succ = T;
|
|
|
|
_size++;
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
void List::firstInsert(int New)
|
|
|
|
{
|
|
|
|
Node T = new node;
|
|
|
|
T->data = New;
|
|
|
|
T->pred = header;
|
|
|
|
T->succ = header->succ;
|
|
|
|
header->succ = T;
|
|
|
|
_size++;
|
|
|
|
}
|
|
|
|
void List::lastInsert(int New)
|
|
|
|
{
|
|
|
|
Node T = new node;
|
|
|
|
T->data = New;
|
|
|
|
T->succ = trailer;
|
|
|
|
T->pred = trailer->pred;
|
|
|
|
trailer->pred = T;
|
|
|
|
_size++;
|
|
|
|
}
|
|
|
|
Node List::predInsertN(Node Old, Node New)
|
|
|
|
{
|
|
|
|
Node T = new node;
|
|
|
|
T = New;
|
|
|
|
T->succ = Old;
|
|
|
|
T->pred = Old->pred;
|
|
|
|
Old->pred = T;
|
|
|
|
_size++;
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
Node List::succInsertN(Node Old, Node New)
|
|
|
|
{
|
|
|
|
Node T = new node;
|
|
|
|
T = New;
|
|
|
|
T->pred = Old;
|
|
|
|
T->succ = Old->succ;
|
|
|
|
Old->succ = T;
|
|
|
|
_size++;
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node List::firstInsertN(Node New)
|
|
|
|
{
|
|
|
|
Node T = new node;
|
|
|
|
T = New;
|
|
|
|
T->pred = header;
|
|
|
|
T->succ = header->succ;
|
|
|
|
header->succ = T;
|
|
|
|
_size++;
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
Node List::lastInsertN(Node New)
|
|
|
|
{
|
|
|
|
Node T = new node;
|
|
|
|
T = New;
|
|
|
|
T->succ = trailer;
|
|
|
|
T->pred = trailer->pred;
|
|
|
|
trailer->pred = T;
|
|
|
|
_size++;
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
Node List::find(int i)
|
|
|
|
{
|
2021-10-22 11:25:46 +00:00
|
|
|
Node p = first();
|
|
|
|
while (0 < i--)
|
2021-10-21 16:13:46 +00:00
|
|
|
{
|
2021-10-22 11:25:46 +00:00
|
|
|
p = p->succ;
|
2021-10-21 16:13:46 +00:00
|
|
|
}
|
2021-10-22 11:25:46 +00:00
|
|
|
|
|
|
|
return p;
|
2021-10-21 16:13:46 +00:00
|
|
|
}
|
|
|
|
int List::get(int i)
|
|
|
|
{
|
|
|
|
return find(i)->data;
|
2021-10-22 11:25:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int List::remove(int i)
|
|
|
|
{
|
|
|
|
|
|
|
|
Node T = find(i);
|
|
|
|
int ans = T->data;
|
|
|
|
removeN(T);
|
|
|
|
return ans;
|
|
|
|
}
|
|
|
|
|
|
|
|
void List::removeN(Node i)
|
|
|
|
{
|
|
|
|
printf("first3:%d\n",first()->data);
|
|
|
|
printf("last%d\n",last()->data);
|
|
|
|
(i->pred)->succ = i->succ;
|
|
|
|
printf("first3.5:%d\n",first()->data);
|
|
|
|
printf("last%d\n",last()->data);
|
|
|
|
(i->succ)->pred = i->pred;
|
|
|
|
printf("first4:%d\n",first()->data);
|
|
|
|
printf("last%d\n",last()->data);
|
|
|
|
delete i;
|
|
|
|
printf("first5:%d\n",first()->data);
|
|
|
|
_size--;
|
|
|
|
}
|
|
|
|
|
|
|
|
void List::sort()
|
|
|
|
{
|
|
|
|
// int sorted_size=0;
|
|
|
|
// int unsorted_size=_size-1;
|
|
|
|
// for(int i=0;i<unsorted_size;i++)
|
|
|
|
// {
|
|
|
|
// Node maxN;
|
|
|
|
// int max=0;
|
|
|
|
|
|
|
|
// Node T=find(0);
|
|
|
|
// printf("OK\n");
|
|
|
|
// maxN->data=T->data;
|
|
|
|
// T->data=max;
|
|
|
|
|
|
|
|
// sorted_size++;
|
|
|
|
// unsorted_size--;
|
|
|
|
// }
|
|
|
|
// Node maxN;
|
|
|
|
// Node p=trailer->pred;
|
|
|
|
// int max;
|
|
|
|
// for(int r=0;r<_size;r++)
|
|
|
|
// {
|
|
|
|
// for(int j=0;j<=r;j++)
|
|
|
|
// {
|
|
|
|
// printf("j%d\n",j);
|
|
|
|
// Node T =find(j);
|
|
|
|
// if(max>T->data)
|
|
|
|
// {
|
|
|
|
// max=T->data;
|
|
|
|
// maxN=T;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// predInsert(find(),)
|
|
|
|
// }
|
2021-10-21 16:13:46 +00:00
|
|
|
}
|