A. 關於數據結構中鄰接表的問題
鄰接表是圖的一種鏈接存儲結構。在鄰接表中,對圖中每個頂點建立一個帶頭結點的單鏈表,所有的頭結點構成一個數組,第i個單鏈表中的結點表示依附於頂點vi的邊。也就是說指的是點,表示的是邊,因為兩點決定了一條邊。以下圖為例:

與0號點相連的有2條邊,一條與1號點相連,一條與3號點相連。所以v0後面有兩個結點,前面的序號分別為1、3,3後沒有了,為空;
與1號點相連的有3條邊,分別與0、2、3號點相連。所以v0後面有3個結點,前面的序號分別為0、2、3,3後沒有了,為空;
與2號點相連的有1條邊,與1號點相連。所以v0後面有1個結點,前面的序號為1,1後沒有了,為空。
對照圖好好理解,應該能明白了。
B. 鄰接表的存儲定義 數據結構
structnode
{
intvalue;//你存放的變數
structnode*next;//鄰接表
};
可以用結構體表示。
C. c語言 鄰接矩陣和鄰接表
/**********************鄰接矩陣*****************/
#include<bits/stdc++.h>//鄰接矩陣
using namespace std;
const int N=300;
int Map[N][N]={0};//鄰接矩陣
int book[N]={0};//結點標記數組(1表示該點訪問過了;0未訪問過)
int n,m;
void dfs(int x)//深度遍歷
{
for(int i=1;i<=n;i++)
{//book[i]==0:i未被訪問過
// Map[x][i]==1:x到i有邊連接
if(book[i]==0&&Map[x][i]==1)
{
book[i]=1;//訪問標記
printf("->[%d]",i);
dfs(i);//前往下一個結點i
}
}
}
void bfs(int x)
{
int q[N]={0};
int fornt=0;
int rear=0;
q[rear++]=x;//源點入隊
while(fornt<rear)
{
int k=q[fornt++];//出隊
for(int i=1;i<=n;i++)
{//擴展一個點周圍可以訪問的點
if(book[i]==0&&Map[k][i]==1)
{
printf("->[%d]",i);
book[i]=1;//訪問標記
q[rear++]=i;//入隊
}
}
}
}
int main()
{
scanf("%d%d",&n,&m);//n個結點,m條邊
for(int i=0;i<m;i++)
{//構造無向圖
int x,y;//輸入2個結點;x->y;y->x;
scanf("%d%d",&x,&y);
Map[x][y]=1;//1代表x,y鄰接
Map[y][x]=1;
}
book[1]=1;//標記結點1
printf("深度遍歷路徑 [%d]",1);
dfs(1);//從結點1深度遍歷 (起始點可以隨便選,1~n)
printf(" ");
memset(book,0,sizeof(book));//標記數組置0,用於廣度遍歷標記
printf("廣度遍歷路徑 [%d]",1);
book[1]=1;//標記結點1
bfs(1);//從結點1廣度遍歷 (起始點可以隨便選,1~n)
return 0;
}
/*5個結點,7條邊
5 7
1 3
1 2
1 4
2 4
2 5
3 5
4 5
*/
/*************************鄰接表*************************************/
#include<bits/stdc++.h>//萬能頭文件,C/C++都能用,包含了所有的頭文件
using namespace std;//鄰接表
const int N=300;
typedef struct st{
int v;//表頭能到達的結點
struct st *next;//指向下一個結點的指針域
}*link,AK;//定義結點類型
struct sx{
AK *next;//表頭指針域
}s[N];//n個結點,n個表頭
int book[N]={0};//結點標記數組(1表示該點訪問過了;0未訪問過)
int n,m;
void create(int x,int y)
{//前插法構建鄰接表
link p;
p=new AK;//開辟新結點
p->v=y;
p->next=s[x].next;
s[x].next=p;
}
void fun()
{//表頭指針賦空(這一步至關重要,沒有這一步無法建表)
for(int i=1;i<=n;i++)
s[i].next=NULL;
}
void dfs(int x)//深度遍歷鄰接表
{
link p=s[x].next;
while(p)
{
if(book[p->v]==0)
{
printf("->[%d]",p->v);
book[p->v]=1;
dfs(p->v);
}
p=p->next;
}
}
void bfs(int x)//廣度遍歷鄰接表
{
int q[N]={0};//隊列
int fornt=0;
int rear=0;
q[rear++]=x;
while(fornt<rear)
{
int k=q[fornt++];
link p=s[k].next;
while(p)
{
if(book[p->v]==0)
{
printf("->[%d]",p->v);
book[p->v]=1;
q[rear++]=p->v;
}
p=p->next;
}
}
}
void input()//列印鄰接表
{//首列是表頭(其後尾隨的是與其鄰接的結點)
//n個表頭
link p;
for(int i=1;i<=n;i++)
{
p=s[i].next;
printf("[%d]",i);
while(p)
{
printf("->[%d]",p->v);
p=p->next;
}
cout<<endl;
}
}
int main()
{
fun();//調用表頭置空
scanf("%d%d",&n,&m);//n個結點,m條邊
for(int i=0;i<m;i++)
{//構造無向鄰接表
int x,y;//輸入2個結點;x->y;y->x;
scanf("%d%d",&x,&y);
create(x,y);//構建有向鄰接表,只調用一個;
create(y,x);//構建無向鄰接表,只調用兩個;
}
book[1]=1;//標記結點1
printf("深度遍歷路徑 [%d]",1);
dfs(1);//從結點1深度遍歷 (起始點可以隨便選,1~n)
printf(" ");
memset(book,0,sizeof(book));//標記數組置0,用於廣度遍歷標記
printf("廣度遍歷路徑 [%d]",1);
book[1]=1;//標記結點1
bfs(1);//從結點1廣度遍歷 (起始點可以隨便選,1~n)
printf(" ");
printf("列印鄰接表結構 ");
input();//列印鄰接表
return 0;
}
/*5個結點,7條邊
5 7
1 3
1 2
1 4
2 4
2 5
3 5
4 5
*/

