实验二第1题
#include
#include
#define maxsize 50
typedef struct
{
int elem[maxsize];
int top;
}SeqStack;
void InitStack(SeqStack *s)
{
s->top=-1;
}
int Push(SeqStack *s,int x)
{
if(s->top==maxsize-1)
return 0;
s->top++;
s->elem[s->top]=x;
return 1;
}
int Pop(SeqStack *s,int *x)
{
if(s->top==-1)
return 0;
else
{
*x=s->elem[s->top];
s->top--;
return 1;
}
}
int Display(SeqStack *s)
{
int m;
if(s->top==-1)printf("顺序栈为空!\n");
else
{
for(m=s->top;m>=0;m--)printf("%d ",s->elem[m]);
printf("\n");
}
}
void main()
{
SeqStack m,n;
InitStack(&m);
InitStack(&n);
int a,b;
printf("输入元素(以$结尾)\n");
while(scanf("%d",&a))
{
if(a=='$')
break;
else Push(&m,a);
}
for(b=m.top;b>=0;b--)
Push(&n,m.elem[b]);
printf("转置前的队列:\n");
Display(&n);
printf("转置后的队列:\n");
Display(&m);
return 0;
}
实验二第2题
#include
#include
#define N 9
typedef int datatype;
typedef struct queuenode
{datatype data;
struct queuenode *next;
}queuenode;
typedef struct
{queuenode *front;
queuenode *rear;
}linkqueue;
typedef struct ct
{datatype a;
datatype b;
}cons;
void initqueue(linkqueue *q);
void enqueue(linkqueue *q,datatype x);
int queueempty(linkqueue *q);
datatype dequeue(linkqueue *q);
//void print(linkqueue *q);
void main()
{
linkqueue *p,*q,*t;
cons *c;
int cm[N][N],cp[N],cq[N];
int i,j,x,m,f=1,cqf=0,cpf=1,cqf1=1;
datatype y;
p=(linkqueue *)malloc(sizeof(linkqueue));
p->front=(queuenode *)malloc(sizeof(queuenode));
p->rear=(queuenode *)malloc(sizeof(queuenode));
q=(linkqueue *)malloc(sizeof(linkqueue));
q->front=(queuenode *)malloc(sizeof(queuenode));
q->rear=(queuenode *)malloc(sizeof(queuenode));
t=(linkqueue *)malloc(sizeof(linkqueue));
t->front=(queuenode *)malloc(sizeof(queuenode));
t->rear=(queuenode *)malloc(sizeof(queuenode));
initqueue(p);
initqueue(q);
printf("input the element of the A set:\n");
scanf("%d",&x);
while(x!=0)
{
enqueue(p,x);
scanf("%d",&x);
}
printf("input the nums of cons:\n");
scanf("%d",&m);
c=(cons *)malloc(sizeof(cons)*m);
printf("input the cons:\n");
for(i=0;i scanf("%d %d",&c[i].a,&c[i].b);
for(i=0;i for(j=0;j cm[i][j]=0;
for(i=0;i {
cm[c[i].a-1][c[i].b-1]=1;
cm[c[i].b-1][c[i].a-1]=1;
}
for(i=0;i cq[i]=0;
for(i=0;i cp[i]=0;
while(!cqf)
{
y=dequeue(p);
if(cpf)
for(i=0;i cp[i]=cm[y-1][i];
if(!cp[y-1])
{
cq[y-1]=f;
for(i=0;i if(!cp[i]) cp[i]=cm[y-1][i];
}
else enqueue(q,y);
for(i=0;i cqf1=cqf1*cq[i];
cqf=cqf1;
cqf1=1;
for(i=0;i if(cp[i]) cpf=0;
if(queueempty(p)&&!queueempty(q))
{
t=p;
p=q;
q=t;
f++;
cpf=1;
for(i=0;i cp[i]=0;
}
}
printf("\nthe devided sets are:\n");
for(j=f;j>0;j--)
{
for(i=0;i {
if(cq[i]==j)
printf("%d ",i+1);
}
printf("\n");
}
}
void initqueue(linkqueue *q)
{
q->front->next=NULL;
q->rear->next=NULL;
}
void enqueue(linkqueue *q,datatype x)
{
queuenode *p=(queuenode *)malloc(sizeof(queuenode));
p->data=x;
p->next=NULL;
if(queueempty(q))
{
q->front->next=p;
q->rear=p;
}
else
{
q->rear->next=p;
q->rear=p;
}
}
int queueempty(linkqueue *q)
{
return q->front->next==NULL&&q->rear->next==NULL;
}
datatype dequeue(linkqueue *q)
{
datatype x;
queuenode *p;
if(queueempty(q))
printf("queue underflow\n");
p=q->front->next;
x=p->data;
q->front->next=p->next;
if(q->rear==p)
q->rear=q->front;
free(p);
return x;
}
/*void print(linkqueue *q)
{
queuenode *p;
printf("\n");
if(q->rear->next==q->front->next)
printf("\nqueue empty!\n");
else
{
p=q->front;
while(p->next!=NULL)
{
p=p->next;
printf("%d ",p->data);
}
}
} */