5好用了
This commit is contained in:
parent
2e8fe9fbb8
commit
06731225c0
2 changed files with 31 additions and 25 deletions
39
ex5/list.cpp
39
ex5/list.cpp
|
@ -43,7 +43,7 @@ Node List::succInsert(Node Old, int New)
|
||||||
T->pred = Old;
|
T->pred = Old;
|
||||||
T->succ = Old->succ;
|
T->succ = Old->succ;
|
||||||
Old->succ = T;
|
Old->succ = T;
|
||||||
T->pred->succ = T;
|
T->succ->pred = T;
|
||||||
_size++;
|
_size++;
|
||||||
return T;
|
return T;
|
||||||
}
|
}
|
||||||
|
@ -85,7 +85,7 @@ Node List::succInsertN(Node Old, Node New)
|
||||||
T->pred = Old;
|
T->pred = Old;
|
||||||
T->succ = Old->succ;
|
T->succ = Old->succ;
|
||||||
Old->succ = T;
|
Old->succ = T;
|
||||||
T->pred->succ = T;
|
T->succ->pred = T;
|
||||||
_size++;
|
_size++;
|
||||||
return T;
|
return T;
|
||||||
}
|
}
|
||||||
|
@ -144,25 +144,26 @@ void List::removeN(Node i)
|
||||||
_size--;
|
_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()
|
void List::sort()
|
||||||
{
|
{
|
||||||
int sorted = 0;
|
Node p=first();
|
||||||
Node p;
|
for (int r = 0; r < _size; r++)
|
||||||
for (int i = 0; i <= _size - 1 - sorted; i++)
|
|
||||||
for (int j = i; j <= _size - 1 - sorted; j++)
|
|
||||||
{
|
{
|
||||||
int max = 0;
|
succInsert(searchN(p->data,r,p),p->data);
|
||||||
Node maxN;
|
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;
|
int _size;
|
||||||
|
|
||||||
protected:
|
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:
|
public:
|
||||||
int getSize()
|
int getSize()
|
||||||
|
@ -32,14 +38,13 @@ public:
|
||||||
void lastInsert(int New);
|
void lastInsert(int New);
|
||||||
Node succInsert(Node Old, int New);
|
Node succInsert(Node Old, int New);
|
||||||
Node predInsert(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);
|
int get(int i);
|
||||||
Node find(int i);
|
Node find(int i);
|
||||||
int remove(int i);
|
int remove(int i);
|
||||||
void removeN(Node i);
|
Node searchN(int value,int n);
|
||||||
|
int valid(Node p);
|
||||||
|
|
||||||
|
|
||||||
void sort();
|
void sort();
|
||||||
|
|
||||||
|
|
Reference in a new issue