Group Blog
 
 
สิงหาคม 2550
 1234
567891011
12131415161718
19202122232425
262728293031 
 
27 สิงหาคม 2550
 
All Blogs
 
FIFO QUEUE 「C Language」

------------------ แบบที่ 1 ใช้อาร์เรย์ขนาด Max - 1 ------------------
#define MAXBUFF 5
#define TEST -1
#define OK 0
#define EMPTY 1
#define FULL 2

int buffer[MAXBUFF]; // 0 to MAXBUFF - 1
int rp;   // Read Pointer
int wp;    // Write Pointer
int status;
int value;

int WriteBuffer(int input) {
  /* Check Status Buffer is full or not */
  if ((wp - rp == -1) || (wp - rp == MAXBUFF - 1)) { // Buffer Full
    return FULL;
  }
  else {
    buffer[wp] = input;  // Write data to Buffer
    wp = ++wp % MAXBUFF; // Increment Write Pointer and Mod by MAXBUFF
    return OK;
  }
}

int ReadBuffer(int *output) {
  /* Check Status Read Buffer */
  if (rp == wp) {    // Buffer Empty
    return EMPTY;
  }
  else {
    *output = buffer[rp]; // Read Buffer to Variable
    buffer[rp] = 0;  // After read, Clear Buffer[rp] = 0
    rp = ++rp % MAXBUFF; // Increment Read Pointer and Mod by MAXBUFF
    return OK;
  }
}


------------------ แบบที่ 2 ใช้อาร์เรย์ทั้งหมด ------------------
#define MAXBUFF 5
#define TEST -1
#define OK 0
#define EMPTY 1
#define FULL 2

int buffer[MAXBUFF];   // 0 to MAXBUFF - 1
int rp;     // Read Pointer -> First Can not Read
int wp;    
int value;
int status;

int WriteBuffer(int data) {
  /* Check Status Buffer is full or not */
  if (wp == rp) {    // Buffer Full
    return FULL;
  }
  else {
    buffer[wp] = data;    // Write data to Buffer
    wp = ++wp % MAXBUFF;  // Increment Write Pointer and Mod by MAXBUFF
    if (rp == -1) {    // Buffer Full
      rp = 0;
    }      
    return OK;
  }
}

int ReadBuffer(int *data) {
  /* Check Status Read Buffer */
  if (rp == -1) {    // Buffer Empty
    return EMPTY;
  }
  else {
    *data = buffer[rp];   // Read Buffer to Variable
    rp = ++rp % MAXBUFF;  // Increment Read Pointer and Mod by MAXBUFF
    if (rp == wp) {    // All Buffer Free
      rp = -1;    // Reset Read and Write Pointer
      wp = 0;
    }
    return OK;
  }
}


-----------------------------------------------------------------------------------

สรุป การทำแบบที่หนึ่ง คือ ใช้ขนาดอาร์เรย์เพียง MAX - 1 ช่อง จะทำให้การเขียนโปรแกรมสั้นลง และทำการตรวจสอบเงื่อนไขง่ายขึ้น และการทำ Test Case จะทำได้ง่ายขึ้น การเกิดบั๊กก็จะน้อยลง แต่ข้อเสียคือ ทำให้เราเสียอาร์เรย์ไป 1 ช่อง

แบบที่สอง เราใช้อาร์เรย์ได้ครบหมดทุกช่อง แต่การเขียนจะซับซ้อนขึ้น โอกาสเกิดบั๊กมีมากกว่าแบบที่หนึ่ง การทำ Test Case ก็จะต้องทำแบบทดสอบมากกว่าแบบที่ 1 ด้วย

จริง ๆ อาจจะเขียนได้มากกว่านี้ เช่น ใช้ Flag มาเป็นตัวจับการใส่เข้าหรืออ่านข้อมูลออกจากอาร์เรย์ แต่ก็ต้องแลกด้วยการเขียนโค้ดที่ซับซ้อน และเปลืองหน่วยความจำมากขึ้น


Create Date : 27 สิงหาคม 2550
Last Update : 21 มีนาคม 2551 2:10:55 น. 1 comments
Counter : 1676 Pageviews.

 


โดย: meaw_1985 วันที่: 27 สิงหาคม 2550 เวลา:10:56:53 น.  

ชื่อ :
Comment :
  *ใช้ code html ตกแต่งข้อความได้เฉพาะสมาชิก
 

I^^
Location :


[ดู Profile ทั้งหมด]

ฝากข้อความหลังไมค์
Rss Feed
Smember
ผู้ติดตามบล็อก : 8 คน [?]




Friends' blogs
[Add I^^'s blog to your web]
Links
 

 Pantip.com | PantipMarket.com | Pantown.com | © 2004 BlogGang.com allrights reserved.