[笔记]:指针 链表 邻接表

1.指针

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
int main(){
    int a;
    scanf("%d",&a); 
    int *b;
    *b=a;
    cout<<*b;
    //直接cout a的话 系统就先找到a的地址 在输出a
    //而用 *b比用a更快 省了一步 
    return 0;
}

2.链表

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<malloc.h>//申请空间的头文件 
#define N 1000
using namespace std;
struct node{
    int data;
    struct node *next;//指针 
};
struct node *p,*head;//都是指针 
int main(){
    int n;
    scanf("%d",&n);
    p=(struct node*)malloc(sizeof(struct node));
    //申请一个struct node类型大小是sizeof(struct node)的空间
    int x;
    scanf("%d",&x);//第一个 
    p->data=x;
    p->next=NULL;
    head=p;//最重要的 头指针 
    for(int i=2;i<=n;i++){
        scanf("%d",&x);
        p=(struct node*)malloc(sizeof(struct node));
        p->data=x;
        //下面这两句挂链 最关键 不能颠倒  head指向最后的结构体  
        p->next=head;//后面来的结构体的next指向前面来的一个结构体 
        head=p;//head指向现在的p 
    }
    p=head;
    while(p!=NULL){//输出 从后倒着找 第一个next里是NULL 输出顺序是颠倒的 
        cout<<p->data<<" ";
        p=p->next;
    }
    return 0;
}

3.邻接表(深搜遍历)
给出无向图的顶点个数n和边数m
下面m行给出x,y,z表示x点到y的边的权值是z
如:
6 8
1 2 1
1 3 1
1 6 1
2 4 1
3 4 1
3 5 1
4 6 1
5 6 1
将图进行遍历

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define N 1000
using namespace std;
struct node{
    int data;
    int k; 
    struct node *next;
}; 
struct node *a[N],*p,*head;//同2.链表
bool flag[N];
void dfs(int x){//遍历过程用dfs
    flag[x]=1;//记录访问过
    struct node *q;
    q=a[x];//起始位置
    while(q!=NULL){
        int t=q->data;
        if(!flag[t]){
            cout<<x<<"->"<<t<<"="<<q->k<<endl;
            dfs(t);//没有走过就访问
        }
        q=q->next;//向下走
    }
}
int main() {
    int n,m;
    scanf("%d%d",&n,&m);
    int x,y,z;
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&x,&y,&z);//z是边的权值
        p=(struct node*)malloc(sizeof(struct node));
        //因为是无向图 x和y y和x 都要记录一遍
        p->data=y;
        p->k=z;
        p->next=a[x];
        a[x]=p;
        p=(struct node*)malloc(sizeof(struct node));
        p->data=x;
        p->k=z;
        p->next=a[y];
        a[y]=p;
    }
    dfs(1);
    return 0;
}

邻接表广搜遍历

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define N 1000
using namespace std;
queue<int>que;
struct node{
    int data;
    int k; 
    struct node *next;
}; 
struct node *a[N],*p,*head;
bool flag[N];
void bfs(int x){
    flag[x]=1;
    que.push(x);
    struct node *q;
    while(!que.empty()){
        q=a[que.front()];
        int s=que.front();
        que.pop();
        while(q!=NULL){
            int t=q->data;
            if(!flag[t]) {
                flag[t]=1;
                cout<<s<<"->"<<t<<"="<<q->k<<endl;
                que.push(t);
            }
            q=q->next;
        }
    }
}
int main() {
    int n,m;
    scanf("%d%d",&n,&m);
    int x,y,z;
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&x,&y,&z);
        p=(struct node*)malloc(sizeof(struct node));
        p->data=y;
        p->k=z;
        p->next=a[x];
        a[x]=p;
        p=(struct node*)malloc(sizeof(struct node));
        p->data=x;
        p->k=z;
        p->next=a[y];
        a[y]=p;
    }
    bfs(1);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值