数据结构(顺序队列/链队列//循环队列)

news/2024/11/28 9:35:17/

顺序队列

#include<iostream>
#include<malloc.h>
using namespace std;
typedef int ElemType;
const int MaxSize=200;
typedef struct
{ElemType data[MaxSize];int rear,front;}SqQueue;void InitQueue(SqQueue *&s)
{s=(SqQueue *)malloc(sizeof(SqQueue));s->rear=s->front=-1;
}void Destroy( SqQueue *&s)
{free(s);
}bool enQueue(SqQueue *&s,ElemType e)
{if(s->rear==MaxSize-1)return false;s->rear++;s->data[s->rear]=e;return true;
}bool deQueue(SqQueue *&s,ElemType &e)
{if(s->front==s->rear)return false;s->front++;e=s->data[s->front];return true;
}
bool EmptyQueue(SqQueue *&s)
{return s->rear==s->front;
}int main()
{SqQueue *s;InitQueue(s);ElemType e;for(int i=0;i<10;i++){cin>>e;enQueue(s,e);}while(!EmptyQueue(s)){deQueue(s,e);cout<<e<<" ";}Destroy(s);
}

链队列

#include<iostream>
#include<malloc.h>
using namespace std;
typedef int ElemType;
typedef struct node
{ElemType data;struct node *next;
}DataNode;typedef struct
{DataNode *front;DataNode *rear;
}LinkQueue;void InitQueue(LinkQueue *&q)
{q=new LinkQueue;q->front=q->rear=NULL;
}void Destroy(LinkQueue *&q)
{DataNode *p=q->front,*r;if(p!=NULL){r=p->next;while(r!=NULL){free(p);p=r;r=p->next;}free(p);free(q);}
}bool EmptyQueue(LinkQueue *q)
{return q->rear==NULL;
}bool enQueue(LinkQueue *&q,ElemType e)
{DataNode *p;p=new DataNode;p->data=e;p->next=NULL;if(q->rear==NULL)//空队列q->front=q->rear=p;else{q->rear->next=p;q->rear=p;}
}bool deQueue(LinkQueue *&q,ElemType &e)
{DataNode *p;if(q->rear==NULL)//空队列return false;p=q->front;if(q->front==q->rear)//只有一个节点q->front=q->rear=NULL;elseq->front=q->front->next;e=p->data;free(p);return true;
}int main()
{   LinkQueue *q;InitQueue(q);ElemType e;for(int i=0;i<10;i++){cin>>e;enQueue(q,e);}while(!EmptyQueue(q)){deQueue(q,e);cout<<e<<" ";}Destroy(q);}

循环队列

#include<iostream>
#include<malloc.h>
using namespace std;
typedef int ElemType;
const int MaxSize=200;typedef struct
{ElemType data[MaxSize];int front,rear;
}SqQueue;void InitQueue(SqQueue *&s)
{s=(SqQueue *)malloc(sizeof(SqQueue));s->rear=s->front=0;}void Destroy( SqQueue *&s)
{free(s);
}bool EmptyQueue(SqQueue *&s)
{return s->rear==s->front;
}bool enQueue(SqQueue *&s,ElemType e)
{if((s->rear+1)%MaxSize==s->front)//队满return false;s->rear++;s->data[s->rear%MaxSize]=e;//s->rear=(s->rear+1)%MaxSize;//s->data[s->rear]=e;return true;
}bool deQueue(SqQueue *&s,ElemType &e)
{if(s->front==s->rear)return false;s->front++;e=s->data[s->front%MaxSize];return true;
}int main()
{SqQueue *s;InitQueue(s);ElemType e;for(int i=0;i<10;i++){cin>>e;enQueue(s,e);}while(!EmptyQueue(s)){deQueue(s,e);cout<<e<<" ";}Destroy(s);
}

http://www.ppmy.cn/news/487358.html

相关文章

数据结构(单链表,顺序结构)

//顺序表基本运算算法 实现算法的初始化&#xff0c;求是否为空表&#xff0c;返回长度&#xff0c;查询&#xff0c;插入&#xff0c;删除 #include <stdio.h> #include <malloc.h> #define MaxSize 50 typedef int ElemType; typedef struct { ElemType data[…

LLM - 基于 Vicuna-13B 参数计算搭建私有 ChatGPT 在线聊天

欢迎关注我的CSDN&#xff1a;https://spike.blog.csdn.net/ 本文地址&#xff1a;https://blog.csdn.net/caroline_wendy/article/details/131312366 LLaMA 和 Vicuna 都是大语言模型(LLM)&#xff0c;两者的差异如下&#xff1a; LLaMA (Large Language Model Meta AI)&#…

木一githab

git常用命令 本地项目代码上传到远端GitLab仓库&#xff1a; https://blog.csdn.net/weixin_42104592/article/details/106196306 GitLab配置ssh key&#xff1a; https://www.cnblogs.com/hafiz/p/8146324.html 拉取&#xff1a; git clone git192.168.200.109:snail/GitTest…

数据结构(链栈-链表)

方法一&#xff1a;引用 #include<iostream> #include<cstdio> #include<malloc.h> using namespace std; typedef int ElemType; const int MaxSize50; typedef struct LNode {ElemType data;struct LNode *next; } LinkList;void InitList(LinkList *&…

数据结构(双链表/循环链表例题 )

有一个带头结点的双链表L设计一个算法让其所有元素逆置&#xff0c;即第一个元素变成最后元素&#xff0c;第二个元素变成倒数第二个元素 typedef struct DNode {ElemType data;struct DNode *prior;struct DNode *next; } Dlinknode; void conversion(Dlinknode *&L) {Dl…

Python可变数据类型和不可变数据类型及函数参数传递

可变数据类型&#xff1a; 列表、字典、集合 当该数据类型的对应变量的值发生了改变&#xff0c;那么它对应的内存地址不发生改变&#xff0c;对于这种数据类型&#xff0c;就称可变数据类型。 不可变数据类型&#xff1a; 整型&#xff0c;字符串、元组 当该数据类型的对应…

数据结构(双链表的逆置)

试写一个算法&#xff0c;对双链表进行就地逆置 (头插法) void Reserve(DLinkNode *&L) {DLinkNode *pNULL,*q;//L->nextNULL;while(p!NULL){qp>next;//用q标记后继节点if(L->next!NULL)//这步发生是&#xff0c;表里一个数都没有&#xff0c;L指向空L->next-…

数据结构(顺序栈)

实现栈的初始化&#xff0c;进栈&#xff0c;出栈&#xff0c;取栈顶&#xff0c;判断是否空栈&#xff0c;销毁栈 #include<iostream> #include<cstdio> #include<malloc.h> using namespace std; typedef int ElemType; const int MaxSize50; typedef str…