데이터 구조는 구조화된 방식으로 구성된 데이터의 모음입니다. 아래 설명과 같이 두 가지 유형으로 나뉩니다 -
-
선형 데이터 구조 − 데이터는 선형 방식으로 구성됩니다. 예를 들어 배열, 구조, 스택, 대기열, 연결 목록이 있습니다.
-
비선형 데이터 구조 − 데이터는 계층적 방식으로 구성됩니다. 예를 들어, 나무, 그래프, 세트, 테이블.
대기열
삽입은 후단에서, 삭제는 전단에서 수행되는 선형 데이터 구조입니다.

대기열의 순서는 FIFO – First In First Out
입니다.작업
- 삽입 – 대기열에 요소 삽입
- 삭제 – 대기열에서 요소를 삭제합니다.
조건
-
Queue over flow - 전체 대기열에 요소를 삽입하려고 합니다.
-
Queue under flow − 빈 대기열에서 요소를 삭제하려고 합니다.
알고리즘
다음은 삽입 알고리즘입니다( ) -
- 대기열 오버플로를 확인합니다.
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