无聊数据结构小作业
#线性表
#include<iostream>
#include<algorithm>
using namespace std;
class LinearList_for_int {
private:
int n;
int *arr = new int[n];
int tail;
public:
LinearList_for_int(int len) {
n = len;
tail = 0;
}
void show() {
for (int i = 0; i < tail; i++)cout << arr[i] << ' ';
}
int len() {
return tail;
}
bool SetItem(int pos, int value) {
pos+=1;
if (pos > tail || pos < 0)return false;
arr[pos - 1] = value;
return true;
}
bool Remove_by_pos(int pos) {
pos+=1;
if (pos >= tail || pos < 0)return false;
for (int i = pos; i < tail; i++) {
arr[i - 1] = arr[i];
}
tail--;
return true;
}
bool append(int value) {
if (tail == n)return false;
arr[tail] = value;
tail++;
return true;
}
bool insert(int pos, int value) {
pos+=1;
if (pos > tail || pos < 0)return false;
if (tail == n)return false;
for (int i = tail; i >= pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos - 1] = value;
tail++;
return true;
}
int check(int value){
int i;
for(i=0;i<tail;i++)
if(value==arr[i])
return i;
if(i==tail)return -1;
}
};
int main() {
LinearList_for_int temp(110);
int n; cin >> n;
for (int i = 1; i <= n; i++) {
int x; cin >> x;
temp.append(x);
}
int m; cin >> m;
temp.Remove_by_pos(m);
temp.show();
return 0;
}
#双向栈
#include<iostream>
#include<algorithm>
using namespace std;
class b_sides_stack{
private:
int head=1, tail=10;
int arr[11];
public:
bool push_in(int x){
while(head<=tail){
if(x%2){
arr[tail]=x;
tail--;
}else{
arr[head]=x;
head++;
}
return true;
}
}
bool pop(){
while(head>1){
head--;
cout<<arr[head]<<' ';
}
while(tail<10){
tail++;
cout<<arr[tail]<<' ';
}
return true;
}
};
int main(){
b_sides_stack bstack;
for(int i=1; i<=10; i++){
int t;
cin>>t;
bstack.push_in(t);
}
for(int i=1; i<=10; i++)bstack.pop();
return 0;
}
#循环队列
#include<iostream>
using namespace std;
class round_queue{
private:
int n;
int *arr=new int[n];
int hh, tt;
int size=0;
public:
round_queue(int s){
this->n=s;
this->hh=0;
this->tt=-1;
}
bool add(int k){
if(size!=n){
if(tt<n-1){
arr[++tt]=k;
size++;
}else{
tt=0;
arr[tt]=k;
size++;
}
return true;
}else return false;
}
bool pop(){
if(size>0){
if(hh+1<=n){
arr[hh++]=0;
size--;
}else{
arr[hh]=0;
hh=0;
size--;
}
return true;
}else return false;
}
void show(){
int s=size;
int h=hh;
while(s--){
if(h<n){
cout<<arr[h++]<<' ';
}else{
h=0;
cout<<arr[h++]<<' ';
}
}
cout<<endl;
}
};
int main(){
round_queue rq(5);
rq.add(1);
rq.add(2);
rq.add(3);
rq.pop();
rq.add(4);
rq.add(5);
rq.show();
rq.add(6);
rq.add(7);
rq.show();
rq.pop();
rq.pop();
rq.pop();
rq.show();
rq.add(8);
rq.add(9);
rq.show();
return 0;
}
#三元组存二维数组
#include<iostream>
#include<cstring>
using namespace std;
const int inf = 1e9;
struct tup{
int r, c, v, ne;
};
class arr{
private:
int n;
int m;
int i = 0;
tup *t;
int *h = new int[n + 5];
public:
arr(int tn = 1, int tm = 1){
this->n = tn;
this->m = tm;
t = new tup[n*m + 10];
for (int i = 0; i < n; i++)h[i] = inf;
}
void insert(int row, int col, int value){
if (row >= 0 && row < n&&col >= 0 && col < m){
t[i] = { row, col, value, h[row] };
h[row] = i;
i++;
}
}
void get(int row, int col){
if (row >= 0 && row < n&&col >= 0 && col < m){
int f = 1;
for (int i = h[row]; i!=inf; i = t[i].ne)
if (t[i].r == row&&t[i].c == col){
f = 0, cout << t[i].v << endl;
break;
}
if (f)cout << 0 << endl;
}
}
void show(){
for (int i = 0; i < n; i++){
int c = 0;
int *l = new int[m];
for (int j = h[i]; j!=inf; j = t[j].ne){
l[t[j].c] = t[j].v;
}
for(int i=0; i<m; i++)cout<<l[i]<<' ';
cout << endl;
}
}
};
int main(){
arr a(6, 6);
a.insert(0, 0, 15);
a.insert(0, 3, 22);
a.insert(0, 5, -15);
a.insert(4, 0, 91);
a.insert(1, 1, 11);
a.insert(1, 2, 3);
a.insert(5, 2, 28);
a.insert(2, 3, -6);
a.insert(100, 100, 100);
a.get(2, 3);
a.get(5, 5);
a.get(100, 100);
a.show();
return 0;
}
#二叉树
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=100;
struct node{
int v;
struct node *left_child = NULL, *right_child = NULL;
}p[N];
bool add(struct node &f, struct node &s, int v, int way){
if(way==0&&f.left_child==NULL){
f.left_child=&s;
s.v=v;
return true;
}
if(way==1&&f.right_child==NULL){
f.right_child=&s;
s.v=v;
return true;
}
else return false;
}
void show_first(struct node &root){
cout<<root.v;
if(root.left_child!=NULL) show_first(*root.left_child);
if(root.right_child!=NULL) show_first(*root.right_child);
}
void show_mid(struct node &root){
if(root.left_child!=NULL)show_mid(*root.left_child);
cout<<root.v;
if(root.right_child!=NULL)show_mid(*root.right_child);
}
void show_last(struct node &root){
if(root.left_child!=NULL)show_last(*root.left_child);
if(root.right_child!=NULL)show_last(*root.right_child);
cout<<root.v;
}
bool equal(struct node &root1, struct node &root2){
if(root1.v!=root2.v)return false;
if(root1.left_child&&root2.left_child)
return equal(*root1.left_child, *root2.left_child);
if(root1.right_child&&root2.right_child)
return equal(*root1.right_child, *root2.right_child);
if(!root1.left_child&&!root2.left_child&&!root1.right_child&&!root2.right_child)
return true;
return false;
}
int depth(struct node &root, int d, int md){
if(root.left_child!=NULL)
md = max(md, depth(*root.left_child, d+1, md));
if(root.right_child!=NULL)
md = max(md, depth(*root.right_child, d+1, md));
md = max(md, d);
return md;
}
int leaves_sum(struct node &root, int sum){
if(root.left_child!=NULL)
sum=leaves_sum(*root.left_child, sum);
if(root.right_child!=NULL)
sum=leaves_sum(*root.right_child, sum);
if(root.left_child==NULL&&root.right_child==NULL){
cout<<root.v<<' ';
sum+=root.v;
}
return sum;
}
int btree_cpy_deep(struct node &root1, struct node t2[], int idx){
t2[idx].v=root1.v;
int rec_id = idx;
if(root1.left_child){
t2[rec_id].left_child=&t2[++idx];
idx = btree_cpy_deep(*root1.left_child, t2, idx);
}
if(root1.right_child){
t2[rec_id].right_child=&t2[++idx];
idx = btree_cpy_deep(*root1.right_child, t2, idx);
}
return idx;
}
int main(){
p[1].v=1;
add(p[1], p[2], 2, 0);
add(p[1], p[3], 3, 1);
add(p[2], p[4], 4, 1);
add(p[4], p[6], 6, 0);
add(p[4], p[7], 7, 1);
add(p[3], p[5], 5, 1);
add(p[5], p[8], 8, 0);
show_first(p[1]);
cout<<endl;
show_mid(p[1]);
cout<<endl;
show_last(p[1]);
cout<<endl<<depth(p[1],1,0)<<endl;
int sum=leaves_sum(p[1], 0);
cout<<endl<<"sum:"<<sum<<endl;
struct node q[N];
btree_cpy_deep(p[1], q, 0);
show_first(q[0]);
cout<<endl;
show_mid(q[0]);
cout<<endl;
show_last(q[0]);
cout<<endl;
cout<<equal(p[1], q[0]);
return 0;
}
#输出所有拓扑排序的结果
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int N=1e6+10;
int h[N], e[N], ne[N], idx;
int q[N];
int d[N];
int st[N];
int sum;
void add(int a, int b){
e[idx]=b, ne[idx]=h[a], h[a]=idx++;
}
void dfs(int step){
if(step==9){
for(int i=1; i<=8; i++)
cout<<q[i];
cout<<endl;
sum+=1;
}
for(int i=1; i<=8; i++){
if(d[i]==0&&st[i]==false){
q[step]=i;
st[i]=true;
for(int k=h[i]; ~k; k=ne[k]){
int j=e[k];
d[j]--;
}
dfs(step+1);
st[i]=false;
for(int k=h[i]; ~k; k=ne[k]){
int j=e[k];
d[j]++;
}
}
}
}
int main(){
memset(h, -1, sizeof h);
add(2, 1);d[1]++;
add(2, 3);d[3]++;
add(2, 4);d[4]++;
add(5, 4);d[4]++;
add(1, 8);d[8]++;
add(3, 8);d[8]++;
add(3, 6);d[6]++;
add(4, 8);d[8]++;
add(4, 6);d[6]++;
add(7, 8);d[8]++;
dfs(1);
cout<<sum;
return 0;
}
#spfa求最长路
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int N=1e6+10, M=N*2, inf=0x3f3f3f3f;
int h1[N], h2[N], e[M], w[M], ne[M], idx;
int distm1[N], distm2[N];
bool st[N];
typedef pair<int, int> pii;
void add(int h[], int a, int b, int c){
e[idx]=b, w[idx]=c, ne[idx]=h[a], h[a]=idx++;
}
int spfa(int distm[], int h[], int flag){
memset(distm, 0, sizeof N*4);
queue<int> q;
if(flag){
distm[1]=0;
q.push(1);
st[1]=true;
}else{
distm[10]=0;
q.push(10);
st[10]=true;
}
while(q.size()){
auto t = q.front();
q.pop();
st[t]=false;
for(int i=h[t]; ~i; i=ne[i]){
int j=e[i];
if(distm[j] < distm[t]+w[i]){
distm[j]=distm[t]+w[i];
if(!st[j]){
q.push(j);
st[j]=true;
}
}
}
}
}
int main(){
memset(h1, -1, sizeof h1);
memset(h2, -1, sizeof h2);
add(h1, 1, 2, 5);
add(h1, 1, 3, 6);
add(h1, 2, 4, 3);
add(h1, 3, 4, 6);
add(h1, 3, 5, 3);
add(h1, 4, 5, 7);
add(h1, 4, 7, 4);
add(h1, 5, 7, 1);
add(h1, 4, 6, 4);
add(h1, 7, 9, 5);
add(h1, 5, 8, 4);
add(h1, 6, 10, 4);
add(h1, 9, 10, 2);
add(h1, 8, 9, 2);
spfa(distm1, h1, 1);
for(int i=1; i<=10; i++)cout<<distm1[i]<<' ';
cout<<endl;
memset(st, 0, sizeof st);
add(h2, 2, 1, 5);
add(h2, 3, 1, 6);
add(h2, 4, 2, 3);
add(h2, 4, 3, 6);
add(h2, 5, 3, 3);
add(h2, 5, 4, 7);
add(h2, 7, 4, 4);
add(h2, 7, 5, 1);
add(h2, 6, 4, 4);
add(h2, 9, 7, 5);
add(h2, 8, 5, 4);
add(h2, 10, 6, 4);
add(h2, 10, 9, 2);
add(h2, 9, 8, 2);
spfa(distm2, h2, 0);
for(int i=1; i<=10; i++)cout<<distm1[10]-distm2[i]<<' ';
cout<<endl;
return 0;
}