D. c語言,關於鄰接表的建立
AdjList 是自定義類型,是char類型,
第二行 typedef將char類型自定義為 VertexType,即VertexType代表了char型,
VertexType a  就相當於 char a;
同理
倒數第五行 typedef將VertexNode自定義為 AdjList[MaxVertexNum]類型,也就是說現在AdjList就代表了一個  「結構體數組」   類型
AdjList adjlist 相當於 VertexNode adjlist[MaxVertexNum]   
這里主要是typedef關鍵字的使用  希望能幫到你
E. c語言圖的遍歷,鄰接表存儲,深度,廣度優先遍歷
(1)	圖的建立,按採用鄰接表作為存儲結構。
(2)	從指定頂點出發進行深度優先搜索遍歷。
(3)	從指定頂點出發進行廣度優先搜索遍歷。
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"math.h"
#define MAX_INT 1000
#define MAX_VERTEX_NUM 20
#define MAX_QUEUE_NUMBER 20
typedef struct ArcNode
{
	int adjvex;
	double adj;
	struct ArcNode *nextarc;
}ArcNode;
typedef struct VexNode
{
	char szName[40];
	ArcNode *firstarc;
}VexNode,AdjList[MAX_VERTEX_NUM];
typedef struct 
{
	AdjList vexs;
	int vexnum,arcnum;
}Net;
//定義隊列
typedef struct{
	int *elem;
	int front, rear;
}Queue;
void InitQueue(Queue &Q)
{
	Q.elem = new int[MAX_QUEUE_NUMBER];
	Q.front = Q.rear = 0;
}
int EmptyQueue(Queue Q)
{
	if(Q.front==Q.rear)
		return 0;
	else 
		return 1;
}
void DestroyQueue(Queue &Q){
	delete []Q.elem;
	Q.front = Q.rear = 0;
}
void EnterQueue(Queue &Q, int e)
{
	if((Q.rear + 1)%MAX_QUEUE_NUMBER != Q.front)
		Q.elem[Q.rear ]	= e;
	else
		printf("隊列滿!\n");
	Q.rear = (Q.rear + 1)%MAX_QUEUE_NUMBER;
}
void LeaveQueue(Queue &Q, int &e)
{
	if(Q.rear != Q.front)
		e = Q.elem[Q.front];
	else
		printf("隊列空!\n");
	Q.front = (Q.front+1)%MAX_QUEUE_NUMBER;
}
int LocateVex(Net ga,char *name)
{
	int i;
	for(i=0;i<ga.vexnum;i++)
		if(strcmp(name,ga.vexs[i].szName)==0)
			return i;
	return -1;
}
void crt_net(Net &ga)
{
	ArcNode *p;
	char name1[40],name2[40];
	int i,j,k;
	double w;
	printf("請輸入頂點數和弧數:");
	scanf("%d%d",&ga.vexnum,&ga.arcnum);
	printf("請依次輸入頂點名:\n");
	for(i=0;i<ga.vexnum;i++)
	{
		scanf("%s",ga.vexs[i].szName);
		ga.vexs[i].firstarc=NULL;
	}
	for(k=0;k<ga.arcnum;k++)
	{
		printf("請輸入相鄰的兩個定點和權值:");
		scanf("%s%s%lf",name1,name2,&w);
		i=LocateVex(ga,name1);
		j=LocateVex(ga,name2);
		p=new ArcNode;
		p->adjvex=j;
		p->adj=w;
		p->nextarc=ga.vexs[i].firstarc;
		ga.vexs[i].firstarc=p;
	}
}
void DFS(Net ga,char *name,int *visited)
{
	int v,w; 
	ArcNode *p;
	v=LocateVex(ga,name);
	visited[v]=1;
	printf("%s ",ga.vexs[v].szName);
	p=ga.vexs[v].firstarc;
	while(p!=NULL)
	{
		w=p->adjvex;
		if(visited[w]==0)
			DFS(ga,ga.vexs[w].szName,visited);
		p=p->nextarc;
	}
}
void DFSTravel(Net ga,char *name)
{
	int v,k=0;
	int visited[20];
	for(v=0;v<ga.vexnum;v++)
		visited[v]=0;
	for(v=LocateVex(ga,name);k!=2;v=(v+1)%(ga.vexnum-1))
	{
		if(v+1==LocateVex(ga,name))
			k++;
		if(visited[v]==0)
			DFS(ga,ga.vexs[v].szName,visited);
		
	}
}
void BFSTravel(Net ga,char *name)
{
	ArcNode *p;
	int v,w,u,k=0;
    Queue Q;
	int visited[20];
	for(v=0;v<ga.vexnum;v++)
		visited[v]=0;
	InitQueue(Q);
	for(v=LocateVex(ga,name);k!=2;v=(v+1)%(ga.vexnum-1))
	{
		if(v+1==LocateVex(ga,name))
			k++;
		if(visited[v]==0)
		{
			visited[v]=1;
			printf("%s ",ga.vexs[v].szName);
			EnterQueue(Q,v);
			while(EmptyQueue(Q)!=0)
			{
				LeaveQueue(Q,u);
				p=ga.vexs[u].firstarc;
				while(p!=NULL)
				{
					w=p->adjvex;
					if(visited[w]==0)
					{
						printf("%s ",ga.vexs[w].szName);
						visited[w]=1;
						EnterQueue(Q,w);
					}
					p=p->nextarc;
				}
			}
		}
	
	}
}
void main()
{
	char name[40];
	Net ga;
	crt_net(ga);
	printf("請輸入深度優先遍歷開始點的名:");
	scanf("%s",name);
	printf("深度優先遍歷:");
	DFSTravel(ga,name);
	printf("\n");
	printf("請輸入廣度優先遍歷開始點的名:");
	scanf("%s",name);
	printf("廣度優先遍歷:");
	BFSTravel(ga,name);
	printf("\n");
}
F. 請教編程大神數據結構題目:c語言使用鄰接鏈表存儲結構實現用戶輸入的無向圖的連通性判斷
#include<stdio.h>
#include<stdlib.h>
#defineMAX_VERTEX_NUM100
typedefstructArcNode{
intadjvex;//該邊的另一個頂點的位置
structArcNode*nextarc;//指向下一條邊
}ArcNode;
typedefstructVNode{
intdata;//頂點的值
ArcNode*firstarc;//指向第一條依附該頂點的邊的指針
}VNode,AdjList[MAX_VERTEX_NUM];
typedefstruct{
AdjListvertices;//頂點數組
intvexnum,arcnum;
}ALGraph;
intLocateVex(ALGraphG,intv){//定位函數
for(inti=0;i<G.vexnum;i++){
if(v==G.vertices[i].data)returni;
}
}
voidCreateUDG(ALGraph&G){
ArcNode*p,*q;
inti,j,k,v1,v2;
printf("分別輸入頂點個數和邊的數目: ");
scanf("%d%d",&G.vexnum,&G.arcnum);
printf("分別輸入各個頂點值: ");
for(i=0;i<G.vexnum;i++){
scanf("%d",&G.vertices[i].data);
G.vertices[i].firstarc=NULL;//初始化
}
printf("分別輸入各條邊的兩個頂點: ");
for(k=0;k<G.arcnum;k++){
scanf("%d%d",&v1,&v2);
i=LocateVex(G,v1);j=LocateVex(G,v2);//定位
p=(ArcNode*)malloc(sizeof(ArcNode));//申請一個結點
p->adjvex=j;p->nextarc=NULL;//賦值
p->nextarc=G.vertices[i].firstarc;//連接結點
G.vertices[i].firstarc=p;//連接結點
q=(ArcNode*)malloc(sizeof(ArcNode));
q->adjvex=i;q->nextarc=NULL;
q->nextarc=G.vertices[j].firstarc;
G.vertices[j].firstarc=q;
}
}
voidPrintUDG(ALGraphG){//輸出鄰接表
inti,j;
for(i=0;i<G.vexnum;i++){
printf("%d:",i);
ArcNode*p;
p=G.vertices[i].firstarc;
while(p!=NULL){
printf("->%d",p->adjvex);
p=p->nextarc;
}
printf(" ");
}
}
intmain(){
ALGraphG;
CreateUDG(G);
PrintUDG(G);
return0;
}
G. C語言中怎樣用鏈表保存結構體數據(動態數據結構)
鏈表有多種形式,如:單向鏈表,雙向鏈表,單向循環鏈表,雙向循環鏈表。將鏈表結構定義為list_t,則該類型中一定(至少)存在一個指向下一節點的指針list_t
*next;除了這個指針,list_t
中可以包含其它類型的數據,包括結構體變數。比如:typedef
struct
{
struct
usr_struct
data;
list_t
*next;
}
list_t;
H. 求一段c語言代碼,題目:建立圖的存儲結構,能夠輸入圖的頂點和邊的信息,並存儲到相應存儲結構中

