5好用了
This commit is contained in:
parent
2e8fe9fbb8
commit
06731225c0
2 changed files with 31 additions and 25 deletions
41
ex5/list.cpp
41
ex5/list.cpp
|
@ -43,7 +43,7 @@ Node List::succInsert(Node Old, int New)
|
|||
T->pred = Old;
|
||||
T->succ = Old->succ;
|
||||
Old->succ = T;
|
||||
T->pred->succ = T;
|
||||
T->succ->pred = T;
|
||||
_size++;
|
||||
return T;
|
||||
}
|
||||
|
@ -85,7 +85,7 @@ Node List::succInsertN(Node Old, Node New)
|
|||
T->pred = Old;
|
||||
T->succ = Old->succ;
|
||||
Old->succ = T;
|
||||
T->pred->succ = T;
|
||||
T->succ->pred = T;
|
||||
_size++;
|
||||
return T;
|
||||
}
|
||||
|
@ -144,25 +144,26 @@ void List::removeN(Node i)
|
|||
_size--;
|
||||
}
|
||||
|
||||
Node List::searchN(int value, int n, Node p)
|
||||
{
|
||||
while (0 <= n--)
|
||||
if (((p = p->pred)->data) <= value)
|
||||
break;
|
||||
return p;
|
||||
}
|
||||
|
||||
void List::sort()
|
||||
{
|
||||
int sorted = 0;
|
||||
Node p;
|
||||
for (int i = 0; i <= _size - 1 - sorted; i++)
|
||||
for (int j = i; j <= _size - 1 - sorted; j++)
|
||||
{
|
||||
int max = 0;
|
||||
Node maxN;
|
||||
Node p=first();
|
||||
for (int r = 0; r < _size; r++)
|
||||
{
|
||||
succInsert(searchN(p->data,r,p),p->data);
|
||||
p=p->succ;
|
||||
removeN(p->pred);
|
||||
}
|
||||
}
|
||||
|
||||
p = find(j);
|
||||
if (max < p->data)
|
||||
{
|
||||
max = p->data;
|
||||
maxN = p;
|
||||
}
|
||||
|
||||
maxN->data = p->data;
|
||||
p->data = max;
|
||||
sorted++;
|
||||
}
|
||||
int List::valid(Node p)
|
||||
{
|
||||
return p && (trailer != p) && (header != p);
|
||||
}
|
15
ex5/list.h
15
ex5/list.h
|
@ -17,6 +17,12 @@ private:
|
|||
int _size;
|
||||
|
||||
protected:
|
||||
Node firstInsertN(Node New);
|
||||
Node lastInsertN(Node New);
|
||||
Node succInsertN(Node old, Node ne);
|
||||
Node predInsertN(Node old, Node ne);
|
||||
void removeN(Node i);
|
||||
Node searchN(int value,int n,Node p);
|
||||
|
||||
public:
|
||||
int getSize()
|
||||
|
@ -32,14 +38,13 @@ public:
|
|||
void lastInsert(int New);
|
||||
Node succInsert(Node Old, int New);
|
||||
Node predInsert(Node Old, int New);
|
||||
Node firstInsertN(Node New);
|
||||
Node lastInsertN(Node New);
|
||||
Node succInsertN(Node old, Node ne);
|
||||
Node predInsertN(Node old, Node ne);
|
||||
|
||||
int get(int i);
|
||||
Node find(int i);
|
||||
int remove(int i);
|
||||
void removeN(Node i);
|
||||
Node searchN(int value,int n);
|
||||
int valid(Node p);
|
||||
|
||||
|
||||
void sort();
|
||||
|
||||
|
|
Reference in a new issue