• 队列的运行算法


    1.链队:

    插入 删除 打印 取队顶
    1. #include
    2. #include
    3. typedef struct Qnode{
    4. int data;
    5. struct Qnode *next;
    6. }Qnode,*QuenePtr;
    7. typedef struct {
    8. QuenePtr front;
    9. QuenePtr rear;
    10. }LinkQueue;
    11. //初始化
    12. void InitQueue(LinkQueue *q){
    13. (*q).front=(QuenePtr)malloc(sizeof (Qnode));
    14. if(!(*q).front) exit(0);
    15. (*q).front->next=NULL;
    16. (*q).rear=(*q).front;
    17. }
    18. //销毁
    19. void DestroyQueue(LinkQueue *q){
    20. while((*q).front){
    21. (*q).rear=(*q).front->next;
    22. free((*q).front);
    23. (*q).front=(*q).rear;
    24. }
    25. }
    26. //判空
    27. int IsEmpty(LinkQueue q){
    28. if(q.rear==q.front) return 1;
    29. else return 0;
    30. }
    31. //入队
    32. void EnQueue(LinkQueue *q,int e){
    33. QuenePtr p=(QuenePtr)malloc(sizeof(Qnode));
    34. if(!p)exit(0);
    35. p->data=e;
    36. p->next=NULL;
    37. (*q).rear->next=p;
    38. (*q).rear=p;
    39. }
    40. //出队
    41. void DeQueue(LinkQueue *q,int *e){
    42. if((*q).rear==((*q).front)) printf("Empty !");
    43. QuenePtr p=(*q).front->next;
    44. *e=p->data;
    45. (*q).front->next=p->next;
    46. if((*q).rear ==p) (*q).rear=(*q).front;
    47. free(p);
    48. }
    49. //打印
    50. void Show(LinkQueue *q){
    51. for(Qnode *p=q->front->next ;p!=NULL;p=p->next){
    52. printf("%d ",p->data);
    53. }
    54. printf("\n");
    55. }
    56. int main(){
    57. LinkQueue q;
    58. InitQueue(&q);
    59. for(int i=0;i<5;i++){
    60. int e; scanf("%d",&e);
    61. EnQueue(&q,e);
    62. }
    63. Show(&q);
    64. if(IsEmpty(q)) printf("empty!");
    65. else printf("not empty");
    66. int e;DeQueue(&q,&e); printf("出队元素是:%d \n",e);
    67. Show(&q);
    68. int val;scanf("%d",&val);
    69. DestroyQueue(&q);
    70. return 0;
    71. }

    2.循环队列

    wenti

    1. #include
    2. #include
    3. #define Maxsize 100
    4. typedef struct {
    5. int *base;
    6. int front ;
    7. int rear;
    8. }SqQueue;
    9. //初始化
    10. void InitQueue (SqQueue *q){
    11. q->base=(int *)malloc (Maxsize *sizeof (int));
    12. if(!q->base) exit(0);
    13. q->front=0;
    14. q->rear=0;
    15. }
    16. //入队
    17. void EnterQueue(SqQueue *q,int e){
    18. if((q->rear+1)%Maxsize==q->front) printf("Full!\n");
    19. q->base[q->rear]=e;
    20. q->rear=(q->rear +1)%Maxsize;
    21. }
    22. //取队头元素
    23. void getElem(SqQueue q,int *e){
    24. if(q.front!=q.rear){
    25. *e=q.base[q.front];
    26. }
    27. else {
    28. printf("Empty! \n");
    29. }
    30. }
    31. //求队长
    32. int Getlength(SqQueue q){
    33. int e;
    34. e=(q.rear-q.front+Maxsize)%Maxsize;
    35. return e;
    36. }
    37. //出队
    38. void PopQueue(SqQueue *q,int *e){
    39. if(q->front==q->rear) printf("Empty! \n");
    40. *e=q->base[q->front];
    41. q->front=(q->front+1)%Maxsize;
    42. }
    43. //打印
    44. void Print(SqQueue q){
    45. for (int i=q.front;i!=q.rear;){
    46. printf("%d ",q.base[i]);
    47. i=(i+1)%Maxsize;
    48. }
    49. printf("\n");
    50. }
    51. int main(){
    52. SqQueue q;
    53. int e;
    54. for(int i=0;i<5;i++){
    55. scanf("%d",&e);
    56. EnterQueue(&q,e);
    57. }
    58. Print(q);
    59. getElem(q,&e);
    60. printf("队顶元素是 %d \n",e);
    61. int length=Getlength(q);
    62. printf("队长是%d \n",length);
    63. PopQueue(&q,&e);
    64. printf("出队元素是%d \n",e);
    65. Print(q);
    66. return 0;
    67. }
    1. #include
    2. #include
    3. #include
    4. #define Maxsize 50
    5. typedef struct {
    6. int *base;
    7. int front;
    8. int rear;
    9. }SqQueue;
    10. void InitQueue(SqQueue *Q){
    11. (*Q).base=(int )malloc(Maxsize*sizeof(int));
    12. if(!(*Q).base)exit(0);
    13. (*Q).front=0;
    14. (*Q).rear=0;
    15. }
    16. void EnterQueue(SqQueue *q,int e){
    17. if(((*q).base+1) %Maxsize==q->front){
    18. printf("Full");
    19. }
    20. q->base[q->rear]=e;
    21. q->rear=(q->rear+1)%Maxsize;
    22. }
    23. void PopQueue(SqQueue *q,int *e){
    24. if(q->front==q->rear){
    25. printf("empty!\n");
    26. }
    27. e=q->base[q->front];
    28. q->front=(q->front+1)%Maxsize;
    29. }
    30. int Emp(SqQueue q){
    31. if(q.front==q.rear) return 0;
    32. else return 1;
    33. }
    34. int main(){
    35. int n,m;
    36. scanf("%d %d",&n,&m);
    37. int a[n];
    38. SqQueue Q;
    39. InitQueue(&Q);
    40. for(int i=0;i
    41. scanf("%d ",&a[i]);
    42. EnterQueue(&Q,a[i]);
    43. }
    44. int e;
    45. for(int i=0;i
    46. PopQueue(&Q,&e);
    47. printf("%d ",e);
    48. }
    49. if(Emp(Q)==1) printf("\n1");
    50. else printf("\n0");
    51. return 0;
    52. }
    1. #include
    2. #include
    3. #define TOTAL_SPACE 5
    4. /**
    5. * Circle int queue.
    6. */
    7. typedef struct CircleIntQueue
    8. {
    9. int data[TOTAL_SPACE];
    10. int head;
    11. int tail;
    12. }*CircleIntQueuePtr;
    13. /**
    14. * Initialize the queue.
    15. */
    16. CircleIntQueuePtr initQueue()
    17. {
    18. CircleIntQueuePtr resultPtr = (CircleIntQueuePtr)malloc(sizeof(struct CircleIntQueue));
    19. resultPtr->head = 0;
    20. resultPtr->tail = 0;
    21. return resultPtr;
    22. }// Of the first constructor
    23. /**
    24. * Enqueue.
    25. *
    26. * @param paraValue The value of the new node.
    27. */
    28. void enqueue(CircleIntQueuePtr paraPtr, int paraValue)
    29. {
    30. if ((paraPtr->tail + 1) % TOTAL_SPACE == paraPtr->head) {
    31. printf("Queue full.\r\n");
    32. return;
    33. } // Of if
    34. paraPtr->data[paraPtr->tail % TOTAL_SPACE] = paraValue;
    35. paraPtr->tail++;
    36. }// Of enqueue
    37. /**
    38. * Dequeue.
    39. *
    40. * @return The value at the head.
    41. */
    42. int dequeue(CircleIntQueuePtr paraPtr)
    43. {
    44. int resultValue;
    45. if (paraPtr->head == paraPtr->tail) {
    46. printf("No element in the queue.\r\n");
    47. return -1;
    48. } // Of if
    49. resultValue = paraPtr->data[paraPtr->head % TOTAL_SPACE];
    50. paraPtr->head++;
    51. return resultValue;
    52. }// Of dequeue
    53. /**
    54. * Output the queue.
    55. */
    56. void outputLinkQueue(CircleIntQueuePtr paraPtr)
    57. {
    58. int i;
    59. if (paraPtr->head == paraPtr->tail)
    60. {
    61. printf("Empty queue.");
    62. return;
    63. } // Of if
    64. printf("Elements in the queue: ");
    65. for (i = paraPtr->head; i < paraPtr->tail; i++)
    66. {
    67. printf("%d, ", paraPtr->data[i % TOTAL_SPACE]);
    68. } // Of for i
    69. printf("\r\n");
    70. }//Of outputLinkQueue
    71. /**
    72. * Unit test.
    73. */
    74. void testLinkQueue()
    75. {
    76. int i = 10;
    77. CircleIntQueuePtr tempPtr = initQueue();
    78. for (; i < 16; i ++)
    79. {
    80. enqueue(tempPtr, i);
    81. }//Of for i
    82. outputLinkQueue(tempPtr);
    83. for (i = 0; i < 6; i ++)
    84. {
    85. printf("dequeue gets %d\r\n", dequeue(tempPtr));
    86. }//Of for i
    87. enqueue(tempPtr, 8);
    88. outputLinkQueue(tempPtr);
    89. }//Of testLinkQueue
    90. /**
    91. * The entrance.
    92. */
    93. int main()
    94. {
    95. testLinkQueue();
    96. return 1;
    97. }

    3.问题 A: 【DS3-6】基于顺序存储的循环队列的基本操作

    [命题人 : admin_ly]
    时间限制 : 1.000 sec  内存限制 : 128 MB
     
    题目描述

    用顺序存储结构实现队列的入队、出队和判断队列是否为空操作。

    输入

    第一行输入两个整数n和m,n表示入队序列A的长度(n个数依次连续入队,中间没有出队的情况),m表示出队序列B的元素数量(m个数依次连续出队,中间没有入队的情况)。第二行为序列A(空格分隔的n个整数),整数之间用空格分隔。

    输出

    第一行输出m个整数,代表出队序列B的各个整数,整数之间用空格分隔。

    第二行输出一个整数表示队列是否为空,队列为空输出0,不为空输出1。

    样例输入 Copy
    1. 5 3
    2. 1 3 5 3 6
    样例输出 Copy
    1. 1 3 5
    2. 1
    1. #include
    2. #include
    3. //数据类型
    4. #define ElemType int
    5. //队列的最大空间
    6. #define MAXSIZE 8
    7. //队列的管理结构
    8. typedef struct Queue
    9. {
    10. ElemType *base; //指向队列空间的基址
    11. int front; //头指针
    12. int rear; //尾指针
    13. }Queue;
    14. //队列初始化
    15. void InitQueue(Queue *Q)
    16. {
    17. //为队列分配存储空间
    18. Q->base = (ElemType *)malloc(sizeof(ElemType) * MAXSIZE);
    19. if(Q->base == NULL) exit(0);
    20. //初始时队列为空,头指针和尾指针都指向0位置
    21. Q->front = 0;
    22. Q->rear = 0;
    23. }
    24. //入队操作
    25. void EnQueue(Queue *Q, ElemType x)
    26. {
    27. //判断循环队列是否已满
    28. if(((Q->rear+1)%MAXSIZE) == Q->front)
    29. printf("Full\n");
    30. //队列未满,将数据入队
    31. Q->base[Q->rear] = x;
    32. //更改尾指针的指向
    33. Q->rear = (Q->rear+1)%MAXSIZE;
    34. }
    35. //打印循环队列中的数据
    36. void ShowQueue(Queue *Q)
    37. {
    38. //遍历循环队列中的元素,并将数据打印
    39. for(int i=Q->front; i!=Q->rear;)
    40. {
    41. printf("%d ",Q->base[i]);
    42. //此操作是为了实现循环遍历
    43. i = (i+1)%MAXSIZE;
    44. }
    45. printf("\n");
    46. }
    47. //出队操作
    48. int DeQueue(Queue *Q)
    49. {
    50. //判断循环队列是否为空
    51. if(Q->front == Q->rear)
    52. printf("empty\n");
    53. //如果非空,实现可循环出队
    54. int e=Q->base[Q->front];
    55. Q->front = (Q->front+1)%MAXSIZE;
    56. return e;
    57. }
    58. int main()
    59. {
    60. Queue Q;
    61. InitQueue(&Q);
    62. int m,n;
    63. scanf("%d %d",&n,&m);
    64. int a[n];
    65. for(int i=1; i<=n; ++i)
    66. {
    67. scanf("%d",&a[i]);
    68. EnQueue(&Q, a[i]);
    69. }
    70. for(int i=0;i
    71. int e=DeQueue(&Q);
    72. printf("%d ",e);
    73. }
    74. if(Q.front==Q.rear) printf("\n0");
    75. else printf("\n1");
    76. return 0;
    77. }

     

    2.问题 B: 【DS3-7】基于循环链表的队列的基本操作

    [命题人 : admin_ly]
    时间限制 : 1.000 sec  内存限制 : 128 MB
     
    题目描述

    用带头结点的循环链表表示队列,并且只设一个指针指向队尾元素结点(不设头指针)。实现该队列的入队、出队和判断队列是否为空操作。

    输入

    第一行输入两个整数n和m,n表示入队序列A的长度(n个数依次连续入队,中间没有出队的情况),m表示出队序列B的元素数量(m个数依次连续出队,中间没有入队的情况)。第二行为序列A(空格分隔的n个整数),整数之间用空格分隔。 

    输出

    第一行输出m个整数,代表出队序列B的各个整数,整数之间用空格分隔。
    第二行输出一个整数表示队列是否为空,队列为空输出0,不为空输出1

    样例输入 Copy
    1. 5 3
    2. 1 3 5 3 6
    样例输出 Copy
    1. 1 3 5
    2. 1
    1. #include
    2. #include
    3. /**
    4. * 链式循环队列结点结构体定义
    5. */
    6. typedef struct QNode {
    7. int data;
    8. struct QNode *next;
    9. struct QNode *rear;
    10. } QNode;
    11. void init(QNode **queue) {
    12. *queue = (QNode *) malloc(sizeof(QNode));
    13. // 将链式队列头结点的 next 和 rear 指针都指向自身,因为是循环的,并且是空队列
    14. (*queue)->next = *queue;
    15. (*queue)->rear = *queue;
    16. }
    17. void enQueue(QNode **rear, int ele) {
    18. QNode *newNode = (QNode *) malloc(sizeof(QNode));
    19. newNode->data = ele;
    20. newNode->next = NULL;
    21. newNode->next = (*rear)->next;
    22. (*rear)->next = newNode;
    23. (*rear) = newNode;
    24. }
    25. int deQueue(QNode **rear) {
    26. if ((*rear)->next == *rear) {
    27. return 0;
    28. }
    29. QNode *headNode = (*rear)->next;
    30. QNode *startNode = headNode->next;
    31. int ele;
    32. ele = startNode->data;
    33. headNode->next = startNode->next;
    34. if (startNode==*rear){
    35. *rear=(*rear)->next;
    36. }
    37. free(startNode);
    38. return ele;
    39. }
    40. int Is_empty(QNode **rear){
    41. if ((*rear)->next == *rear) {
    42. return 0;
    43. }
    44. else return 1;
    45. }
    46. int main() {
    47. QNode *queue;
    48. init(&queue);
    49. int n,m;
    50. scanf("%d %d",&n,&m);
    51. int a[n];
    52. for(int i=0;i
    53. scanf("%d",&a[i]);
    54. enQueue(&(queue->rear), a[i]);
    55. }
    56. for(int i=0;i
    57. int p= deQueue(&(queue->rear));
    58. printf("%d ",p);
    59. }
    60. printf("\n");
    61. if(Is_empty(&(queue->rear))==1){
    62. printf("1");
    63. }
    64. else printf("0");
    65. return 0;
    66. }

    AHNU-DS

    3。问题 C: 【DS3-08】使用队列判断整数是否为回文数

    [命题人 : 2022069]

    时间限制 : 1.000 sec  内存限制 : 128 MB
     

    题目描述

    给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。

    回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

    例如,121 是回文,而 123 不是。

    输入

    一个整数x

    输出

    如果 x 是一个回文整数,返回 true ;否则,返回 false 。

    样例输入 Copy
    121
    样例输出 Copy
    true
    1. #include
    2. #include
    3. #define MAX_SIZE 100
    4. typedef struct {
    5. int data[MAX_SIZE];
    6. int front;
    7. int rear;
    8. } Queue;
    9. void initQueue(Queue *q) {
    10. q->front = 0;
    11. q->rear = -1;
    12. }
    13. bool isQueueEmpty(Queue *q) {
    14. return q->rear < q->front;
    15. }
    16. bool isQueueFull(Queue *q) {
    17. return q->rear - q->front + 1 == MAX_SIZE;
    18. }
    19. void enqueue(Queue *q, int value) {
    20. if (isQueueFull(q)) {
    21. printf("Queue is full");
    22. return;
    23. }
    24. q->data[++q->rear] = value;
    25. }
    26. int dequeue(Queue *q) {
    27. if (isQueueEmpty(q)) {
    28. return -1;
    29. }
    30. return q->data[q->front++];
    31. }
    32. int dequeue1(Queue *q) {
    33. if (isQueueEmpty(q)) {
    34. return -1;
    35. }
    36. return q->data[q->rear--];
    37. }
    38. bool isPalindrome(int x) {
    39. if (x < 0) { // 负数不是回文数
    40. return false;
    41. }
    42. Queue q;
    43. initQueue(&q);
    44. int temp = x;
    45. while (temp!= 0) { // 将整数的每一位入队
    46. enqueue(&q,temp%10);temp=temp/10;
    47. }
    48. while (!isQueueEmpty(&q)) { // 从队列头和队列尾分别取出数字比较
    49. if(dequeue(&q)!=dequeue1(&q)||dequeue(&q)&&dequeue1(&q)!=-1)
    50. return false;
    51. }
    52. return true;
    53. }
    54. int main() {
    55. int num;
    56. scanf("%d", &num);
    57. if (isPalindrome(num)) {
    58. printf("true");
    59. } else {
    60. printf("false");
    61. }
    62. return 0;
    63. }

  • 相关阅读:
    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