Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct node {
- int data;
- struct node *next;
- };
- struct node* front = NULL;
- struct node* rear = NULL;
- struct node* temp;
- int prime(int num)
- {
- int ct=0;
- for(int i=2; i<=num/2; i++)
- {
- if(num%i==0)
- ct++;
- }
- if(num == 1) return 0;
- if(ct>0)
- return 0;
- else
- return 1;
- }
- void Enqueue()
- {
- int val,num;
- cout<<"How many elements do you Insert in queue ?"<<endl;
- cin >> num;
- cout << "Insert the elements: ";
- while(num--)
- {
- cin>>val;
- int p=prime(val);
- if(p){
- if (rear == NULL) {
- rear = new node;
- rear->next = NULL;
- rear->data = val;
- front = rear;
- } else {
- temp=new node;
- rear->next = temp;
- temp->data = val;
- temp->next = NULL;
- rear = temp;
- }
- }
- }
- }
- void Dequeue()
- {
- temp = front;
- if (front == NULL) {
- cout<<"Oops!Queue is empty. May be you insert non-prime number!"<<endl;
- return;
- }
- else
- if(front->data > 30){
- if (temp->next != NULL) {
- temp = temp->next;
- cout<<front->data<<" has been deleted from the queue!"<<endl;
- delete(front);
- front = temp;
- } else {
- cout<<front->data<<" has been deleted from the queue!"<<endl;
- delete(front);
- front = NULL;
- rear = NULL;
- }
- }
- else
- {
- cout << "Can't be deleted because the value isn't greater than 30" << endl;
- return;
- }
- }
- void Display()
- {
- temp = front;
- if ((front == NULL) && (rear == NULL)) {
- cout<<"Queue is empty. May be you insert non-prime number!"<<endl;
- return;
- }
- cout<<"Queue elements are: ";
- while (temp != NULL) {
- cout<<temp->data<<" ";
- temp = temp->next;
- }
- cout<<endl;
- }
- int main()
- {
- int choice;
- cout<<"1) Enqueue"<<endl;
- cout<<"2) Dequeue"<<endl;
- cout<<"3) Display"<<endl;
- while(1)
- {
- cout<<"Enter your choice : "<<endl;
- cin>>choice;
- switch (choice) {
- case 1: Enqueue();
- break;
- case 2: Dequeue();
- break;
- case 3: Display();
- break;
- default: cout<<"Invalid choice"<<endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement