- #include
- #include
-
- typedef struct Qnode{
- int data;
- struct Qnode *next;
- }Qnode,*QuenePtr;
-
- typedef struct {
- QuenePtr front;
- QuenePtr rear;
- }LinkQueue;
- //初始化
- void InitQueue(LinkQueue *q){
- (*q).front=(QuenePtr)malloc(sizeof (Qnode));
- if(!(*q).front) exit(0);
- (*q).front->next=NULL;
- (*q).rear=(*q).front;
- }
- //销毁
- void DestroyQueue(LinkQueue *q){
- while((*q).front){
- (*q).rear=(*q).front->next;
- free((*q).front);
- (*q).front=(*q).rear;
- }
- }
- //判空
- int IsEmpty(LinkQueue q){
- if(q.rear==q.front) return 1;
- else return 0;
- }
- //入队
- void EnQueue(LinkQueue *q,int e){
- QuenePtr p=(QuenePtr)malloc(sizeof(Qnode));
- if(!p)exit(0);
- p->data=e;
- p->next=NULL;
- (*q).rear->next=p;
- (*q).rear=p;
-
- }
- //出队
- void DeQueue(LinkQueue *q,int *e){
- if((*q).rear==((*q).front)) printf("Empty !");
- QuenePtr p=(*q).front->next;
- *e=p->data;
- (*q).front->next=p->next;
- if((*q).rear ==p) (*q).rear=(*q).front;
- free(p);
- }
- //打印
- void Show(LinkQueue *q){
- for(Qnode *p=q->front->next ;p!=NULL;p=p->next){
- printf("%d ",p->data);
- }
- printf("\n");
- }
-
- int main(){
- LinkQueue q;
- InitQueue(&q);
-
- for(int i=0;i<5;i++){
- int e; scanf("%d",&e);
- EnQueue(&q,e);
- }
- Show(&q);
-
- if(IsEmpty(q)) printf("empty!");
- else printf("not empty");
-
- int e;DeQueue(&q,&e); printf("出队元素是:%d \n",e);
- Show(&q);
-
- int val;scanf("%d",&val);
-
-
- DestroyQueue(&q);
- return 0;
- }

