Compare commits
7 Commits
30ad8892de
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
4dbe12e593
|
|||
|
701d84242d
|
|||
|
0789614ef1
|
|||
|
cc06e5fbdb
|
|||
|
eb1cf4176f
|
|||
|
0ca7ce28fd
|
|||
|
276575ca62
|
@@ -83,10 +83,6 @@ bool StudentList::insertNode(Student dataIn) {
|
||||
ListNode *newNode; // A new node
|
||||
ListNode *pCur; // To traverse the list
|
||||
|
||||
// Allocate a new node and store num there.
|
||||
newNode = new ListNode;
|
||||
newNode->stu = dataIn;
|
||||
|
||||
// Initialize pointers
|
||||
pCur = head->forw;
|
||||
|
||||
@@ -98,6 +94,10 @@ bool StudentList::insertNode(Student dataIn) {
|
||||
// Return false early if the name is a duplicate
|
||||
if (pCur->stu.name == dataIn.name) return false;
|
||||
|
||||
// Allocate a new node and store num there.
|
||||
newNode = new ListNode;
|
||||
newNode->stu = dataIn;
|
||||
|
||||
// Insert the new node between pPre and pCur
|
||||
ListNode *pPre = pCur->back; // The previous node
|
||||
pPre->forw = newNode;
|
||||
|
||||
8
07-heaps/.idea/.gitignore
generated
vendored
Normal file
8
07-heaps/.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
1
07-heaps/.idea/.name
generated
Normal file
1
07-heaps/.idea/.name
generated
Normal file
@@ -0,0 +1 @@
|
||||
07_heaps
|
||||
2
07-heaps/.idea/07-heaps.iml
generated
Normal file
2
07-heaps/.idea/07-heaps.iml
generated
Normal file
@@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module classpath="CMake" type="CPP_MODULE" version="4" />
|
||||
6
07-heaps/.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
6
07-heaps/.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
</profile>
|
||||
</component>
|
||||
7
07-heaps/.idea/misc.xml
generated
Normal file
7
07-heaps/.idea/misc.xml
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CMakePythonSetting">
|
||||
<option name="pythonIntegrationState" value="YES" />
|
||||
</component>
|
||||
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||
</project>
|
||||
8
07-heaps/.idea/modules.xml
generated
Normal file
8
07-heaps/.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/07-heaps.iml" filepath="$PROJECT_DIR$/.idea/07-heaps.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
07-heaps/.idea/vcs.xml
generated
Normal file
6
07-heaps/.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
7
07-heaps/CMakeLists.txt
Normal file
7
07-heaps/CMakeLists.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.28)
|
||||
project(07_heaps)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
add_executable(07_heaps main.cpp
|
||||
)
|
||||
13
07-heaps/Customer.cpp
Normal file
13
07-heaps/Customer.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
/* *~*~*
|
||||
Implementation file for the Customer class
|
||||
Written By: Iurii Tatishchev
|
||||
Changed by: Iurii Tatishchev
|
||||
IDE: CLion
|
||||
*~**/
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "Customer.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* Write your code here */
|
||||
46
07-heaps/Customer.h
Normal file
46
07-heaps/Customer.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/* *~*~*
|
||||
Specification file for the Customer class
|
||||
Written By: Iurii Tatishchev
|
||||
Changed by: Iurii Tatishchev
|
||||
IDE: CLion
|
||||
*~**/
|
||||
#ifndef CUSTOMER_H_
|
||||
#define CUSTOMER_H_
|
||||
|
||||
#include <utility>
|
||||
|
||||
using std::string, std::ostream;
|
||||
|
||||
class Customer; // Forward Declaration
|
||||
|
||||
class Customer {
|
||||
private:
|
||||
int year;
|
||||
int mileage;
|
||||
int seq;
|
||||
string name;
|
||||
|
||||
int priority;
|
||||
int serial;
|
||||
|
||||
public:
|
||||
Customer() : year(0), mileage(0), seq(0), name("") {};
|
||||
|
||||
Customer(int y, int m, int s, string n) : year(y), mileage(m), seq(s), name(std::move(n)) {
|
||||
priority = calcPriority();
|
||||
serial = calcSerial();
|
||||
};
|
||||
|
||||
int calcPriority() { return mileage / 1000 + year - seq; }
|
||||
|
||||
int getSerial() const { return serial; }
|
||||
|
||||
int calcSerial() { return priority * 100 + (100 - seq); }
|
||||
|
||||
friend ostream &operator<<(ostream &os, const Customer &c) {
|
||||
os << c.year << " " << c.mileage << " (" << c.getSerial() << ") [" << c.name << "]";
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
135
07-heaps/Heap.h
Normal file
135
07-heaps/Heap.h
Normal file
@@ -0,0 +1,135 @@
|
||||
/* *~*~*
|
||||
Specification file for the Heap class: min- or max-heap of integers
|
||||
Written By: Iurii Tatishchev
|
||||
Changed by: Iurii Tatishchev
|
||||
IDE: CLion
|
||||
*~**/
|
||||
|
||||
#ifndef HEAP_H_
|
||||
#define HEAP_H_
|
||||
|
||||
template<typename T>
|
||||
class Heap {
|
||||
private:
|
||||
T *heapAry;
|
||||
int heapSize;
|
||||
int count;
|
||||
|
||||
void _reHeapUp(int lastndx, int (compareFunc)(const T&, const T&));
|
||||
|
||||
void _reHeapDown(int rootndx, int (compareFunc)(const T&, const T&));
|
||||
|
||||
int _findParent(int index) { return (index <= 0) ? (-1) : (index - 1) / 2; }
|
||||
|
||||
int _findLeftChild(int index) { return (2 * index + 1 >= count) ? (-1) : (2 * index + 1); }
|
||||
|
||||
int _findRightChild(int index) { return (2 * index + 2 >= count) ? (-1) : (2 * index + 2); }
|
||||
|
||||
public:
|
||||
Heap() {
|
||||
count = 0;
|
||||
heapSize = 128;
|
||||
heapAry = new T[heapSize];
|
||||
}
|
||||
|
||||
Heap(int n) {
|
||||
count = 0;
|
||||
heapSize = n;
|
||||
heapAry = static_cast<int *>(new T[heapSize]);
|
||||
}
|
||||
|
||||
~Heap() { delete[] heapAry; }
|
||||
|
||||
int getCount() const { return count; }
|
||||
|
||||
int getSize() const { return heapSize; }
|
||||
|
||||
bool isEmpty() const { return count == 0; }
|
||||
|
||||
bool isFull() const { return count == heapSize; }
|
||||
|
||||
bool insertHeap(T &itemIn, int (compareFunc)(const T&, const T&));
|
||||
|
||||
bool deleteHeap(T &itemOut, int (compareFunc)(const T&, const T&));
|
||||
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void Heap<T>::_reHeapUp(int lastndx, int (compareFunc)(const T&, const T&)) {
|
||||
|
||||
// base case, newElement is heap's root
|
||||
if (lastndx == 0) return;
|
||||
|
||||
int parentIndex = _findParent(lastndx);
|
||||
// base case, newElement satisfies heap property
|
||||
// min heap - child not less than parent
|
||||
// max heap - child not greater than parent
|
||||
if (compareFunc(heapAry[lastndx], heapAry[parentIndex]) != -1) return;
|
||||
|
||||
// swap and continue recursing
|
||||
std::swap(heapAry[lastndx], heapAry[parentIndex]);
|
||||
_reHeapUp(parentIndex, compareFunc);
|
||||
}
|
||||
|
||||
/* *~*~*
|
||||
The private member function _reHeapDown rearranges the heap after delete by moving the
|
||||
data in the root down to the correct location in the heap
|
||||
*~**/
|
||||
template <typename T>
|
||||
void Heap<T>::_reHeapDown(int rootdex, int (compareFunc)(const T&, const T&)) {
|
||||
int maxChildIndex = -1;
|
||||
|
||||
int left = _findLeftChild(rootdex);
|
||||
int right = _findRightChild(rootdex);
|
||||
|
||||
// if there is a left child
|
||||
if (left != -1) {
|
||||
// if there is a right child that is
|
||||
// min heap - less than the left child
|
||||
// max heap - greater than the left child
|
||||
if (right != -1 && compareFunc(heapAry[right], heapAry[left]) == -1) maxChildIndex = right;
|
||||
else maxChildIndex = left;
|
||||
}
|
||||
|
||||
// base case, heap property satisfied
|
||||
// min heap - children greater than parent
|
||||
// max heap - children less than parent
|
||||
if (maxChildIndex == -1 || compareFunc(heapAry[rootdex], heapAry[maxChildIndex]) == -1) return;
|
||||
|
||||
// swap and continue recursing
|
||||
std::swap(heapAry[rootdex], heapAry[maxChildIndex]);
|
||||
_reHeapDown(maxChildIndex, compareFunc);
|
||||
}
|
||||
|
||||
/* *~*~*
|
||||
The public member function insertHeap inserts a new item into a heap.
|
||||
It calls _reheapUp.
|
||||
*~**/
|
||||
template <typename T>
|
||||
bool Heap<T>::insertHeap(T& newItem, int (compareFunc)(const T&, const T&)) {
|
||||
if (isFull()) return false;
|
||||
|
||||
heapAry[count] = newItem;
|
||||
_reHeapUp(count, compareFunc);
|
||||
count++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* *~*~*
|
||||
The public member function deleteHeap deletes the root of the heap and
|
||||
passes back the root's data. It calls _reheapDown.
|
||||
*~**/
|
||||
template <typename T>
|
||||
bool Heap<T>::deleteHeap(T& returnItem, int (compareFunc)(const T&, const T&)) {
|
||||
if (isEmpty()) return false;
|
||||
|
||||
returnItem = heapAry[0];
|
||||
heapAry[0] = heapAry[count - 1];
|
||||
count--;
|
||||
_reHeapDown(0, compareFunc);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
||||
105
07-heaps/main.cpp
Normal file
105
07-heaps/main.cpp
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
Heaps - ADT
|
||||
|
||||
This program will read data about overbooked customers,
|
||||
find their priority and serial numbers, build a heap, then display
|
||||
customers in priority sequence
|
||||
|
||||
Written By: Iurii Tatishchev
|
||||
Changed By: Iurii Tatishchev
|
||||
IDE: CLion
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include "Customer.h"
|
||||
#include "Heap.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Function Prototypes
|
||||
int compareCustomer(const Customer &c1, const Customer &c2);
|
||||
|
||||
void processArrival(Heap<Customer> &heap, ifstream &inputFile, int seq);
|
||||
|
||||
void processServe(Heap<Customer> &heap);
|
||||
|
||||
void displayRejected(Heap<Customer> &heap);
|
||||
|
||||
int main() {
|
||||
// Get input file name
|
||||
string inputFileName;
|
||||
cout << "Input file name: ";
|
||||
getline(cin, inputFileName);
|
||||
cout << endl;
|
||||
|
||||
// Open input file
|
||||
ifstream inputFile(inputFileName);
|
||||
if (!inputFile.good()) {
|
||||
cerr << "Error: could not open file " << inputFileName << "\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Create heap
|
||||
Heap<Customer> heap;
|
||||
|
||||
// Process input file
|
||||
char command;
|
||||
int seq = 1;
|
||||
while (inputFile >> command) {
|
||||
switch (command) {
|
||||
case 'A':
|
||||
processArrival(heap, inputFile, seq);
|
||||
seq++;
|
||||
break;
|
||||
case 'S':
|
||||
processServe(heap);
|
||||
break;
|
||||
default:
|
||||
cerr << "Error: invalid command " << command << "\n";
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
cout << "Served overbooked customers: " << seq - heap.getCount() - 1 << "\n\n";
|
||||
|
||||
// Display rejected customers
|
||||
int rejectedCustomers = heap.getCount();
|
||||
displayRejected(heap);
|
||||
cout << "Rejected overbooked customers: " << rejectedCustomers << "\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int compareCustomer(const Customer &c1, const Customer &c2) {
|
||||
if (c1.getSerial() < c2.getSerial()) return 1;
|
||||
if (c1.getSerial() > c2.getSerial()) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void processArrival(Heap<Customer> &heap, ifstream &inputFile, int seq) {
|
||||
int years, mileage;
|
||||
string name;
|
||||
inputFile >> years >> mileage;
|
||||
getline(inputFile >> ws, name);
|
||||
Customer customer(years, mileage, seq, name);
|
||||
heap.insertHeap(customer, compareCustomer);
|
||||
}
|
||||
|
||||
void processServe(Heap<Customer> &heap) {
|
||||
if (heap.isEmpty()) {
|
||||
cout << "Heap is empty" << endl;
|
||||
return;
|
||||
}
|
||||
Customer customer;
|
||||
heap.deleteHeap(customer, compareCustomer);
|
||||
cout << customer << endl;
|
||||
}
|
||||
|
||||
void displayRejected(Heap<Customer> &heap) {
|
||||
Customer customer;
|
||||
while (!heap.isEmpty()) {
|
||||
heap.deleteHeap(customer, compareCustomer);
|
||||
cout << customer << endl;
|
||||
}
|
||||
}
|
||||
30
07-heaps/overbooked.txt
Normal file
30
07-heaps/overbooked.txt
Normal file
@@ -0,0 +1,30 @@
|
||||
A 5 53000 Robert Hill
|
||||
A 3 89000 Amanda Trapp
|
||||
A 3 90000 Jonathan Nguyen
|
||||
A 5 56000 Tom Martin
|
||||
A 1 21000 Mary Lou Gilley
|
||||
S
|
||||
S
|
||||
S
|
||||
A 3 89000 Bob Che
|
||||
A 7 72000 Warren Rexroad
|
||||
A 2 65000 Vincent Gonzales
|
||||
A 3 34000 Paula Hung
|
||||
S
|
||||
S
|
||||
A 6 21000 Lou Masson
|
||||
A 4 42000 Steve Chu
|
||||
A 3 99000 Linda Lee
|
||||
S
|
||||
S
|
||||
A 3 69000 Dave Lightfoot
|
||||
A 3 83000 Daniel Oh
|
||||
A 5 50000 Sue Andrews
|
||||
A 2 73000 Joanne Brown
|
||||
S
|
||||
A 7 96000 Paul Ng
|
||||
S
|
||||
A 5 53000 Steven Chen
|
||||
A 2 65000 Vladimir Johnson
|
||||
S
|
||||
A 7 72000 Peter Edwards
|
||||
21
07-heaps/overbooked_1.txt
Normal file
21
07-heaps/overbooked_1.txt
Normal file
@@ -0,0 +1,21 @@
|
||||
A 5 53000 Robert Hill
|
||||
A 3 89000 Amanda Trapp
|
||||
A 3 90000 Jonathan Nguyen
|
||||
A 5 56000 Tom Martin
|
||||
A 1 21000 Mary Lou Gilley
|
||||
A 3 89000 Bob Che
|
||||
A 7 72000 Warren Rexroad
|
||||
A 6 21000 Lou Masson
|
||||
A 2 65000 Vincent Gonzales
|
||||
A 3 34000 Paula Hung
|
||||
A 4 42000 Steve Chu
|
||||
A 3 99000 Linda Lee
|
||||
A 3 69000 Dave Lightfoot
|
||||
A 3 83000 Daniel Oh
|
||||
A 5 50000 Sue Andrews
|
||||
A 2 73000 Joanne Brown
|
||||
A 7 96000 Paul Ng
|
||||
A 5 53000 Steven Chen
|
||||
A 2 65000 Vladimir Johnson
|
||||
A 7 72000 Peter Edwards
|
||||
A 6 21000 Zoe Smith
|
||||
10
07-heaps/overbooked_2.txt
Normal file
10
07-heaps/overbooked_2.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
A 5 53000 Robert Hill
|
||||
A 3 89000 Amanda Trapp
|
||||
A 2 65000 Vincent Gonzales
|
||||
S
|
||||
A 3 34000 Paula Hung
|
||||
A 4 42000 Steve Chu
|
||||
S
|
||||
A 2 65000 Vladimir Johnson
|
||||
S
|
||||
A 7 72000 Warren Rexroad
|
||||
5
07-heaps/test.txt
Normal file
5
07-heaps/test.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
A 5 53000 Robert Hill
|
||||
A 3 89000 Amanda Trapp
|
||||
A 3 90000 Jonathan Nguyen
|
||||
S
|
||||
A 5 56000 Tom Martin
|
||||
Reference in New Issue
Block a user