7.3 网络流的准备工作 队列,邻接矩阵,邻接表
果然坑,有木有,~~ 队列,邻接矩阵,邻接表知道如何用,就是实现不了~~汗~~
只好自己看书 自己定义了一个邻接表,可以以按照原情况进行存储;
如:
1->2->3->4 表示的是 1点和2,3,4点相连
2->1->3 表示的是2点和1,3,点相连
3->4->5 表示的是3点和4,5点相连的
下面附上自己的 构建邻接表的程序:
post code:
自制邻接表:
#include<stdio.h> #include<stdlib.h> typedef struct stu{ int data; struct stu *next; } node; void init(); void add(); node p[6]; //p[i]代表了开始存放的节点是i,设计了一个链表,看存在多少个点和i相连。 node *pa[6]; //定义了一个 指针数组,用来存放指针 int n,m,i; void print(); int main() { init(); for(i=0;i<6;i++) print(); while(scanf("%d %d",&n,&m)&&n!=-1) { add(); } for(i=0;i<6;i++) print(); system("pause"); } void init() { int i; for(i=0;i<6;i++) { p[i].data=i; p[i].next=NULL; pa[i]=&p[i]; } } void add() //添加不同的节点,如过存在相同的就不添加,否则在链表后面添加节点。 { int flag=0; node *q,*t; q=(node *)malloc(sizeof(node)); q->data=m; q->next=NULL; t=pa[n]; while(t->next!=NULL) { if(t->data!=m){t=t->next;} else{flag=1;break;} } if(flag==0)t->next=q; } void print() //对链表进行依次的输出 { node *t; printf("%d ",pa[i]->data); t=pa[i]; t=t->next; while(t!=NULL) { printf(" %d",t->data); t=t->next; } printf("\n"); }
有必要声明一下对结构体的应用: //自己都~~~忘记了~~ 注意 -> 和 . 点的使用。
#include<stdio.h> #include<stdlib.h> typedef struct stu{ int i; float j; char a[20]; } node; node p={1,5.3,"acdasfds"},*q; int main() { q=&p; printf("%d %f %s\n",p.i,p.j,p.a); printf("%d %f %s\n",q->i,q->j,q->a); printf("%d %f %s\n",(*q).i,(*q).j,(*q).a); system("pause"); }