zoukankan      html  css  js  c++  java
  • [笔记]:指针 链表 邻接表 标签: 指针链表 2017-05-16 15:28 38人阅读 评论(0) 收藏

    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;
    }
  • 相关阅读:
    RSDS pdb格式
    关于windbg报错"No symbols for ntdll. Cannot continue."问题
    WinDbg常用命令系列---.cmdtree
    正确创建本地C++发布构建PDBS
    PDB文件会影响性能吗?
    每个开发人员必须知道PDB文件知识
    Windbg妙用
    在x64计算机上捕获32位进程的内存转储
    为什么我的堆栈上会有奇怪的函数名?(关于符号的讨论)
    redis入门基础
  • 原文地址:https://www.cnblogs.com/xljxlj/p/7183658.html
Copyright © 2011-2022 走看看