Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

อธิบายการลบองค์ประกอบในคิวโดยใช้ภาษา C


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

  • โครงสร้างข้อมูลเชิงเส้น − ข้อมูลถูกจัดระเบียบในลักษณะเชิงเส้น ตัวอย่างเช่น อาร์เรย์ โครงสร้าง สแตก คิว รายการเชื่อมโยง

  • โครงสร้างข้อมูลไม่เชิงเส้น - ข้อมูลถูกจัดระเบียบตามลำดับชั้น เช่น ต้นไม้ กราฟ ชุด ตาราง

คิว

เป็นโครงสร้างข้อมูลเชิงเส้นที่มีการแทรกที่ส่วนท้ายและการลบเสร็จสิ้นที่ส่วนหน้า

อธิบายการลบองค์ประกอบในคิวโดยใช้ภาษา C

ลำดับของคิวคือ FIFO – เข้าก่อนออกก่อน

ปฏิบัติการ

  • Insert – การแทรกองค์ประกอบลงในคิว
  • Delete – การลบองค์ประกอบออกจากคิว

เงื่อนไข

  • คิวโอเวอร์โฟลว์ - พยายามแทรกองค์ประกอบลงในคิวเต็ม

  • คิวภายใต้โฟลว์ - พยายามลบองค์ประกอบออกจากคิวที่ว่างเปล่า

อัลกอริทึม

รับด้านล่างเป็นอัลกอริทึมสำหรับการแทรก ( ) −

  • ตรวจสอบคิวล้น
if (r==n)
printf ("Queue overflow")
  • มิฉะนั้น ให้แทรกองค์ประกอบในคิว
q[r] = item
r++

รับด้านล่างเป็นอัลกอริทึมสำหรับ การลบ ( )

  • ตรวจสอบคิวภายใต้โฟลว์
if (f==r)
printf ("Queue under flow")
  • มิฉะนั้น ให้ลบองค์ประกอบออกจากคิว
item = q[f]
f++

รับด้านล่างเป็นอัลกอริทึมสำหรับ การแสดงผล ( )

  • ตรวจสอบว่าคิวว่างหรือไม่
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 \n");
      printf("2.Delete an element from queue\n");
      printf("3.Display elements of queue \n");
      printf("4.Quit \n");
      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 \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");
   }
}
void delete(){
   if (front == - 1 || front > rear){
      printf("Queue Underflow \n");
      return ;
   }
   else{
      printf("Element deleted from queue is : %d\n",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