代碼
/*輸入:先輸入兩個數,代表點的數量和邊的數量,
再輸入各個邊,起點編號 > 終點編號,編號從0開始
例子:
6 10
0 3
0 4
1 4
1 3
3 5
0 1
4 5
5 2
4 2
4 3
輸出:
0 1 4 3 5 2
思路:
- 用vector建立鄰接表 
- 計算每個點的入度 
- 如果是偏序無環的,一定存在入度為0的點,輸出並且刪除它,同時刪除它出發的邊,更新其他點的入度 
- 循環直到移除所有點,輸出順序就是拓撲排序 
*/
#include<iostream>
#include<vector>
using namespace std;
int main() {
freopen("in.txt","r",stdin);//重定向輸入流//in.txt 建在程序所在的文件夾里
int M,N;
scanf("%d%d",&M,&N);//M個點,N條邊
vector<int> maps[M];
int innode[M]={0};//入度
for(int i=0;i<N;i++)
{
int tx,ty;
scanf("%d%d",&tx,&ty);
maps[tx].push_back(ty);
++innode[ty];
}
for(int time=0;time<M;time++)
for(int i=0;i<M;i++)
{
if(innode[i]==0)
{
printf("%d ",i);
for(vector<int>::iterator it = maps[i].begin(); it != maps[i].end(); ++it)
{
--innode[*it];
}
innode[i]=-1;
break;
}
}
}
I. 請編寫一個完整的程序,建立有向圖的鄰接表存儲結構,要求:
給你一個鄰接表的完整程序:
#include <iostream.h>
struct node
{
 int data;
 node *next;
};
class list
{
  public:
   list(){head=NULL;};
   void MakeEmpty();
   int Length();
   void Insert(int x,int i);//將x插入到第i個結點(不含頭結點)的之後
   void   Insertlist(int a,int b);//將節點b插入a之前
   int Delete(int x);
   int Remove(int i);
   int Find(int x);
   void Display();
  private:
   node *head;
};
  void list::Display()
 {
    node *current=head;
    while (current!=NULL)
   { 
      cout<<current->data<<" ";
      current=current->next;
    }
    cout<<endl;
 } 
  void list::MakeEmpty()
 {
    head=NULL;
  } 
  int list::Length()
  {int n=1;
   node *q=head;
   if(q==NULL)
         n=1;
   else
      while(q!=NULL)
      {
          n++;
          q=q->next;
       }
   return n;
   }
  int list::Find(int x)//在鏈表中查找數值為x的結點,成功返回1,否則返回0
 {
    node *p=head;
    while(p!=NULL&&p->data!=x)
    p=p->next;
    if(p->data==x)
       return 1;
    else
       return 0;
 }
  void list::Insert (int x,int i)//將x插入到第i個結點(不含頭結點)的之後;
  {
    node *p;//p中放第i個結點
    node *q;//q中放i後的結點
    node *h;//h中存要插入的結點
    h=new node;
    h->data =x;
    p=head;
    if(p->next !=NULL) //鏈表不是只有一個結點或者空鏈表時候 
    {
        int n=1;
        while(p->next !=NULL)
        {
            n++;
            p=p->next ;
         }// 得到鏈表的結點的個數
        p=head;//使p重新等於鏈首
        if(i==n)//i=n時,直接加在最後面就行了
       {
            while(p->next !=NULL)
            p=p->next;  
            p->next=h;
            h->next =NULL;
        }
        else if(i<n&&i>1)//先找到第i個結點,用p存第i個結點,用q存i後的結點,用h存要插入的結點
            {
                for(int j=1;j<i;j++)     
                    p=p->next;//找到第i個結點,用p存第i個結點
                q=p->next;//q存i後的結點          
                p->next=h;
                h->next=q;    
      
             } 
             else    
                cout<<"超出鏈表結點個數的范圍"<<endl;
         }
       else
            cout<<"這個鏈表是空鏈表或者結點位置在首位"<<endl;
  }
  void list::Insertlist(int a,int b)//將b插入到結點為a之前
  {
       node *p,*q,*s;//p所指向的結點為a,s所指為要插入的數b,q所指向的是a前的結點
       s=new node;
       s->data=b;
       p=head;
       if(head==NULL)//空鏈表的時候
       {
            head=s;
            s->next=NULL;
       }
       else
            if(p->data==a)//a在鏈首時候
            {
                s->next=p;
                head=s;
             }
             else
            {
                while(p->data!=a&&p->next!=NULL)//使p指向結點a,q指向a之前的結點
                {
                   q=p;
                    p=p->next;
                 }
                 if(p->data==a)//若有結點a時候
                 {
                    q->next=s;
                    s->next=p;
                  }
                  else//沒有a的時候
                  {
                      p->next=s;
                      s->next=NULL;
                   }
             }
  }
