Skip to content

Instantly share code, notes, and snippets.

@hckim16
Created September 20, 2017 22:03
Show Gist options
  • Select an option

  • Save hckim16/35cfdd0fd29e7711199a4e4610401a59 to your computer and use it in GitHub Desktop.

Select an option

Save hckim16/35cfdd0fd29e7711199a4e4610401a59 to your computer and use it in GitHub Desktop.
Priority Queue using DLL structure with information taken from sources listed plus help from Arielle Patrice, Bergen Community College
//http://www.cprogramming.com/snippets/source-code/priority-queue-with-linked-list
//http://www.sanfoundry.com/cpp-program-implements-priority-queue/
//https://www.youtube.com/watch?v=yIUFT6AKBGE
//http://faculty.cs.niu.edu/~mcmahon/CS241/Notes/doubly_linked.html
#include<iostream>
#include<cstdlib>
using namespace std;
class DNode
{ // Establish DNode Class
private:
DNode *next; // Node to use in list
DNode *prev;
// Node element values
int key;
string name;
string ff;
double price;
int time;
friend class DLinkedList; // Give access to DLinkedList
};
class DLinkedList
{
private:
DNode *head; // List Sentinels
DNode *tail;
public:
DLinkedList(); // Constructor
~DLinkedList(); // Destructor
bool empty(); // Bool - is the list empty
const int& front() const; // Get Front Element
const int& back() const; // Get Tail Element
void insertBack(const int& k, const string& n, const string& f, const double& p, const int& t);
void insert(const int& k, const string& n, const string& f, const double& p, const int& t);
//void remove();
void removeFront();
void removeBack();
void removeMin();
void display(); // Display from Header
protected:
void insert(DNode *v, const int& k, const string& n, const string& f, const double& p, const int& t);
void remove(DNode *v);
};
bool DLinkedList::empty()
{ return(head-> next == tail); }
const int& DLinkedList::front() const
{ return head-> next-> key; }
const int& DLinkedList::back() const
{ return tail-> prev-> key; }
int ffPoints(string);
int price$$(double);
int arrivalTime(int);
int main ()
{
DLinkedList list = DLinkedList(); // create variabl
int pkey = 0;
int ffpkey;
int ppkey;
int tpkey;
string name;
string fflyer;
double price;
int time;
char y;
cout << "Please enter the following information." << endl; // get input
cout << "Name: ";
cin >> name;
cout << "Frequent Flyer Status: ";
cin >> fflyer;
ffpkey = ffPoints(fflyer);
cout << "ticket price paid: ";
cin >> price;
ppkey = price$$(price);
cout << "Check in time at gate in minutes: ";
cin >> time;
tpkey = arrivalTime(time);
pkey = tpkey + ffpkey + ppkey;
list.insertBack(pkey, name, fflyer, price, time);
cout << "Do you want to add an passenger (Y/N): ";
cin >> y;
while(y == 'y' || y == 'Y')
{
cout << "Please enter the following information." << endl; // get input
cout << "Name: ";
cin >> name;
cout << "Frequent Flyer Status: ";
cin >> fflyer;
ffpkey = ffPoints(fflyer);
cout << "ticket price paid: ";
cin >> price;
ppkey = price$$(price);
cout << "Check in time at gate in minutes: ";
cin >> time;
tpkey = arrivalTime(time);
pkey = tpkey + ffpkey + ppkey;
list.insertBack(pkey, name, fflyer, price, time);
cout << "Do you want to add an passenger (Y/N): ";
cin >> y;
cout << endl;
}
int choice;
do
{
cout << "Enter the number for the selection you want: \n";
cout << "1. Identify passenger with highest priority and remove from list.\n";
cout << "2. Display names of remaining passengers.\n";
cout << "3. Quit Program.\n";
cout << endl;
cout << "Enter your selection (1, 2, or 3): ";
cin >> choice;
while(choice < 1 || choice > 3)
{
cout << "The valid choices are 1 through 3. Please\n"
<< "select one of those." << endl;
cout << "Enter your selection: ";
cin >> choice;
cout << endl;
}
switch(choice)
{
case 1: // display function described below
list.removeMin();
cout << endl;
break;
case 2: // search function described below
list.display();
cout << endl;
break;
case 3: // ends program
cout << "Thank you for using this program.\n";
cout << endl;
}
}while(choice != 3);
return 0;
}
DLinkedList::DLinkedList()
{
head = new DNode;
tail = new DNode;
head-> next = tail;
tail-> prev = head;
}
DLinkedList::~DLinkedList()
{
while(!empty()) removeFront();
delete head;
delete tail;
}
void DLinkedList::remove(DNode * v)
{
DNode *u = v-> prev;
DNode *w = v-> next;
u-> next = w;
w-> prev = u;
delete v;
}
void DLinkedList::removeFront()
{ remove(head-> next); }
void DLinkedList::removeBack()
{ remove(tail-> prev); }
void DLinkedList::removeMin()
{
if(!empty())
{
DNode *minNode = head-> next;
DNode *searchNode = head-> next;
while(searchNode-> next != tail)
{
searchNode = searchNode-> next;
if(searchNode-> key < minNode-> key)
{
minNode = searchNode;
}
}
if(minNode-> prev != head)
{
if(minNode-> next != tail)
{
minNode-> prev-> next = minNode-> next;
minNode-> next-> prev = minNode-> prev;
}
else
{
minNode-> prev-> next = tail;
tail-> prev = minNode-> prev;
}
}
else
{
minNode-> next-> prev = head;
head-> next = minNode-> next;
}
cout << "Customer with highest priority key is: " << endl;
cout << minNode-> key << " " << minNode-> name << " " << minNode-> ff << " " << minNode-> price << " " << minNode-> time << endl;
cout << "Customer name has been removed from the list" << endl;
delete(minNode);
cout << endl;
}
else
{
cout << "The list is empty." << endl;
}
}
void DLinkedList::insert(DNode* v, const int& k, const string& n, const string& f, const double& p, const int& t)
{
DNode *u = new DNode;
u-> key = k;
u-> name = n;
u-> ff = f;
u-> price = p;
u-> time = t;
u-> next = v;
u-> prev = v-> prev;
u-> prev-> next = u;
v-> prev = u;
}
void DLinkedList::insertBack(const int& k, const string& n, const string& f, const double& p, const int& t)
{ insert(tail, k, n, f, p, t); }
void DLinkedList::display()
{
DNode* temp = head-> next;
if(empty())
cout << "Queue is empty" << endl;
else
{
while (temp != tail)
{
cout << temp-> key << " " << temp-> name << " " << temp-> ff << " " << temp-> price << " " << temp-> time << endl;
temp = temp-> next;
}
}
cout << endl;
}
int k;
int ffPoints(string status)
{
if(status == "PLAT")
k = 0;
else if(status == "GOLD")
k = 20;
else if(status == "SLVR")
k = 30;
else
k = 40;
return k;
}
int price$$(double pricePaid)
{
if (pricePaid >= 500)
k = 0;
else if(pricePaid < 500 && pricePaid >= 450)
k = 5;
else if(pricePaid < 450 && pricePaid >= 400)
k = 10;
else if(pricePaid < 400 && pricePaid >= 350)
k = 15;
else if(pricePaid < 350 && pricePaid >= 300)
k = 20;
else if(pricePaid < 300 && pricePaid >= 250)
k = 25;
else if(pricePaid < 250 && pricePaid >= 200)
k = 30;
else if(pricePaid< 200 && pricePaid >= 150)
k = 35;
else if(pricePaid < 150 && pricePaid >= 100)
k = 40;
else if(pricePaid < 100 && pricePaid >= 50)
k = 45;
else
k = 50;
return k;
}
int arrivalTime(int min)
{
if(min >= 60)
k = 0;
else
k = 60 - min;
return k;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment