Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

연결 목록을 사용하여 큐를 구현하는 C++ 프로그램


대기열은 요소 모음을 포함하는 추상 데이터 구조입니다. 큐는 FIFO 메커니즘을 구현합니다. 즉, 먼저 삽입된 요소도 먼저 삭제됩니다. 즉, 가장 최근에 추가된 요소가 대기열에서 먼저 제거됩니다.

연결 리스트를 사용하여 큐를 구현하는 프로그램은 다음과 같습니다 -

예시

#include <iostream>
using namespace std;
struct node {
   int data;
   struct node *next;
};
struct node* front = NULL;
struct node* rear = NULL;
struct node* temp;
void Insert() {
   int val;
   cout<<"Insert the element in queue : "<<endl;
   cin>>val;
   if (rear == NULL) {
      rear = (struct node *)malloc(sizeof(struct node));
      rear->next = NULL;
      rear->data = val;
      front = rear;
   } else {
      temp=(struct node *)malloc(sizeof(struct node));
      rear->next = temp;
      temp->data = val;
      temp->next = NULL;
      rear = temp;
   }
}
void Delete() {
   temp = front;
   if (front == NULL) {
      cout<<"Underflow"<<endl;
      return;
   }
   else
   if (temp->next != NULL) {
      temp = temp->next;
      cout<<"Element deleted from queue is : "<<front->data<<endl;
      free(front);
      front = temp;
   } else {
      cout<<"Element deleted from queue is : "<<front->data<<endl;
      free(front);
      front = NULL;
      rear = NULL;
   }
}
void Display() {
   temp = front;
   if ((front == NULL) && (rear == NULL)) {
      cout<<"Queue is empty"<<endl;
      return;
   }
   cout<<"Queue elements are: ";
   while (temp != NULL) {
      cout<<temp->data<<" ";
      temp = temp->next;
   }
   cout<<endl;
}
int main() {
   int ch;
   cout<<"1) Insert element to queue"<<endl;
   cout<<"2) Delete element from queue"<<endl;
   cout<<"3) Display all the elements of queue"<<endl;
   cout<<"4) Exit"<<endl;
   do {
      cout<<"Enter your choice : "<<endl;
      cin>>ch;
      switch (ch) {
         case 1: Insert();
         break;
         case 2: Delete();
         break;
         case 3: Display();
         break;
         case 4: cout<<"Exit"<<endl;
         break;
         default: cout<<"Invalid choice"<<endl;
      }
   } while(ch!=4);
   return 0;
}

출력

위 프로그램의 출력은 다음과 같습니다.

1) Insert element to queue
2) Delete element from queue
3) Display all the elements of queue
4) Exit
Enter your choice : 1
Insert the element in queue : 4
Enter your choice : 1
Insert the element in queue : 3
Enter your choice : 1
Insert the element in queue : 5
Enter your choice : 2
Element deleted from queue is : 4
Enter your choice : 3
Queue elements are : 3 5
Enter your choice : 7
Invalid choice
Enter your choice : 4
Exit

위의 프로그램에서 Insert() 함수는 요소를 큐에 삽입합니다. 후면이 NULL이면 대기열이 비어 있고 단일 요소가 삽입됩니다. 그렇지 않으면 노드가 필요한 요소와 함께 후면 뒤에 삽입되고 해당 노드가 후면으로 설정됩니다. 이것은 아래에 표시됩니다 -

void Insert() {
   int val;
   cout<<"Insert the element in queue : "<<endl;
   cin>>val;
   if (rear == NULL) {
      rear = (struct node *)malloc(sizeof(struct node));
      rear->next = NULL;
      rear->data = val;
      front = rear;
   } else {
      temp=(struct node *)malloc(sizeof(struct node));
      rear->next = temp;
      temp->data = val;
      temp->next = NULL;
      rear = temp;
   }
}

Delete() 함수에서 큐에 요소가 없으면 언더플로 상태입니다. 큐에 삭제되는 요소가 하나만 있고 앞과 뒤가 NULL로 설정되는 경우. 그렇지 않으면 앞의 요소가 삭제되고 앞의 요소가 다음 요소를 가리킵니다. 이것은 아래에 표시됩니다 -

void Delete() {
   temp = front;
   if (front == NULL) {
      cout<<"Underflow"<<endl;
      return;
   } else
   if (temp->next != NULL) {
      temp = temp->next;
      cout<<"Element deleted from queue is : "<<front->data<<endl;
      free(front);
      front = temp;
   } else {
      cout<<"Element deleted from queue is : "<<front->data<<endl;
      free(front);
      front = NULL;
      rear = NULL;
   }
}

display() 함수에서 앞과 뒤가 NULL이면 큐는 비어 있습니다. 그렇지 않으면 모든 대기열 요소는 temp 변수의 도움으로 while 루프를 사용하여 표시됩니다. 이것은 아래에 표시됩니다 -

void Display() {
   temp = front;
   if ((front == NULL) && (rear == NULL)) {
      cout<<"Queue is empty"<<endl;
      return;
   }
   cout<<"Queue elements are: ";
   while (temp != NULL) {
      cout<<temp->data<<" ";
      temp = temp->next;
   }
   cout<<endl;
}

main() 함수는 사용자가 대기열을 삽입, 삭제 또는 표시하려는 경우 선택 항목을 제공합니다. 사용자 응답에 따라 스위치를 사용하여 해당 기능을 호출합니다. 사용자가 잘못된 응답을 입력하면 인쇄됩니다. 이에 대한 코드 스니펫은 다음과 같습니다. -

int main() {
   int ch;
   cout<<"1) Insert element to queue"<<endl;
   cout<<"2) Delete element from queue"<<endl;
   cout<<"3) Display all the elements of queue"<<endl;
   cout<<"4) Exit"<<endl;
   do {
      cout<<"Enter your choice : "<<endl;
      cin>>ch;
      switch (ch) {
         case 1: Insert();
         break;
         case 2: Delete();
         break;
         case 3: Display();
         break;
         case 4: cout<<"Exit"<<endl;
         break;
         default: cout<<"Invalid choice"<<endl;
      }
   } while(ch!=4);
   return 0;
}