当前位置:首页 » 服务存储 » 图存储数据库邻接
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

图存储数据库邻接

发布时间: 2022-07-05 22:27:52

⑴ 在线急求熟悉图的两种常用的存储结构,邻接矩阵和邻接表。

#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define maxvernum 100
typedef struct node
{
int adjvex;
struct node *next;
}nodetype;
typedef struct frontnode
{
int data;
struct node *next;
}frontnodetype;
frontnodetype adjlist[maxvernum];
/*********************************************/
void main()
{
void bfs(frontnodetype g[],int v,int c[]);
void travelgraph(frontnodetype g[],int n);
frontnodetype adjlist[6];
int i,j,m;
for(i=1;i<=5;i++)
{
adjlist[i].data=i;
adjlist[i].next=NULL;
}
for(j=1;j<=5;j++)
{
printf("进入第%d次循环\n",j);
printf("开始输入前请输入一个不为0的m值以便输入\n");
scanf("%d",&m);
while(m!=0)
{
int x;
printf("请输入结点序号(x=0表示后面没有结点):\n");
if(x!=0)
{
scanf("%d",&x);
nodetype *p;
p=(nodetype *)malloc(sizeof(nodetype));
p->adjvex=x;p->next=NULL;
p->next=adjlist[j].next;
adjlist[j].next=p;
}
else break;
printf("是否停止?(m为0时停止输入,m为1时继续输入。)\n");
scanf("%d",&m);
}
}
printf("广度优先搜索序列为:\n");
travelgraph(adjlist,5);
}
/************************************************/
void bfs(frontnodetype g[],int v,int c[])
{
int q[6],r=0,f=0;
nodetype *p;
c[v]=1;
printf("%d\n",v);
q[0]=v;
whille(f<=r)
{
v=q[f++];
p=g[v].next;
while(p!=NNLL)
{
v=p->adjvex;
if(c[v]==0)
{
c[v]=1;
printf("%d\n",v);
}
p=p->next;
}
}
}
/************************************************/
void travelgraph(frontnodetype g[],int n)
{
int v;
int c[6];
for(v=1;v<=n;v++)
c[v]=0;
for(v=1;v<=n;v++)
if(c[v]==0)
dfs(g,v,c);

}

⑵ 如何把图片存到数据库

通常对用户上传的图片需要保存到数据库中。解决方法一般有两种:一种是将图片保存的路径存储到数据库;另一种是将图片以二进制数据流的形式直接写入数据库字段中。以下为具体方法:
一、保存图片的上传路径到数据库:
string uppath="";//用于保存图片上传路径
//获取上传图片的文件名
string fileFullname = this.FileUpload1.FileName;
//获取图片上传的时间,以时间作为图片的名字可以防止图片重名
string dataName = DateTime.Now.ToString("yyyyMMddhhmmss");
//获取图片的文件名(不含扩展名)
string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") + 1);
//获取图片扩展名
string type = fileFullname.Substring(fileFullname.LastIndexOf(".") + 1);
//判断是否为要求的格式
if (type == "bmp" || type == "jpg" || type == "jpeg" || type == "gif" || type == "JPG" || type == "JPEG" || type == "BMP" || type == "GIF")
{
//将图片上传到指定路径的文件夹
this.FileUpload1.SaveAs(Server.MapPath("~/upload") + "\\" + dataName + "." + type);
//将路径保存到变量,将该变量的值保存到数据库相应字段即可
uppath = "~/upload/" + dataName + "." + type;
}
二、将图片以二进制数据流直接保存到数据库:
引用如下命名空间:
using System.Drawing;
using System.IO;
using System.Data.SqlClient;
设计数据库时,表中相应的字段类型为iamge
保存:
//图片路径
string strPath = this.FileUpload1.PostedFile.FileName.ToString ();
//读取图片
FileStream fs = new System.IO.FileStream(strPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
//存入
SqlConnection myConn = new SqlConnection("Data Source=.;Initial Catalog=stumanage;User ID=sa;Password=123");
string strComm = " INSERT INTO stuInfo(stuid,stuimage) VALUES(107,@photoBinary )";//操作数据库语句根据需要修改
SqlCommand myComm = new SqlCommand(strComm, myConn);
myComm.Parameters.Add("@photoBinary", SqlDbType.Binary, photo.Length);
myComm.Parameters["@photoBinary"].Value = photo;
myConn.Open();
if (myComm.ExecuteNonQuery() > 0)
{
this.Label1.Text = "ok";
}
myConn.Close();
读取:
...连接数据库字符串省略
mycon.Open();
SqlCommand command = new
SqlCommand("select stuimage from stuInfo where stuid=107", mycon);//查询语句根据需要修改
byte[] image = (byte[])command.ExecuteScalar ();
//指定从数据库读取出来的图片的保存路径及名字
string strPath = "~/Upload/zhangsan.JPG";
string strPhotoPath = Server.MapPath(strPath);
//按上面的路径与名字保存图片文件
BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));
bw.Write(image);
bw.Close();
//显示图片
this.Image1.ImageUrl = strPath;
采用俩种方式可以根据实际需求灵活选择。

