使用C語言解釋佇列中元素的刪除
資料結構是以結構化的方式組織資料的集合。它分為以下兩種型別:
線性資料結構 − 資料以線性方式組織。例如,陣列、結構體、棧、佇列、連結串列。
非線性資料結構 − 資料以層次化的方式組織。例如,樹、圖、集合、表。
另請閱讀: 資料結構和型別
佇列
佇列是一種線性資料結構,其中元素的插入在隊尾進行,刪除在隊頭進行。

佇列的順序是 FIFO – 先進先出
操作
- 插入 – 將元素插入佇列。
- 刪除 – 從佇列中刪除元素。
條件
佇列溢位 − 嘗試將元素插入到已滿的佇列中。
佇列下溢 − 嘗試從空的佇列中刪除元素。
演算法
以下是插入 ( ) 的演算法:
- 檢查佇列是否溢位。
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語言刪除佇列元素的程式
以下是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
");
printf("2.Delete an element from queue
");
printf("3.Display elements of queue
");
printf("4.Quit
");
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
");
}
}
}
void insert(){
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow
");
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
");
else{
printf("Queue is :
");
for (i = front; i <= rear; i++)
printf("%d ", array[i]);
printf("
");
}
}
void delete(){
if (front == - 1 || front > rear){
printf("Queue Underflow
");
return ;
}
else{
printf("Element deleted from queue is : %d
",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
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP