Code:
#include<conio.h>
#include<stdio.h>
#include<alloc.h>
#define FileIn "Graph.inp"
typedef int ElementType;
typedef struct Node{
ElementType Element;
Node*Next;
};
typedef Node*Stack;
//Tao stack rong
Node*Make_Node(ElementType x){
Node*temp=(Node*)malloc(sizeof(Node));
temp->Element =x;
return temp;
}
//khoi tao 1 Node
Stack MakeNull_Stack(){
Stack temp=(Stack)malloc(sizeof(Stack));
temp->Next=NULL;
return temp;
}
//kiem tra stack rong
char Empty_Stack(Stack S){
return S->Next==NULL;
}
//them pt vao ngan xep
void Push(Stack S,ElementType x){
Node*P=Make_Node(x);
P->Next=S->Next;
S->Next=P;
}
//xoa pt ra khoi ngan xep
void Pop(Stack S){
if(!Empty_Stack(S)){
Node*temp=S->Next;
S->Next=temp->Next;
free(temp);
}
}
//xuat phan tu ra khoi ngan xep
void Out_Stack(Stack L){
Stack temp=L;
while(!Empty_Stack(temp)){
printf("%d\t",temp->Element);
temp=temp->Next;
}
}
//doc file
void Read_File( char **A, unsigned int &n, unsigned int &Start)
{
FILE *f= fopen(FileIn,"rb");
fscanf(f,"%d",&n,&Start);
printf("This Matrix \n%d \n ",Start);
*A=(char*)malloc(sizeof(char)*n);
for(int i=0;i<n;i++)
{
A[i]=(char*)malloc(sizeof(char)*n);
for(int j=0; j<n;j++)
fscanf(f,"%d",&A[i][j]);
printf("%d",A[i][j]);
}
printf("\n");
Start-- ;
fclose(f);
}
//ham tiem kiem duong di Hamilton
void Hamilton(char **A, unsigned int n, unsigned int Start){
Stack S= MakeNull_Stack();
Stack L= MakeNull_Stack();
unsigned int Count = 0;
char F=0;
char *M = new char[n];
Push(S,Start);
for(int i=0;i<n;i++)
M[i]=0;
while(!Empty_Stack(S)){
if(!Empty_Stack(S)){
Start=Top(S);
Push(L,Start);
M[Start]=1;
Count++;
}
if(Count<n)
{
F=0;
for(i=0;i<n;i++)
if(A[Start][i]==1&&M[i]==0)
{
Push(S,i);
F=i;
}
}
if(Count ==n||F==1)
if(Count==n){
printf("\n");
Out_Stack(L);
}
while(!Empty_Stack(S) && Top(S)==Top(L)){
M[Top(S)]=0;
Pop(S);
Pop(L);
Count--;
}
}
}
Stack Input_Stack(unsigned int n){
Stack temp=MakeNull_Stack();
unsigned int i=0;
ElementType x;
while(i<n)
{
printf("Element %d=",i);
scanf("%d",&x);
Push(temp,x);
i++;
}
return temp;
}
void main(){
clrscr();
char **A;
unsigned int n,Start;
Read_File(A,n,Start);
printf("Result!");
Hamilton(A,n,Start);
getch();
}
Bài này báo lỗi ở hàm Top (Function 'Top' should have a prototype) mà em không biết cách sửa. Thầy xem giúp em.