数据与结构实验三:队列操作实现及应用

1. 实验目的

(1)能实现队列的顺序存储和链式存储下的基本操作;

(2)能设计队列结构并用队列基本操作解决实际问题。

2. 实验内容

(1)第一关:队列数据结构设计及基本操作实现:包括队列初始化、判断队列是否为空、入队、出队运算和求队头元素等操作;

(2)第二关:利用栈和队列操作实现回文判断。

任务描述

本关任务: (1)编写实现队列,至少包括建立空队列、判断队列是否为空、入队、出队和求队头元素等功能, (2)设计主程序调用队列和自己在前面实验中实现的栈实现回文判断算法。

相关知识

队列与栈相关知识请参考课本。 使用队列和栈进行回文判断的算法较为简单,此处仅仅是为了练习栈与队列的使用而设计的场景,具体算法如下:

(1)读入字符串到一维数组

(2)将字符串中的字符依次复制到队列中

(3)将字符串中的字符依次压入栈中

(4)循环,出队一个字符,出栈一个字符,进行比较字符是否相等。

输入提示

(1)scanf函数一般格式为scanf(“%s”,st),但scanf默认回车和空格是输入不同组之间的间隔和结束符号,所以输入带空格,tab或者回车的字符串是不可以的。 (2)可以用scanf(“%[^/n]”,str)//遇到回车结束,/n换成c则表示遇到字符c结束 (3)这里假设所输入的字符串没有空格,tab或者回车之类的字符

编程要求

根据提示,在右侧编辑器完整自己编写代码实现。

测试说明

平台会对你编写的代码进行测试:

测试输入:123abc 预期输出:no

测试输入:abcdcba 预期输出:yes

#include <stdio.h>
typedef int DataType;
struct seqQueue
{
   int MAXNUM;//用于记录顺序队列中能存放的最大元素个数,整型 
    int front, rear;//用于存放顺序队列的队首,队尾
    DataType* element;//用于存放数据元素的连续空间的起始地址

};

typedef struct seqQueue *PseqQueue;
PseqQueue createNullQueue_seq(int m)
{

 if (m == 0)
        return NULL;
    else
    {
        PseqQueue Q = (PseqQueue)malloc(sizeof(struct seqQueue));
        Q->element = (DataType*)malloc(sizeof(struct seqQueue));
        Q->front = Q->rear = 0;
        Q->MAXNUM = m;
        return Q;
 
    }
}

int isNullQueue_seq(PseqQueue Q)
{
  
        return -1;
    else
    {
        if (Q->front == Q->rear)
            return 1;
        else
            return 0;
    }

}



int enQueue_seq(PseqQueue Q ,DataType x)
{
   if (isFullQueue_seq(Q))
        return 0;
    else
    {
        Q->element[Q->rear] = x;
        Q->rear++;
        return 1;
    }

 

}

DataType delQueue_seq(PseqQueue Q)
{
    if (Q->front == Q->rear)
        return 1;
    else
    {
        DataType a = Q->element[Q->front];
        Q->front++;
        return a;
    }

}


DataType front_seq(PseqQueue Q)
{
    if (Q->front == Q->rear)
        return -1;
    else
        return (Q->element[Q->front]);
}
#include <stdio.h>
#include <stdlib.h>
typedef char DataType;
typedef struct node
{
    DataType info;
    struct node *next;
}queuedata;
 
typedef struct queuerecord{
    queuedata *front, *rear ;
}linkqueue;
typedef struct node *pqueuedata;
typedef struct queuerecord *plinkqueue;
 
plinkqueue createqueue( )
{
    plinkqueue p=(plinkqueue)malloc(sizeof(linkqueue));
    p->front=NULL;
    p->rear=NULL;
 
}
 
int emptyequeue(plinkqueue q)
{
    plinkqueue p;
    p=q;
    if(p->front==NULL)
        return 1;
    else
        return 0;
 
 
}
 
void pushqueue(DataType x, plinkqueue q)
{
    pqueuedata p=(pqueuedata)malloc(sizeof(struct node));
    if(q->front==NULL&&q->rear==NULL)
    {
        q->front=p;
        q->rear=p;
    }
    p->info=x;
    p->next=NULL;
    q->rear->next=p;
    q->rear=q->rear->next;
 
 
}
 
 
DataType popqueue(plinkqueue q)
{
    pqueuedata p=q->front;
    DataType temp=q->front->info;
    q->front=q->front->next;
    free(p);
    return temp;
 
}
 
struct node1{
    DataType element;
    struct node1 *next;
};
 
typedef struct node1 *PtrToNode;
typedef struct node1 * stack;
 
 
int emptystack(stack s)
{
 
    if(s->element==0&&s->next==NULL)
        return 1;
    else
        return 0;
 
 
}
stack createStack(void)
{
 
    stack top=(stack)malloc(sizeof(struct node1));
    top->element=0;
    top->next=NULL;
    return top;
 
 
}
 
 
void pushstack(DataType  x,stack s)
{
 
    stack temp=(struct node1 *)malloc(sizeof(struct node1));
    temp->element=x;
    temp->next=s->next;
    s->next=temp;
 
 
}
DataType popstack(stack s)
{
 
    if(emptystack(s))
        return -1;
    else
    {
        stack node=s->next;
        int tmp;
        tmp=node->element;
        s->next=node->next;
        free(node);
 
        return tmp;
 
    }
 
}
 
int palindrome(char src[])
{
    plinkqueue p1=createqueue();
    stack p2=createStack();
    int i=0;
    int j=0;
    while(src[i]!='\0')
    {
        pushqueue(src[i], p1);
        i++;
    }
    while(src[j]!='\0')
    {
        pushstack(src[j],p2);
        j++;
    }
    while(emptyequeue(p1)!=1)
    {
        DataType x1=popqueue(p1);
        DataType x2=popstack(p2);
        if(x1!=x2)
            return 0;
    }
    return 1;
}
 
int main(void)
{
 
    char a[100]={'0'};
    scanf("%s",a);
 
    int res=palindrome(a);
    if(res==1)
        printf("Yes\n");
    else
        printf("No\n");
 
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值