首页 > 后端开发 > C++ > 正文

用C语言讲解删除队列中的元素

王林
发布: 2023-08-28 09:45:15
转载
2068人浏览过

数据结构是以结构化方式组织的数据集合。它分为两种类型,如下所述 -

  • 线性数据结构 - 数据以线性方式组织。例如,数组、结构体、堆栈、队列、链表。

  • 非线性数据结构 - 数据以分层方式组织。例如,树、图、集合、表。

队列

它是一种线性数据结构,插入在后端完成,删除是在前端完成的。

用C语言讲解删除队列中的元素

立即学习C语言免费学习笔记(深入)”;

队列的顺序是 FIFO – 先进先出

操作

  • 插入 – 将元素插入队列.
  • Delete – 从队列中删除一个元素。

条件

  • 队列溢出− 尝试将元素插入满队列。

    腾讯混元
    腾讯混元

    腾讯混元大由腾讯研发的大语言模型,具备强大的中文创作能力、逻辑推理能力,以及可靠的任务执行能力。

    腾讯混元 65
    查看详情 腾讯混元
  • 队列处于流状态 − 尝试从空队列中删除元素。

算法

下面给出的是插入 ( ) 的算法 -

  • 检查队列溢出。
if (r==n)
printf ("Queue overflow")
登录后复制
  • 否则,将一个元素插入到队列中。
q[r] = item
r++
登录后复制
给出的是一个删除算法的HTML代码:

Given below is an algorithm for deletion ( )

  • Check for queue under flow.
if (f==r)
printf ("Queue under flow")
登录后复制
<ul class="list"><li>否则,从队列中删除一个元素。</li></ul>
item = q[f]
f++
登录后复制

下面给出的是display ( )的算法 -

  • 检查队列是否为空。
  • ul>
    if (f==r)
    printf("Queue is empty")
    登录后复制
    • 否则,打印从‘f’到‘r’的所有元素。
    for(i=f; i<r; i++)
    printf ("%d", q[i]);
    登录后复制

    程序

    以下是用于在队列中删除元素的C程序 −

    #include <stdio.h>
    #define MAX 50
    void insert();
    int array[MAX];
    int rear = - 1;
    int front = - 1;
    main(){
       int add_item;
       int choice;
       while (1){
          printf("1.Insert element to queue </p><p>");
          printf("2.Delete an element from queue</p><p>");
          printf("3.Display elements of queue </p><p>");
          printf("4.Quit </p><p>");
          printf("Enter your choice : ");
          scanf("%d", &choice);
          switch (choice){
             case 1:
                insert();
             break;
             case 2:
                delete();
             case 3:
                display();
             break;
             case 4:
                exit(1);
             default:
             printf("Wrong choice </p><p>");
          }
       }
    }
    void insert(){
       int add_item;
       if (rear == MAX - 1)
          printf("Queue Overflow </p><p>");
       else{
          if (front == - 1)
          /*If queue is initially empty */
          front = 0;
          printf("Inset the element in queue : ");
          scanf("%d", &add_item);
          rear = rear + 1;
          array[rear] = add_item;
       }
    }
    void display(){
       int i;
       if (front == - 1)
          printf("Queue is empty </p><p>");
       else{
          printf("Queue is : </p><p>");
          for (i = front; i <= rear; i++)
             printf("%d ", array[i]);
             printf("</p><p>");
       }
    }
    void delete(){
       if (front == - 1 || front > rear){
          printf("Queue Underflow </p><p>");
          return ;
       }
       else{
          printf("Element deleted from queue is : %d</p><p>",array[front]);
          front = front + 1;
       }
    }
    登录后复制

    输出

    当执行上述程序时,会产生以下结果 -

    1.Insert element to queue
    2.Delete an element from queue
    3.Display elements of queue
    4.Quit
    Enter your choice: 1
    Inset the element in queue: 12
    1.Insert element to queue
    2.Delete an element from queue
    3.Display elements of queue
    4.Quit
    Enter your choice: 1
    Inset the element in queue: 23
    1.Insert element to queue
    2.Delete an element from queue
    3.Display elements of queue
    4.Quit
    Enter your choice: 1
    Inset the element in queue: 34
    1.Insert element to queue
    2.Delete an element from queue
    3.Display elements of queue
    4.Quit
    Enter your choice: 2
    Element deleted from queue is: 12
    Queue is:
    23 34
    1.Insert element to queue
    2.Delete an element from queue
    3.Display elements of queue
    4.Quit
    Enter your choice: 2
    Element deleted from queue is: 23
    Queue is:
    34
    1.Insert element to queue
    2.Delete an element from queue
    3.Display elements of queue
    4.Quit
    Enter your choice: 4
    登录后复制

以上就是用C语言讲解删除队列中的元素的详细内容,更多请关注php中文网其它相关文章!

C语言速学教程(入门到精通)
C语言速学教程(入门到精通)

C语言怎么学习?C语言怎么入门?C语言在哪学?C语言怎么学才快?不用担心,这里为大家提供了C语言速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号