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

来源:互联网 发布:淘宝网夏季竹凉席坐垫 编辑:程序博客网 时间:2024/06/13 21:47

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 1000using 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 1000using 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 1000using 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;}
原创粉丝点击