โครงสร้างข้อมูลคือการเก็บรวบรวมข้อมูลที่จัดโครงสร้างในลักษณะที่มีโครงสร้าง แบ่งออกเป็นสองประเภทตามที่อธิบายไว้ด้านล่าง −
-
โครงสร้างข้อมูลเชิงเส้น − ข้อมูลถูกจัดระเบียบในลักษณะเชิงเส้น ตัวอย่างเช่น อาร์เรย์ โครงสร้าง สแตก คิว รายการเชื่อมโยง
-
โครงสร้างข้อมูลไม่เชิงเส้น - ข้อมูลถูกจัดระเบียบตามลำดับชั้น เช่น ต้นไม้ กราฟ ชุด ตาราง
คิว
เป็นโครงสร้างข้อมูลเชิงเส้นที่มีการแทรกที่ส่วนท้ายและการลบเสร็จสิ้นที่ส่วนหน้า

ลำดับของคิวคือ FIFO – เข้าก่อนออกก่อน
ปฏิบัติการ
- Insert – การแทรกองค์ประกอบลงในคิว
- Delete – การลบองค์ประกอบออกจากคิว
เงื่อนไข
-
คิวโอเวอร์โฟลว์ - พยายามแทรกองค์ประกอบลงในคิวเต็ม
-
คิวภายใต้โฟลว์ - พยายามลบองค์ประกอบออกจากคิวที่ว่างเปล่า
อัลกอริทึม
รับด้านล่างเป็นอัลกอริทึมสำหรับ การแทรก ( ) −
- ตรวจสอบคิวล้น
if (r==n)
printf ("Queue overflow") - มิฉะนั้น ให้แทรกองค์ประกอบในคิว
q[r] = item r++
โปรแกรม
ต่อไปนี้เป็นโปรแกรม 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 \n");
printf("2.Display elements of queue \n");
printf("3.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice){
case 1:
insert();
break;
case 2:
display();
break;
case 3:
exit(1);
default:
printf("Wrong choice \n");
}
}
}
void insert(){
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
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 \n");
else{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", array[i]);
printf("\n");
}
} ผลลัพธ์
เมื่อโปรแกรมข้างต้นทำงาน มันจะให้ผลลัพธ์ดังต่อไปนี้ −
1.Insert element to queue 2.Display elements of queue 3.Quit Enter your choice: 1 Inset the element in queue: 34 1.Insert element to queue 2.Display elements of queue 3.Quit Enter your choice: 1 Inset the element in queue: 24 1.Insert element to queue 2.Display elements of queue 3.Quit Enter your choice: 2 Queue is: 34 24 1.Insert element to queue 2.Display elements of queue 3.Quit Enter your choice: 3