>  기사  >  백엔드 개발  >  C 언어를 사용하여 대기열의 요소 삭제를 설명합니다.

C 언어를 사용하여 대기열의 요소 삭제를 설명합니다.

王林
王林앞으로
2023-08-28 09:45:15956검색

데이터 구조는 구조화된 방식으로 구성된 데이터의 모음입니다. 아래와 같이 두 가지 유형으로 나누어집니다. -

  • 선형 데이터 구조 - 데이터가 선형 방식으로 구성됩니다. 예를 들어 배열, 구조, 스택, 큐 및 연결된 목록이 있습니다.

  • 비선형 데이터 구조 - 데이터는 계층적 방식으로 구성됩니다. 예를 들어 트리, 그래프, 세트, ​​테이블이 있습니다.

Queue

선형 데이터 구조로 삽입은 백엔드에서, 삭제는 프런트엔드에서 이루어집니다.

C 언어를 사용하여 대기열의 요소 삭제를 설명합니다.

큐의 순서는 FIFO입니다. 선입선출

작업

  • 삽입 – 큐에 요소를 삽입합니다.
  • 삭제 – 큐에서 요소를 삭제합니다.

Conditions

  • 큐 오버플로 - 전체 큐에 요소를 삽입해 보세요.

  • 큐가 스트리밍 상태입니다 - 빈 큐에서 요소를 제거하려고 시도합니다.

알고리즘

아래는 ( )를 삽입하는 알고리즘입니다. -

  • 대기열 오버플로를 확인하세요.
if (r==n)
printf ("Queue overflow")
  • 그렇지 않으면 대기열에 요소를 삽입하세요.
q[r] = item
r++
는 아래에

삭제( )

    흐름 아래의 대기열을 확인하는 알고리즘을 제공합니다.
  • if (f==r)
    printf ("Queue under flow")
    item = q[f]
    f++
아래는

display( ) 알고리즘입니다. -

    은 대기열이 비어 있는지 확인합니다.
  • if (f==r)
    printf("Queue is empty")
    ul>
      그렇지 않으면 'f'부터 'r'까지 모든 요소를 ​​인쇄합니다.
    • for(i=f; i<r; i++)
      printf ("%d", q[i]);
    Program

    다음은 큐에서 요소를 제거하는 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 </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;
       }
    }
    Output

    위 프로그램을 실행하면 다음과 같은 결과가 나옵니다. -

    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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제