⑶ 为什么图(数据结构)的边链表结点不直接存放邻接点的信息而是存放邻接点在顶点表中的位置

关系数据库,尽量简化、独立每个子库的数据,便于修改。

⑷ 如何用邻接表存储图结构

我看不太懂这个程序,不过我有些过图的邻接表表示,看对你有没有帮助吧。
#include <iostream>
#include <fstream>
#include <vector>

typedef int QElemTyep;
#include "queue.h"
using namespace std;
typedef int Status;
#define MAX_VERTEX_NUM 30 //图的最大顶点数
enum BOOL {False,True};
BOOL visited[MAX_VERTEX_NUM]; //全局变量--访问标志数组

typedef struct ArcNode{
//弧结点
int adjvex; //该弧所指向的顶点的位置
struct ArcNode *nextarc; //指向下一条弧的指针
InfoType *info; //保存边的信息,可以简单的改为 int w;
}ArcNode;
typedef struct VNode{
VertexType data;
ArcNode *firstarc;
}VNode,AdjList[MAX_VERTEX_NUM];

class Graph{
public: AdjList vertices; //记录顶点信息,指向第一条依附该顶点的弧的指针
int vexnum,arcnum; //图的当前顶点和弧数
int GraphKind; //图的种类,0---无向图,1---有向图
Graph(int vexnum,int arcnum,int kind)
{
this->vexnum=vexnum;
this->arcnum=arcnum;
this->GraphKind=kind;
}
};
void CreateGraph(Graph &G,VertexType *V,ArcType *VR){
//构造邻接表结构的图G

int i;
ArcNode *s;
for(i=1;i<=G.vexnum;i++) //初始化指针数组
{
G.vertices[i].data=V[i];
G.vertices[i].firstarc=NULL;
}
for(i=1;i<=G.arcnum;i++)
{
s=(ArcNode *)malloc(sizeof(ArcNode)); //生成一个弧结点
s->nextarc=G.vertices[VR[i].start].firstarc; //插入到邻接表中
s->adjvex=VR[i].end;
G.vertices[VR[i].start].firstarc=s;

if(G.GraphKind==0) {
//若是无向图,再插入到终点的弧链中
s=(ArcNode *)malloc(sizeof(ArcNode));
s->nextarc=G.vertices[VR[i].end].firstarc;
s->adjvex=VR[i].start;
G.vertices[VR[i].end].firstarc=s;
}
}
}

⑸ 图片如何存入数据库

1、新建一个数据库,数据库名为Image,表名为image。并为表添加ID,tupian两个列。

⑹ 图的存储结构可以采用邻接矩阵和邻接表,对于个有n 个顶点,e条边的有向图, (1)计算存储结构分别

邻接表所需的存储空间为e(边数),但不适合查询两点间是否存在路径
邻接矩阵所需的存储空间为你n^2,适合查询两点间是否存在路径
对于第二问,邻接表所需的存储空间为9900,邻接矩阵所需的存储空间为你n^2=10000,差不多,所以选性能更优的邻接矩阵
实际上像(2)这种稠密图(其实是个满图)一般适合邻接矩阵

⑺ 有向图的邻接表存储如图所示,请画出其邻接矩阵存储结构

有向图的邻接表存储如图所示,其邻接矩阵存储如图:

⑻ 【数据结构】判断题:存储图的邻接矩阵中,邻接矩阵的大小不但与图的顶点个数有关,而且与图的边数也有关

图的邻接矩阵的大小只与顶点个数有关,与定点个数的平方成正比

⑼ 图的邻接矩阵和邻接表的存储结构各有什么特点

图的邻接表数据类型描述如下:
const int N=maxn; // maxn表示图中最大顶点数
const int E=maxe ; // maxe图中最大边数
struct Edge{
int u,v; //边所邻接的两个顶点
int w; //边的权值
int next; //边指针,指向下一条边的内存池地址
}edge[E]; // 静态内存池,用于分配边
int head[N]; // 表头
int num; // 内存池的指针

⑽ 数据库中如何存储有向图

方法一:用邻接表的方法存储,
先开辟一块大小为n的存储空间,每一块存储空间为一个链表的开头,一次将数据增加到相应的链表里。
方法二:用邻接矩阵的方法存储,
开辟一个矩阵,在相接的两个点对应的序号的位置存储为1,其余为0,例如:矩阵a[100][100],1和2相连,则a[1][2]=0