吉林大学数据结构--图的深度优先遍历,图的广度优先遍历,图的创建(邻接表)

#include <stdio.h>
#include <stdlib.h>
typedef struct edge{
int veradj;
int cost;
struct edge*link;
}edge;
typedef struct head{
int vername;
edge*adjacent;
}head;

edge*creatnode(edge*p,int c,int a){//创建顶点
p=(edge*)malloc(sizeof(edge));
p->veradj=c;
p->cost=a;
p->link=NULL;
return p;
}
head*build(int n,head*headt[]){//创建邻接图
int i=0;
for(i=0;i<n;i++)
{headt[i]=(head*)malloc(sizeof(head));
headt[i]->vername=i;
headt[i]->adjacent=NULL;}
int c;int cost;
edge*p,*q;
for(i=0;i<n;i++)
{
scanf("%d%d",&c,&cost);
while(c!=-1){
p=creatnode(p,c,cost);
if(headt[i]->adjacent==NULL)
      {headt[i]->adjacent=p;
      q=p;}
      else{
            q->link=p;
            q=p;
      }
      scanf("%d%d",&c,&cost);
}

}
return headt[0];
}
void print(head*a[],int n){
int i=0;
edge*p;
for(i=0;i<n;i++)
{p=a[i]->adjacent;
printf("%d:\t",i);
while(p!=NULL){
      printf("%d\t%d\t",p->veradj,p->cost);
      p=p->link;
}
printf("\n");
}
}
void DFS(head*headt[],int v,int visit[]){//图的深度优先遍历(递归)
printf("%d\t",headt[v]->vername);
visit[v]=1;
edge*p;
p=headt[v]->adjacent;
while(p!=NULL){
            if(visit[p->veradj]!=1)
   DFS(headt,p->veradj,visit);
      p=p->link;
}
}
void DFS_k(head*headt[],int v,int visit[]){//图的深度优先遍历算法(辅助堆栈)//headt[]头链表,v开始是节点的序号,visit[]是否被访问,
int i=-1;
int s[10];
s[++i]=v;
edge*p;
while(i!=-1){
      v=s[i];
      i--;
if(visit[v]==0){
      printf("%d\t",v);//写成打印headt[v]->vername更严谨
      p=headt[v]->adjacent;
      visit[v]=1;
}while(p){
if(visit[p->veradj]==0)
      {s[++i]=p->veradj;}
p=p->link;
}
}
}
void DFS_KS(head*headt[],int v,int visit[]){//图的深度优先遍历算法(处理节点不是整形的情况)//没有必要
int i=-1;
edge*s[10];
edge*p;
p=headt[v];
s[++i]=p;
while(i!=-1){
p=s[i];
i--;
if(visit[p->veradj]==0)
{
      printf("%d\t",p->veradj);
      visit[p->veradj]=1;
      p=headt[p->veradj]->adjacent;
}
while(p){
if(visit[p->veradj]==0)
s[++i]=p;
p=p->link;
}
}
}
void BFS(head*headt[],int v,int visit[]){//图的非递归广度优先遍历
int s[10];
int count=0,top=0,tail=0;
printf("%d\t",v);//当然写成打印headt[v]->vername更严谨点
visit[v]=1;
s[tail]=v;
tail=(tail+1)%10;
count++;
edge*p;
while(count!=0){
v=s[top];
top=(top+1)%10;
count--;
p=headt[v]->adjacent;
while(p){
if(visit[p->veradj]==0)
  {printf("%d\t",p->veradj);
  s[tail]=p->veradj;
  tail=(tail+1)%10;
  count++;
  visit[p->veradj]=1;
  }
      p=p->link;
}  
}
}
int main()
{  int n,i;
scanf("%d",&n);
    head*headt[10];
    int visit[100];
    for(i=0;i<n;i++)
      visit[i]=0;
   headt[0]=build(n,headt);
    print(headt,n);
    printf("\n");
    
    return 0;
}
/*4
1 0
2 0
3 0
-1 0
0 0
3 0
-1 0
0 0
3 0
-1 0
0 0
1 0
2 0
-1 0*///课本p168图6.6//联通图,调用一次深度,广度遍历即可
/*5
1 2
2 7
4 1
-1 0
-1 0
3 5
-1 0
-1 0
0 3
3 4
-1 0*///课本p168图6.5//非连通图,循环多次调用深度,广度遍历

/*在main中调用方式:
for(i=0;i<n;i++)
    {     if(visit[i]==0)
          DFS_k(headt,i,visit);}
*///非连通图

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一道简单的表达式求值题目,可以使用栈来实现。具体步骤如下: 1. 定义两个栈,一个用来存储数字,一个用来存储运算符。 2. 从左到右遍历表达式中的每个字符。 3. 如果当前字符是数字,则将其压入数字栈中。 4. 如果当前字符是运算符,则将其压入运算符栈中。 5. 如果当前字符是右括号,则从数字栈中弹出两个数字,从运算符栈中弹出一个运算符,进行计算,并将结果压入数字栈中。 6. 遍历完整个表达式后,数字栈中剩下的数字就是表达式的值。 下面是代码实现: ```python def calculate(s: str) -> int: num_stack = [] op_stack = [] i = 0 while i < len(s): if s[i].isdigit(): j = i while j < len(s) and s[j].isdigit(): j += 1 num_stack.append(int(s[i:j])) i = j elif s[i] == '+' or s[i] == '-': while op_stack and op_stack[-1] != '(': num2 = num_stack.pop() num1 = num_stack.pop() op = op_stack.pop() if op == '+': num_stack.append(num1 + num2) else: num_stack.append(num1 - num2) op_stack.append(s[i]) i += 1 elif s[i] == '*' or s[i] == '/': while op_stack and (op_stack[-1] == '*' or op_stack[-1] == '/'): num2 = num_stack.pop() num1 = num_stack.pop() op = op_stack.pop() if op == '*': num_stack.append(num1 * num2) else: num_stack.append(num1 // num2) op_stack.append(s[i]) i += 1 elif s[i] == '(': op_stack.append(s[i]) i += 1 elif s[i] == ')': while op_stack[-1] != '(': num2 = num_stack.pop() num1 = num_stack.pop() op = op_stack.pop() if op == '+': num_stack.append(num1 + num2) elif op == '-': num_stack.append(num1 - num2) elif op == '*': num_stack.append(num1 * num2) else: num_stack.append(num1 // num2) op_stack.pop() i += 1 else: i += 1 while op_stack: num2 = num_stack.pop() num1 = num_stack.pop() op = op_stack.pop() if op == '+': num_stack.append(num1 + num2) elif op == '-': num_stack.append(num1 - num2) elif op == '*': num_stack.append(num1 * num2) else: num_stack.append(num1 // num2) return num_stack[0] ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值