int list::Delete(int x)//刪除鏈表中值為x的結點,成功返回1,否則返回0;
{
    node *p,*q;
    p=head;
    if(p==NULL)
        return 0;
    if(p->data==x)
    {
         head=p->next;
         delete p;
         return 1;
     }
     else
     {
          while(p->data!=x&&p->next!=NULL)
          {    q=p;
               p=p->next;
           }
           if(p->data==x)
           {
                q->next =p->next;
                delete p;
                return 1;
            }
            else 
                return 0;
      }
}
int list::Remove(int i)
{
    node *p,*q;
    p=head;
    if(p!=NULL)
    {   int n=1;
        while(p->next !=NULL)
        {
             n++;
             p=p->next ;
         }//得到鏈表結點的個數
        p=head;
        if(i==n)//i結點在結尾的時候
        {
            while(p->next!=NULL)
            {
                 q=p;
                 p=p->next;
             }
             q->next=NULL;
             delete p;
             return 1;
         }
         else if(i<n&&i>1)//i結點在中間的時候
            {
                for(int j=1;j<i;j++)
                {      
                     q=p;//q中放i前的結點
                     p=p->next ;//p中放第i個結點   
                 }
                q->next=p->next; 
                delete p;
                return 1;    
             }
          else if(i==1)//i結點在首位的時候
             {
                 q=p->next;
                 head=q;
                 delete p;           
                 return 1;
              }  
             else
                 return 0;  
             }
         else
        return 0;
 
}
void main()
{
 list A;
 int data[10]={1,2,3,4,5,6,7,8,9,10};
A.Insertlist(0,data[0]);
for(int i=1;i<10;i++)
A.Insertlist(0,data[i]);
A.Display();
menu:cout<<"1.遍歷鏈表"<<'\t'<<"2.查找鏈表"<<'\t'<<"3.插入鏈表"<<endl;
     cout<<"4.刪除鏈表"<<'\t'<<"5.鏈表長度"<<'\t'<<"6.置空鏈表"<<endl;
    int m;
do
{
cout<<"請輸入你想要進行的操作(選擇對應操作前面的序號):"<<endl;
cin>>m;
}while(m<1||m>6);//當輸入的序號不在包括中,讓他重新輸入
switch(m)
{
case 1:
{
A.Display ();         
       goto menu;
};break;
    case 2:
{
cout<<"請輸入你想要找到的結點:"<<endl;
        int c;
         cin>>c;//輸入你想要找到的結點
        if(A.Find (c)==1)
{
     cout<<"可以找到"<<c<<endl;
 A.Display ();//重新顯示出鏈表A
    
}
        else
{
     cout<<"鏈表中不存在"<<c<<endl;
  A.Display ();//重新顯示出鏈表A
    
} 
goto menu;
};break;
    case 3:        
{ 
cout<<"請選擇你要插入的方式(選擇前面的序號進行選擇)"<<endl;
cout<<"1.將特定的結點加入到特定的結點前"<<'\t'<<"2.將特定的結點加到特定的位置後"<<endl;
int b1;
do
{
        cout<<"請輸入你想要插入的方式(選擇前面的序號進行選擇):"<<endl;
        cin>>b1;
}while(b1<1||b1>2);//當輸入的序號不在包括中,讓他重新輸入
if(b1==1)
{
cout<<"請輸入你想要插入的數和想要插入的結點(為此結點之前插入):"<<endl;
            int a1,a2;
            cin>>a1>>a2;
            A.Insertlist  (a1,a2);//將a1插入到結點為a2結點之前
cout<<"此時鏈表為:"<<endl;
A.Display ();//重新顯示出鏈表A 
}
    else
{
cout<<"請輸入你想要插入的數和想要插入的位置(為此結點之後插入):"<<endl;
            int a1,a2;
            cin>>a1>>a2;
            A.Insert (a1,a2);//將a1插入到結點位置為a2的結點之後
cout<<"此時鏈表為:"<<endl;
A.Display ();//重新顯示出鏈表A
} 
goto menu;   
};break;
case 4: 
{
cout<<"請選擇你要刪除的方式(選擇前面的序號進行選擇)"<<endl;
            cout<<"1.刪除特定的結點"<<'\t'<<"2.刪除特定位置的結點"<<endl;
int b1;
do
{
        cout<<"請輸入你想要插入的方式(選擇前面的序號進行選擇):"<<endl;
        cin>>b1;
}while(b1<1||b1>2);//當輸入的序號不在包括中,讓他重新輸入
    if(b1==1)
{
          cout<<"請輸入你想要刪除的結點:"<<endl;
          int a;
          cin>>a;//輸入你想要刪除的結點
          if(A.Delete (a)==1)
  {
       cout<<"成功刪除"<<a<<endl;
   cout<<"刪除後的鏈表為:"<<endl;
   A.Display ();
  }
      else
  { 
   cout<<"此鏈表為:"<<endl;
   A.Display ();//重新顯示出鏈表A 
       cout<<"鏈表中不存在"<<a<<endl;
   
  }
  
}
else
{
              cout<<"請輸入你想要刪除的結點位置:"<<endl;
              int b;
          cin>>b;//輸入你想要刪除的結點的位置
          if(A.Remove(b)==1)
  {
      cout<<"成功刪除第"<<b<<"個結點"<<endl;
  cout<<"刪除後的鏈表為:"<<endl;
  A.Display ();//重新顯示出鏈表A
  }
  else
  {
  cout<<"當前鏈表的結點個數為:"<<A.Length ()<<endl;
         cout<<"您輸入的結點位置越界"<<endl;
  }
  
}
goto menu;
};break;
case 5:
{
cout<<"這個鏈表的結點數為:"<<A.Length ()<<endl;
goto menu;
};break;
case 6:
{
A.MakeEmpty ();
cout<<"這個鏈表已經被置空"<<endl;
goto menu;
};break; 
}
}
評論(3)|1
sunnyfulin |六級採納率46%
擅長:C/C++JAVA相關Windows數據結構及演算法網路其它產品
按默認排序|按時間排序
其他1條回答
2012-04-23 17:41121446881|六級
我寫了一個C語言的,只給你兩個結構體和一個初始化函數:
#include "stdio.h"
#include "malloc.h"
struct adjacentnext//鄰接表項結構體
{
	int element;
	int quanvalue;
	struct adjacentnext *next;
};
struct adjacenthead//鄰接表頭結構體
{
	char flag;
	int curvalue;
	int element;
	struct adjacenthead *previous;
	struct adjacentnext *son;
	
};
//初始化圖,用鄰接表實現
struct adjacenthead *mapinitialnize(int mapsize)
{
	struct adjacenthead *ahlists=NULL;
	struct adjacentnext *newnode=NULL;
	int i;
	int x,y,z;
	ahlists=malloc(sizeof(struct adjacenthead)*mapsize);
	if(ahlists==NULL)
		return NULL;
	for(i=0;i<mapsize;i++)
	{
		ahlists[i].curvalue=0;
		ahlists[i].flag=0;
		ahlists[i].previous=NULL;
		ahlists[i].son=NULL;
		ahlists[i].element=i+1;
	}
	scanf("%d%d%d",&x,&y,&z);//輸入源結點,目的結點,以及源結點到目的結點的路權值
	while(x!=0&&y!=0)//x,y至少有一個零就結束
	{
		newnode=malloc(sizeof(struct adjacentnext));
		newnode->element=y;
		newnode->quanvalue=z;
		newnode->next=ahlists[x-1].son;
		ahlists[x-1].son=newnode;
		scanf("%d%d%d",&x,&y,&z);
	}
	return ahlists;//返回鄰接表頭
}
