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;
|
2021-10-22 17:10:00 +00:00
|
|
|
T->pred->succ = T;
|
2021-10-21 16:13:46 +00:00
|
|
|
_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;
|
2021-10-23 02:54:30 +00:00
|
|
|
T->succ->pred = T;
|
2021-10-21 16:13:46 +00:00
|
|
|
_size++;
|
|
|
|
return T;
|
|
|
|
}
|
2021-10-27 15:40:44 +00:00
|
|
|
int List::firstInsert(int New)
|
2021-10-21 16:13:46 +00:00
|
|
|
{
|
|
|
|
Node T = new node;
|
|
|
|
T->data = New;
|
|
|
|
T->pred = header;
|
|
|
|
T->succ = header->succ;
|
|
|
|
header->succ = T;
|
2021-10-22 17:10:00 +00:00
|
|
|
T->succ->pred = T;
|
2021-10-21 16:13:46 +00:00
|
|
|
_size++;
|
2021-10-27 15:40:44 +00:00
|
|
|
return New;
|
2021-10-21 16:13:46 +00:00
|
|
|
}
|
2021-10-27 15:40:44 +00:00
|
|
|
int List::lastInsert(int New)
|
2021-10-21 16:13:46 +00:00
|
|
|
{
|
|
|
|
Node T = new node;
|
|
|
|
T->data = New;
|
|
|
|
T->succ = trailer;
|
|
|
|
T->pred = trailer->pred;
|
|
|
|
trailer->pred = T;
|
2021-10-22 17:10:00 +00:00
|
|
|
T->pred->succ = T;
|
2021-10-21 16:13:46 +00:00
|
|
|
_size++;
|
2021-10-27 15:40:44 +00:00
|
|
|
return New;
|
2021-10-21 16:13:46 +00:00
|
|
|
}
|
|
|
|
Node List::predInsertN(Node Old, Node New)
|
|
|
|
{
|
|
|
|
Node T = new node;
|
|
|
|
T = New;
|
|
|
|
T->succ = Old;
|
|
|
|
T->pred = Old->pred;
|
|
|
|
Old->pred = T;
|
2021-10-22 17:10:00 +00:00
|
|
|
T->pred->succ = T;
|
2021-10-21 16:13:46 +00:00
|
|
|
_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;
|
2021-10-23 02:54:30 +00:00
|
|
|
T->succ->pred = T;
|
2021-10-21 16:13:46 +00:00
|
|
|
_size++;
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node List::firstInsertN(Node New)
|
|
|
|
{
|
|
|
|
Node T = new node;
|
|
|
|
T = New;
|
|
|
|
T->pred = header;
|
|
|
|
T->succ = header->succ;
|
|
|
|
header->succ = T;
|
2021-10-22 17:10:00 +00:00
|
|
|
T->pred->succ = T;
|
2021-10-21 16:13:46 +00:00
|
|
|
_size++;
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
Node List::lastInsertN(Node New)
|
|
|
|
{
|
|
|
|
Node T = new node;
|
|
|
|
T = New;
|
|
|
|
T->succ = trailer;
|
|
|
|
T->pred = trailer->pred;
|
|
|
|
trailer->pred = T;
|
2021-10-22 17:10:00 +00:00
|
|
|
T->pred->succ = T;
|
2021-10-21 16:13:46 +00:00
|
|
|
_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)
|
|
|
|
{
|
|
|
|
(i->pred)->succ = i->succ;
|
|
|
|
(i->succ)->pred = i->pred;
|
|
|
|
delete i;
|
|
|
|
_size--;
|
|
|
|
}
|
|
|
|
|
2021-10-23 02:54:30 +00:00
|
|
|
Node List::searchN(int value, int n, Node p)
|
2021-10-22 11:25:46 +00:00
|
|
|
{
|
2021-10-23 02:54:30 +00:00
|
|
|
while (0 <= n--)
|
|
|
|
if (((p = p->pred)->data) <= value)
|
|
|
|
break;
|
|
|
|
return p;
|
|
|
|
}
|
2021-10-22 11:25:46 +00:00
|
|
|
|
2021-10-23 02:54:30 +00:00
|
|
|
void List::sort()
|
|
|
|
{
|
|
|
|
Node p=first();
|
|
|
|
for (int r = 0; r < _size; r++)
|
|
|
|
{
|
|
|
|
succInsert(searchN(p->data,r,p),p->data);
|
|
|
|
p=p->succ;
|
|
|
|
removeN(p->pred);
|
|
|
|
}
|
|
|
|
}
|
2021-10-22 11:25:46 +00:00
|
|
|
|
2021-10-23 02:54:30 +00:00
|
|
|
int List::valid(Node p)
|
|
|
|
{
|
|
|
|
return p && (trailer != p) && (header != p);
|
2021-10-21 16:13:46 +00:00
|
|
|
}
|