2.
1. Create a class node with following attributes
a. Data
b. Next pointer
c. constructor and destructor
d. accessor and mutator
2. Now create a class LLQueue with following attributes
a. Front pointer
b. Rear pointer
c. Enqueue: Adds an item to the queue.
d. Dequeue: Removes an item from the queue.
e. Peek
f. Length
g. Clever_display
h. Clever_search
i. Create a priority queue, that rearranges the colors in a rainbow fashion.
i. Red Orange Yellow Green Indigo Blue Voilet
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <map>
#include <vector>
#include<queue>
using namespace std;
class Node{
public:
string data;
Node *next;
Node();
Node(string);
string getData(){
return data;
}
void setData(int data){
this->data = data;
}
Node* getNext(){
return this->next;
}
void setNext(Node *node){
this->next = node;
}
void display(){
cout<<this->data;
}
~Node(){}
};
Node::Node(){
next = NULL;
}
Node::Node(string data){
this->data = data;
next = NULL;
}
class LLQueue{
public:
string peek, Length;
Node *front, *rear;
LLQueue()
{
front = rear = NULL;
}
void enQueue(string x)
{
// Create a new LL node
Node* temp = new Node(x);
// If queue is empty, then
// new node is front and rear both
if (rear == NULL) {
front = rear = temp;
return;
}
// Add the new node at
// the end of queue and change rear
rear->next = temp;
rear = temp;
}
// Function to remove
// a key from given queue q
void deQueue()
{
// If queue is empty, return NULL.
if (front == NULL)
return;
// Store previous front and
// move front one node ahead
Node* temp = front;
front = front->next;
// If front becomes NULL, then
// change rear also as NULL
if (front == NULL)
rear = NULL;
delete (temp);
}
void Clever_display();
void Cleaver_search();
void Priority_Queue(){
cout<<" Colours of a rainbow: Red Orange Yellow Green Indigo Blue Voilet";
}
};
int main()
{
LLQueue q;
q.enQueue("red");
q.enQueue("blue");
q.deQueue();
q.deQueue();
q.enQueue("red");
q.enQueue("blue");
q.enQueue("violet");
//q.deQueue();
cout << "Queue Front : " << (q.front)->data << endl;
cout << "Queue Rear : " << (q.rear)->data<<endl;
q.Priority_Queue();
}
Comments
Leave a comment