wenti
- #include
- #include
- #define Maxsize 100
-
- typedef struct {
- int *base;
- int front ;
- int rear;
- }SqQueue;
-
- //初始化
- void InitQueue (SqQueue *q){
- q->base=(int *)malloc (Maxsize *sizeof (int));
- if(!q->base) exit(0);
- q->front=0;
- q->rear=0;
- }
- //入队
- void EnterQueue(SqQueue *q,int e){
- if((q->rear+1)%Maxsize==q->front) printf("Full!\n");
- q->base[q->rear]=e;
- q->rear=(q->rear +1)%Maxsize;
- }
- //取队头元素
- void getElem(SqQueue q,int *e){
- if(q.front!=q.rear){
- *e=q.base[q.front];
- }
- else {
- printf("Empty! \n");
- }
- }
- //求队长
- int Getlength(SqQueue q){
- int e;
- e=(q.rear-q.front+Maxsize)%Maxsize;
- return e;
- }
- //出队
- void PopQueue(SqQueue *q,int *e){
- if(q->front==q->rear) printf("Empty! \n");
- *e=q->base[q->front];
- q->front=(q->front+1)%Maxsize;
- }
- //打印
- void Print(SqQueue q){
- for (int i=q.front;i!=q.rear;){
- printf("%d ",q.base[i]);
- i=(i+1)%Maxsize;
- }
- printf("\n");
- }
-
- int main(){
- SqQueue q;
- int e;
- for(int i=0;i<5;i++){
- scanf("%d",&e);
- EnterQueue(&q,e);
- }
- Print(q);
- getElem(q,&e);
- printf("队顶元素是 %d \n",e);
-
- int length=Getlength(q);
- printf("队长是%d \n",length);
-
- PopQueue(&q,&e);
- printf("出队元素是%d \n",e);
- Print(q);
- return 0;
- }
-
- #include
- #include
- #include
- #define Maxsize 50
-
- typedef struct {
- int *base;
- int front;
- int rear;
- }SqQueue;
-
- void InitQueue(SqQueue *Q){
- (*Q).base=(int )malloc(Maxsize*sizeof(int));
- if(!(*Q).base)exit(0);
- (*Q).front=0;
- (*Q).rear=0;
- }
-
- void EnterQueue(SqQueue *q,int e){
- if(((*q).base+1) %Maxsize==q->front){
- printf("Full");
- }
- q->base[q->rear]=e;
- q->rear=(q->rear+1)%Maxsize;
- }
-
- void PopQueue(SqQueue *q,int *e){
- if(q->front==q->rear){
- printf("empty!\n");
- }
- e=q->base[q->front];
- q->front=(q->front+1)%Maxsize;
- }
-
- int Emp(SqQueue q){
- if(q.front==q.rear) return 0;
- else return 1;
- }
-
- int main(){
- int n,m;
- scanf("%d %d",&n,&m);
- int a[n];
- SqQueue Q;
- InitQueue(&Q);
- for(int i=0;i
- scanf("%d ",&a[i]);
- EnterQueue(&Q,a[i]);
- }
- int e;
- for(int i=0;i
- PopQueue(&Q,&e);
- printf("%d ",e);
- }
-
- if(Emp(Q)==1) printf("\n1");
- else printf("\n0");
- return 0;
- }
-
- #include
- #include
-
- #define TOTAL_SPACE 5
-
- /**
- * Circle int queue.
- */
- typedef struct CircleIntQueue
- {
- int data[TOTAL_SPACE];
-
- int head;
-
- int tail;
- }*CircleIntQueuePtr;
-
- /**
- * Initialize the queue.
- */
- CircleIntQueuePtr initQueue()
- {
- CircleIntQueuePtr resultPtr = (CircleIntQueuePtr)malloc(sizeof(struct CircleIntQueue));
- resultPtr->head = 0;
- resultPtr->tail = 0;
-
- return resultPtr;
- }// Of the first constructor
-
- /**
- * Enqueue.
- *
- * @param paraValue The value of the new node.
- */
- void enqueue(CircleIntQueuePtr paraPtr, int paraValue)
- {
- if ((paraPtr->tail + 1) % TOTAL_SPACE == paraPtr->head) {
- printf("Queue full.\r\n");
- return;
- } // Of if
-
- paraPtr->data[paraPtr->tail % TOTAL_SPACE] = paraValue;
- paraPtr->tail++;
- }// Of enqueue
-
- /**
- * Dequeue.
- *
- * @return The value at the head.
- */
- int dequeue(CircleIntQueuePtr paraPtr)
- {
- int resultValue;
- if (paraPtr->head == paraPtr->tail) {
- printf("No element in the queue.\r\n");
- return -1;
- } // Of if
-
- resultValue = paraPtr->data[paraPtr->head % TOTAL_SPACE];
- paraPtr->head++;
-
- return resultValue;
- }// Of dequeue
-
- /**
- * Output the queue.
- */
- void outputLinkQueue(CircleIntQueuePtr paraPtr)
- {
- int i;
- if (paraPtr->head == paraPtr->tail)
- {
- printf("Empty queue.");
- return;
- } // Of if
-
- printf("Elements in the queue: ");
- for (i = paraPtr->head; i < paraPtr->tail; i++)
- {
- printf("%d, ", paraPtr->data[i % TOTAL_SPACE]);
- } // Of for i
-
- printf("\r\n");
- }//Of outputLinkQueue
-
- /**
- * Unit test.
- */
- void testLinkQueue()
- {
- int i = 10;
- CircleIntQueuePtr tempPtr = initQueue();
- for (; i < 16; i ++)
- {
- enqueue(tempPtr, i);
- }//Of for i
-
- outputLinkQueue(tempPtr);
-
- for (i = 0; i < 6; i ++)
- {
- printf("dequeue gets %d\r\n", dequeue(tempPtr));
- }//Of for i
-
- enqueue(tempPtr, 8);
- outputLinkQueue(tempPtr);
- }//Of testLinkQueue
-
- /**
- * The entrance.
- */
- int main()
- {
- testLinkQueue();
- return 1;
-
- }
题目描述
用顺序存储结构实现队列的入队、出队和判断队列是否为空操作。
输入
第一行输入两个整数n和m,n表示入队序列A的长度(n个数依次连续入队,中间没有出队的情况),m表示出队序列B的元素数量(m个数依次连续出队,中间没有入队的情况)。第二行为序列A(空格分隔的n个整数),整数之间用空格分隔。
输出
第一行输出m个整数,代表出队序列B的各个整数,整数之间用空格分隔。
第二行输出一个整数表示队列是否为空,队列为空输出0,不为空输出1。
样例输入 Copy
- 5 3
- 1 3 5 3 6
样例输出 Copy
- 1 3 5
- 1
-
-
- #include
- #include
-
- //数据类型
- #define ElemType int
- //队列的最大空间
- #define MAXSIZE 8
- //队列的管理结构
- typedef struct Queue
- {
- ElemType *base; //指向队列空间的基址
- int front; //头指针
- int rear; //尾指针
- }Queue;
- //队列初始化
- void InitQueue(Queue *Q)
- {
- //为队列分配存储空间
- Q->base = (ElemType *)malloc(sizeof(ElemType) * MAXSIZE);
- if(Q->base == NULL) exit(0);
- //初始时队列为空,头指针和尾指针都指向0位置
- Q->front = 0;
- Q->rear = 0;
- }
-
- //入队操作
- void EnQueue(Queue *Q, ElemType x)
- {
- //判断循环队列是否已满
- if(((Q->rear+1)%MAXSIZE) == Q->front)
- printf("Full\n");
- //队列未满,将数据入队
- Q->base[Q->rear] = x;
- //更改尾指针的指向
- Q->rear = (Q->rear+1)%MAXSIZE;
- }
-
- //打印循环队列中的数据
- void ShowQueue(Queue *Q)
- {
- //遍历循环队列中的元素,并将数据打印
- for(int i=Q->front; i!=Q->rear;)
- {
- printf("%d ",Q->base[i]);
- //此操作是为了实现循环遍历
- i = (i+1)%MAXSIZE;
- }
- printf("\n");
- }
-
- //出队操作
- int DeQueue(Queue *Q)
- {
- //判断循环队列是否为空
- if(Q->front == Q->rear)
- printf("empty\n");
- //如果非空,实现可循环出队
- int e=Q->base[Q->front];
- Q->front = (Q->front+1)%MAXSIZE;
- return e;
- }
-
- int main()
- {
- Queue Q;
- InitQueue(&Q);
- int m,n;
- scanf("%d %d",&n,&m);
- int a[n];
- for(int i=1; i<=n; ++i)
- {
- scanf("%d",&a[i]);
- EnQueue(&Q, a[i]);
- }
- for(int i=0;i
- int e=DeQueue(&Q);
- printf("%d ",e);
- }
- if(Q.front==Q.rear) printf("\n0");
- else printf("\n1");
- return 0;
- }
-
题目描述
用带头结点的循环链表表示队列,并且只设一个指针指向队尾元素结点(不设头指针)。实现该队列的入队、出队和判断队列是否为空操作。
输入
第一行输入两个整数n和m,n表示入队序列A的长度(n个数依次连续入队,中间没有出队的情况),m表示出队序列B的元素数量(m个数依次连续出队,中间没有入队的情况)。第二行为序列A(空格分隔的n个整数),整数之间用空格分隔。
输出
第一行输出m个整数,代表出队序列B的各个整数,整数之间用空格分隔。
第二行输出一个整数表示队列是否为空,队列为空输出0,不为空输出1
样例输入 Copy
- 5 3
- 1 3 5 3 6
样例输出 Copy
- 1 3 5
- 1
- #include
- #include
-
- /**
- * 链式循环队列结点结构体定义
- */
- typedef struct QNode {
-
- int data;
-
- struct QNode *next;
- struct QNode *rear;
- } QNode;
- void init(QNode **queue) {
- *queue = (QNode *) malloc(sizeof(QNode));
- // 将链式队列头结点的 next 和 rear 指针都指向自身,因为是循环的,并且是空队列
- (*queue)->next = *queue;
- (*queue)->rear = *queue;
- }
-
-
- void enQueue(QNode **rear, int ele) {
- QNode *newNode = (QNode *) malloc(sizeof(QNode));
- newNode->data = ele;
- newNode->next = NULL;
- newNode->next = (*rear)->next;
- (*rear)->next = newNode;
- (*rear) = newNode;
- }
-
-
- int deQueue(QNode **rear) {
- if ((*rear)->next == *rear) {
- return 0;
- }
-
- QNode *headNode = (*rear)->next;
- QNode *startNode = headNode->next;
- int ele;
- ele = startNode->data;
- headNode->next = startNode->next;
-
- if (startNode==*rear){
- *rear=(*rear)->next;
- }
- free(startNode);
- return ele;
- }
-
-
- int Is_empty(QNode **rear){
- if ((*rear)->next == *rear) {
- return 0;
- }
- else return 1;
- }
-
- int main() {
- QNode *queue;
- init(&queue);
- int n,m;
- scanf("%d %d",&n,&m);
- int a[n];
- for(int i=0;i
- scanf("%d",&a[i]);
- enQueue(&(queue->rear), a[i]);
- }
-
- for(int i=0;i
- int p= deQueue(&(queue->rear));
- printf("%d ",p);
- }
- printf("\n");
- if(Is_empty(&(queue->rear))==1){
- printf("1");
- }
- else printf("0");
- return 0;
-
-
-
-
-
- }
-
3。问题 C: 【DS3-08】使用队列判断整数是否为回文数
[命题人 : 2022069]
时间限制 : 1.000 sec 内存限制 : 128 MB
题目描述
给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。
回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
例如,121 是回文,而 123 不是。
输入
一个整数x
输出
如果 x 是一个回文整数,返回 true ;否则,返回 false 。
样例输入 Copy
121
样例输出 Copy
true
- #include
- #include
-
- #define MAX_SIZE 100
-
- typedef struct {
- int data[MAX_SIZE];
- int front;
- int rear;
- } Queue;
-
- void initQueue(Queue *q) {
- q->front = 0;
- q->rear = -1;
- }
-
- bool isQueueEmpty(Queue *q) {
- return q->rear < q->front;
- }
-
- bool isQueueFull(Queue *q) {
- return q->rear - q->front + 1 == MAX_SIZE;
- }
-
- void enqueue(Queue *q, int value) {
- if (isQueueFull(q)) {
- printf("Queue is full");
- return;
- }
- q->data[++q->rear] = value;
- }
-
- int dequeue(Queue *q) {
- if (isQueueEmpty(q)) {
- return -1;
- }
- return q->data[q->front++];
- }
- int dequeue1(Queue *q) {
- if (isQueueEmpty(q)) {
- return -1;
- }
- return q->data[q->rear--];
- }
- bool isPalindrome(int x) {
- if (x < 0) { // 负数不是回文数
- return false;
- }
- Queue q;
- initQueue(&q);
- int temp = x;
- while (temp!= 0) { // 将整数的每一位入队
- enqueue(&q,temp%10);temp=temp/10;
- }
- while (!isQueueEmpty(&q)) { // 从队列头和队列尾分别取出数字比较
- if(dequeue(&q)!=dequeue1(&q)||dequeue(&q)&&dequeue1(&q)!=-1)
- return false;
- }
- return true;
- }
-
- int main() {
- int num;
- scanf("%d", &num);
- if (isPalindrome(num)) {
- printf("true");
- } else {
- printf("false");
- }
- return 0;
- }
-
相关阅读:
TCP详解之滑动窗口
HPC、AI与云计算:当智能时代三叉戟在亚马逊云科技完美融合
正确选择数据库安全运维平台的几个原则
京东健康、平安健康To B各有倚仗
【MyBatis】五、MyBatis的缓存机制与逆向工程
1.1.C++项目:仿muduo库实现并发服务器之any类的设计
UltraEdit\UEStudio 的 SSHTelnet 功能教程
[docker]笔记-网络故障处理
Windows系统使用MinGW编译二维码库Zbar
什么是动态规划?
-
原文地址:https://blog.csdn.net/m0_74161592/article/details/133867952