initial commit
This commit is contained in:
8
02-queues/.idea/.gitignore
generated
vendored
Normal file
8
02-queues/.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
02-queues/.idea/.name
generated
Normal file
1
02-queues/.idea/.name
generated
Normal file
@@ -0,0 +1 @@
|
||||
02_queues
|
||||
2
02-queues/.idea/02-queues.iml
generated
Normal file
2
02-queues/.idea/02-queues.iml
generated
Normal file
@@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module classpath="CMake" type="CPP_MODULE" version="4" />
|
||||
6
02-queues/.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
6
02-queues/.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>
|
||||
4
02-queues/.idea/misc.xml
generated
Normal file
4
02-queues/.idea/misc.xml
generated
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||
</project>
|
||||
8
02-queues/.idea/modules.xml
generated
Normal file
8
02-queues/.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/02-queues.iml" filepath="$PROJECT_DIR$/.idea/02-queues.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
02-queues/CMakeLists.txt
Normal file
6
02-queues/CMakeLists.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.28)
|
||||
project(02_queues)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
add_executable(02_queues main.cpp)
|
||||
102
02-queues/QueueADT.h
Normal file
102
02-queues/QueueADT.h
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
Written by: Iurii Tatishchev
|
||||
=================================
|
||||
Queue template
|
||||
*/
|
||||
#ifndef QUEUE_ADT_H
|
||||
#define QUEUE_ADT_H
|
||||
|
||||
template<class T>
|
||||
class Queue {
|
||||
private:
|
||||
// Structure for the stack nodes
|
||||
struct QueueNode {
|
||||
T value; // Value in the node
|
||||
QueueNode *next; // Pointer to next node
|
||||
};
|
||||
|
||||
QueueNode *front; // Pointer to the first node
|
||||
QueueNode *rear; // Pointer to the last node
|
||||
int length; // Number of nodes in the queue
|
||||
|
||||
public:
|
||||
Queue() { front = rear = nullptr; length = 0; } // Constructor
|
||||
~Queue(); // Destructor
|
||||
|
||||
// Queue operations
|
||||
bool isEmpty() { return length == 0; }
|
||||
|
||||
bool push(T);
|
||||
|
||||
T pop();
|
||||
|
||||
T peek() { return front->value; }
|
||||
|
||||
T peekRear() { return rear->value; }
|
||||
|
||||
int getLength() { return length; }
|
||||
};
|
||||
|
||||
/*
|
||||
Member function push: inserts the argument into the queue
|
||||
*/
|
||||
template<class T>
|
||||
bool Queue<T>::push(T item) {
|
||||
QueueNode *newNode; // Pointer to a new node
|
||||
|
||||
// Allocate a new node and store item there.
|
||||
newNode = new QueueNode;
|
||||
if (!newNode)
|
||||
return false;
|
||||
newNode->value = item;
|
||||
newNode->next = nullptr;
|
||||
|
||||
// Update links and counter
|
||||
if (!front) // front is NULL: empty queue
|
||||
front = newNode;
|
||||
else
|
||||
rear->next = newNode;
|
||||
|
||||
rear = newNode;
|
||||
length++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
Member function deletes the value at the front
|
||||
of the queue and returns it.
|
||||
Assume queue has at least one node
|
||||
*/
|
||||
template<class T>
|
||||
T Queue<T>::pop() {
|
||||
// Save item to return.
|
||||
T item = front->value;
|
||||
|
||||
// Save pointer to old front to delete it
|
||||
QueueNode* oldFront = front;
|
||||
|
||||
// Set front to next node and delete old front
|
||||
front = front->next;
|
||||
delete oldFront;
|
||||
|
||||
// Update length
|
||||
length--;
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
/*
|
||||
Destructor
|
||||
*/
|
||||
template<class T>
|
||||
Queue<T>::~Queue() {
|
||||
while (front != nullptr) {
|
||||
QueueNode* nextNode = front->next;
|
||||
delete front;
|
||||
front = nextNode;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
69
02-queues/main.cpp
Normal file
69
02-queues/main.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
Written by: Iurii Tatishchev
|
||||
=================================
|
||||
Project: Queue ADT
|
||||
Parallel queues. Create two queues:
|
||||
- the first queue contains the names of the students enrolled in CIS 22C
|
||||
- the second queue contains the number of units each student is taking this quarter
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "QueueADT.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
|
||||
// Create the first queue (strings)
|
||||
Queue<string> names;
|
||||
|
||||
// Loop to enter an unknown number of names, one per line.
|
||||
// The loop stops when you enter #.
|
||||
// As you are entering names, they are to be inserted into the first queue.
|
||||
string item;
|
||||
getline(cin, item);
|
||||
while (item != "#") {
|
||||
names.push(item);
|
||||
getline(cin, item);
|
||||
}
|
||||
|
||||
if (names.isEmpty()) {
|
||||
cout << "Empty Queues!\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Test the getLength function: - display the number of elements in the first queue
|
||||
cout << names.getLength();
|
||||
|
||||
// Create the second queue (doubles)
|
||||
Queue<double> numbers;
|
||||
|
||||
// Test the getLength function: - display the number of elements in the second queue
|
||||
// (it should be 0!)
|
||||
cout << " " << numbers.getLength() << "\n";
|
||||
|
||||
// Write another loop to enter the number of units (double) into a parallel queue.
|
||||
for (int i = 0; i < names.getLength(); i++) {
|
||||
double units;
|
||||
cin >> units;
|
||||
numbers.push(units);
|
||||
}
|
||||
|
||||
// Display the two queues in parallel.
|
||||
for (int i = names.getLength(); i > 0; i--) {
|
||||
names.push(names.pop());
|
||||
numbers.push(numbers.pop());
|
||||
cout << names.peekRear() << " " << numbers.peekRear() << "\n";
|
||||
}
|
||||
|
||||
// On the next line display the front and rear elements in the first queue.
|
||||
cout << "Front of the queues: " << names.peek() << " " << numbers.peek() << "\n";
|
||||
|
||||
// On the last line display the front and the rear elements in the second queue.
|
||||
cout << "Rear of the queues: " << names.peekRear() << " " << numbers.peekRear() << "\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
86
02-queues/queues_01_strings.cpp
Normal file
86
02-queues/queues_01_strings.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
/**~*~*~*
|
||||
CIS 22C
|
||||
Project: Queue of strings
|
||||
|
||||
Written by: Iurii Tatishchev
|
||||
IDE: CLion
|
||||
*~*/
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
class Queue_str
|
||||
{
|
||||
private:
|
||||
// Structure for the queue nodes
|
||||
struct QueueNode {
|
||||
string value; // Value in the node
|
||||
QueueNode *next; // Pointer to next node
|
||||
};
|
||||
|
||||
QueueNode *front; // Pointer to the first node
|
||||
QueueNode *rear; // Pointer to the last node
|
||||
int length; // Number of nodes in the queue
|
||||
|
||||
public:
|
||||
Queue_str(){ front = rear = NULL; length = 0; } //Constructor
|
||||
//~Queue_str(); // Destructor
|
||||
|
||||
// Queue operations
|
||||
bool isEmpty() { return length == 0; }
|
||||
bool push(string);
|
||||
// string pop();
|
||||
string peek() { return front->value; }
|
||||
string peekRear() { return rear->value; }
|
||||
int getLength() { return length; }
|
||||
};
|
||||
|
||||
/**~*~*
|
||||
Member function push: inserts the argument into the queue
|
||||
*~**/
|
||||
bool Queue_str::push(string item)
|
||||
{
|
||||
QueueNode *newNode; // Pointer to a new node
|
||||
|
||||
// Allocate a new node and store num there.
|
||||
newNode = new QueueNode;
|
||||
if (!newNode)
|
||||
return false;
|
||||
newNode->value = item;
|
||||
newNode->next = NULL;
|
||||
|
||||
// Update links and counter
|
||||
if (!front) // front is NULL: empty queue
|
||||
front = newNode;
|
||||
else
|
||||
rear->next = newNode;
|
||||
|
||||
rear = newNode;
|
||||
length++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
Queue_str que;
|
||||
string item;
|
||||
|
||||
getline(cin, item);
|
||||
while (item != "#") {
|
||||
que.push(item);
|
||||
getline(cin, item);
|
||||
}
|
||||
|
||||
cout << que.getLength() << "\n";
|
||||
if (que.isEmpty()) {
|
||||
cout << "Empty Queue!\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
cout << que.peek() << "\n";
|
||||
cout << que.peekRear() << "\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
93
02-queues/queues_02_destructor.cpp
Normal file
93
02-queues/queues_02_destructor.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
/**~*~*~*
|
||||
CIS 22C
|
||||
Project: Queue of strings
|
||||
|
||||
Written by: Iurii Tatishchev
|
||||
IDE: CLion
|
||||
*~*/
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Queue_str {
|
||||
private:
|
||||
// Structure for the queue nodes
|
||||
struct QueueNode {
|
||||
string value; // Value in the node
|
||||
QueueNode *next; // Pointer to next node
|
||||
};
|
||||
|
||||
QueueNode *front; // Pointer to the first node
|
||||
QueueNode *rear; // Pointer to the last node
|
||||
int length; // Number of nodes in the queue
|
||||
|
||||
public:
|
||||
Queue_str() { front = rear = NULL; length = 0; } //Constructor
|
||||
~Queue_str(); // Destructor
|
||||
|
||||
// Queue operations
|
||||
bool isEmpty() { return length == 0; }
|
||||
|
||||
bool push(string);
|
||||
|
||||
// string pop();
|
||||
string peek() { return front->value; }
|
||||
|
||||
string peekRear() { return rear->value; }
|
||||
|
||||
int getLength() { return length; }
|
||||
};
|
||||
|
||||
/**~*~*
|
||||
Member function push: inserts the argument into the queue
|
||||
*~**/
|
||||
bool Queue_str::push(string item) {
|
||||
QueueNode *newNode; // Pointer to a new node
|
||||
|
||||
// Allocate a new node and store num there.
|
||||
newNode = new QueueNode;
|
||||
if (!newNode)
|
||||
return false;
|
||||
newNode->value = item;
|
||||
newNode->next = NULL;
|
||||
|
||||
// Update links and counter
|
||||
if (!front) // front is NULL: empty queue
|
||||
front = newNode;
|
||||
else
|
||||
rear->next = newNode;
|
||||
|
||||
rear = newNode;
|
||||
length++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**~*~*~*
|
||||
Destructor
|
||||
*~**/
|
||||
Queue_str::~Queue_str()
|
||||
{
|
||||
while (front != nullptr) {
|
||||
QueueNode* nextNode = front->next;
|
||||
cout << front->value << " - deleted!" << endl;
|
||||
delete front;
|
||||
front = nextNode;
|
||||
}
|
||||
|
||||
cout << "Empty queue!" << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
Queue_str que;
|
||||
string item;
|
||||
|
||||
getline(cin, item);
|
||||
while (item != "#") {
|
||||
que.push(item);
|
||||
getline(cin, item);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
02-queues/queues_03_pop
Executable file
BIN
02-queues/queues_03_pop
Executable file
Binary file not shown.
113
02-queues/queues_03_pop.cpp
Normal file
113
02-queues/queues_03_pop.cpp
Normal file
@@ -0,0 +1,113 @@
|
||||
/**~*~*~*
|
||||
CIS 22C
|
||||
Project: Queue of strings (pop)
|
||||
|
||||
Written by: Iurii Tatishchev
|
||||
IDE: CLion
|
||||
*~*/
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Queue_str {
|
||||
private:
|
||||
// Structure for the queue nodes
|
||||
struct QueueNode {
|
||||
string value; // Value in the node
|
||||
QueueNode *next; // Pointer to next node
|
||||
};
|
||||
|
||||
QueueNode *front; // Pointer to the first node
|
||||
QueueNode *rear; // Pointer to the last node
|
||||
int length; // Number of nodes in the queue
|
||||
|
||||
public:
|
||||
Queue_str() { front = rear = NULL; length = 0; } //Constructor
|
||||
~Queue_str(); // Destructor
|
||||
|
||||
// Queue operations
|
||||
bool isEmpty() { return length == 0; }
|
||||
|
||||
bool push(string);
|
||||
|
||||
string pop();
|
||||
string peek() { return front->value; }
|
||||
|
||||
string peekRear() { return rear->value; }
|
||||
|
||||
int getLength() { return length; }
|
||||
};
|
||||
|
||||
/**~*~*
|
||||
Member function push: inserts the argument into the queue
|
||||
*~**/
|
||||
bool Queue_str::push(string item) {
|
||||
QueueNode *newNode; // Pointer to a new node
|
||||
|
||||
// Allocate a new node and store num there.
|
||||
newNode = new QueueNode;
|
||||
if (!newNode)
|
||||
return false;
|
||||
newNode->value = item;
|
||||
newNode->next = NULL;
|
||||
|
||||
// Update links and counter
|
||||
if (!front) // front is NULL: empty queue
|
||||
front = newNode;
|
||||
else
|
||||
rear->next = newNode;
|
||||
|
||||
rear = newNode;
|
||||
length++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**~*~*~*
|
||||
Destructor
|
||||
*~**/
|
||||
Queue_str::~Queue_str() {
|
||||
while (front != nullptr) {
|
||||
QueueNode* nextNode = front->next;
|
||||
delete front;
|
||||
front = nextNode;
|
||||
}
|
||||
}
|
||||
|
||||
/**~*~*~*
|
||||
Member function dequeue deletes the value at the front
|
||||
of the queue and returns it.
|
||||
Assume queue has at least one node
|
||||
*~**/
|
||||
string Queue_str::pop() {
|
||||
string item = front->value;
|
||||
QueueNode* oldFront = front;
|
||||
front = front->next;
|
||||
delete oldFront;
|
||||
length--;
|
||||
return item;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
Queue_str que;
|
||||
string item;
|
||||
|
||||
getline(cin, item);
|
||||
while (item != "#") {
|
||||
que.push(item);
|
||||
getline(cin, item);
|
||||
}
|
||||
|
||||
if (que.isEmpty()) {
|
||||
cout << "Empty Queue!\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (!que.isEmpty()) {
|
||||
cout << que.pop() << "\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user