数据结构实验二

实验二第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);
}
}
} */

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孟大帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值