Advertisement
Nayeemzaman

Queue

Jul 17th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct node {
  5.    int data;
  6.    struct node *next;
  7.  
  8. };
  9.  
  10. struct node* front = NULL;
  11. struct node* rear = NULL;
  12. struct node* temp;
  13.  
  14. int prime(int num)
  15. {
  16.     int ct=0;
  17.  
  18.     for(int i=2; i<=num/2; i++)
  19.     {
  20.         if(num%i==0)
  21.             ct++;
  22.     }
  23.     if(num == 1) return 0;
  24.     if(ct>0)
  25.         return 0;
  26.     else
  27.         return 1;
  28. }
  29. void Enqueue()
  30. {
  31.    int val,num;
  32.    cout<<"How many elements do you Insert in queue ?"<<endl;
  33.    cin >> num;
  34.    cout << "Insert the elements: ";
  35.    while(num--)
  36.    {
  37.        cin>>val;
  38.        int p=prime(val);
  39.        if(p){
  40.            if (rear == NULL) {
  41.               rear = new node;
  42.               rear->next = NULL;
  43.               rear->data = val;
  44.               front = rear;
  45.            } else {
  46.               temp=new node;
  47.               rear->next = temp;
  48.               temp->data = val;
  49.               temp->next = NULL;
  50.               rear = temp;
  51.            }
  52.        }
  53.    }
  54. }
  55. void Dequeue()
  56. {
  57.    temp = front;
  58.    if (front == NULL) {
  59.       cout<<"Oops!Queue is empty. May be you insert non-prime number!"<<endl;
  60.       return;
  61.    }
  62.    else
  63.    if(front->data > 30){
  64.        if (temp->next != NULL) {
  65.           temp = temp->next;
  66.           cout<<front->data<<" has been deleted from the queue!"<<endl;
  67.           delete(front);
  68.           front = temp;
  69.        } else {
  70.           cout<<front->data<<" has been deleted from the queue!"<<endl;
  71.           delete(front);
  72.           front = NULL;
  73.           rear = NULL;
  74.        }
  75.    }
  76.    else
  77.    {
  78.        cout << "Can't be deleted because the value isn't greater than 30" << endl;
  79.        return;
  80.    }
  81. }
  82. void Display()
  83. {
  84.  
  85.    temp = front;
  86.    if ((front == NULL) && (rear == NULL)) {
  87.       cout<<"Queue is empty. May be you insert non-prime number!"<<endl;
  88.       return;
  89.    }
  90.    cout<<"Queue elements are: ";
  91.    while (temp != NULL) {
  92.       cout<<temp->data<<" ";
  93.       temp = temp->next;
  94.    }
  95.    cout<<endl;
  96. }
  97. int main()
  98. {
  99.    int choice;
  100.    cout<<"1) Enqueue"<<endl;
  101.    cout<<"2) Dequeue"<<endl;
  102.    cout<<"3) Display"<<endl;
  103.  
  104. while(1)
  105. {
  106.    cout<<"Enter your choice : "<<endl;
  107.  
  108.    cin>>choice;
  109.    switch (choice) {
  110.       case 1: Enqueue();
  111.          break;
  112.       case 2: Dequeue();
  113.          break;
  114.       case 3: Display();
  115.          break;
  116.       default: cout<<"Invalid choice"<<endl;
  117.    }
  118. }
  119.  
  120.    return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement