5勉强能用了,泪目

This commit is contained in:
iridiumR 2021-10-22 00:13:46 +08:00
parent 3783c65a76
commit 8ccaf99287
4 changed files with 442 additions and 0 deletions

24
ex5/ex5.cpp Normal file
View file

@ -0,0 +1,24 @@
#include "vec.cpp"
#include "list.cpp"
#include <time.h>
#include <stdio.h>
int main()
{
srand(time(NULL));
Vec v(54,2);
v.printall();
List l;
printf("¿ªÊ¼½¨Á¢Á´±í\n");
for(int i=0;i<=53;i++)
{
l.firstInsert(v.get(i));
printf("%d\n",l[0]);
}
return 0;
}

106
ex5/list.cpp Normal file
View file

@ -0,0 +1,106 @@
#include "list.h"
List::List()
{
_size = 0;
header->data = 0;
header->succ = trailer;
header->pred = NULL;
trailer->succ = NULL;
trailer->pred = header;
}
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)
{
Node ans = first();
for (int i = 0; i < _size; i++)
{
ans = ans->succ;
}
return ans;
}
int List::get(int i)
{
return find(i)->data;
}

72
ex5/list.h Normal file
View file

@ -0,0 +1,72 @@
#include <iostream>
#include <string>
#define Node node *
typedef struct node
{
int data;
Node pred;
Node succ;
} node;
class List
{
private:
Node header = new node;
Node trailer = new node;
int _size;
protected:
int clear();
public:
List();
Node first() { return header->succ; }
Node last() { return trailer->pred; }
void firstInsert(int New);
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 operator[](int i)
{
Node p = first();
while (0 < i--)
p = p->succ;
return p->data;
}
};
class PUKE
{
private:
std::string PU[54] = {
"1A", "1B", "1C", "1D",
"2A", "2B", "2C", "2D",
"3A", "3B", "3C", "3D",
"4A", "4B", "4C", "4D",
"5A", "5B", "5C", "5D",
"6A", "6B", "6C", "6D",
"7A", "7B", "7C", "7D",
"8A", "8B", "8C", "8D",
"9A", "9B", "9C", "9D",
"10A", "10B", "10C", "10D",
"JA", "JB", "JC", "JD",
"QA", "QB", "QC", "QD",
"KA", "KB"
"KC",
"KD",
"BlackJoker", "RadJoker"};
public:
std::string p(int a)
{
return PU[a];
}
};

240
ex5/vec.cpp Normal file
View file

@ -0,0 +1,240 @@
#include "vec.h"
Vec::Vec(int len, int mode)
{
_used = len - 1;
_len = len * 2;
_v = new int[_len];
// for (int i = 0; i <= _used; i++)
// _v[i] = i + 1;
if (mode == 1)
{
for (int i = 0; i <= _used; i++)
_v[i] = rand() % (2 * (_used + 1));
for (int i = 0; i <= _used; i++)
swap(i, rand() % (2 * (_used + 1)));
}
else if (mode == 2)
{
for (int i = 0; i <= _used; i++)
_v[i] = _used - i;
for (int i = 0; i <= _used; i++)
swap(i, rand() % ((_used + 1)));
}
}
int Vec::get(int a)
{
return _v[a];
}
void Vec::put(int a, int value)
{
_v[a] = value;
}
void Vec::swap(int a, int b)
{
int temp = _v[a];
_v[a] = _v[b];
_v[b] = temp;
}
void Vec::expand()
{
_len = _len * 2;
int *p = new int[_len];
for (int i = 0; i <= _used; i++)
p[i] = _v[i];
delete[] _v;
_v = p;
}
void Vec::shrink()
{
if (((double)_used / (double)_len) >= 0.25)
return;
_len = _len >> 1;
int *p = new int[_len];
for (int i = 0; i <= _used; i++)
p[i] = _v[i];
delete[] _v;
_v = p;
}
//位置 数值
int Vec::insert(int locate, int value)
{
if (locate == _used + 1)
{
_used++;
_v[_used] = value;
return 0;
}
if (locate < 0 || locate > _used)
return -1;
_used++;
if (_used >= _len)
expand();
for (int i = _used; i > locate; i--)
_v[i] = _v[i - 1];
_v[locate] = value;
return 0;
}
//位置 删除个数
int Vec::remove(int locate, int value)
{
if (locate < 0 || ((_used - value) < 0) || ((locate + value - 1) > _used))
return -1;
_used = _used - value;
for (int i = locate; i <= _used; i++)
_v[i] = _v[i + value];
shrink();
return value;
}
//删除某一元素
int Vec::remove_sorted(int value)
{
int j, i;
for (i = 0; i <= _used && _v[i] != value; i++)
;
for (j = 1; _v[i + j] == value; j++)
;
return remove(i, j);
}
int Vec::count(int value)
{
int j, i;
for (i = 0; i <= _used && _v[i] != value; i++)
;
for (j = 0; _v[i + j] == value && i + j <= _used; j++)
;
if (j == 0)
return -1;
return j;
}
int Vec::find(int value)
{
int i = 0;
for (i; i <= _used; i++)
{
if (_v[i] == value)
{
return i;
}
}
return -1;
}
int Vec::getlen()
{
return _len;
}
int Vec::getused()
{
return _used;
}
void Vec::printall()
{
for (int i = 0; i <= _used; i++)
printf("%d\n", _v[i]);
}
void Vec::bubbleSort()
{
int n = _used;
bool sorted = false;
while (!sorted)
{
sorted = true;
for (int i = 1; i <= n; i++)
{
if (_v[i - 1] > _v[i])
{
swap(i - 1, i);
sorted = false;
}
}
n--;
}
_sorted = true;
}
void Vec::mergeSort(int lo, int hi)
{
if (lo >= hi)
{
return;
}
int mi = lo + (hi - lo) / 2;
mergeSort(lo, mi);
mergeSort(mi + 1, hi);
merge(lo, mi, hi);
_sorted = true;
}
int Vec::search(int e)
{
if (_sorted == false)
return -1;
int i = 0;
for (i; i <= _used && _v[i] <= e; i++)
;
return i - 1;
}
void Vec::merge(int lo, int mi, int hi)
{
int i = lo, j = mi + 1, k = 0;
int *temp = new int[hi - lo + 1];
while (i <= mi && j <= hi)
{
if (_v[i] <= _v[j])
temp[k++] = _v[i++];
else
temp[k++] = _v[j++];
}
while (i <= mi)
temp[k++] = _v[i++];
while (j <= hi)
temp[k++] = _v[j++];
for (i = lo, k = 0; i <= hi; i++, k++)
_v[i] = temp[k];
delete[] temp;
}