数据结构是以结构化方式组织的数据集合。它分为两种类型,如下所述 -
线性数据结构 - 数据以线性方式组织。例如,数组、结构体、堆栈、队列、链表。
非线性数据结构 - 数据以分层方式组织。例如,树、图、集合、表。
它是一种线性数据结构,插入在后端完成,删除是在前端完成的。
队列的顺序是 FIFO – 先进先出
队列溢出− 尝试将元素插入满队列。
队列处于流状态 − 尝试从空队列中删除元素。
下面给出的是插入 ( ) 的算法 -
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"); printf("2.Display elements of queue
"); printf("3.Quit
"); 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
"); } } } 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("
"); } }
当执行上述程序时,会产生以下结果 -
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