commit a828b3525a1f024139a56f46ae043aea77cf340f Author: Iurii Tatishchev Date: Sun Apr 28 11:54:46 2024 -0700 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..24da4b9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,117 @@ +# Created by https://www.toptal.com/developers/gitignore/api/clion +# Edit at https://www.toptal.com/developers/gitignore?templates=clion + +### CLion ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +**/.idea/**/workspace.xml +**/.idea/**/tasks.xml +**/.idea/**/usage.statistics.xml +**/.idea/**/dictionaries +**/.idea/**/shelf + +# AWS User-specific +**/.idea/**/aws.xml + +# Generated files +**/.idea/**/contentModel.xml + +# Sensitive or high-churn files +**/.idea/**/dataSources/ +**/.idea/**/dataSources.ids +**/.idea/**/dataSources.local.xml +**/.idea/**/sqlDataSources.xml +**/.idea/**/dynamic.xml +**/.idea/**/uiDesigner.xml +**/.idea/**/dbnavigator.xml + +# Gradle +**/.idea/**/gradle.xml +**/.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +**/cmake-build-*/ + +# Mongo Explorer plugin +**/.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +**/out/ + +# mpeltonen/sbt-idea plugin +**/.idea_modules/ + +# JIRA plugin +**/atlassian-ide-plugin.xml + +# Cursive Clojure plugin +**/.idea/replstate.xml + +# SonarLint plugin +**/.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +**/com_crashlytics_export_strings.xml +**/crashlytics.properties +**/crashlytics-build.properties +**/fabric.properties + +# Editor-based Rest Client +**/.idea/httpRequests + +# Android studio 3.1+ serialized cache file +**/.idea/caches/build_file_checksums.ser + +### CLion Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +# https://plugins.jetbrains.com/plugin/7973-sonarlint +**/.idea/**/sonarlint/ + +# SonarQube Plugin +# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin +**/.idea/**/sonarIssues.xml + +# Markdown Navigator plugin +# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced +**/.idea/**/markdown-navigator.xml +**/.idea/**/markdown-navigator-enh.xml +**/.idea/**/markdown-navigator/ + +# Cache file creation bug +# See https://youtrack.jetbrains.com/issue/JBR-2257 +**/.idea/$CACHE_FILE$ + +# CodeStream plugin +# https://plugins.jetbrains.com/plugin/12206-codestream +**/.idea/codestream.xml + +# Azure Toolkit for IntelliJ plugin +# https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij +**/.idea/**/azureSettings.xml + +# End of https://www.toptal.com/developers/gitignore/api/clion \ No newline at end of file diff --git a/01-stacks/.idea/.gitignore b/01-stacks/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/01-stacks/.idea/.gitignore @@ -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 diff --git a/01-stacks/.idea/.name b/01-stacks/.idea/.name new file mode 100644 index 0000000..553c112 --- /dev/null +++ b/01-stacks/.idea/.name @@ -0,0 +1 @@ +01_stacks \ No newline at end of file diff --git a/01-stacks/.idea/01-stacks.iml b/01-stacks/.idea/01-stacks.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/01-stacks/.idea/01-stacks.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/01-stacks/.idea/codeStyles/codeStyleConfig.xml b/01-stacks/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/01-stacks/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/01-stacks/.idea/inspectionProfiles/Project_Default.xml b/01-stacks/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..03d9549 --- /dev/null +++ b/01-stacks/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/01-stacks/.idea/misc.xml b/01-stacks/.idea/misc.xml new file mode 100644 index 0000000..79b3c94 --- /dev/null +++ b/01-stacks/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/01-stacks/.idea/modules.xml b/01-stacks/.idea/modules.xml new file mode 100644 index 0000000..fccaca4 --- /dev/null +++ b/01-stacks/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/01-stacks/CMakeLists.txt b/01-stacks/CMakeLists.txt new file mode 100644 index 0000000..610ff5b --- /dev/null +++ b/01-stacks/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.28) +project(01_stacks) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(01_stacks main.cpp) diff --git a/01-stacks/Lab Stacks 4 - max (Stack ADT).zip b/01-stacks/Lab Stacks 4 - max (Stack ADT).zip new file mode 100644 index 0000000..715cdb9 Binary files /dev/null and b/01-stacks/Lab Stacks 4 - max (Stack ADT).zip differ diff --git a/01-stacks/StackADT.h b/01-stacks/StackADT.h new file mode 100644 index 0000000..0e8d1f7 --- /dev/null +++ b/01-stacks/StackADT.h @@ -0,0 +1,95 @@ +/* +Written by: Iurii Tatishchev +============================= + Stack template +*/ + +#ifndef STACK_ADT +#define STACK_ADT + +template +class Stack { +private: + // Structure for the stack nodes + struct StackNode { + T value; // Value in the node + StackNode *next; // Pointer to next node + }; + + StackNode *top; // Pointer to the stack top + int length; + +public: + // Constructor + Stack(){ top = nullptr; length = 0; } + // Destructor + ~Stack(); + + // Stack operations: + bool push(T item); + T pop(); + T peek() { return top->value; }; + bool isEmpty() { return length == 0; }; + int getLength() { return length; } ; +}; + +/* + Member function push inserts the argument onto + the stack. +*/ +template +bool Stack::push(T item) { + StackNode *newNode; // Pointer to a new node + + // Allocate a new node and store item there. + newNode = new StackNode; + if (!newNode) + return false; + newNode->value = item; + + // Update links and counter + newNode->next = top; + top = newNode; + length++; + + return true; +} + +/* + Member function pop deletes the value at the top + of the stack and returns it. + Assume stack is not empty. +*/ +template +T Stack::pop() { + T val = this->top->value; + + StackNode* oldTop = this->top; + this->top = this->top->next; + this->length--; + delete oldTop; + + return val; +} + +/* + Destructor: + Traverses the list deleting each node (without calling pop) +*/ +template +Stack::~Stack() { + StackNode *currNode; + + // Position nodePtr at the top of the stack. + currNode = top; + + // Traverse the list deleting each node. + while (currNode) { + StackNode* nextNode = currNode->next; + delete currNode; + currNode = nextNode; + } +} + + +#endif diff --git a/01-stacks/in1.txt b/01-stacks/in1.txt new file mode 100644 index 0000000..dba85ca --- /dev/null +++ b/01-stacks/in1.txt @@ -0,0 +1 @@ +10 20 30 1 40 0 50 -2 15 25 -3 60 70 \ No newline at end of file diff --git a/01-stacks/main.cpp b/01-stacks/main.cpp new file mode 100644 index 0000000..518e002 --- /dev/null +++ b/01-stacks/main.cpp @@ -0,0 +1,118 @@ +/* +Written by: Iurii Tatishchev +============================= +CIS 22C +Project: Stack ADT +*/ + +#include +#include +#include +#include + +#include "StackADT.h" + +using namespace std; + +void printInfo(); +void processNumbers(const string&, Stack &); +void printStack(Stack &); + +int main() { + printInfo(); + + cout << "Enter input file name: "; + string filename; + getline(cin, filename); // assume valid + cout << endl; + + // Create a stack + Stack s; + + // Process the numbers in the file + processNumbers(filename, s); + + // Print remaining stack elements + printStack(s); + + return 0; +} + + +/* +This function displays the project's title +*/ +void printInfo() { + cout << " ~*~ Project: Stack ADT ~*~ " << endl; +} + +void processNumbers(const string& filename, Stack &s) { + ifstream inFile(filename); + // Check if the file was opened successfully + if (!inFile.is_open()) { + cout << "There was an error opening \"" << filename << "\". Exiting.\n"; + exit(1); + } + cout << "\nInput File: " << filename << "\n"; + + int number; + Stack maxStack; + inFile >> number; + while(inFile.good()) { + switch (number) { + // Each time we read 0, display the number of elements in the stack. + case 0: + cout << "Count: " << s.getLength() << "\n"; + break; + // Each time we read 1, display the top element of the stack and the maximum element in the stack. + case 1: + // If there are no elements in the stack display "Top: Empty". + if (s.isEmpty()) { + cout << "Top: Empty\n"; + break; + } + cout << "Top: " << s.peek() << "\n"; + cout << "Max: " << maxStack.peek() << "\n"; + break; + default: + // Each time we read a number greater than 1, we push it onto the stack. + if (number > 1) { + s.push(number); + maxStack.push(max(maxStack.isEmpty()? 0 : maxStack.peek(), number)); + } + // Each time we read a negative number, + // we pop and print the number removed from the stack and the largest value in the stack. + // If there are no numbers in the stack, display "Pop: Empty". + // If there's only one number in the stack, after removing it, print it. + else { + if (s.isEmpty()) { + cout << "Pop: Empty\n"; + } else { + cout << "Pop: " << s.pop() << "\n"; + maxStack.pop(); + if (!maxStack.isEmpty()) cout << "Max: " << maxStack.peek() << "\n"; + } + } + break; + } + inFile >> number; + } + // Don't forget to close the file + inFile.close(); +} + +/* +This function pops and prints all elements in the stack. +*/ +void printStack(Stack &s) { + cout << "Stack: "; + if (s.isEmpty()) { + cout << "Empty\n"; + return; + } + + while (s.getLength() != 1) { + cout << s.pop() << ", "; + } + cout << s.pop() << ".\n"; +} diff --git a/01-stacks/myFile.txt b/01-stacks/myFile.txt new file mode 100644 index 0000000..86817c0 --- /dev/null +++ b/01-stacks/myFile.txt @@ -0,0 +1 @@ +0 1 -6 10 50 30 -1 70 20 -5 -4 -3 -2 \ No newline at end of file diff --git a/01-stacks/numbers1.txt b/01-stacks/numbers1.txt new file mode 100644 index 0000000..4fecd48 --- /dev/null +++ b/01-stacks/numbers1.txt @@ -0,0 +1 @@ +10 30 20 1 70 0 40 50 -2 0 85 95 -3 60 75 \ No newline at end of file diff --git a/01-stacks/stacks_01_strings.cpp b/01-stacks/stacks_01_strings.cpp new file mode 100644 index 0000000..b3838f6 --- /dev/null +++ b/01-stacks/stacks_01_strings.cpp @@ -0,0 +1,84 @@ +/**~*~*~* +CIS 22C +Project: Stack of strings + +Written by: Iurii Tatishchev +IDE: CLion +*~*/ +#include +#include + +using namespace std; + +class Stack_str { +private: + // Structure for the stack nodes + struct StackNode { + string value; // Value in the node + StackNode *next; // Pointer to next node + }; + + StackNode *top; // Pointer to the stack top + int length; + +public: + Stack_str(){ top = NULL; length = 0; } //Constructor + //~Stack_str(); // Destructor + + // Stack operations + bool isEmpty() { + return length == 0; + } + + bool push(string); + + // string pop(); + string peek() { + return top->value; + } + + int getLength() { + return length; + } +}; + +/**~*~*~* + Member function push: pushes the argument onto the stack. +*~**/ +bool Stack_str::push(string item) { + StackNode *newNode; // Pointer to a new node + + // Allocate a new node and store item there. + newNode = new StackNode; + if (!newNode) + return false; + newNode->value = item; + + // Update links and counter + newNode->next = top; + top = newNode; + length++; + + return true; +} + + +int main() { + + Stack_str s; + string item; + + + getline(cin, item); + while (item != "0") { + s.push(item); + cin.clear(); + getline(cin, item); + } + + cout << s.getLength() << "\n"; + cout << (s.isEmpty() ? "Empty Stack!" : s.peek()) << "\n"; + cout << s.getLength() << "\n"; + + return 0; +} diff --git a/01-stacks/stacks_02_destructor b/01-stacks/stacks_02_destructor new file mode 100755 index 0000000..0f5c98c Binary files /dev/null and b/01-stacks/stacks_02_destructor differ diff --git a/01-stacks/stacks_02_destructor.cpp b/01-stacks/stacks_02_destructor.cpp new file mode 100644 index 0000000..a05a9cb --- /dev/null +++ b/01-stacks/stacks_02_destructor.cpp @@ -0,0 +1,89 @@ +/**~*~*~* +CIS 22C +Project: Stack of strings (Destructor) + +Written by: +IDE: +*~*/ +#include +#include + +using namespace std; + +class Stack_str { +private: + // Structure for the stack nodes + struct StackNode { + string value; // Value in the node + StackNode *next; // Pointer to next node + }; + + StackNode *top; // Pointer to the stack top + int length; // Number of nodes + +public: + Stack_str(){ top = NULL; length = 0; } // Constructor + ~Stack_str(); // Destructor + + // Stack operations + // bool isEmpty(); + bool push(string); + // string pop(); + // string peek(); + // int getLength(); +}; + +/**~*~*~* + Member function push: pushes the argument onto the stack. +*~**/ +bool Stack_str::push(string item) { + StackNode *newNode; // Pointer to a new node + + // Allocate a new node and store item there. + newNode = new StackNode; + if (!newNode) + return false; + newNode->value = item; + + // Update links and counter + newNode->next = top; + top = newNode; + length++; + + return true; +} + +/**~*~*~* + Destructor +*~**/ +Stack_str::~Stack_str() { + StackNode *currNode; + + // Position nodePtr at the top of the stack. + currNode = top; + + // Traverse the list deleting each node. + while (currNode) { + StackNode* nextNode = currNode->next; + cout << currNode->value << " - deleted!" << endl; + delete currNode; + currNode = NULL; + currNode = nextNode; + } + cout << "Empty stack!" << endl; +} + +int main() { + + Stack_str s; + string item; + + getline(cin, item); + while (item != "0") { + s.push(item); + cin.clear(); + getline(cin, item); + } + + return 0; +} \ No newline at end of file diff --git a/01-stacks/stacks_03_pop b/01-stacks/stacks_03_pop new file mode 100755 index 0000000..3b96ccf Binary files /dev/null and b/01-stacks/stacks_03_pop differ diff --git a/01-stacks/stacks_03_pop.cpp b/01-stacks/stacks_03_pop.cpp new file mode 100644 index 0000000..d2d9ee3 --- /dev/null +++ b/01-stacks/stacks_03_pop.cpp @@ -0,0 +1,102 @@ +/**~*~*~* +CIS 22C +Project: Stack of strings (pop) + +Written by: Iurii Tatishchev +IDE: CLion +*~*/ +#include +#include + +using namespace std; + +class Stack_str { +private: + // Structure for the stack nodes + struct StackNode { + string value; // Value in the node + StackNode *next; // Pointer to next node + }; + + StackNode *top; // Pointer to the stack top + int length; + +public: + Stack_str(){ top = NULL; length = 0; } //Constructor + // ~Stack_str(); // Destructor + + // Stack operations + bool isEmpty() { + return length == 0; + } + + bool push(string); + + string pop(); + + string peek() { + return top->value; + } + + int getLength() { + return length; + } +}; + +/**~*~*~* + Member function push: pushes the argument onto the stack. +*~**/ +bool Stack_str::push(string item) { + StackNode *newNode; // Pointer to a new node + + // Allocate a new node and store num there. + newNode = new StackNode; + if (!newNode) + return false; + newNode->value = item; + + // Update links and counter + newNode->next = top; + top = newNode; + length++; + + return true; +} + +/**~*~*~* + Member function pop pops the value at the top + of the stack off, and returns it + Assume stack is not empty +*~**/ +string Stack_str::pop() { + string val = this->top->value; + + StackNode* oldTop = this->top; + this->top = this->top->next; + this->length--; + delete oldTop; + + return val; +} + + +int main() { + + Stack_str s; + string item; + + getline(cin, item); + while (item != "0") { + s.push(item); + cin.clear(); + getline(cin, item); + } + + if (s.isEmpty()) cout << "Empty Stack!\n"; + + while (!s.isEmpty()) { + cout << s.pop() << "\n"; + } + + return 0; +} \ No newline at end of file diff --git a/02-queues/.idea/.gitignore b/02-queues/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/02-queues/.idea/.gitignore @@ -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 diff --git a/02-queues/.idea/.name b/02-queues/.idea/.name new file mode 100644 index 0000000..d80a61b --- /dev/null +++ b/02-queues/.idea/.name @@ -0,0 +1 @@ +02_queues \ No newline at end of file diff --git a/02-queues/.idea/02-queues.iml b/02-queues/.idea/02-queues.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/02-queues/.idea/02-queues.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/02-queues/.idea/inspectionProfiles/Project_Default.xml b/02-queues/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..03d9549 --- /dev/null +++ b/02-queues/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/02-queues/.idea/misc.xml b/02-queues/.idea/misc.xml new file mode 100644 index 0000000..79b3c94 --- /dev/null +++ b/02-queues/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/02-queues/.idea/modules.xml b/02-queues/.idea/modules.xml new file mode 100644 index 0000000..9fad917 --- /dev/null +++ b/02-queues/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/02-queues/CMakeLists.txt b/02-queues/CMakeLists.txt new file mode 100644 index 0000000..b6dfef0 --- /dev/null +++ b/02-queues/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.28) +project(02_queues) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(02_queues main.cpp) diff --git a/02-queues/QueueADT.h b/02-queues/QueueADT.h new file mode 100644 index 0000000..ce9c187 --- /dev/null +++ b/02-queues/QueueADT.h @@ -0,0 +1,102 @@ +/* + Written by: Iurii Tatishchev + ================================= + Queue template +*/ +#ifndef QUEUE_ADT_H +#define QUEUE_ADT_H + +template +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 +bool Queue::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 +T Queue::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 +Queue::~Queue() { + while (front != nullptr) { + QueueNode* nextNode = front->next; + delete front; + front = nextNode; + } +} + +#endif + diff --git a/02-queues/main.cpp b/02-queues/main.cpp new file mode 100644 index 0000000..cf55bb6 --- /dev/null +++ b/02-queues/main.cpp @@ -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 +#include + +#include "QueueADT.h" + + +using namespace std; + +int main() { + + // Create the first queue (strings) + Queue 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 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; +} diff --git a/02-queues/queues_01_strings.cpp b/02-queues/queues_01_strings.cpp new file mode 100644 index 0000000..41dba8e --- /dev/null +++ b/02-queues/queues_01_strings.cpp @@ -0,0 +1,86 @@ +/**~*~*~* +CIS 22C +Project: Queue of strings + +Written by: Iurii Tatishchev +IDE: CLion +*~*/ +#include +#include +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; +} \ No newline at end of file diff --git a/02-queues/queues_02_destructor.cpp b/02-queues/queues_02_destructor.cpp new file mode 100644 index 0000000..e0fc08e --- /dev/null +++ b/02-queues/queues_02_destructor.cpp @@ -0,0 +1,93 @@ +/**~*~*~* +CIS 22C +Project: Queue of strings + +Written by: Iurii Tatishchev +IDE: CLion +*~*/ +#include +#include + +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; +} \ No newline at end of file diff --git a/02-queues/queues_03_pop b/02-queues/queues_03_pop new file mode 100755 index 0000000..205a57f Binary files /dev/null and b/02-queues/queues_03_pop differ diff --git a/02-queues/queues_03_pop.cpp b/02-queues/queues_03_pop.cpp new file mode 100644 index 0000000..2e5d4fa --- /dev/null +++ b/02-queues/queues_03_pop.cpp @@ -0,0 +1,113 @@ +/**~*~*~* +CIS 22C +Project: Queue of strings (pop) + +Written by: Iurii Tatishchev +IDE: CLion +*~*/ +#include +#include + +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; +} \ No newline at end of file diff --git a/03-lists/.idea/.gitignore b/03-lists/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/03-lists/.idea/.gitignore @@ -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 diff --git a/03-lists/.idea/.name b/03-lists/.idea/.name new file mode 100644 index 0000000..bc8b685 --- /dev/null +++ b/03-lists/.idea/.name @@ -0,0 +1 @@ +03_lists \ No newline at end of file diff --git a/03-lists/.idea/03_lists.iml b/03-lists/.idea/03_lists.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/03-lists/.idea/03_lists.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/03-lists/.idea/inspectionProfiles/Project_Default.xml b/03-lists/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..03d9549 --- /dev/null +++ b/03-lists/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/03-lists/.idea/misc.xml b/03-lists/.idea/misc.xml new file mode 100644 index 0000000..79b3c94 --- /dev/null +++ b/03-lists/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/03-lists/.idea/modules.xml b/03-lists/.idea/modules.xml new file mode 100644 index 0000000..72fb86b --- /dev/null +++ b/03-lists/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/03-lists/01_insert.cpp b/03-lists/01_insert.cpp new file mode 100644 index 0000000..83e2847 --- /dev/null +++ b/03-lists/01_insert.cpp @@ -0,0 +1,53 @@ +/* + CIS 22C + + This program builds and displays a sorted list. + The list is sorted in ascending order by name. + + Requirement: + Change the insertNode() function to sort the list in descending order by gpa. + + Written by: Iurii Tatishchev + Reviewed & Modified by: Iurii Tatishchev + IDE: CLion + + */ +#include +#include +#include "StudentList.h" +using namespace std; + +void buildList(StudentList &); + +int main() +{ + // Define a StudentList object + StudentList list; + + buildList(list); + list.displayList(); + + return 0; +} +/* ************************************************** +This function builds a sorted linked list with data from the keyboard. +The list is sorted in ascending order by the students' names. +It calls the insertNode() function that inserts the new data at the right location in the linked list. +An input line contains the gpa of a student follow by its name (assume it is a single word name) +To stop reading enter "#" +************************************************** */ +void buildList(StudentList &list) +{ + string line; + getline(cin, line); + while (line != "#") + { + stringstream temp(line); // create a stringstream named temp with data from line + Student newStu; + temp >> newStu.gpa; // read gpa from temp + temp >> newStu.name; // read name from temp + list.insertNode(newStu); + getline(cin, line); + } + +} \ No newline at end of file diff --git a/03-lists/04_student_node_class.cpp b/03-lists/04_student_node_class.cpp new file mode 100644 index 0000000..de7614e --- /dev/null +++ b/03-lists/04_student_node_class.cpp @@ -0,0 +1,82 @@ +/* + CIS 22C + + This program builds and displays a sorted list. + The list is sorted in ascending order by name. + + Requirement: + Change the insertNode() function to sort the list in descending order by gpa. + + Written by: Iurii Tatishchev + Reviewed & Modified by: Iurii Tatishchev + IDE: CLion + + */ +#include +#include +#include "StudentList.h" + +using namespace std; + +void buildList(StudentList &); + +void deleteTestDriver(StudentList &); + +int main() { + // Define a StudentList object + StudentList list; + + buildList(list); // insert data into the list + + list.displayList(); + string answer; + cout << "Test Delete [Y/N]?\n"; + cin >> answer; + if (answer == "Y" || answer == "y") { + deleteTestDriver(list); + list.displayList(); + } + return 0; +} + +// *************************************************** +// This function builds a sorted linked list with data from the keyboard. +// The list is sorted in ascending order by the students' names. +// It calls the insertNode() function that inserts the new data at the right location in the linked list. +// An input line contains the gpa of a student follow by its name (assume it is a single word name) +// To stop reading enter "#" +// *************************************************** +void buildList(StudentList &list) { + string line, name; + double gpa; + + getline(cin, line); + while (line != "#") { + stringstream temp(line); // create a stringstream named temp with data from line + temp >> gpa; // read gpa from temp + temp >> name; // read name from temp + Student newStu(gpa, name); + list.insertNode(newStu); + getline(cin, line); + } +} + +// *************************************************** +// This function is a test driver for the +// linked list delete function +// *************************************************** +void deleteTestDriver(StudentList &list) { + string toDelete; + + cout << "Enter strings to be deleted (# to stop)" << endl; + cin >> toDelete; + while (toDelete != "#") { + cout << " " << toDelete; + if (list.deleteNode(toDelete)) + cout << " - deleted\n"; + else + cout << " - not found\n"; + cin >> toDelete; + } + cout << endl; +} diff --git a/03-lists/CMakeLists.txt b/03-lists/CMakeLists.txt new file mode 100644 index 0000000..bc05cd4 --- /dev/null +++ b/03-lists/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.28) +project(03_lists) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(03_lists main.cpp StudentList.cpp + LinkedList.cpp + Park.cpp) diff --git a/03-lists/Lab Singly-Linked Lists (Park).zip b/03-lists/Lab Singly-Linked Lists (Park).zip new file mode 100644 index 0000000..2986014 Binary files /dev/null and b/03-lists/Lab Singly-Linked Lists (Park).zip differ diff --git a/03-lists/LinkedList.cpp b/03-lists/LinkedList.cpp new file mode 100644 index 0000000..36d3138 --- /dev/null +++ b/03-lists/LinkedList.cpp @@ -0,0 +1,157 @@ +// Implementation file for the LinkedList class +// Written By: Iurii Tatishchev +// Reviewed by: Iurii Tatishchev +// IDE: CLion + +#include +#include "LinkedList.h" + +using namespace std; + +// ************************************************** +// Constructor +// This function allocates and initializes a sentinel node +// A sentinel (or dummy) node is an extra node added before the first data record. +// This convention simplifies and accelerates some list-manipulation algorithms, +// by making sure that all links can be safely dereferenced and that every list +// (even one that contains no data elements) always has a "first" node. +// ************************************************** +LinkedList::LinkedList() { + head = new Node; // head points to the sentinel node + head->next = NULL; + length = 0; +} + +// ************************************************** +// The insertNode() function inserts a new node in a +// sorted linked list +// ************************************************** +void LinkedList::insertNode(Park dataIn) { + Node *newNode; // A new node + Node *pCur; // To traverse the list + Node *pPre; // The previous node + + // Allocate a new node and store dataIn there. + newNode = new Node; + newNode->park = dataIn; + + // Initialize pointers + pPre = head; + pCur = head->next; + + // Find location: skip all nodes whose code is less than dataIn's code + while (pCur && newNode->park.getCode() > pCur->park.getCode()) { + pPre = pCur; + pCur = pCur->next; + } + + // Insert the new node between pPre and pCur + pPre->next = newNode; + newNode->next = pCur; + + // Update the counter + length++; +} + +// ************************************************** +// The deleteNode() function searches for a node +// in a sorted linked list; if found, the node is +// deleted from the list and from memory. +// ************************************************** +bool LinkedList::deleteNode(string target) { + Node *pCur; // To traverse the list + Node *pPre; // To point to the previous node + bool deleted = false; + + // Initialize pointers + pPre = head; + pCur = head->next; + + // Find node containing the target: Skip all nodes whose code is less than the target + while (pCur != NULL && pCur->park.getCode() < target) { + pPre = pCur; + pCur = pCur->next; + } + + // If found, delete the node + if (pCur && pCur->park.getCode() == target) { + pPre->next = pCur->next; + delete pCur; + deleted = true; + length--; + } + return deleted; +} + +// ************************************************** +// displayList() shows the value +// stored in each node of the linked list +// pointed to by head, except the sentinel node +// ************************************************** +void LinkedList::displayList() const { + Node *pCur; // To move through the list + + // Position pCur: skip the head of the list. + pCur = head->next; + + // While pCur points to a node, traverse the list. + while (pCur) { + // Display the value in this node. + pCur->park.hDdisplay(); + + // Move to the next node. + pCur = pCur->next; + } + cout << endl; +} + +// ************************************************** +// The searchList() function looks for a target park +// in the sorted linked list: if found, returns true +// and copies the data in that node to the output parameter +// ************************************************** +bool LinkedList::searchList(string target, Park &dataOut) const { + bool found = false; // assume target not found + Node *pCur; // To move through the list + + // Position pCur: skip the head of the list. + pCur = head->next; + + // While pCur points to a node, traverse the list. + while (pCur && pCur->park.getCode() != target) { + pCur = pCur->next; + } + + // If the target was found, copy the data + if (pCur) { + dataOut = pCur->park; + found = true; + } + + return found; +} + +// ************************************************** +// Destructor +// This function deletes every node in the list. +// ************************************************** +LinkedList::~LinkedList() { + Node *pCur; // To traverse the list + Node *pNext; // To hold the address of the next node + + // Position nodePtr: skip the head of the list + pCur = head->next; + // While pCur is not at the end of the list... + while (pCur != NULL) { + // Save a pointer to the next node. + pNext = pCur->next; + + // Delete the current node. + delete pCur; + + // Position pCur at the next node. + pCur = pNext; + } + + delete head; // delete the sentinel node +} diff --git a/03-lists/LinkedList.h b/03-lists/LinkedList.h new file mode 100644 index 0000000..eda8cad --- /dev/null +++ b/03-lists/LinkedList.h @@ -0,0 +1,37 @@ +// Specification file for the LinkedList class +// Written By: Iurii Tatishchev +// Reviewed by: Iurii Tatishchev +// IDE: CLion + +#ifndef LINKED_LIST_H +#define LINKED_LIST_H + +#include "Park.h" + +class LinkedList { +private: + struct Node { + Park park; + Node *next; + }; + + Node *head; + int length; + +public: + LinkedList(); // constructor + ~LinkedList(); // destructor + + // Linked list operations + int getLength() const { return length; } + + void insertNode(Park); + + bool deleteNode(string); + + void displayList() const; + + bool searchList(string, Park &) const; +}; + +#endif diff --git a/03-lists/ListNode.h b/03-lists/ListNode.h new file mode 100644 index 0000000..c3e51f3 --- /dev/null +++ b/03-lists/ListNode.h @@ -0,0 +1,34 @@ +// Specification file for the ListNode class +// Written by: A. Student +// Reviewed by: +// IDE + +#ifndef LISTNODE_H +#define LISTNODE_H + +#include +#include "Student.h" + +using std::string; + +class ListNode { +private: + Student stu; // store data + ListNode *next; // a pointer to the next node in the list + +public: + // Constructor + ListNode(const Student &dataIn, ListNode *next = NULL) { stu = dataIn; } + + // setters + // set a pointer to a pointer in the node + void setNext(ListNode *nextPtr) { next = nextPtr; } + + // getters + // return pointer in the node + ListNode *getNext() const { return next; } + + Student getData() { return stu; } // return stu object in the listnode +}; + +#endif diff --git a/03-lists/Park.cpp b/03-lists/Park.cpp new file mode 100644 index 0000000..fb6fe3d --- /dev/null +++ b/03-lists/Park.cpp @@ -0,0 +1,58 @@ +// Implementation file for the Park class +// Written By: Iurii Tatishchev +// Reviewed by: Iurii Tatishchev +// IDE: CLion + + +#include +#include +#include + +#include "Park.h" + +using namespace std; + +// ************************************************** +// Constructor +// ************************************************** +Park::Park() { + code = ""; + state = ""; + name = ""; + description = ""; + year = -1; +} + +// ************************************************** +// Overloaded Constructor +// ************************************************** +Park::Park(string cd, string st, string nm, string dsc, int yr) { + code = cd; + state = st; + name = nm; + description = dsc; + year = yr; +} + +// *********************************************************** +// Displays the values of a Park object member variables +// on one line (horizontal display) +// *********************************************************** +void Park::hDdisplay() const { + cout << code << " "; + cout << state << " "; + cout << year << " "; + cout << name << " " << endl; +} + +// *********************************************************** +// Displays the values of a Park object member variables +// one per line (vertical display) +// *********************************************************** +void Park::vDisplay() const { + cout << name << endl; + cout << " \"" << description << "\"" << endl; + cout << year << endl; + cout << state << endl; +} + diff --git a/03-lists/Park.h b/03-lists/Park.h new file mode 100644 index 0000000..63b7477 --- /dev/null +++ b/03-lists/Park.h @@ -0,0 +1,64 @@ +// Specification file for the Park class +// Written By: Iurii Tatishchev +// Reviewed by: Iurii Tatishchev +// IDE: CLion + +#ifndef PARK_H +#define PARK_H + +// #include +#include +// #include + +// using namespace std; +// ^^^^^ This statement +// in a header file of a complex project could create +// namespace management problems for the entire project +// (such as name collisions). +// Do not write using namespace at the top level in a header file! + +using std::string; + +class Park { +private: + string code; // the unique park identifier + string state; + string name; + string description; + int year; + +public: + //constructors + Park(); + + Park(string, string, string, string, int); + + //setters + void setCode(string cd) { code = cd; } + + void setState(string st) { state = st; } + + void setName(string nm) { name = nm; } + + void setDesc(int dsc) { description = dsc; } + + void setYear(int yr) { year = yr; } + + //getters + string getCode() const { return code; } + + string getState() const { return state; } + + string getName() const { return name; } + + string getDesc() const { return description; } + + int getYear() const { return year; } + + //other functions + void hDdisplay() const; + + void vDisplay() const; +}; + +#endif diff --git a/03-lists/Student.h b/03-lists/Student.h new file mode 100644 index 0000000..ac7f5c2 --- /dev/null +++ b/03-lists/Student.h @@ -0,0 +1,34 @@ +// Specification file for the Student class +// Written by: Iurii Tatishchev +// Reviewed & Modified by: Iurii Tatishchev +// IDE: CLion + +#ifndef STUDENT_H +#define STUDENT_H + +//using namespace std; //<==== This statement +// in a header file of a complex project could create +// namespace management problems for the entire project +// (such as name collisions). +// Do not write namespace using statements at the top level in a header file! + +#include +using std::string; + +class Student { +private: + double gpa; + string name; + +public: + Student() { gpa = 0 ; name = ""; } + Student(double gpa, string name) { this->gpa = gpa; this->name = name; } + + // Setters and getters + void setGpa(double gpa) { this->gpa = gpa; } + void setName(string name) { this->name = name; } + double getGpa() const { return gpa; } + string getName() const { return name; } + +}; +#endif diff --git a/03-lists/StudentList.cpp b/03-lists/StudentList.cpp new file mode 100644 index 0000000..3e29540 --- /dev/null +++ b/03-lists/StudentList.cpp @@ -0,0 +1,130 @@ +// Implementation file for the Student List class +// Written by: Iurii Tatishchev +// Reviewed, Debugged, & Modified by: Iurii Tatishchev +// IDE: CLion + +#include // For cout and NULL +#include "StudentList.h" +using namespace std; + +// ************************************************** +// Constructor +// This function allocates and initializes a sentinel node +// A sentinel (or dummy) node is an extra node added before the first data record. +// This convention simplifies and accelerates some list-manipulation algorithms, +// by making sure that all links can be safely dereferenced and that every list +// (even one that contains no data elements) always has a "first" node. +// ************************************************** +StudentList::StudentList() { + head = new ListNode(Student()); // head points to the sentinel node + head->setNext(NULL); + count = 0; +} + +// ************************************************** +// displayList shows the value +// stored in each node of the linked list +// pointed to by head. +// ************************************************** +void StudentList::displayList() const { + ListNode *pCur; // To move through the list + + // Position pCur: skip the head of the list. + pCur = head->getNext(); + + // While pCur points to a node, traverse the list. + while (pCur) + { + // Display the value in this node. + // cout << pCur->stu.getGpa() << " " << pCur->stu.getName() << endl; + cout << pCur->getData().getGpa() << " " << pCur->getData().getName() << endl; + + // Move to the next node. + pCur = pCur->getNext(); + } + cout << endl; +} + +// ************************************************** +// The insertNode function inserts a node with +// dataIn copied to its stu member. +// ************************************************** +void StudentList::insertNode(Student dataIn) { + ListNode *newNode; // A new node + ListNode *pCur; // To traverse the list + ListNode *pPre; // The previous node + + // Allocate a new node and store num there. + newNode = new ListNode(dataIn); + + // Initialize pointers + pPre = head; + pCur = head->getNext(); + + // Find location: skip all nodes are alphabetically less than dataIn + while (pCur != NULL && pCur->getData().getName() < dataIn.getName()) { + pPre = pCur; + pCur = pCur->getNext(); + } + + // Insert the new node between pPre and pCur + pPre->setNext(newNode); + newNode->setNext(pCur); + + // Update the counter + count++; +} + +// ************************************************** +// The deleteNode function searches for a node +// with target as its value. The node, if found, is +// deleted from the list and from memory. +// ************************************************** +bool StudentList::deleteNode(string target) { + ListNode *pCur; // To traverse the list + ListNode *pPre; // To point to the previous node + bool deleted = false; + + // Initialize pointers + pPre = head; + pCur = head->getNext(); + // Find node containing the target: Skip all nodes whose name is less than the target + while (pCur != NULL && pCur->getData().getName() < target) { + pPre = pCur; + pCur = pCur->getNext(); + } + + // If found, delete the node + if (pCur != NULL && pCur->getData().getName() == target) { + pPre->setNext(pCur->getNext()); + delete pCur; + deleted = true; + count--; + } + return deleted; + +} + +// ************************************************** +// Destructor +// This function deletes every node in the list. +// ************************************************** +StudentList::~StudentList() { + ListNode *pCur; // To traverse the list + ListNode *pNext; // To point to the next node + + // Position nodePtr at the head of the list. + pCur = head->getNext(); + // While pCur is not at the end of the list... + while (pCur != NULL) { + // Save a pointer to the next node. + pNext = pCur->getNext(); + // Delete the current node + delete pCur; + + // Position pCur at the next node. + pCur = pNext; + } + delete head; // delete the sentinel node + +} \ No newline at end of file diff --git a/03-lists/StudentList.h b/03-lists/StudentList.h new file mode 100644 index 0000000..eb40b27 --- /dev/null +++ b/03-lists/StudentList.h @@ -0,0 +1,29 @@ +// Specification file for the StudentList class +// Written by: Iurii Tatishchev +// Reviewed by: Iurii Tatishchev +// IDE: CLion + +#ifndef STUDENTLIST_H +#define STUDENTLIST_H + +#include "ListNode.h" + +class StudentList { +private: + ListNode *head; // List head pointer + int count; // To keep track of the number of nodes in the list + +public: + StudentList(); // Constructor + ~StudentList(); // Destructor + + // Linked list operations + int getCount() const { return count; } + void insertNode(Student); + bool deleteNode(string); + //void searchList() const; + void displayList() const; + +}; + +#endif // STUDENTLIST_H diff --git a/03-lists/main.cpp b/03-lists/main.cpp new file mode 100644 index 0000000..e9f85e7 --- /dev/null +++ b/03-lists/main.cpp @@ -0,0 +1,161 @@ +/* + CIS 22C: Homework 3 + + Build and procees a sorted linked list of Park objects. + The list is sorted in ascending order by the park code. + Assume that the park code is unique. + + Written by: Iurii Tatishchev + Reviewed & Modified by: Iurii Tatishchev + IDE: CLion + */ + +/* + * + Reads data about National Parks from a text file and inserts them into a sorted linked list. The list is sorted in ascending order by the unique park identifier. For instance, the unique park identifier for the Arches National Park is ARC. + Displays the list and the number of parks in the list. + Searches the list: prompts the user to enter a park code (i.e the unique park identifier), searches for that code: if found, displays related data, otherwise displays an error message, then searches again for another code, until the user enter “Q” to stop searching. + Deletes nodes from the list: prompts the user to enter a code, searches for that code: if found, it removes it from the list, otherwise displays an error message, then searches again for another code, until the user enter “Q” to stop deleting. + + Read and understand this program (draw UML diagrams, the linked list, and hierarchy charts). Then do the following: + + In main.cpp: provide calling statements for the basic linked list functions + Build and run the program: search will not work, but display and delete should work + In LinkedList.cpp: finish writing the searchList() function + Build and run the program: now search should work too + In Park.h and Park.cpp overload the stream insertion operator. The overloaded operator is going to replace the hDisplay() function in the Park class, and it is going to be used in LinkedList.cpp, in displayList() as shown below + Build and run the program + + */ + +#include +#include +#include +#include +#include "LinkedList.h" + +using namespace std; + +void buildList(const string &filename, LinkedList &list); + +void deleteManager(LinkedList &list); + +void searchManager(const LinkedList &list); + +void displayManager(const LinkedList &list); + +int main() { + + string inputFileName = "national_parks.txt"; + LinkedList list; + + buildList(inputFileName, list); + displayManager(list); + searchManager(list); + deleteManager(list); + displayManager(list); + return 0; +} + +/* + This function reads data about national parks from a file and inserts them + into a sorted linked list. The list is sorted in ascending order by code. +*/ +void buildList(const string &filename, LinkedList &list) { + ifstream inputFile(filename); + cout << "Reading data from \"" << filename << "\"" << endl; + + if (!inputFile) { + cout << "Error opening the input file: \"" << filename << "\"" << endl; + exit(EXIT_FAILURE); + } + + string line; + while (getline(inputFile, line)) { + int year; + string code, name, state, dsc; + + stringstream temp(line); // create temp with data from line + temp >> code; // read from temp + temp >> state; + temp >> year; + temp.ignore(); // to ignore space in front of name + getline(temp, name, ';'); // stop reading name at ';' + temp.ignore(); // to ignore space in front of description + getline(temp, dsc); + // create a Park object and initialize it with data from file + Park aPark(code, state, name, dsc, year); + list.insertNode(aPark); + } + + inputFile.close(); +} + +/* + Delete manager: delete items from the list until the user enters Q to quit + deleting + Input Parameter: list + */ +void deleteManager(LinkedList &list) { + string targetCode = ""; + + cout << endl << " Delete" << endl; + cout << "=======" << endl; + + while (targetCode != "Q") { + cout << "Enter a park code (or Q to stop deleting):" << endl; + cin >> targetCode; + + if (targetCode != "Q") { + if (list.deleteNode(targetCode)) + cout << " " << targetCode << " has been deleted!" << endl; + else + cout << "Park \"" << targetCode << "\" was not found in this list." << endl; + } + } + cout << "___________________END DELETE SECTION_____" << endl; +} + +/* + Search manager: search the list until the user enters Q to quit searching + Input Parameter: list + */ +void searchManager(const LinkedList &list) { + string targetCode = ""; + Park aPark; + + cout << endl << " Search" << endl; + cout << "=======" << endl; + + while (targetCode != "Q") { + cout << "Enter a park code (or Q to stop searching):" << endl; + cin >> targetCode; + + if (targetCode != "Q") { + if (list.searchList(targetCode, aPark)) + aPark.vDisplay(); + else + cout << "Park \"" << targetCode << "\" was not found in this list." << endl; + } + } + cout << "___________________END SEARCH SECTION _____" << endl; +} + +/* +Display manager: + - displays the number of national parks in this list + - calls the displayList() function upon request +Input Parameter: list + */ +void displayManager(const LinkedList &list) { + string action; + + cout << "Number of National Parks in this list: " << list.getLength() << endl; + + cout << "\nDisplay list [Y/N]? "; + cin >> action; + if (action == "Y" || action == "y") { + cout << endl; + list.displayList(); + } +} diff --git a/04-dl-lists/.idea/.gitignore b/04-dl-lists/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/04-dl-lists/.idea/.gitignore @@ -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 diff --git a/04-dl-lists/.idea/.name b/04-dl-lists/.idea/.name new file mode 100644 index 0000000..7eeaea2 --- /dev/null +++ b/04-dl-lists/.idea/.name @@ -0,0 +1 @@ +04_dl_lists \ No newline at end of file diff --git a/04-dl-lists/.idea/04-dl-lists.iml b/04-dl-lists/.idea/04-dl-lists.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/04-dl-lists/.idea/04-dl-lists.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/04-dl-lists/.idea/inspectionProfiles/Project_Default.xml b/04-dl-lists/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..03d9549 --- /dev/null +++ b/04-dl-lists/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/04-dl-lists/.idea/misc.xml b/04-dl-lists/.idea/misc.xml new file mode 100644 index 0000000..79b3c94 --- /dev/null +++ b/04-dl-lists/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/04-dl-lists/.idea/modules.xml b/04-dl-lists/.idea/modules.xml new file mode 100644 index 0000000..5f16169 --- /dev/null +++ b/04-dl-lists/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/04-dl-lists/.idea/vcs.xml b/04-dl-lists/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/04-dl-lists/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/04-dl-lists/CMakeLists.txt b/04-dl-lists/CMakeLists.txt new file mode 100644 index 0000000..4408395 --- /dev/null +++ b/04-dl-lists/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.28) +project(04_dl_lists) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(04_dl_lists main.cpp + StudentList.cpp) diff --git a/04-dl-lists/StudentList.cpp b/04-dl-lists/StudentList.cpp new file mode 100644 index 0000000..85b5e26 --- /dev/null +++ b/04-dl-lists/StudentList.cpp @@ -0,0 +1,104 @@ +// Sorted Circular Doubly-Linked List with Sentinel Node +// Implementation file for the Student List class +// Written by: Iurii Tatishchev +// Reviewed & Modified by: Iurii Tatishchev +// IDE: CLion +#include // For cout and NULL +#include "StudentList.h" +using namespace std; + +// ************************************************** +// Constructor +// This function allocates and initializes a sentinel node +// A sentinel (or dummy) node is an extra node added before the first data record. +// This convention simplifies and accelerates some list-manipulation algorithms, +// by making sure that all links can be safely dereferenced and that every list +// (even one that contains no data elements) always has a "first" node. +// ************************************************** +StudentList::StudentList() +{ + head = new ListNode; // head points to the sentinel node + + head->stu.gpa = -1; + head->stu.name = ""; + head->forw = head; + /* Write your code here */ + count = 0; +} + +// ************************************************** +// display list forwards: shows the value +// stored in each node of the linked list +// pointed to by head. from A to Z +// ************************************************** +void StudentList::displayListForw() const +{ + ListNode *pCur; // To move through the list + + // Position pCur: skip the head of the list. + pCur = head->forw; + + // While pCur points to a node, traverse the list. + while (pCur != head) + { + // Display the value in this node. + cout << pCur->stu.gpa << " " << pCur->stu.name << endl; + + // Move to the next node. + pCur = pCur->forw; + } + cout << endl; +} +// ************************************************** +// display list forwards: shows the value +// stored in each node of the linked list +// pointed to by head, from Z to A +// ************************************************** + +void StudentList::displayListBack() const +{ + /* Write your code here */ + + cout << endl; +} +// ************************************************** +// The insertNode function inserts a node with +// stu copied to its value member. +// ************************************************** +void 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; + + // Find location: skip all nodes whose name is less than dataIn's name + while (pCur != head && pCur->stu.name < dataIn.name) + { + pCur = pCur->forw; + } + + // Insert the new node between pPre and pCur + ListNode *pPre = pCur->back; // The previous node + pPre->forw = newNode; + newNode->forw = pCur; + + /* Write your code here */ + + // Update the counter + count++; +} + +// ************************************************** +// Destructor * +// This function deletes every node in the list. * +// ************************************************** +StudentList::~StudentList() +{ + +} diff --git a/04-dl-lists/StudentList.h b/04-dl-lists/StudentList.h new file mode 100644 index 0000000..3b822d5 --- /dev/null +++ b/04-dl-lists/StudentList.h @@ -0,0 +1,45 @@ +// Sorted Circular Doubly-Linked List with Sentinel Node +// Specification file for the Student List class +// Written by: Iurii Tatishchev +// Reviewed by: Iurii Tatishchev +// IDE: CLion + +#ifndef STUDENTLIST_H +#define STUDENTLIST_H + +#include + +struct Student +{ + double gpa; + std::string name; +}; + +class StudentList +{ +private: + // Declare a structure for the list + struct ListNode + { + Student stu; // The value in this node + ListNode *forw; // To point to the next node + ListNode *back; // To point to the previous node + }; + + ListNode *head; // List head pointer + int count; // To keep track of the number of nodes in the list + + +public: + StudentList(); // Constructor + ~StudentList(); // Destructor + + // Linked list operations + int getCount() const {return count;} + void insertNode(Student); + //bool deleteNode(double); + //void searchList() const; + void displayListForw() const; + void displayListBack() const; +}; +#endif diff --git a/04-dl-lists/main.cpp b/04-dl-lists/main.cpp new file mode 100644 index 0000000..d376020 --- /dev/null +++ b/04-dl-lists/main.cpp @@ -0,0 +1,78 @@ +/* + CIS 22C + + Sorted Circular Doubly-Linked List with Sentinel Node + + This program: +- Creates a sorted linked list (student name and gpa) . The list is sorted in ascending order by name. +- Displays the list forwards(A to Z) +- Displays the list backwards(Z to A) + + Requirements: Finish writing the following three functions: +- default constructor +- insertNode() +- displayListBack() + + Written by: Iurii Tatishchev + Reviewed by: Iurii Tatishchev + IDE: CLion + + */ + +#include +#include +#include "StudentList.h" + +using namespace std; + +void buildList(StudentList &); + +int main() +{ + // Define a StudentList object + StudentList list; + + buildList(list); // insert data into the list + cout << "There are " << list.getCount() << " elements in the list." << endl; + + string answer; + cout << "Insert [Y/N]? "; + getline(cin, answer); + if (answer == "Y" || answer == "y") + { + Student s; + cout << "Enter gpa then enter name:" << endl; + cin >> s.gpa >> s.name; + list.insertNode(s); + cout << "There are " << list.getCount() << " elements in the list." << endl; + } + cout << endl; + list.displayListForw(); + /* Write your code here: display the list from Z to A */ + return 0; +} + +/* ************************************************** +This function builds a sorted linked list with data from the keyboard. +The list is sorted in ascending order by the students' names. +It calls the insertNode() function that inserts the new data at the right location in the linked list. +An input line contains the gpa of a student follow by its name (assume it is a single word name) +To stop reading enter "#" +************************************************** */ +void buildList(StudentList &list) +{ + string line; + getline(cin, line); + while (line != "#") + { + stringstream temp(line); // create a stringstream named temp with data from line + Student newStu; + temp >> newStu.gpa; // read gpa from temp + temp >> newStu.name; // read name from temp + list.insertNode(newStu); + getline(cin, line); + } + +} + +