1.线性结构:
(1)顺序表:
DS顺序表--存储结构与操作
#include<bits/stdc++.h>
using namespace std;
class clist
{
private:
int *data;
int length;
int maxle;
public:
clist()
{
maxle=1000;
length=0;
data=new int[maxle];
}
~clist()
{
delete []data;
}
clist(int m,int as[])
{
int j;
maxle=1000;
data=new int[maxle];
length=m;
for(int i=0; i<length; i++)
{
data[i]=as[i];
}
cout<<length<<" ";
for(j=0; j<length; j++)
cout<<data[j]<<" ";
cout<<endl;
}
void insert(int i,int e)
{
if(i<1||i>length+1)
cout<<"error"<<endl;
else
{
int j;
for(j=length; j>i-1; j--)
{
data[j]=data[j-1];
}
data[i-1]=e;
length++;
cout<<length<<" ";
for(j=0; j<length; j++)
cout<<data[j]<<" ";
cout<<endl;
}
}
void desert(int i)
{
if(i<1||i>length)
cout<<"error"<<endl;
else
{
int j;
for(j=i-1; j<length; j++)
{
data[j]=data[j+1];
}
length--;
cout<<length<<" ";
for(j=0; j<length; j++)
cout<<data[j]<<" ";
cout<<endl;
}
}
void index(int i)
{
if(i<1||i>length)
cout<<"error"<<endl;
else
cout<<data[i-1];
}
};
int main()
{
int num;
cin >>num;
int a[num];
int i;
for(i=0; i<num; i++)
cin >> a[i];
clist p(num,a);
int num1;
int e1;
cin >> num1 >> e1;
p.insert(num1,e1);
int num2;
int e2;
cin >> num2 >> e2;
p.insert(num2,e2);
int num3;
int num4;
cin >> num3;
p.desert(num3);
cin >> num4;
p.desert(num4);
int num5;
int num6;
cin >>num5;
p.index(num5);
cin >> num6;
p.index(num6);
}
DS顺序表--连续操作
#include<bits/stdc++.h>
using namespace std;
class clist
{
private:
int *data;
int length;
int maxle;
public:
clist()
{
maxle=1000;
length=0;
data=new int[maxle];
}
~clist()
{
delete []data;
}
clist(int m,int as[])
{
int j;
maxle=1000;
data=new int[maxle];
length=m;
for(int i=0; i<length; i++)
{
data[i]=as[i];
}
cout<<length<<" ";
for(j=0; j<length; j++)
cout<<data[j]<<" ";
cout<<endl;
}
void insert(int i,int e)
{
if(i<1||i>length+1)
cout<<"error"<<endl;
else
{
int j;
for(j=length; j>i-1; j--)
{
data[j]=data[j-1];
}
data[i-1]=e;
length++;
cout<<length<<" ";
for(j=0; j<length; j++)
cout<<data[j]<<" ";
cout<<endl;
}
}
void desert(int i)
{
if(i<1||i>length)
cout<<"error"<<endl;
else
{
int j;
for(j=i-1; j<length; j++)
{
data[j]=data[j+1];
}
length--;
cout<<length<<" ";
for(j=0; j<length; j++)
cout<<data[j]<<" ";
cout<<endl;
}
}
void index(int i)
{
if(i<1||i>length)
cout<<"error"<<endl;
else
cout<<data[i-1];
}
void multiinsert(int i, int n, int item[])
{
if(i<1||i>length+1)
cout<<"error"<<endl;
else
{
int j;
int c;
for(j=length+n; j>i+n-2; j--)
{
data[j]=data[j-n];
}
for(j=i-1,c=0; j<i+n-1; j++,c++)
data[j]=item[c];
length=length+n;
cout<<length<<" ";
for(j=0; j<length; j++)
cout<<data[j]<<" ";
cout<<endl;
}
}
void multidel(int i, int n)
{
if(i<1||i>length)
cout<<"error"<<endl;
else
{
int j;
for(j=i-1; j<length; j++)
{
data[j]=data[j+n];
}
length=length-n;
cout<<length<<" ";
for(j=0; j<length; j++)
cout<<data[j]<<" ";
cout<<endl;
}
}
};
int main()
{
int num;
cin >>num;
int a[num];
int i;
for(i=0; i<num; i++)
cin >> a[i];
clist p(num,a);
int nu;
cin >> nu;
int numm;
cin >> numm;
int b[numm];
for(i=0; i<numm; i++)
cin>>b[i];
p.multiinsert(nu,numm,b);
int num1;
int num2;
cin >> num1 >> num2;
p.multidel(num1,num2);
}
DS顺序表--合并操作
#include<bits/stdc++.h>
using namespace std;
class clist
{
private:
int *data;
int length;
int maxle;
public:
clist()
{
maxle=1000;
length=0;
data=new int[maxle];
}
~clist()
{
delete []data;
}
clist(int n[],int m[],int n1,int m1)
{
maxle=1000;
data=new int[maxle];
length=n1+m1;
/*int x=0;
int y=0;
int i;
for(i=0;i<length;i++)
{
if(n[x]==0)
{
data[i]=m[y];
y++;
}
if(m[y]==0)
{
data[i]=n[x];
x++;
}
else if(n[x]>m[y])
{
data[i]=m[y];
y++;
}
else if(n[x]<m[y])
{
data[i]=n[x];
x++;
}
}*/
int i;
int j;
int z[length];
for(i=0; i<n1; i++)
z[i]=n[i];
for(i=n1,j=0; i<length; i++,j++)
z[i]=m[j];
sort(z,z+length);
for(i=0; i<length; i++)
data[i]=z[i];
}
void insert(int i,int e)
{
if(i<1||i>length+1)
cout<<"error"<<endl;
else
{
int j;
for(j=length; j>i-1; j--)
{
data[j]=data[j-1];
}
data[i-1]=e;
length++;
cout<<length<<" ";
for(j=0; j<length; j++)
cout<<data[j]<<" ";
cout<<endl;
}
}
void desert(int i)
{
if(i<1||i>length)
cout<<"error"<<endl;
else
{
int j;
for(j=i-1; j<length; j++)
{
data[j]=data[j+1];
}
length--;
cout<<length<<" ";
for(j=0; j<length; j++)
cout<<data[j]<<" ";
cout<<endl;
}
}
void index(int i)
{
if(i<1||i>length)
cout<<"error"<<endl;
else
cout<<data[i-1];
}
void multiinsert(int i, int n, int item[])
{
if(i<1||i>length+1)
cout<<"error"<<endl;
else
{
int j;
int c;
for(j=length+n; j>i+n-2; j--)
{
data[j]=data[j-n];
}
for(j=i-1,c=0; j<i+n-1; j++,c++)
data[j]=item[c];
length=length+n;
cout<<length<<" ";
for(j=0; j<length; j++)
cout<<data[j]<<" ";
cout<<endl;
}
}
void multidel(int i, int n)
{
if(i<1||i>length)
cout<<"error"<<endl;
else
{
int j;
for(j=i-1; j<length; j++)
{
data[j]=data[j+n];
}
length=length-n;
cout<<length<<" ";
for(j=0; j<length; j++)
cout<<data[j]<<" ";
cout<<endl;
}
}
void display()
{
int i;
cout<<length<<" ";
for(i=0; i<length; i++)
cout<<data[i]<<" ";
cout<<endl;
}
};
int main()
{
int i;
int num1;
cin >> num1;
int a[num1]= {0};
for(i=0; i<num1; i++)
cin >> a[i];
int num2;
cin >> num2;
int b[num2]= {0};
for(i=0; i<num2; i++)
cin >> b[i];
clist p(a,b,num1,num2);
p.display();
}
DS顺序表之循环移位
#include<bits/stdc++.h>
using namespace std;
class clist
{
private:
int *data;
int length;
int maxle;
public:
clist()
{
maxle=1000;
length=0;
data=new int[maxle];
}
~clist()
{
delete []data;
}
clist(int n,int a[])
{
maxle=1000;
data=new int[maxle];
length=n;
int i;
for(i=0; i<length; i++)
data[i]=a[i];
}
void insert(int i,int e)
{
if(i<1||i>length+1)
cout<<"error"<<endl;
else
{
int j;
for(j=length; j>i-1; j--)
{
data[j]=data[j-1];
}
data[i-1]=e;
length++;
cout<<length<<" ";
for(j=0; j<length; j++)
cout<<data[j]<<" ";
cout<<endl;
}
}
void desert(int i)
{
if(i<1||i>length)
cout<<"error"<<endl;
else
{
int j;
for(j=i-1; j<length; j++)
{
data[j]=data[j+1];
}
length--;
cout<<length<<" ";
for(j=0; j<length; j++)
cout<<data[j]<<" ";
cout<<endl;
}
}
void index(int i)
{
if(i<1||i>length)
cout<<"error"<<endl;
else
cout<<data[i-1];
}
void multiinsert(int i, int n, int item[])
{
if(i<1||i>length+1)
cout<<"error"<<endl;
else
{
int j;
int c;
for(j=length+n; j>i+n-2; j--)
{
data[j]=data[j-n];
}
for(j=i-1,c=0; j<i+n-1; j++,c++)
data[j]=item[c];
length=length+n;
cout<<length<<" ";
for(j=0; j<length; j++)
cout<<data[j]<<" ";
cout<<endl;
}
}
void multidel(int i, int n)
{
if(i<1||i>length)
cout<<"error"<<endl;
else
{
int j;
for(j=i-1; j<length; j++)
{
data[j]=data[j+n];
}
length=length-n;
cout<<length<<" ";
for(j=0; j<length; j++)
cout<<data[j]<<" ";
cout<<endl;
}
}
void display()
{
int i;
for(i=0; i<length; i++)
cout<<data[i]<<" ";
cout<<endl;
}
void move(int f,int n)
{
int i;
int a[length];
for(i=0; i<length; i++)
a[i]=data[i];
if(f==1)
{
for(i=0; i<length; i++)
data[(i+n)%length]=a[i];
}
else if(f==0)
{
for(i=length-1; i>=0; i--)
data[i]=a[(i+n)%length];
}
}
};
int main()
{
int num;
cin >> num;
int as[num];
int i;
for(i=0; i<num; i++)
{
cin >> as[i];
}
clist p(num,as);
p.display();
int flag1;
int e1;
cin >> flag1 >> e1;
p.move(flag1,e1);
p.display();
int flag2;
int e2;
cin >> flag2 >>e2;
p.move(flag2,e2);
p.display();
}
DS线性表—多项式相加
#include<bits/stdc++.h>
using namespace std;
class node
{
private:
int data1;
int data2;
node *next;
public:
node(int e1=0,int e2=0,node *p=NULL):data1(e1),data2(e2),next(p)
{
}
~node()
{
}
friend class Llist;
};
class Llist
{
private:
node *head;
int length;
node *indexd(int i);
public:
Llist()
{
}
Llist(int as[],int bs[],int num)//头插法
{
length=num;
node *s;
head=new node;
int i;
for(i=length-1; i>=0; i--)
{
s=new node(as[i],bs[i],head->next);
head->next=s;
}
}
/* void insert(int i,int e)
{
if(i<0||i>length+1)
cout<<"error"<<endl;
else
{
node *p;
node *q=indexd(i-1);
p=new node(e,q->next);
q->next=p;
length++;
node *ps;
ps=head->next;
while(ps)
{
cout <<ps->data<<" ";
ps=ps->next;
}
cout <<endl;
}
}
void desert(int i)
{
if(i<1||i>length)
cout<<"error"<<endl;
else
{
node *p;
node *q=indexd(i-1);
p=q->next;
q->next=p->next;
delete p;
length--;
node *ps;
ps=head->next;
while(ps)
{
cout <<ps->data<<" ";
ps=ps->next;
}
cout <<endl;
}
}
void index(int i)
{
if(i<1||i>length)
cout<<"error"<<endl;
else
{
node *p;
p=indexd(i);
cout<<p->data<<endl;
}
}
void swaps(int i,int j)
{
if(i<1||j<1||i>length||j>length)
cout << "error"<<endl;
else
{
node *p;
node *q;
p=indexd(i);
q=indexd(j);
node *p1;
node *p2;
node *q1;
node *q2;
p1=indexd(i-1);
p2=indexd(i+1);
q1=indexd(j-1);
q2=indexd(j+1);
p1->next=q;
q->next=p2;
q1->next=p;
p->next=q2;
node *ps;
ps=head->next;
while(ps)
{
cout <<ps->data<<" ";
ps=ps->next;
}
cout <<endl;
}
}
void LL_merge(Llist La, Llist Lb)
{
int len=La.length+Lb.length;
int cs[len]= {0};
Llist Lc(cs,len);
node *pa=La.head->next;
node *pb=Lb.head->next;
node *pc=Lc.head;
while(pa&&pb)
{
if(pa->data<pb->data)
{
pc->next=pa;
pa=pa->next;
}
else if(pa->data>pb->data)
{
pc->next=pb;
pb=pb->next;
}
pc=pc->next;
}
pc->next=pa?pa:pb?pb:NULL;
La.head->next=NULL;
Lb.head->next=NULL;
node *ps;
ps=Lc.head->next;
while(ps)
{
cout <<ps->data<<" ";
ps=ps->next;
}
cout <<endl;
}*/
void display()
{
node *p;
p=head->next;
while(p->next)
{
if(p->data2==0)
{
if(p->data1<0)
cout<<"(" <<p->data1<<")" <<" "<<"+"<<" ";
else
cout<<p->data1<<" "<<"+"<<" ";
}
else
{
if(p->data2>0)
{
if(p->data1<0)
cout<<"(" <<p->data1<<")" <<"x^"<<p->data2<<" "<<"+"<<" ";
else
cout<<p->data1<<"x^"<<p->data2<<" "<<"+"<<" ";
}
else if(p->data2<0)
{
if(p->data1<0)
cout<<"(" <<p->data1<<")" <<"x^"<<"("<<p->data2<<")"<<" "<<"+"<<" ";
else
cout<<p->data1<<"x^"<<"("<<p->data2<<")"<<" "<<"+"<<" ";
}
}
p=p->next;
}
if(p->data1<0)
cout<<"(" <<p->data1<<")" <<"x^"<<p->data2<<endl;
else
cout<<p->data1<<"x^"<<p->data2<<endl;
}
void jiafa(Llist La,Llist Lb)
{
int len=La.length+Lb.length;
int e[len]= {1};
int f[len]= {1};
Llist Lc(e,f,len);
node *pa=La.head->next;
node *pb=Lb.head->next;
node *pc=Lc.head;
while(pa&&pb)
{
if((pa->data2)<(pb->data2))
{
pc->next=pa;
pa=pa->next;
pc=pc->next;
}
else if((pa->data2)>(pb->data2))
{
pc->next=pb;
pb=pb->next;
pc=pc->next;
}
else if((pa->data2)==(pb->data2))
{
if(pa->data1+pb->data1!=0)
{
pa->data1=pa->data1+pb->data1;
pc->next=pa;
pa=pa->next;
pb=pb->next;
pc=pc->next;
}
if(pa->data1+pb->data1==0)
{
pa=pa->next;
pb=pb->next;
}
}
}
pc->next=pa?pa:pb?pb:NULL;
La.head->next=NULL;
Lb.head->next=NULL;
Lc.display();
}
~Llist()
{
node *p;
node *q;
head = new node;
p=head;
while(p)
{
q=p->next;
delete p;
p=q;
}
head = NULL;
}
};
node *Llist::indexd(int i)
{
if((i<0)||(i>length))
return NULL;
node *p;
//head=new node;
p=head;
int j;
for(j=0; j<i; j++)
{
p=p->next;
}
return p;
}
int main()
{
int t;
cin >> t;
while(t--)
{
int i;
int num1;
cin >> num1;
int as[num1];
int bs[num1];
for(i=0; i<num1; i++)
cin >> as[i] >> bs[i];
Llist p(as,bs,num1);
p.display();
int num2;
cin >> num2;
int cs[num2];
int ds[num2];
for(i=0; i<num2; i++)
cin >> cs[i] >> ds[i];
Llist q(cs,ds,num2);
q.display();
Llist res;
res.jiafa(p,q);
}
cout<<endl;
}
(2)链表:
DS单链表--存储结构与操作
#include<bits/stdc++.h>
using namespace std;
class node
{
private:
int data;
node *next;
public:
node(int e=0,node *p=NULL):data(e),next(p)
{
}
~node()
{
}
friend class Llist;
};
class Llist
{
private:
node *head;
int length;
node *indexd(int i);
public:
Llist()
{
}
Llist(int as[],int num)//头插法
{
length=num;
node *s;
head=new node;
int i;
for(i=length-1; i>=0; i--)
{
s=new node(as[i],head->next);
head->next=s;
}
}
void insert(int i,int e)
{
if(i<0||i>length+1)
cout<<"error"<<endl;
else
{
node *p;
node *q=indexd(i-1);
p=new node(e,q->next);
q->next=p;
length++;
node *ps;
ps=head->next;
while(ps)
{
cout <<ps->data<<" ";
ps=ps->next;
}
cout <<endl;
}
}
void desert(int i)
{
if(i<1||i>length)
cout<<"error"<<endl;
else
{
node *p;
node *q=indexd(i-1);
p=q->next;
q->next=p->next;
delete p;
length--;
node *ps;
ps=head->next;
while(ps)
{
cout <<ps->data<<" ";
ps=ps->next;
}
cout <<endl;
}
}
void index(int i)
{
if(i<1||i>length)
cout<<"error"<<endl;
else
{
node *p;
p=indexd(i);
cout<<p->data<<endl;
}
}
void display()
{
node *p;
p=head->next;
while(p)
{
cout <<p->data<<" ";
p=p->next;
}
cout <<endl;
}
~Llist()
{
node *p;
node *q;
head = new node;
p=head;
while(p)
{
q=p->next;
delete p;
p=q;
}
head = NULL;
}
};
node *Llist::indexd(int i)
{
if((i<0)||(i>length+1))
return NULL;
node *p;
//head=new node;
p=head;
int j;
for(j=0; j<i; j++)
{
p=p->next;
}
return p;
}
int main()
{
int n;
cin >> n;
int a[n];
int i;
for(i=0; i<n; i++)
{
cin >> a[i];
}
Llist u(a,n);
u.display();
int i1;
int e1;
cin >> i1 >> e1;
u.insert(i1,e1);
int i2;
int e2;
cin >> i2 >> e2;
u.insert(i2,e2);
int i3;
cin >> i3;
u.desert(i3);
int i4;
cin >> i4;
u.desert(i4);
int i5;
cin >> i5;
u.index(i5);
int i6;
cin >> i6;
u.index(i6);
}
DS单链表--结点交换
#include<bits/stdc++.h>
using namespace std;
class node
{
private:
int data;
node *next;
public:
node(int e=0,node *p=NULL):data(e),next(p)
{
}
~node()
{
}
friend class Llist;
};
class Llist
{
private:
node *head;
int length;
node *indexd(int i);
public:
Llist()
{
}
Llist(int as[],int num)//头插法
{
length=num;
node *s;
head=new node;
int i;
for(i=length-1; i>=0; i--)
{
s=new node(as[i],head->next);
head->next=s;
}
}
void insert(int i,int e)
{
if(i<0||i>length+1)
cout<<"error"<<endl;
else
{
node *p;
node *q=indexd(i-1);
p=new node(e,q->next);
q->next=p;
length++;
node *ps;
ps=head->next;
while(ps)
{
cout <<ps->data<<" ";
ps=ps->next;
}
cout <<endl;
}
}
void desert(int i)
{
if(i<1||i>length)
cout<<"error"<<endl;
else
{
node *p;
node *q=indexd(i-1);
p=q->next;
q->next=p->next;
delete p;
length--;
node *ps;
ps=head->next;
while(ps)
{
cout <<ps->data<<" ";
ps=ps->next;
}
cout <<endl;
}
}
void index(int i)
{
if(i<1||i>length)
cout<<"error"<<endl;
else
{
node *p;
p=indexd(i);
cout<<p->data<<endl;
}
}
void swaps(int i,int j)
{
if(i<1||j<1||i>length||j>length)
cout << "error"<<endl;
else
{
node *p;
node *q;
p=indexd(i);
q=indexd(j);
node *p1;
node *p2;
node *q1;
node *q2;
p1=indexd(i-1);
p2=indexd(i+1);
q1=indexd(j-1);
q2=indexd(j+1);
p1->next=q;
q->next=p2;
q1->next=p;
p->next=q2;
node *ps;
ps=head->next;
while(ps)
{
cout <<ps->data<<" ";
ps=ps->next;
}
cout <<endl;
}
}
void display()
{
node *p;
p=head->next;
while(p)
{
cout <<p->data<<" ";
p=p->next;
}
cout <<endl;
}
~Llist()
{
node *p;
node *q;
head = new node;
p=head;
while(p)
{
q=p->next;
delete p;
p=q;
}
head = NULL;
}
};
node *Llist::indexd(int i)
{
if((i<0)||(i>length))
return NULL;
node *p;
//head=new node;
p=head;
int j;
for(j=0; j<i; j++)
{
p=p->next;
}
return p;
}
int main()
{
int n;
cin >> n;
int a[n];
int i;
for(i=0; i<n; i++)
{
cin >> a[i];
}
Llist u(a,n);
u.display();
int num1;
int num2;
cin >> num1 >> num2;
u.swaps(num1,num2);
int num3;
int num4;
cin >> num3 >> num4;
u.swaps(num3,num4);
}
DS单链表--合并
#include<bits/stdc++.h>
using namespace std;
class node
{
private:
int data;
node *next;
public:
node(int e=0,node *p=NULL):data(e),next(p)
{
}
~node()
{
}
friend class Llist;
};
class Llist
{
private:
node *head;
int length;
node *indexd(int i);
public:
Llist()
{
}
Llist(int as[],int num)//头插法
{
length=num;
node *s;
head=new node;
int i;
for(i=length-1;i>=0;i--)
{
s=new node(as[i],head->next);
head->next=s;
}
}
void insert(int i,int e)
{
if(i<0||i>length+1)
cout<<"error"<<endl;
else
{
node *p;
node *q=indexd(i-1);
p=new node(e,q->next);
q->next=p;
length++;
node *ps;
ps=head->next;
while(ps)
{
cout <<ps->data<<" ";
ps=ps->next;
}
cout <<endl;
}
}
void desert(int i)
{
if(i<1||i>length)
cout<<"error"<<endl;
else
{
node *p;
node *q=indexd(i-1);
p=q->next;
q->next=p->next;
delete p;
length--;
node *ps;
ps=head->next;
while(ps)
{
cout <<ps->data<<" ";
ps=ps->next;
}
cout <<endl;
}
}
void index(int i)
{
if(i<1||i>length)
cout<<"error"<<endl;
else
{
node *p;
p=indexd(i);
cout<<p->data<<endl;
}
}
void swaps(int i,int j)
{
if(i<1||j<1||i>length||j>length)
cout << "error"<<endl;
else
{
node *p;
node *q;
p=indexd(i);
q=indexd(j);
node *p1;
node *p2;
node *q1;
node *q2;
p1=indexd(i-1);
p2=indexd(i+1);
q1=indexd(j-1);
q2=indexd(j+1);
p1->next=q;
q->next=p2;
q1->next=p;
p->next=q2;
node *ps;
ps=head->next;
while(ps)
{
cout <<ps->data<<" ";
ps=ps->next;
}
cout <<endl;
}
}
void LL_merge(Llist La, Llist Lb)
{
int len=La.length+Lb.length;
int cs[len]={0};
Llist Lc(cs,len);
node *pa=La.head->next;
node *pb=Lb.head->next;
node *pc=Lc.head;
while(pa&&pb)
{
if(pa->data<=pb->data)
{
pc->next=pa;
pa=pa->next;
}
else if(pa->data>pb->data)
{
pc->next=pb;
pb=pb->next;
}
pc=pc->next;
}
pc->next=pa?pa:pb?pb:NULL;
La.head->next=NULL;
Lb.head->next=NULL;
node *ps;
ps=Lc.head->next;
while(ps)
{
cout <<ps->data<<" ";
ps=ps->next;
}
cout <<endl;
}
void display()
{
node *p;
p=head->next;
while(p)
{
cout <<p->data<<" ";
p=p->next;
}
cout <<endl;
}
~Llist()
{
node *p;
node *q;
head = new node;
p=head;
while(p)
{
q=p->next;
delete p;
p=q;
}
head = NULL;
}
};
node *Llist::indexd(int i)
{
if((i<0)||(i>length))
return NULL;
node *p;
//head=new node;
p=head;
int j;
for(j=0;j<i;j++)
{
p=p->next;
}
return p;
}
int main()
{
int n;
cin >> n;
int as[n];
int i;
for(i=0;i<n;i++)
cin>>as[i];
Llist p(as,n);
int m;
cin >> m;
int bs[m];
for(i=0;i<m;i++)
cin>>bs[i];
Llist q(bs,m);
q.LL_merge(p,q);
}
DS循环链表—约瑟夫环(Ver. I - A)
#include<bits/stdc++.h>
using namespace std;
class node
{
private:
int data;
node *next;
public:
node(int e=0,node *p=NULL):data(e),next(p)
{
}
~node()
{
}
friend class whilelist;
};
class whilelist
{
private:
node* head;
int length;
public:
whilelist(int n)
{
head = new node;
head->data = 1;
node *q=head;
length = n;
int i;
for (i = 1; i < n; i++)
{
node* p = new node;
p->data = i + 1;
q->next = p;
q = p;
}
q->next = head;
}
void ddel(node *del)
{
node* ps = del;
while (ps->next != del)
{
ps= ps->next;
}
ps->next = del->next;
length--;
delete del;
}
void oout(int Sd, int Kd)
{
node* p;
p=head;
node* q;
int i;
for (i = 1; i < Sd; i++)
{
p = p->next;
}
while (length != 1)
{
for (i = 1; i < Kd; i++)
{
p = p->next;
}
cout << p->data << ' ';
q = p;
p = p->next;
ddel(q);
}
cout << p->data << " "<<endl;
ddel(p);
}
};
int main()
{
int ns, ks, ss;
while (cin >> ns)
{
whilelist p(ns);
cin >> ks >> ss;
p.oout(ss, ks);
}
}
(3)栈:
DS堆栈--逆序输出(STL栈使用)
#include<bits/stdc++.h>
#include<stack>
using namespace std;
void rever(string a,int num)
{
int i;
stack<char> s;
for(i=0; i<num; i++)
s.push(a[i]);
while(!s.empty())
{
cout<<s.top();
s.pop();
}
cout<<endl;
}
int main()
{
int t;
cin >> t;
while(t--)
{
string str;
cin >> str;
int len;
len=str.length();
rever(str,len);
}
}
DS堆栈--行编辑
#include<bits/stdc++.h>
#include<stack>
using namespace std;
void edit(string str,int num)
{
int i;
int j;
stack<char> s1;
stack<char> s2;
for(i=0; i<num; i++)
{
if(str[i]!='#')
s1.push(str[i]);
else if(str[i]=='#'&&s1.empty()==false)
s1.pop();
}
if(s1.empty()==true)
cout<<"NULL"<<endl;
else
{
while(!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
while(!s2.empty())
{
cout<<s2.top();
s2.pop();
}
}
cout<<endl;
}
int main()
{
int t;
cin >> t;
while(t--)
{
string st;
int len;
cin >> st;
len=st.length();
edit(st,len);
}
}
DS堆栈--迷宫求解
#include<bits/stdc++.h>
#include<stack>
using namespace std;
class point
{
public:
int xp;
int yp;
point (int n1,int n2):xp(n1),yp(n2)
{
}
};
int main()
{
int t;
cin >>t;
while(t--)
{
int i,j;
int num;
cin >>num;
int **as=new int*[num];
for(i=0; i<num; i++)
as[i]=new int [num];
for(i=0; i<num; i++)
for(j=0; j<num; j++)
cin >>as[i][j];
stack<point> path;
point p(0,0);
path.push(p);
as[0][0]=1;
int ii=0;
int jj=0;
while(1)
{
if(jj+1<num&&as[ii][jj+1]==0)
{
as[ii][jj+1]=1;
jj++;
point q(ii,jj);
path.push(q);
}
else if(ii+1<num&&as[ii+1][jj]==0)
{
as[ii+1][jj]=1;
ii++;
point q(ii,jj);
path.push(q);
}
else if(jj-1>=0&&as[ii][jj-1]==0)
{
as[ii][jj-1]=1;
jj--;
point q(ii,jj);
path.push(q);
}
else if(ii-1>=0&&as[ii-1][jj]==0)
{
as[ii-1][jj]=1;
ii--;
point q(ii,jj);
path.push(q);
}
else
{
path.pop();
if(!path.empty())
{
ii=path.top().xp;
jj=path.top().yp;
}
}
if(path.empty()||(ii==num-1&&jj==num-1))
break;
}
if(path.empty())
{
cout<<"no path"<<endl;
}
else
{
stack<point> path1;
while(!path.empty())
{
path1.push(path.top());
path.pop();
}
i = 0;
while (!path1.empty())
{
if ((++i) % 4 == 0)
{
cout << '[' << path1.top().xp << ',' << path1.top().yp << ']' << "--" << endl;
}
else
{
cout << '[' << path1.top().xp << ',' << path1.top().yp << ']' << "--";
}
path1.pop();
}
cout << "END" << endl;
}
for (i=0; i<num; i++)
delete []as[i];
delete[]as;
}
}
DS堆栈--括号匹配
#include<bits/stdc++.h>
#include<stack>
using namespace std;
void match(string str,int l)
{
int i;
stack<char> s;
int flag=0;
for(i=0; i<l; i++)
{
if(str[i]=='('||str[i]=='['||str[i]=='{')
s.push(str[i]);
if((s.empty()!=true)&&(str[i]==')'||str[i]==']'||str[i]=='}'))
{
if(str[i]==')')
{
if(s.top()!='(')
{
cout<<"error"<<endl;
break;
}
else
{
s.pop();
}
}
if(str[i]==']')
{
if(s.top()!='[')
{
cout<<"error"<<endl;
break;
}
else
{
s.pop();
}
}
if(str[i]=='}')
{
if(s.top()!='{')
{
cout<<"error"<<endl;
break;
}
else
{
s.pop();
}
}
}
else if((s.empty()==true)&&(str[i]==')'||str[i]==']'||str[i]=='}'))
{
cout <<"error"<<endl;
flag=1;
break;
}
}
if(flag==0)
{
if(s.empty()==true)
cout<<"ok"<<endl;
if(s.empty()!=true)
cout<<"error"<<endl;
}
}
int main()
{
int t;
cin >> t;
while(t--)
{
string st;
int len;
cin >> st;
len=st.length();
match(st,len);
}
}
DS堆栈--表达式计算
#include<bits/stdc++.h>
#include<stack>
#define N 7
using namespace std;
char prior[N][N]=
{
'>','>','<','<','<','>','>',
'>','>','<','<','<','>','>',
'>','>','>','>','<','>','>',
'>','>','>','>','<','>','>',
'<','<','<','<','<','=',' ',
'>','>','>','>',' ','>','>',
'<','<','<','<','<',' ','='
};
float operate(float as,unsigned char thetad,float bs)
{
if(thetad=='+')
return as+bs;
else if(thetad=='-')
return as-bs;
else if(thetad=='*')
return as*bs;
else if(thetad=='/')
return as/bs;
}
char opset[N]= {'+','-','*','/','(',')','#'};
bool in(char test,char *testopc)
{
int j;
for(j=0; j<7; j++)
if(test==testopc[j])
return true;
return false;
}
char precede(char aopd,char bopd)
{
int p;
int q;
for(p=0; p<N; p++)
if(opset[p]==aopd)
break;
for(q=0; q<=N; q++)
if(opset[q]==bopd)
break;
return prior[p][q];
}
float evaluate(string eexp)
{
stack<char> ooptr;
stack<double> oopnd;
char tempdata[200];
double data,as,bs,r;
char theta,dcr[2];
char cs;
int i=0;
ooptr.push('#');
cs=eexp[0];
strcpy(tempdata,"\0");
while(cs!='#'||ooptr.top()!='#')
{
if(!in(cs,opset))
{
dcr[0]=cs;
dcr[1]='\0';
strcat(tempdata,dcr);
cs=eexp[++i];
if(in(cs,opset))
{
data=(float)atof(tempdata);
oopnd.push(data);
strcpy(tempdata,"\0");
}
}
else
{
switch(precede(ooptr.top(),cs))
{
case'=':
ooptr.pop();
cs=eexp[++i];
break;
case'<':
ooptr.push(cs);
cs=eexp[++i];
break;
case'>':
theta=ooptr.top();
ooptr.pop();
bs=oopnd.top();
oopnd.pop();
as=oopnd.top();
oopnd.pop();
oopnd.push(operate(as,theta,bs));
break;
}
}
}
return oopnd.top();
}
int main()
{
int t;
string ex;
double res;
cin>>t;
for(int i=0; i<t; i++)
{
cin>>ex;
res=evaluate(ex);
cout<<fixed<<setprecision(4)<<res<<endl;
}
}
(4)队列:
DS队列之银行排队
#include<bits/stdc++.h>
#include<queue>
using namespace std;
int main()
{
int a=0;
int b=0;
int c=0;
int num;
cin >> num;
queue<int> as;
queue<int> bs;
queue<int> cs;
int i;
int time;
char p[num];
for(i=0; i<num; i++)
cin>>p[i];
for(i=0; i<num; i++)
{
cin>>time;
if(p[i]=='A')
{
as.push(time);
a++;
}
else if(p[i]=='B')
{
bs.push(time);
b++;
}
else if(p[i]=='C')
{
cs.push(time);
c++;
}
}
int sum1=0;
int sum2=0;
int sum3=0;
while(as.empty()!=true)
{
sum1+=as.front();
as.pop();
}
while(bs.empty()!=true)
{
sum2+=bs.front();
bs.pop();
}
while(cs.empty()!=true)
{
sum3+=cs.front();
cs.pop();
}
cout<<sum1/a<<endl<<sum2/b<<endl<<sum3/c<<endl;
}
DS队列+堆栈--数制转换
#include<bits/stdc++.h>
#include<stack>
#include<queue>
using namespace std;
void zh(double n,int k)
{
int n1;
double n2;
int i;
n1=(int)n;
n2=n-n1;
stack<int> zheng;
queue<int> xiao;
while(n1!=0)
{
zheng.push(n1%k);
n1=n1/k;
}
for(i=0; i<3; i++)
{
xiao.push((int)(n2*k));
n2=n2*k-(int)(n2*k);
}
while(zheng.empty()!=1)
{
if(zheng.top()<10)
cout<<zheng.top();
if(zheng.top()==10)
cout<<"A";
if(zheng.top()==11)
cout<<"B";
if(zheng.top()==12)
cout<<"C";
if(zheng.top()==13)
cout<<"D";
if(zheng.top()==14)
cout<<"E";
if(zheng.top()==15)
cout<<"F";
zheng.pop();
}
cout<<".";
for(i=0; i<3; i++)
{
if(xiao.front()<10)
cout<<xiao.front();
if(xiao.front()==10)
cout<<"A";
if(xiao.front()==11)
cout<<"B";
if(xiao.front()==12)
cout<<"C";
if(xiao.front()==13)
cout<<"D";
if(xiao.front()==14)
cout<<"E";
if(xiao.front()==15)
cout<<"F";
xiao.pop();
}
cout<<endl;
}
int main()
{
int t;
cin >>t;
while(t--)
{
double num;
int jinzhi;
cin>>num>>jinzhi;
zh(num,jinzhi);
}
}
银行业务队列简单模拟
#include<bits/stdc++.h>
//#include<stack>
#include<queue>
using namespace std;
int main()
{
int num;
cin >>num;
int i;
int nc;
queue<int> a;
queue<int> b;
for(i=0; i<num; i++)
{
cin>>nc;
if(nc%2==1)
a.push(nc);
else if(nc%2==0)
b.push(nc);
}
int m=a.size();
int n=b.size();
//cout<<m<<" "<<n<<endl;
if(m!=1)
{
while(m!=0&&n!=0&&m!=1)
{
cout<<a.front()<<" ";
a.pop();
m--;
cout<<a.front()<<" ";
a.pop();
m--;
cout<<b.front()<<" ";
b.pop();
n--;
}
while(m!=0)
{
if(m!=1)
{
cout<<a.front()<<" ";
a.pop();
m--;
}
if(m==1&&n==0)
{
cout<<a.front();
a.pop();
m--;
}
if(m==1&&n!=0)
{
cout<<a.front()<<" ";
a.pop();
m--;
}
}
while(n!=0)
{
if(n!=1)
{
cout<<b.front()<<" ";
b.pop();
n--;
}
else if(n==1)
{
cout<<b.front();
b.pop();
n--;
}
}
}
else if(m==1)
{
cout<<a.front()<<" ";
a.pop();
while(n!=0)
{
if(n!=1)
{
cout<<b.front()<<" ";
b.pop();
n--;
}
else if(n==1)
{
cout<<b.front();
b.pop();
n--;
}
}
}
}
DS栈+队列—排队游戏
#include<bits/stdc++.h>
#include <queue>
#include <stack>
using namespace std;
class boy
{
public:
char cc;
int num;
boy ()
{
}
boy (char cval, int nval) :cc(cval),num(nval)
{
}
};
int main()
{
stack<boy> Boy;
string str;
cin >> str;
int j;
int len = str.length();
for (j=0; j<len; j++)
{
if (str[j] == str[0])
{
boy asc(str[0], j);
Boy.push(asc);
}
else
{
cout<<Boy.top().num<<" "<<j<<endl;
Boy.pop();
}
}
}
DS串应用--KMP算法
#include<bits/stdc++.h>
#include<string>
using namespace std;
class mystring
{
private:
string mainstr;
int size;
void getnext(string p,int next[])
{
int j;
int k;
j=0;
k=-1;
next[j]=k;
while(j<p.size()-1)
{
if(k==-1||p[j]==p[k])
next[++j]=++k;
else
k=next[k];
}
}
int kmpfind(string p,int pos,int next[])
{
int i;
int j;
for(i=0,j=0; i<size&&j<(int)p.size();)
{
if(j==-1||mainstr[i]==p[j])
{
i++;
j++;
}
else
j=next[j];
}
if(j==p.size())
return i-j;
return -1;
}
public:
mystring(string as)
{
mainstr=as;
size=as.length();
}
int kmpfindsubstr(string p,int pos)
{
int i;
int L=p.length();
int *next=new int[L];
getnext(p,next);
for(i=0; i<L; i++)
cout<<next[i]<<" ";
cout<<endl;
int v=-1;
v=kmpfind(p,pos,next);
delete []next;
return v;
}
};
int main()
{
int t;
cin >> t;
while(t--)
{
string sv;
string tv;
cin >> sv;
cin >> tv;
mystring s(sv);
mystring t(tv);
cout<<s.kmpfindsubstr(tv,0)+1<<endl;
}
}
2.树:
(1)二叉树的遍历与应用:
DS二叉树—二叉树构建与遍历(不含框架)
#include<bits/stdc++.h>
using namespace std;
typedef struct mynode
{
char data;
struct mynode* lchild;
struct mynode* rchild;
} mytree;
void create(mytree *&te)
{
char sc;
cin >>sc;
if (sc=='#')
te=NULL;
else
{
te= new mynode;
te->data = sc;
create(te->lchild);
create(te->rchild);
}
}
void display1(mytree* tc)
{
if (tc!=NULL)
{
cout << tc->data;
display1(tc->lchild);
display1(tc->rchild);
}
}
void display2(mytree* tb)
{
if (tb!=NULL)
{
display2(tb->lchild);
cout << tb->data;
display2(tb->rchild);
}
}
void display3(mytree* tg)
{
if (tg!=NULL)
{
display3(tg->lchild);
display2(tg->rchild);
cout << tg->data;
}
}
int main()
{
int t;
cin >>t;
while (t--)
{
mytree* test;
create(test);
display1(test);
cout << endl;
display2(test);
cout << endl;
display3(test);
cout << endl;
}
}
DS二叉树--叶子数量
#include<bits/stdc++.h>
using namespace std;
typedef struct mynode
{
char data;
struct mynode* lchild;
struct mynode* rchild;
} mytree;
int cnt=0;
void create(mytree *&te)
{
char sc;
cin >>sc;
if (sc=='0')
te=NULL;
else
{
te= new mynode;
te->data = sc;
create(te->lchild);
create(te->rchild);
}
}
void display1(mytree* tc)
{
if (tc!=NULL)
{
if(tc->lchild==NULL&&tc->rchild==NULL)
cnt++;
display1(tc->lchild);
display1(tc->rchild);
}
}
void display2(mytree* tb)
{
if (tb!=NULL)
{
display2(tb->lchild);
cout << tb->data;
display2(tb->rchild);
}
}
void display3(mytree* tg)
{
if (tg!=NULL)
{
display3(tg->lchild);
display2(tg->rchild);
cout << tg->data;
}
}
int main()
{
int t;
//int res;
cin >>t;
while (t--)
{
mytree* test;
create(test);
display1(test);
cout<<cnt<<endl;
cnt=0;
}
}
DS二叉树——二叉树之父子结点
#include<bits/stdc++.h>
using namespace std;
typedef struct mynode
{
char data;
struct mynode* parent;
struct mynode* lchild;
struct mynode* rchild;
} mytree;
//int cnt=0;
mynode *pre=NULL;
void create(mytree *&te)
{
char sc;
cin >>sc;
if (sc=='0')
te=NULL;
else
{
te= new mynode;
te->data = sc;
te->parent=pre;
pre=te;
create(te->lchild);
pre=te;
create(te->rchild);
}
}
void display1(mytree* tc)
{
if (tc!=NULL)
{
if(tc->lchild==NULL&&tc->rchild==NULL)
cout<<tc->data<<" ";
display1(tc->lchild);
display1(tc->rchild);
}
}
void display2(mytree* tb)
{
if (tb!=NULL)
{
display2(tb->lchild);
cout << tb->data;
display2(tb->rchild);
}
}
void display3(mytree* tg)
{
if (tg!=NULL)
{
display3(tg->lchild);
display2(tg->rchild);
cout << tg->data;
}
}
void display4(mytree* tc)
{
if (tc!=NULL)
{
if(tc->lchild==NULL&&tc->rchild==NULL)
cout<<tc->parent->data<<" ";
display4(tc->lchild);
display4(tc->rchild);
}
}
int main()
{
int t;
//int res;
cin >>t;
while (t--)
{
mytree* test;
create(test);
display1(test);
cout<<endl;
display4(test);
cout<<endl;
/*cout<<cnt<<endl;
cnt=0;*/
}
}
DS二叉树--层次遍历
#include<bits/stdc++.h>
#include<queue>
using namespace std;
typedef struct mynode
{
char data;
struct mynode* parent;
struct mynode* lchild;
struct mynode* rchild;
} mytree;
//int cnt=0;
mynode *pre=NULL;
void create(mytree *&te)
{
char sc;
cin >>sc;
if (sc=='0')
te=NULL;
else
{
te= new mynode;
te->data = sc;
te->parent=pre;
pre=te;
create(te->lchild);
pre=te;
create(te->rchild);
}
}
void display1(mytree* tc)
{
if (tc!=NULL)
{
if(tc->lchild==NULL&&tc->rchild==NULL)
cout<<tc->data<<" ";
display1(tc->lchild);
display1(tc->rchild);
}
}
void display2(mytree* tb)
{
if (tb!=NULL)
{
display2(tb->lchild);
cout << tb->data;
display2(tb->rchild);
}
}
void display3(mytree* tg)
{
if (tg!=NULL)
{
display3(tg->lchild);
display2(tg->rchild);
cout << tg->data;
}
}
void display4(mytree* tc)
{
if (tc!=NULL)
{
if(tc->lchild==NULL&&tc->rchild==NULL)
cout<<tc->parent->data<<" ";
display4(tc->lchild);
display4(tc->rchild);
}
}
void display5 (mytree* tp)
{
queue<mytree*> tq;
mytree *cs;
tq.push(tp);
while(!tq.empty())
{
cs=tq.front();
cout<<cs->data;
if(cs->lchild)
tq.push(cs->lchild);
if(cs->rchild)
tq.push(cs->rchild);
tq.pop();
}
}
int main()
{
int t;
//int res;
cin >>t;
while (t--)
{
mytree* test;
create(test);
display5(test);
cout<<endl;
}
}
DS树--二叉树高度
#include<bits/stdc++.h>
#include<queue>
using namespace std;
typedef struct mynode
{
char data;
struct mynode* parent;
struct mynode* lchild;
struct mynode* rchild;
}mytree;
//int cnt=0;
mynode *pre=NULL;
void create(mytree *&te)
{
char sc;
cin >>sc;
if (sc=='0')
te=NULL;
else
{
te= new mynode;
te->data = sc;
te->parent=pre;
pre=te;
create(te->lchild);
pre=te;
create(te->rchild);
}
}
void display1(mytree* tc)
{
if (tc!=NULL)
{
if(tc->lchild==NULL&&tc->rchild==NULL)
cout<<tc->data<<" ";
display1(tc->lchild);
display1(tc->rchild);
}
}
void display2(mytree* tb)
{
if (tb!=NULL)
{
display2(tb->lchild);
cout << tb->data;
display2(tb->rchild);
}
}
void display3(mytree* tg)
{
if (tg!=NULL)
{
display3(tg->lchild);
display2(tg->rchild);
cout << tg->data;
}
}
void display4(mytree* tc)
{
if (tc!=NULL)
{
if(tc->lchild==NULL&&tc->rchild==NULL)
cout<<tc->parent->data<<" ";
display4(tc->lchild);
display4(tc->rchild);
}
}
void display5 (mytree* tp)
{
queue<mytree*> tq;
mytree *cs;
tq.push(tp);
while(!tq.empty())
{
cs=tq.front();
cout<<cs->data;
if(cs->lchild)
tq.push(cs->lchild);
if(cs->rchild)
tq.push(cs->rchild);
tq.pop();
}
}
void display6 (mytree* tc)
{
if (tc!=NULL)
{
if(tc->lchild==NULL&&tc->rchild==NULL)
cout<<tc->data<<" ";
display1(tc->lchild);
display1(tc->rchild);
}
}
int display7(mytree *tl)
{
if(tl==NULL)
return 0;
return
max(display7(tl->lchild), display7(tl->rchild))+1;
}
int main()
{
int t;
//int res;
cin >>t;
while (t--)
{
mytree* test;
create(test);
cout<<display7(test)<<endl;
}
}
DS二叉树——二叉树之数组存储
/*#include<bits/stdc++.h>
#include<queue>
using namespace std;
typedef struct mynode
{
char data;
struct mynode* parent;
struct mynode* lchild;
struct mynode* rchild;
}mytree;
//int cnt=0;
mynode *pre=NULL;
void create(mytree *&te)
{
char sc;
cin >>sc;
if (sc=='0')
te=NULL;
else
{
te= new mynode;
te->data = sc;
te->parent=pre;
pre=te;
create(te->lchild);
pre=te;
create(te->rchild);
}
}
void display1(mytree* tc)
{
if (tc!=NULL)
{
if(tc->lchild==NULL&&tc->rchild==NULL)
cout<<tc->data<<" ";
display1(tc->lchild);
display1(tc->rchild);
}
}
void display2(mytree* tb)
{
if (tb!=NULL)
{
display2(tb->lchild);
cout << tb->data;
display2(tb->rchild);
}
}
void display3(mytree* tg)
{
if (tg!=NULL)
{
display3(tg->lchild);
display2(tg->rchild);
cout << tg->data;
}
}
void display4(mytree* tc)
{
if (tc!=NULL)
{
if(tc->lchild==NULL&&tc->rchild==NULL)
cout<<tc->parent->data<<" ";
display4(tc->lchild);
display4(tc->rchild);
}
}
void display5 (mytree* tp)
{
queue<mytree*> tq;
mytree *cs;
tq.push(tp);
while(!tq.empty())
{
cs=tq.front();
cout<<cs->data;
if(cs->lchild)
tq.push(cs->lchild);
if(cs->rchild)
tq.push(cs->rchild);
tq.pop();
}
}
void display6 (mytree* tp)
{
if (tc!=NULL)
{
if(tc->lchild==NULL&&tc->rchild==NULL)
cout<<tc->data<<" ";
display1(tc->lchild);
display1(tc->rchild);
}
}
int main()
{
int t;
//int res;
cin >>t;
while (t--)
{
mytree* test;
create(test);
display5(test);
cout<<endl;
}
}*/
#include<bits/stdc++.h>
using namespace std;
class mytree
{
private:
int length;
int *treed;
public:
mytree()
{
cin >> length;
treed = new int[length];
int i;
for (i=1;i<=length;i++)
{
cin >> treed[i];
}
}
void display1(int i)
{
if (treed[i]!= 0&&i<= length)
{
cout << treed[i] << " ";
display1(2*i);
display1(2*i +1);
}
}
};
int main()
{
int t;
cin >> t;
while (t--)
{
mytree tcs;
tcs.display1(1);
cout << endl;
}
}
(2)树结构与应用:
树的先序遍历(双亲转先序)
#include<bits/stdc++.h>
using namespace std;
typedef struct node
{
char data;
int parent;
} treenode;
typedef struct tree
{
node tnode[145];
int n;
} tree;
void ini(tree &exa)
{
int i;
for(i=0; i<145; i++)
{
exa.tnode[i].data='0';
exa.tnode[i].parent=-1;
}
exa.n=0;
}
void create(tree &exa)
{
char nodeval;
int parentval;
cin>>exa.n;
int i;
for(i=0; i<exa.n; i++)
{
cin>>nodeval;
if(nodeval!='0')
{
exa.tnode[i].data=nodeval;
}
}
for(i=0; i<exa.n; i++)
{
cin>>parentval;
if(parentval!=-1)
{
exa.tnode[i].parent=parentval;
}
}
}
void findparent(tree exa,int i)
{
int j;
for(j=0; j<exa.n; j++)
{
if(exa.tnode[j].parent==i)
{
cout<<exa.tnode[j].data;
findparent(exa,j);
}
}
//cout<<endl;
}
int main()
{
int t;
cin>>t;
while(t--)
{
tree asou;
ini(asou);
create(asou);
findparent(asou,-1);
cout<<endl;
}
}
树的后根遍历(孩子链表法)
#include<bits/stdc++.h>
using namespace std;
struct tnode
{
char cdata;
int ch[100];
};
void backdisplay(tnode exa[], int p)
{
int j;
for (j=0; exa[p].ch[j]!=-1; j++)
backdisplay(exa, exa[p].ch[j]);
cout << exa[p].cdata;
}
int main()
{
int num, mum;
cin >> num >> mum;
tnode asou[num];
int i;
int j;
for (i=0; i<num; i++)
{
cin >> asou[i].cdata;
for (j = 0;; j++)
{
cin >> asou[i].ch[j];
if (asou[i].ch[j] == -1)
break;
}
}
backdisplay(asou,mum);
}
树结构转换(先序转双亲)
/*#include<bits/stdc++.h>
#include<queue>
using namespace std;
typedef struct mynode
{
char data;
struct mynode* parent;
struct mynode* lchild;
struct mynode* rchild;
}mytree;
//int cnt=0;
mynode *pre=NULL;
void create(mytree *&te)
{
char sc;
cin >>sc;
if (sc=='0')
te=NULL;
else
{
te= new mynode;
te->data = sc;
te->parent=pre;
pre=te;
create(te->lchild);
pre=te;
create(te->rchild);
}
}
void display1(mytree* tc)
{
if (tc!=NULL)
{
if(tc->lchild==NULL&&tc->rchild==NULL)
cout<<tc->data<<" ";
display1(tc->lchild);
display1(tc->rchild);
}
}
void display2(mytree* tb)
{
if (tb!=NULL)
{
display2(tb->lchild);
cout << tb->data;
display2(tb->rchild);
}
}
void display3(mytree* tg)
{
if (tg!=NULL)
{
display3(tg->lchild);
display2(tg->rchild);
cout << tg->data;
}
}
void display4(mytree* tc)
{
if (tc!=NULL)
{
if(tc->lchild==NULL&&tc->rchild==NULL)
cout<<tc->parent->data<<" ";
display4(tc->lchild);
display4(tc->rchild);
}
}
void display5 (mytree* tp)
{
queue<mytree*> tq;
mytree *cs;
tq.push(tp);
while(!tq.empty())
{
cs=tq.front();
cout<<cs->data;
if(cs->lchild)
tq.push(cs->lchild);
if(cs->rchild)
tq.push(cs->rchild);
tq.pop();
}
}
void display6 (mytree* tp)
{
if (tc!=NULL)
{
if(tc->lchild==NULL&&tc->rchild==NULL)
cout<<tc->data<<" ";
display1(tc->lchild);
display1(tc->rchild);
}
}
int main()
{
int t;
//int res;
cin >>t;
while (t--)
{
mytree* test;
create(test);
display5(test);
cout<<endl;
}
}*/
#include<bits/stdc++.h>
using namespace std;
class mynode
{
public:
char data;
mynode *lchild;
mynode *rchild;
mynode(char rval)
{
data=rval;
lchild=NULL;
rchild=NULL;
}
};
class mytree
{
private:
mynode *root;
int length;
void create(mynode *&asou);
public:
mytree()
{
root=NULL;
length=0;
}
void create()
{
create(root);
}
mynode *getroot()
{
return root;
}
int getlength()
{
return length;
}
};
void mytree::create(mynode *&asoul)
{
char ch;
cin>>ch;
if (ch!='#')
{
length++;
asoul=new mynode(ch);
create(asoul->lchild);
create(asoul->rchild);
}
else
{
asoul=NULL;
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
mytree asou;
asou.create();
struct momfuf
{
mynode *num;
int pum;
} pitao[asou.getlength()];
pitao[0].num = asou.getroot();
pitao[0].pum = -1;
int i;
int j;
int z;
for (i = 0, j = 1; i < asou.getlength(); i++)
{
if (pitao[i].num->lchild)
{
pitao[j].num = pitao[i].num->lchild;
pitao[j++].pum = i;
}
if (pitao[i].num->rchild)
{
pitao[j].num = pitao[i].num->rchild;
pitao[j++].pum = i;
}
}
for (j=0; j<asou.getlength()- 1; j++)
cout << pitao[j].num->data << ' ';
cout << pitao[j].num->data << endl;
for (z=0; z<asou.getlength()-1; z++)
cout << pitao[z].pum << ' ';
cout << pitao[z].pum << endl;
}
}
树的双亲结构转孩子链表结构
#include<bits/stdc++.h>
using namespace std;
typedef struct node
{
char data;
int parent;
} treenode;
typedef struct tree
{
node tnode[145];
int n;
} tree;
void ini(tree &exa)
{
int i;
for(i=0; i<145; i++)
{
exa.tnode[i].data='0';
exa.tnode[i].parent=-1;
}
exa.n=0;
}
void create(tree &exa)
{
char nodeval;
int parentval;
cin>>exa.n;
int i;
for(i=0; i<exa.n; i++)
{
cin>>nodeval;
if(nodeval!='0')
{
exa.tnode[i].data=nodeval;
}
}
for(i=0; i<exa.n; i++)
{
cin>>parentval;
if(parentval!=-1)
{
exa.tnode[i].parent=parentval;
}
}
}
void findparent(tree exa,int i)
{
int j;
for(j=0; j<exa.n; j++)
{
if(exa.tnode[j].parent==i)
{
cout<<exa.tnode[j].data;
findparent(exa,j);
}
}
//cout<<endl;
}
void trans(tree exa)
{
int i;
int j=0;
int k;
int temp=0;
for(k=0; k<exa.n; k++)
{
for(i=0; i<exa.n; i++)
{
if(exa.tnode[i].parent==j)
{
if(temp==0)
{
cout<<exa.tnode[j].data<<" ";
temp=1;
}
cout<<i<<" ";
}
}
if(temp==0)
cout<<exa.tnode[k].data<<" "<<"-1"<<" ";
temp=0;
cout<<endl;
j++;
}
}
int main()
{
tree asou;
ini(asou);
create(asou);
trans(asou);
}
DS森林叶子编码
/*#include<bits/stdc++.h>
using namespace std;
int num;
int bum;
class mynode1
{
private:
char data;
mynode1 **child;
public:
mynode1()
{
child=new mynode1*[bum];
int i;
for(i=0; i<bum; i++)
child[i]=NULL;
}
friend class Forest;
};
class mynode2
{
private:
char data;
mynode2 *lchild;
mynode2 *rchild;
public:
mynode2()
{
lchild=NULL;
rchild=NULL;
}
friend class Forest;
};
class Forest
{
protected:
mynode1 **rroot1;
mynode2 **rroot2;
mynode1* cre()
{
mynode1 *as;
int i;
char ch;
cin>>ch;
if(ch!='0')
{
as=new mynode1;
as->data=ch;
for(i=0; i<bum; i++)
as->child[i]=cre();
}
else
as=NULL;
return as;
}
void transfor(mynode1 *as,mynode2 *&bs,int j)
{
if(j<bum&&as->child[j])
{
bs=new mynode2;
bs->data=as->child[j]->data;
transfor(as, bs->rchild, j+1);
transfor(as->child[j], bs->lchild, 0);
}
else
bs=NULL;
}
void res(mynode2 *ds,string cs)
{
int j;
if(ds)
{
res(ds->lchild, cs+'0');
res(ds->rchild, cs+'1');
if(!ds->lchild&&!ds->rchild)
{
int length;
length=cs.length() ;
for(j=0; j<length; j++)
{
if(j!=length-1)
cout<<cs[j]<<" ";
else
cout<<cs[j]<<endl;
}
}
}
}
public:
Forest()
{
rroot1=new mynode1*[num];
rroot2=new mynode2*[num];
}
void chuangjian()
{
int z;
for(z=0; z<num; z++)
rroot1[z]=cre();
}
void transfor()
{
int o;
int j;
for(j=0; j<num;j++)
{
rroot2[j]=new mynode2;
rroot2[j]->data=rroot1[j]->data;
transfor(rroot1[j],rroot2[j]->lchild,0);
}
for(o=0; o<num-1; o++)
rroot2[o]->rchild=rroot2[o+1];
}
void res()
{
string pp;
res(rroot2[0],pp);
}
};
int main()
{
cin>>num;
cin>>bum;
Forest asou;
asou.chuangjian();
asou.transfor();
asou.res();
}*/
#include<bits/stdc++.h>
using namespace std;
#define op 0
int bum;
int num;
class mynode1
{
public:
char data;
mynode1 **child;
mynode1()
{
child = new mynode1*[bum];
}
friend class mytree1;
friend class mytree2;
};
class mynode2
{
public:
char data;
mynode2 *fchild;
mynode2 *nchild;
friend class mytree1;
friend class mytree2;
};
class mytree1
{
public:
mynode1 *rroot;
string s;
int pods;
mytree1(string sval)
{
s.assign(sval);
pods = op;
rroot = cre();
}
mynode1* cre()
{
int i;
if(s[pods] == '0')
{
pods ++;
return NULL;
}
mynode1 *cs = new mynode1;
cs->data = s[pods];
pods++;
for(i = 0; i < bum; i ++)
{
cs->child[i] = cre();
}
return cs;
}
mynode2* transfor(mynode1 *as)
{
int i;
mynode2 *cs =NULL;
if(as)
{
cs = new mynode2;
cs->data = as->data;
int flag = 0;
while(!as->child[flag] && flag < bum)
flag ++;
if(flag != bum)
cs->fchild = transfor(as->child[flag]);
else
cs->fchild = transfor(NULL);
if(cs->fchild)
{
mynode2 *ds = cs->fchild;
for( i =flag+1; i <bum; i ++)
{
ds->nchild = transfor(as->child[i]);
if(ds->nchild)
ds= ds->nchild;
}
}
}
return cs;
}
};
class mytree2
{
public:
mynode2 *rroot;
mytree2(mytree1 ps)
{
rroot = ps.transfor(ps.rroot);
}
};
void res(mynode2 *cs, string pp)
{
if(!cs->fchild && !cs->nchild)
{
int j;
for(j= 0; j < pp.size(); j ++)
{
if(j)
cout << " ";
cout << pp[j];
}
cout << endl;
}
if(cs->fchild)
res(cs->fchild, pp+ '0');
if(cs->nchild)
res(cs->nchild, pp + '1');
}
int main()
{
cin >> num >> bum;
mynode2 *ls;
int i;
for(i =0; i <num; i ++)
{
string str;
char opc;
while(cin >> opc)
{
str+=opc;
if(getchar() == '\n')
break;
}
mytree1 asou(str);
mytree2 bsou(asou);
if(i != 0)
{
mynode2 *rs = ls;
while(rs->nchild)
rs = rs->nchild;
rs->nchild = bsou.rroot;
}
else
{
ls = bsou.rroot;
}
}
res(ls,"");
return 0;
}
F. 先序+中序还原二叉树
#include<bits/stdc++.h>
using namespace std;
typedef struct mynode
{
char data;
mynode *lchild;
mynode *rchild;
} mynode;
char qian[64];
char li[78];
mynode *create(char *qian,char *li,int length)
{
mynode *asou=new mynode();
if(length==0)
return NULL;
asou->data=qian[0];
int i;
int j;
for(j=0; j<length; j++)
if(li[j]==qian[0])
break;
asou->lchild=create(qian+1,li,j);
asou->rchild=create(qian+j+1,li+j+1,length-j-1);
return asou;
}
int height(mynode *exa)
{
int depth=0;
if(exa)
{
int ld;
ld=height(exa->lchild);
int rd;
rd=height(exa->rchild);
if(ld>rd)
depth=ld+1;
else
depth=rd+1;
}
return depth;
}
int main()
{
int num;
cin>>num;
cin>>qian;
cin>>li;
mynode *asx=create(qian,li,num);
cout<<height(asx);
}
(3)赫夫曼编码及应用:
DS二叉树--赫夫曼树的构建与编码
#include<bits/stdc++.h>
using namespace std;
const int maxnn= 10086;
class hnode
{
public:
int wei;
int par;
int lchild;
int rchild;
};
class htree
{
public:
int length;
int lnumth;
hnode *p;
string *c;
void sec(int ins, int* sc1, int* sc2)
{
int ws1, ws2;
ws1 = ws2 = maxnn;
*sc1 = *sc2 = 0;
for (int i = 1; i <= ins; ++i)
{
if ( p[i].wei < ws1 && p[i].par== 0)
{
ws2 = ws1;
*sc2 = *sc1;
ws1 = p[i].wei;
*sc1 = i;
}
else if(p[i].wei<ws2 && p[i].par==0)
{
ws2 = p[i].wei;
*sc2 = i;
}
}
}
void create(int ns, int w[])
{
lnumth = ns;
length = 2*ns-1;
c = new string[lnumth + 1];
p = new hnode[2 * ns];
int i;
for (i = 1; i <= lnumth; i++)
p[i].wei = w[i - 1];
for (i = 1; i <= length; i++)
{
if (i > lnumth)
p[i].wei=0;
p[i].par=0;
p[i].lchild = 0;
p[i].rchild = 0;
}
create();
}
void create()
{
int j, sc1, sc2;
for (j = lnumth + 1; j <= length; j++)
{
sec(j- 1, &sc1, &sc2);
p[j].lchild = sc1;
p[j].rchild = sc2;
p[j].wei =p[sc1].wei+p[sc2].wei;
p[sc1].par = j;
p[sc2].par = j;
}
}
void cod();
};
void htree::cod()
{
char *csd;
int cs, fs, begin;
csd = new char[lnumth];
csd[lnumth - 1] = '\0';
for (int i = 1; i <= lnumth; i++)
{
begin= lnumth - 1;
for (cs= i, fs=p[i].par; fs!= 0; cs = fs, fs = p[fs].par)
{
if (p[fs].lchild == cs)
{
csd[--begin] = '0';
}
else
{
csd[--begin] = '1';
}
}
c[i] = new char[lnumth - begin];
c[i].assign(&csd[begin]); // 把cd中从start到末尾的编码复制到huffCode中
}
}
int main()
{
int nu, i,j;
int wct[486];
htree h;
cin >> nu;
for (i= 0; i < nu; i++)
{
cin >> wct[i];
}
h.create(nu, wct);
h.cod();
for (j = 1; j <= nu; j++)
{
cout << h.p[j].wei << '-';
cout << h.c[j] << endl;
}
}
DS二叉树--赫夫曼树解码
#include<bits/stdc++.h>
using namespace std;
const int maxnn= 10086;
class hnode
{
public:
int wei;
int par;
int lchild;
int rchild;
char uu;
};
class htree
{
public:
int length;
int lnumth;
hnode *p;
string *c;
int dcod(string cstr, char tstr[])
{
int i, kj, cs;
char chr;
cs= length;
kj=0;
for (i =0; i < cstr.length(); i++)
{
chr= cstr[i];
if (chr == '0')
{
cs = p[cs].lchild;
}
else if (chr == '1')
{
cs= p[cs].rchild;
}
else if (chr!= '0' && chr!='1')
{
return -5;
}
if (p[cs].rchild == 0&& p[cs].lchild == 0 )
{
tstr[kj] = p[cs].uu;
kj++;
cs = length;
}
else
{
chr = '\0';
}
}
if (chr == '\0')
return -6;
else
tstr[kj] = '\0';
return 2;
}
void sec(int ins, int* sc1, int* sc2)
{
int ws1, ws2;
ws1 = ws2 = maxnn;
*sc1 = *sc2 = 0;
for (int i = 1; i <= ins; ++i)
{
if ( p[i].wei < ws1 && p[i].par== 0)
{
ws2 = ws1;
*sc2 = *sc1;
ws1 = p[i].wei;
*sc1 = i;
}
else if(p[i].wei<ws2 && p[i].par==0)
{
ws2 = p[i].wei;
*sc2 = i;
}
}
}
void create(int ns, int w[],char u[])
{
lnumth = ns;
length = 2*ns-1;
c = new string[lnumth + 1];
p = new hnode[2 * ns];
int i;
for (i = 1; i <= lnumth; i++)
{
p[i].wei = w[i - 1];
p[i].uu = u[i - 1];
}
for (i = 1; i <= length; i++)
{
if (i > lnumth)
{
p[i].wei=0;
p[i].uu='0';
}
p[i].par=0;
p[i].lchild = 0;
p[i].rchild = 0;
}
create();
}
void create()
{
int j, sc1, sc2;
for (j = lnumth + 1; j <= length; j++)
{
sec(j- 1, &sc1, &sc2);
p[j].lchild = sc1;
p[j].rchild = sc2;
p[j].wei =p[sc1].wei+p[sc2].wei;
p[sc1].par = j;
p[sc2].par = j;
}
}
void cod();
};
void htree::cod()
{
char *csd;
int cs, fs, begin;
csd = new char[lnumth];
csd[lnumth - 1] = '\0';
for (int i = 1; i <= lnumth; i++)
{
begin= lnumth - 1;
for (cs= i, fs=p[i].par; fs!= 0; cs = fs, fs = p[fs].par)
{
if (p[fs].lchild == cs)
{
csd[--begin] = '0';
}
else
{
csd[--begin] = '1';
}
}
c[i] = new char[lnumth - begin];
c[i].assign(&csd[begin]);
}
}
int main()
{
int nu, i,j;
int t;
string op;
int wct[486];
char yu[154];
char ous[775];
htree h;
cin >> nu;
for (i= 0; i < nu; i++)
{
cin >> wct[i];
}
for (i= 0; i < nu; i++)
{
cin >> yu[i];
}
h.create(nu, wct,yu);
h.cod();
/*for (j = 1; j <= nu; j++)
{
cout << h.p[j].wei << '-';
cout << h.c[j] << endl;
}*/
cin >> t;
while(t--)
{
cin >> op;
if (h.dcod(op, ous) ==2)
{
cout << ous << endl;
}
else
{
cout << "error" << endl;
}
}
}
DS树--带权路径和
#include<bits/stdc++.h>
using namespace std;
class hnode
{
public:
char data;
int wei;
hnode *lchild;
hnode *rchild;
hnode()
{
wei=0;
lchild=NULL;
rchild=NULL;
}
};
class htree
{
public:
hnode *rroot;
int lnum;
string str3;
int *str4;
htree()
{
int j;
cin>>str3;
cin>>lnum;
str4 = new int[lnum];
for(j=0; j<lnum; j++)
cin>>str4[j];
}
void create1(hnode *&ps, int &is, int &js)
{
if(str3[is]=='0')
{
ps=NULL;
}
else
{
ps = new hnode;
if(str3[is]>=65 && str3[is]<90)
{
ps->data = str3[is];
ps->wei = str4[js];
js++;
}
is=is+1;
create1(ps->lchild, is, js);
is=is+1;
create1(ps->rchild, is, js);
}
}
void create2();
void p1();
void p2(hnode *ps, int lev, int &tr)
{
if(ps)
{
if(!ps->lchild && !ps->rchild)
{
tr += ps->wei*lev;
}
lev=lev+1;
p2(ps->lchild, lev, tr);
p2(ps->rchild, lev, tr);
}
}
};
/*void htree::create1(hnode *&ps, int &is, int &js)
{
if(str3[is]=='0')
{
ps=new hnode;
ps=NULL;
}
else
{
ps = new hnode;
if(str3[is]>=86 && str3[is]<=105)
{
ps->data = str3[is];
p->wei = str4[js];
js++;
}
is=is+1;
create1(ps->lchild, is, js);
is=is+1;
create1(ps->rchild, is, js);
}
}*/
void htree::create2()
{
int js;
js=0;
int is;
is=0;
create1(rroot, js, is);
}
void htree::p1()
{
int lev;
lev=0;
int rd=0;
p2(rroot, lev, rd);
cout<<rd<<endl;
}
int main()
{
int t;
cin>>t;
while(t--)
{
htree mt;
mt.create2();
mt.p1();
}
}
DS树--二叉树之最大路径
#include<bits/stdc++.h>
using namespace std;
int maxnn=0;
int pos123= 0 ;
class hnode
{
public:
char data;
int wei;
hnode *lchild;
hnode *rchild;
hnode *par;
hnode()
{
wei=0;
lchild=NULL;
rchild=NULL;
par=NULL;
}
};
class htree
{
public:
hnode *rroot;
int lnum;
string str3;
int *str4;
int poss;
string st;
void fc()
{
fc(rroot);
}
/*htree()
{
int j;
cin>>str3;
cin>>lnum;
str4 = new int[lnum];
for(j=0; j<lnum; j++)
cin>>str4[j];
}*/
void fc(hnode *ts)
{
if(ts)
{
fc(ts->lchild);
fc(ts->rchild);
if(ts->lchild == NULL && ts->rchild == NULL)
{
int he;
he= fp(ts,0);
he+= rroot->wei;
if(he>maxnn)
maxnn = he;
}
}
}
void create1(hnode *&ps, int &is, int &js)
{
if(str3[is]=='0')
{
ps=NULL;
}
else
{
ps = new hnode;
if(str3[is]>=65 && str3[is]<90)
{
ps->data = str3[is];
ps->wei = str4[js];
js++;
}
is=is+1;
create1(ps->lchild, is, js);
is=is+1;
create1(ps->rchild, is, js);
}
}
void create2();
void p1();
void p2(hnode *ps, int lev, int &tr)
{
if(ps)
{
if(!ps->lchild && !ps->rchild)
{
tr += ps->wei*lev;
}
lev=lev+1;
p2(ps->lchild, lev, tr);
p2(ps->rchild, lev, tr);
}
}
int fp(hnode *ts, int he)
{
if(ts!=rroot)
{
he+= ts->wei;
he=fp(ts->par,he);
}
return he;
}
void c1(string stc,int *acs)
{
poss = 0;
st.assign(stc);
rroot = c2(acs);
}
hnode *c2(int *ac)
{
hnode *ts;
char chr;
chr = st[poss++];
if(chr != '0')
{
ts = new hnode();
ts->data = chr;
ts->wei = ac[pos123++];
ts->lchild = c2(ac);
if(ts->lchild)
ts->lchild->par = ts;
ts->rchild = c2(ac);
if(ts->rchild)
ts->rchild->par =ts;
}
else
ts=NULL;
return ts;
}
};
/*void htree::create1(hnode *&ps, int &is, int &js)
{
if(str3[is]=='0')
{
ps=new hnode;
ps=NULL;
}
else
{
ps = new hnode;
if(str3[is]>=86 && str3[is]<=105)
{
ps->data = str3[is];
p->wei = str4[js];
js++;
}
is=is+1;
create1(ps->lchild, is, js);
is=is+1;
create1(ps->rchild, is, js);
}
}*/
void htree::create2()
{
int js;
js=0;
int is;
is=0;
create1(rroot, js, is);
}
void htree::p1()
{
int lev;
lev=0;
int rd=0;
p2(rroot, lev, rd);
cout<<rd<<endl;
}
int main()
{
int t;
cin>>t;
while(t--)
{
maxnn=-890;
pos123= 0;
string scs;
cin>>scs;
int num;
cin>>num;
int asou[num];
int i;
for(i=0; i<num; i++)
cin>>asou[i];
htree mytree;
mytree.c1(scs,asou);
mytree.fc();
cout<<maxnn<<endl;
}
}