有没有谁做过数据结构课程设计,题目为:

2025-01-19 11:54:15
推荐回答(1个)
回答1:

题目1 马踏棋盘问题
#include "stdio.h"
#define M 70
#define FALSE 0
#define TRUE 1
typedef struct node
{
int vec,x,y;
struct node *link;
}listnode;

#define MAXSIZE 70
typedef struct
{
int stack[MAXSIZE];
int top;
}seqstack;
seqstack *s;
void setnull(seqstack *s)
{
s->top=-1;
}
int push(seqstack *s,int x)
{
if(s->top>=MAXSIZE-1)
{
printf("stack overflow!\n");
return FALSE;
}
else
{
s->stack[++s->top]=x;/*栈顶指针上移,数据元素入栈*/
return TRUE;
}
}

int pop(seqstack *s)/*出当前栈s的栈顶元素*/
{ int p;
if(s->top<0)
{
printf("stack empty!\n");/* 栈空,返回空值*/
return NULL;
}
else
{
s->top--;
return(s->stack[s->top+1]);
}/*由于return语句的特点,必须先使 top减1,然后再执行return语句。而此时栈顶元素的表示应该为s->top+1*/
}

void creatlist(listnode ag[])
{
listnode *p;
int i,j,x,y,x1,x2,y1,y2,k;
k=1;

for(i=1;i<=8;i++)
{
for(j=1;j<=8;j++)
{
ag[k].x=i;
ag[k].y=j;
ag[k].vec=8*ag[k].x+ag[k].y-8;
ag[k].link=NULL;
printf("%d(%d,%d)",ag[k].vec,ag[k].x,ag[k].y);
k++;
}
printf("\n");
}
for(i=1;i<=64;i++)
{
/*printf("*%d*",i); */
setnull(s);
x1=ag[i].x;
y1=ag[i].y;
if((x1+1>0)&&(y1+2>0)&&(x1+1<=8)&&(y1+2<=8))
{
x2=x1+1;
y2=y1+2;
j=8*x2+y2-8;
/*printf("%d ",j);*/
push(s,j);

}
if((x1+1>0)&&(y1-2>0)&&(x1+1<=8)&&(y1-2<=8))
{
x2=x1+1;
y2=y1-2;
j=8*x2+y2-8;
/*printf("%d ",j);*/
push(s,j);
}

if((x1-1>0)&&(y1+2>0)&&(x1-1<=8)&&(y1+2<=8))
{
x2=x1-1;
y2=y1+2;
j=8*x2+y2-8;
/*printf("%d ",j);*/
push(s,j);
}
if((x1-1>0)&&(y1-2>0)&&(x1-1<=8)&&(y1-2<=8))
{
x2=x1-1;
y2=y1-2;
j=8*x2+y2-8;
/*printf("%d ",j);*/
push(s,j);
}
if((x1+2>0)&&(y1-1>0)&&(x1+2<=8)&&(y1-1<=8))
{
x2=x1+2;
y2=y1-1;
j=8*x2+y2-8;
/*printf("%d ",j);*/
push(s,j);
}
if((x1+2>0)&&(y1+1>0)&&(x1+2<=8)&&(y1+1<=8))
{
x2=x1+2;
y2=y1+1;
j=8*x2+y2-8;
/*printf("%d ",j);*/
push(s,j);
}
if((x1-2>0)&&(y1+1>0)&&(x1-2<=8)&&(y1+1<=8))
{
x2=x1-2;
y2=y1+1;
j=8*x2+y2-8;
/*printf("%d ",j);*/
push(s,j);
}
if((x1-2>0)&&(y1-1>0)&&(x1-2<=8)&&(y1-1<=8))
{
x2=x1-2;
y2=y1-1;
j=8*x2+y2-8;
/*printf("%d ",j);*/
push(s,j);
}
do
{
j=pop(s);
x2=ag[j].x;
y2=ag[j].y;
p=(listnode *)malloc(sizeof(listnode));
p->vec=j;
p->x=x2;
p->y=y2;
p->link=ag[i].link;
ag[i].link=p;
/*printf(" %d(%d,%d) ",p->vec,p->x,p->y);*/
p=(listnode *)malloc(sizeof(listnode));
p->vec=i;
p->x=x1;
p->y=y1;
p->link=ag[j].link;
ag[j].link=p;
/*printf(" %d(%d,%d) ",p->vec,p->x,p->y);*/

} while(s->top>=0);

}

}

void dfs(listnode ag[],int v,int flag[])
{
listnode *p;
int i;
flag[v]=1;
printf("(%d,%d)",ag[v].x,ag[v].y);
p=ag[v].link;
while(p!=NULL)
{
i=p->vec;
if(flag[i]==0)
dfs(ag,i,flag);
p=p->link;
}

}
/*void blt(listnode ag[M],int n)
{
int i;
int flag[M];
for(i=1;i<=n;i++)
flag[i]=0;
for(i=1;i<=n;i++)
if(flag[i]==0)
dfs(ag,i,flag);
}*/
void main()
{
listnode ag[M];
int flag[M];
creatlist(ag);
dfs(ag,28,flag);
}