C語言應用3 環形佇列

2021-10-07 13:36:52 字數 3336 閱讀 9816

環形佇列應用面廣,可以應用在第一講所講的at指令和盲區資料的傳輸。這裡我再講一些應用,對於一些支援透明傳輸的裝置,當連線建立成功後,資料就可以在兩個裝置之間快速傳輸,這時候如果直接處理資料,很容易出現接收資料的速度和裝置處理速度不匹配的情況,俗稱丟包。這時可以使用環形佇列,將資料儲存,等使用的時候,一次性取出進行處理。

使用codeblock進行模擬,調整後可移動到其他裝置。

3.1. main.c

/**

author:versifierl

email:[email protected]

date:2020/06/24

version:1.0

function:fifo, ring_queue.

comment:

1.underscorecase

2._t stand for structure

3.structure typedef and define all is uppercase.

**/#include

#include

#include

#include

"ring_queue.h"

intmain()

ring_queue_write

(&ring_queue_t, i+

'0');}

//copy to data

char data[ring_queue_length]

;for

(i=0

; i<

sizeof

(data)

; i++

)return0;

}

3.2. ring_queue.h
#define ring_queue_length 5

/*the structure of ring queue*/

typedef

struct

ring_queue;

void

ring_queue_init

(ring_queue *ring_queue_t)

;void

ring_queue_write

(ring_queue *ring_queue_t,

char data)

;void

ring_queue_read

(ring_queue *ring_queue_t)

;int

ring_queue_length_get

(ring_queue *ring_queue_t)

;int

ring_queue_is_full

(ring_queue *ring_queue_t)

;int

ring_queue_is_empty

(ring_queue *ring_queue_t)

;void

ring_queue_read_all

(ring_queue *ring_queue_t)

;char

*ring_queue_copy

(ring_queue *ring_queue_t)

;

3.3. ring_queue.c
#include

"ring_queue.h"

#include

"stdio.h"

/* * function: initialize ring queue

* para: ring queue structure pointer

* return: void

*/void

ring_queue_init

(ring_queue *ring_queue_t)

/* * function: write one char to ring queue

* para1: ring queue structure pointer

* para2: the data to write

* return: void

*/void

ring_queue_write

(ring_queue *ring_queue_t,

char data)

/* * function: read one char from ring queue

* para1: ring queue structure pointer

* return: void

*/void

ring_queue_read

(ring_queue *ring_queue_t)

/* * function: get the current length of ring queue

* para1: ring queue structure pointer

* return: length

*/int

ring_queue_length_get

(ring_queue *ring_queue_t)

/* * function: determine if the queue is full

* para1: ring queue structure pointer

* return: 1 -> yes

0 -> no

*/int

ring_queue_is_full

(ring_queue *ring_queue_t)

/* * function: determine if the queue is empty

* para1: ring queue structure pointer

* return: 1 -> yes

0 -> no

*/int

ring_queue_is_empty

(ring_queue *ring_queue_t)

/* * function: print the ring queue

* para1: ring queue structure pointer

* return: void

*/void

ring_queue_read_all

(ring_queue *ring_queue_t)}/*

* function: copy the ring queue

* para1: ring queue structure pointer

* return: ring queue data pointer

*/char

*ring_queue_copy

(ring_queue *ring_queue_t)

1 3 環形佇列

實現 public class circlearrayqueuedemo catch exception e class circlearrayqueue public boolean isfull public boolean isempty public void addqueue int va...

3 環形鍊錶

submissions definition for singly linked list.type listnode struct 方法一 快慢指標 12ms 快指標一次走兩步,慢指標一次走一步,如果鍊錶有環,那麼兩個指標始終會相遇。時間複雜度 o n 空間複雜度 o 1 func hascycl...

141 環形鍊錶(C語言)簡單題

一 雜湊方法 雜湊方法,雜湊計算寫得一般,沒有找到合適的雜湊值計算方法。非常浪費空間。bool hascycle struct listnode head while p return false 142 環形鍊錶 ii struct listnode detectcycle struct list...