資料結構是以結構化方式組織的資料集合。它分為兩種類型,如下所述 -
線性資料結構 - 資料以線性方式組織。例如,陣列、結構體、堆疊、佇列、鍊錶。
非線性資料結構 - 資料以分層方式組織。例如,樹、圖、集合、表。
它是一種線性資料結構,插入在後端完成,刪除是在前端完成的。
佇列的順序是FIFO – 先進先出
佇列溢位− 嘗試將元素插入滿隊列。
佇列處於串流狀態 − 嘗試從空佇列中刪除元素。
下面給出的是插入 ( ) 的演算法 -
if (r==n) printf ("Queue overflow")
q[r] = item r++給的是一個刪除演算法的HTML程式碼:
deletion ( ) −
if (f==r) printf ("Queue under flow")
item = q[f] f++
display ( )的演算法 -
##檢查佇列是否為空。if (f==r) printf("Queue is empty")
for(i=f; i<r; i++) printf ("%d", q[i]);
#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 </p><p>"); printf("2.Delete an element from queue</p><p>"); printf("3.Display elements of queue </p><p>"); printf("4.Quit </p><p>"); 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 </p><p>"); } } } void insert(){ int add_item; if (rear == MAX - 1) printf("Queue Overflow </p><p>"); 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 </p><p>"); else{ printf("Queue is : </p><p>"); for (i = front; i <= rear; i++) printf("%d ", array[i]); printf("</p><p>"); } } void delete(){ if (front == - 1 || front > rear){ printf("Queue Underflow </p><p>"); return ; } else{ printf("Element deleted from queue is : %d</p><p>",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
以上是用C語言講解刪除佇列中的元素的詳細內容。更多資訊請關注PHP中文網其他相關文章!