1. 程式人生 > >to be continue

to be continue

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100

typedef struct node
{
int adjvex;
struct node*next;

}Edgenode;

typedef struct
{
int vertex;
Edgenode* firstedge;
}Vertexnode;

typedef struct
{
int data[MAXSIZE];
int top;
}Seqstack;

void init_seqstack(Seqstack** s)
{
s=(Seqstack

)malloc(sizeof(Seqstack));
(*s)->top=-1;
}

int Empty_seqstack(Seqstack*s)
{
if(s->top==-1)
return 1;
else
return 0;
}

void Push_seqstack(Seqstack*s,int x)
{
if(s->top==MAXSIZE-1)
printf(“stack is full\n”);
else
{
s->top++;
s->data[s->top]=x;
}
}

void Pop_seqstack(Seqstack*s)
{
if(s->top==-1)
printf(“stack is empty\n”);
else
{
s->top–;
}
}

void creatgraph(Vertexnode g[],int n,int e)//g為頂點表,n為節點數,e為邊數
{
Edgenode* p;
int i,j,k,x;
for(i=0;i<n;i++)//初始化頂點資訊
{
printf(“input vertex info\n”);
scanf("%d",&x);
g[i].vertex=x;
g[i].firstedge=NULL;
}
for(k=0;k<e;k++)
{
printf(“input edge of(i,j)”);
scanf("%d%d",&i,&j);
p=(Edgenode*)malloc(sizeof(Edgenode));
p->adjvex=j;
p->next=g[i].firstedge;
g[i].firstedge=p;
p=(Edgenode*)malloc(sizeof(Edgenode));
p->adjvex=i;
p->next=g[j].firstedge;
g[j].firstedge=p;

}

}

void DFS(Vertexnode g[])
{
int i=0,j=0;
// Edgenode *edg;
int visited[MAXSIZE]={0};
Seqstack s,*p=&s;
init_seqstack(&p);

j=g[i].vertex;
printf("%d",j);
visited[i]=1;
Push_seqstack(p,i);

while(!Empty_seqstack(p))
{
    if(g[]!=NULL)
    {
        if(visited[edg->adjvex]!=0)
            printf("%d",edg->adjvex);
        Push_seqstack(p,edg->adjvex);
        edg=g[edg->adjvex].firstedge;
    }
    else
    {
        Pop_seqstack(p);
    }


}

}

int main()
{
Vertexnode g[4];
creatgraph(g,4,4);
DFS(g);
printf(“Hello world!\n”);
return 0;
}