⑴ 在線急求熟悉圖的兩種常用的存儲結構,鄰接矩陣和鄰接表。
#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