//常见数据结构基础
//1.链表
struct node{//链表结点
int data;
node* next;
};
//创建链表
node* create(int Array[])
{
node * p,* pre,*head;
head=new node;//创建头结点,new相当于malloc;
head->next=NULL;
pre=head;
for(int i=0;i<5;i++)
{
p=new node;//新建结点
p->data=Array[i];
p->next=NULL;
pre->next=p;
pre=p;
}
return head;
}
//查找链表
//以head为头结点的链表计数元素个数x的个数
int search(node* head,int x)
{
int count=0;
node* p=head->next;
while(p!=NULL)
{
if(p->data==x) count++;
}
p=p->next;
return count;//返回x的个数
}
//插入链表
//将x插入以head为头结点的链表的第pos个位置上
void insert(node* head,int post,int x)
{
node* p=head;
for(int i=0;i<pos-1;i++)
{
p->next;
} //p在post
node* q=new node;
q->data=x;
q->next=p->next;
p->next=q;//p->q->(p->next)
}
}
//删除元素
//删除head头结点链表中所有数据域为x的结点
void del(node* head,int x)
{
node* p=head->next;
node* pre=head;//p是要删除的,pre为p的前一个
while(p!=NULL)
{
if(p->data==x)
{
pre->next=p->next;
delete(p);
p=pre->next;//重新将p指向pre->next,为下一次删除做准备
} else{
pre=p;
p=p->next;
}
}
}
//静态链表
struct Node{
typename data;
int next;
}node[size];
//2.搜索
//DFS:
void DFS(int index,int nowk,int sum,int facSum)
{
//当前访问fac[index],nowk 选中个数,sum选中数的和,facSum选中底数和
if(sum==n&&nowk==k)
{
if(facSum>maxFacSum)
{
ans=temp;
maxFacSum=facSum;
}
return;
}
if(sum>n||nowk>k)return;
if(index-1>=0)
{ //fac[0]不需要选择
temp.push_back(index);
DFS(index,nowk+1,sum+fac[index],facSum+index);//选
temp.pop_back();
DFS(index-1,nowk,sum,facSum);//不选
}
}
//BFS:
int BFS(int x,int y,int z)
{
int tot=0;
queue<node> Q;
Node.x=x,Node.y=y,Node.z=z;
Q.push(Node);
inq[x][y][z]=true;
while(!Q.empty())
{
node top=Q.front();
Q.pop();
tot++;
for(int i=0;i<6;i++)
{
int newX=top.x+X[i];
int newY=top.y+Y[i];
int newZ=top.z+Y[i];
if(judge(newX,newY,newZ)){
Node.x=newX,Node.y=newY,Node.z=newZ;
Q.push(Node);
inq[newX][newY][newZ]=true;
}
}
}
/*
void BFS(int s)
{
queue<int> q;
q.push(s);
while(!q.empty())
{
取出队首元素
访问队首元素
将队首元素出队
将top的下一层节点中未曾入队的结点全部入队,并设置为已入队
}
}
*/
//二叉树
//定义
struct node{
typename data;
node* lchild;
node* rchild;
};
//新建结点
node* newNode(int v)
{
node* Node=new node;
Node->data=v;
Node->lchild=Node->rchild=NULL;
return Node;
}
//查找结点
void search(node* root,int x,int newdata)
{ //找到数据为x的,改成newdata
if(root==NULL) return;
if(root->data==x) root->data=newdata;
search(root->lchild,x,newdata);
search(root->rchild,x,newdata);
}
//插入结点
void insert(node* &root,int x)
{
if(root==NULL){root=newNode(x);return;}
if(x在左子树) insert(root->lchild,x);
else insert(root->rchild,x);
}
//二叉树创建
node* Create(inr data[],int n)
{
node* root=NULL;
for(int i=0;i<n;i++)
{
insert(root,data[i]);
}
return root;
}
//先序遍历
void preorder(node* root,vector<int>& vi)
{
if(root==NULL) return;
vi.push_back(root->data);
preorder(root->lchild,vi);
preorder(root->rchild,vi);
}
//中序遍历
void inorder(node* root,vector<int>& vi)
{
if(root==NULL) return;
inorder(root->lchild,vi);
vi.push_back(root->data);
inorder(root->rchild,vi);
}
//后序遍历
void postorder(node* root,vector<int>& vi)
{
if(root==NULL) return;
postorder(root->lchild,vi);
postorder(root->rchild,vi);
vi.push_back(root->data);
}
//层序遍历
struct node{
int data;
int layer;
node* lchild;
node* rchild;
};
void LayerOrder(node* root)
{
queue<node*>q;
root->layer=1;
q.push(root);
while(!q.empty())
{
node* now=q.front();
q.pop();
printf("%d ",now->data);
if(now->lchild!=NULL)
{
now->lchild->layer=now->layer+1;
q.push(now->lchild);
}
if(now->rchild!=NULL)
{
now->rchild->layer=now->layer+1;
q.push(now->rchild);
}
}
}
//给定一颗二叉树的先序遍历和中序遍历,重建这棵二叉树
node* create(int preL,int preR,int inL,int inR)
{
if(preL>preR) return NULL;
node* root=new node;
root->data=pre[preL];
for(k=inL;k<=inR;k++)
{
if(in[k]==pre[preL])break;
}
int numLeft=k-inL;
root->lchild=create(preL+1,preL+numLeft,inL,k-1);
root->rchild=create(preL+numLeft+1,preR,k+1,inR);
return root;
}
//3.二叉查找树
//查找
void search(node* root,int x)
{
if(root==NULL) {printf("search failed\n");return}
if(x==root->data) printf("%d\n",x);
else if(x<root->data) search(root->lchild,x);
else search(root->rchild,x);
}
//插入
void insert(node* &root,int x)
{
if(root==NULL){root=newNode(x);return;}
if(x==root->data) return;
else if(x<root->data) insert(root->lchild,x);
else insert(root->rchild,x);
}
//建树
node* Create(int data[],int n)
{
node* root=NULL;
for(int i=0;i<n;i++)
{
insert(root,data[i]);
}
return root;
}
//找最大值的结点
node* findMax(node* root)
{
while(root->rchild!=NULL)
root=root->rchild;
return root;
}
//4.dijkstra
//伪代码:
/*
Dijkstra(G,d[],s)
{
初始化;
for(循环n次)
{
u=使d[u]最小的还未被访问的定点标号;
记u已被访问
for(从u出发能到达的所有顶点v)
{
if(v从未被访问&&以u为中介点使u到顶点v的最短距离d[v]更优)
{
优化d[v];
}
}
}
}
*/
const int MAXV=1000;
const int INF=1000000000;
int n,G[MAXV][MAXV]; //n为顶点数
int d[MAXV]; //起点到各点最短路径长度
bool vis[MAXV]={false}//标记数组,vis[i]==true表示已经访问,初值都是false
void Dijkstra(int a)
{
fill(d,d+MAXV,INF);
d[s]=0;
for(int i=0;i<n;i++)
{
int u=-1;MIN=INF;
for(int j=0;j<n;j++)
{
if(vis[j]==false&&d[j]<MIN)
{
u=j;
MIN=d[j];
}
}
}
if(u==-1)return;
vis[u]=true;
for(int v=0;v<n;v++)
{
if(vis[v]==false&&G[u][v]!=INF&&d[u]+G[u][v]<d[v])
{
d[v]=d[u]+G[u][v];
}
}
}