代码人生 发表于 2023-2-9 20:28:16

求助,一个简单的数组先进先出C程序!!!

求助,一个简单的数组先进先出C程序!!!

神农鼎 发表于 2023-2-9 20:42:45


求助,一个简单的数组先进先出C程序!!!
===我不知道梁工的串口程序有没有这段程序,发个链接你看下,
===不行请梁工再亲自写段程序给您

【新提醒】求STC8H/STC8G系列 4个串口同时通信的演示程序,Modbus 演示程序 - 串行口,DMA支持的4组串口 国芯论坛-STC全球32位8051爱好者互助交流社区 - STC全球32位8051爱好者互助交流社区 (stcaimcu.com)

不知用到先进先出没有

QQ624353765 发表于 2023-2-10 00:12:30


angmall 发表于 2023-2-10 08:25:56


#include <stdio.h>
# define SIZE 100
void enqueue();
void dequeue();
void show();
int inp_arr;
int Rear = - 1;
int Front = - 1;
main()
{
    int ch;
    while (1)
    {
      printf("1.Enqueue Operation\n");
      printf("2.Dequeue Operation\n");
      printf("3.Display the Queue\n");
      printf("4.Exit\n");
      printf("Enter your choice of operations : ");
      scanf("%d", &ch);
      switch (ch)
      {
            case 1:
            enqueue();
            break;
            case 2:
            dequeue();
            break;
            case 3:
            show();
            break;
            case 4:
            exit(0);
            default:
            printf("Incorrect choice \n");
      }
    }
}

void enqueue()
{
    int insert_item;
    if (Rear == SIZE - 1)
       printf("Overflow \n");
    else
    {
      if (Front == - 1)
      
      Front = 0;
      printf("Element to be inserted in the Queue\n : ");
      scanf("%d", &insert_item);
      Rear = Rear + 1;
      inp_arr = insert_item;
    }
}

void dequeue()
{
    if (Front == - 1 || Front > Rear)
    {
      printf("Underflow \n");
      return ;
    }
    else
    {
      printf("Element deleted from the Queue: %d\n", inp_arr);
      Front = Front + 1;
    }
}

void show()
{
   
    if (Front == - 1)
      printf("Empty Queue \n");
    else
    {
      printf("Queue: \n");
      for (int i = Front; i <= Rear; i++)
            printf("%d ", inp_arr);
      printf("\n");
    }
}

输出。
1.Enqueue Operation
2.Dequeue Operation
3.Display the Queue
4.Exit
Enter your choice of operations : 1
Element to be inserted in the Queue: 10

1.Enqueue Operation
2.Dequeue Operation
3.Display the Queue
4.Exit
Enter your choice of operations : 1
Element to be inserted in the Queue: 20

1.Enqueue Operation
2.Dequeue Operation
3.Display the Queue
4.Exit
Enter your choice of operations : 3
Queue:
10 20

1.Enqueue Operation
2.Dequeue Operation
3.Display the Queue
4.Exit
Enter your choice of operations : 2
Element deleted from the Queue: 10

1.Enqueue Operation
2.Dequeue Operation
3.Display the Queue
4.Exit
Enter your choice of operations: 3
Queue:
20

梁工 发表于 2023-2-10 11:25:13

两个变量做指示,write用于写(入),read用于读(出)。read追着write跑即可,相等则无新数据。

zhp 发表于 2023-2-10 17:16:31



STC-ISP下载软件里面的串口范例程序中
缓冲区就是最简单的先进先出的应用范例(FIFO队列环)

代码人生 发表于 2023-2-12 14:20:06

zhp 发表于 2023-2-10 17:16
STC-ISP下载软件里面的串口范例程序中
缓冲区就是最简单的先进先出的应用范例(FIFO队列环)



谢谢提示

神农鼎 发表于 2023-2-12 15:08:38

本以为是最简单的演示程序,结果还有最简单的:最简单的先进先出的应用范例(FIFO队列环)

代码人生 发表于 2023-2-12 23:27:07

神农鼎 发表于 2023-2-12 15:08
本以为是最简单的演示程序,结果还有最简单的:最简单的先进先出的应用范例(FIFO队列环) ...

经过测试,软件例程里串口那个确实是最简单的,几行代码就完成了需要的先进先出程序。
移植到我的工程里,运行了一下,输入端的几路脉冲信号都能按时间顺序放进数组,然后通过串口发送到上位机。
感谢官方支持,感谢热心网友支持!!!
页: [1]
查看完整版本: 求助,一个简单的数组先进先出C程序!!!