From a828b3525a1f024139a56f46ae043aea77cf340f Mon Sep 17 00:00:00 2001 From: Iurii Tatishchev Date: Sun, 28 Apr 2024 11:54:46 -0700 Subject: [PATCH] initial commit --- .gitignore | 117 +++++++++++++ 01-stacks/.idea/.gitignore | 8 + 01-stacks/.idea/.name | 1 + 01-stacks/.idea/01-stacks.iml | 2 + .../.idea/codeStyles/codeStyleConfig.xml | 5 + .../inspectionProfiles/Project_Default.xml | 6 + 01-stacks/.idea/misc.xml | 4 + 01-stacks/.idea/modules.xml | 8 + 01-stacks/CMakeLists.txt | 6 + 01-stacks/Lab Stacks 4 - max (Stack ADT).zip | Bin 0 -> 3889 bytes 01-stacks/StackADT.h | 95 +++++++++++ 01-stacks/in1.txt | 1 + 01-stacks/main.cpp | 118 +++++++++++++ 01-stacks/myFile.txt | 1 + 01-stacks/numbers1.txt | 1 + 01-stacks/stacks_01_strings.cpp | 84 +++++++++ 01-stacks/stacks_02_destructor | Bin 0 -> 18624 bytes 01-stacks/stacks_02_destructor.cpp | 89 ++++++++++ 01-stacks/stacks_03_pop | Bin 0 -> 18472 bytes 01-stacks/stacks_03_pop.cpp | 102 +++++++++++ 02-queues/.idea/.gitignore | 8 + 02-queues/.idea/.name | 1 + 02-queues/.idea/02-queues.iml | 2 + .../inspectionProfiles/Project_Default.xml | 6 + 02-queues/.idea/misc.xml | 4 + 02-queues/.idea/modules.xml | 8 + 02-queues/CMakeLists.txt | 6 + 02-queues/QueueADT.h | 102 +++++++++++ 02-queues/main.cpp | 69 ++++++++ 02-queues/queues_01_strings.cpp | 86 ++++++++++ 02-queues/queues_02_destructor.cpp | 93 ++++++++++ 02-queues/queues_03_pop | Bin 0 -> 18456 bytes 02-queues/queues_03_pop.cpp | 113 ++++++++++++ 03-lists/.idea/.gitignore | 8 + 03-lists/.idea/.name | 1 + 03-lists/.idea/03_lists.iml | 2 + .../inspectionProfiles/Project_Default.xml | 6 + 03-lists/.idea/misc.xml | 4 + 03-lists/.idea/modules.xml | 8 + 03-lists/01_insert.cpp | 53 ++++++ 03-lists/04_student_node_class.cpp | 82 +++++++++ 03-lists/CMakeLists.txt | 8 + 03-lists/Lab Singly-Linked Lists (Park).zip | Bin 0 -> 14942 bytes 03-lists/LinkedList.cpp | 157 +++++++++++++++++ 03-lists/LinkedList.h | 37 ++++ 03-lists/ListNode.h | 34 ++++ 03-lists/Park.cpp | 58 +++++++ 03-lists/Park.h | 64 +++++++ 03-lists/Student.h | 34 ++++ 03-lists/StudentList.cpp | 130 ++++++++++++++ 03-lists/StudentList.h | 29 ++++ 03-lists/main.cpp | 161 ++++++++++++++++++ 04-dl-lists/.idea/.gitignore | 8 + 04-dl-lists/.idea/.name | 1 + 04-dl-lists/.idea/04-dl-lists.iml | 2 + .../inspectionProfiles/Project_Default.xml | 6 + 04-dl-lists/.idea/misc.xml | 4 + 04-dl-lists/.idea/modules.xml | 8 + 04-dl-lists/.idea/vcs.xml | 6 + 04-dl-lists/CMakeLists.txt | 7 + 04-dl-lists/StudentList.cpp | 104 +++++++++++ 04-dl-lists/StudentList.h | 45 +++++ 04-dl-lists/main.cpp | 78 +++++++++ 63 files changed, 2291 insertions(+) create mode 100644 .gitignore create mode 100644 01-stacks/.idea/.gitignore create mode 100644 01-stacks/.idea/.name create mode 100644 01-stacks/.idea/01-stacks.iml create mode 100644 01-stacks/.idea/codeStyles/codeStyleConfig.xml create mode 100644 01-stacks/.idea/inspectionProfiles/Project_Default.xml create mode 100644 01-stacks/.idea/misc.xml create mode 100644 01-stacks/.idea/modules.xml create mode 100644 01-stacks/CMakeLists.txt create mode 100644 01-stacks/Lab Stacks 4 - max (Stack ADT).zip create mode 100644 01-stacks/StackADT.h create mode 100644 01-stacks/in1.txt create mode 100644 01-stacks/main.cpp create mode 100644 01-stacks/myFile.txt create mode 100644 01-stacks/numbers1.txt create mode 100644 01-stacks/stacks_01_strings.cpp create mode 100755 01-stacks/stacks_02_destructor create mode 100644 01-stacks/stacks_02_destructor.cpp create mode 100755 01-stacks/stacks_03_pop create mode 100644 01-stacks/stacks_03_pop.cpp create mode 100644 02-queues/.idea/.gitignore create mode 100644 02-queues/.idea/.name create mode 100644 02-queues/.idea/02-queues.iml create mode 100644 02-queues/.idea/inspectionProfiles/Project_Default.xml create mode 100644 02-queues/.idea/misc.xml create mode 100644 02-queues/.idea/modules.xml create mode 100644 02-queues/CMakeLists.txt create mode 100644 02-queues/QueueADT.h create mode 100644 02-queues/main.cpp create mode 100644 02-queues/queues_01_strings.cpp create mode 100644 02-queues/queues_02_destructor.cpp create mode 100755 02-queues/queues_03_pop create mode 100644 02-queues/queues_03_pop.cpp create mode 100644 03-lists/.idea/.gitignore create mode 100644 03-lists/.idea/.name create mode 100644 03-lists/.idea/03_lists.iml create mode 100644 03-lists/.idea/inspectionProfiles/Project_Default.xml create mode 100644 03-lists/.idea/misc.xml create mode 100644 03-lists/.idea/modules.xml create mode 100644 03-lists/01_insert.cpp create mode 100644 03-lists/04_student_node_class.cpp create mode 100644 03-lists/CMakeLists.txt create mode 100644 03-lists/Lab Singly-Linked Lists (Park).zip create mode 100644 03-lists/LinkedList.cpp create mode 100644 03-lists/LinkedList.h create mode 100644 03-lists/ListNode.h create mode 100644 03-lists/Park.cpp create mode 100644 03-lists/Park.h create mode 100644 03-lists/Student.h create mode 100644 03-lists/StudentList.cpp create mode 100644 03-lists/StudentList.h create mode 100644 03-lists/main.cpp create mode 100644 04-dl-lists/.idea/.gitignore create mode 100644 04-dl-lists/.idea/.name create mode 100644 04-dl-lists/.idea/04-dl-lists.iml create mode 100644 04-dl-lists/.idea/inspectionProfiles/Project_Default.xml create mode 100644 04-dl-lists/.idea/misc.xml create mode 100644 04-dl-lists/.idea/modules.xml create mode 100644 04-dl-lists/.idea/vcs.xml create mode 100644 04-dl-lists/CMakeLists.txt create mode 100644 04-dl-lists/StudentList.cpp create mode 100644 04-dl-lists/StudentList.h create mode 100644 04-dl-lists/main.cpp 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 0000000000000000000000000000000000000000..715cdb921d1dc0510a00c9c77f5bcd976fc6c9ae GIT binary patch literal 3889 zcmbVP&2Jk;6yFd^!&E*XRpLVeJ*8FaggEiqyLL7Xk)pH`RiLsI7Z4IcoAo#zx}II_ zj?+d}d**=DBS!?P#2JoANT??+aN>YK;sk#H2M&k}@6FiWwIes$Y&ve{&71f8y-#o2 z`-{(Cww9JG>%-3CB4iK_yBxNA zsYG}L8`i;INZq@Th<_mvEE;1Y?$H=kp6(Du^(_i{T5@#k?2B< z#?&p@i^?mlu_Ow3n%*7`4tbK64cwYxy^YE{a4l2Cy+$hQ&+M4t0Z?$uE9?OSe5QbK zuZ2N0RIt4b%QvOs2?!bEz9&Mi$!@_i9dg+XTUy<4(F){@5gu_B3dzfXkZY5pl@@Gl zfS;zr0f%Eh6kTmVf8arv2ce%(ASe}mo^X9aJjW`5ABISJDi$krIHNF$fIbAKj}xwj ziG*s4J!Fh+tg=pDq|h76K#52~SEO<1pQNDroG_>^uH#OqkW*32W@Z$^Nwnb6>LWN^ zPggLH#2%CwR{Swq-_nSbg#(og11t;>F8^|hUNco>QxbAB(9#5UZ1%1n0y%njXoFuQVsq8k(U*T=O(p`)L9CZiaKPRJEE7IhlIH+?b+Vbsg)f-k#Z zrbr4xAq>E-nP#b;SvXE)qyQ^eoy;SVg;&S0llaFt(MJqIkt#!pz8kn7^mQpf`JPaH z8p|{Q=?KG&0W%$f4S~~G^h)g2p9j0g*wVD-xAjGAYawgv>b@$}(>7G0R)u;MoGMg2 zpuMd(UKLyvHP|?BRlx74%~PY^=L k;w?OjkZ+i21h_;9#T&J~f8hlx$rthG9#;Jmv}0NS1B+h94gdfE literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..0f5c98c6a0d7364ae210eb11d6031d842754f07f GIT binary patch literal 18624 zcmeHPeQ;FQb-$|*5EziaKxItLg0YL^u;>d6W~djW6;I~Fsy-Y-z|X7QMOq{6irpuW z+|m*YyQ~#uRO+!w>Y2)Mnlf!NanjU7W0$U>1IF#tI7wY5=}eUl_N)rA5YOGIt8Bgi^4^EC$-*5t;euJ z$CDw)lVU>GdTrMU{oqmY%TFPkdi`3@2^+LLLu!Lc>l;@1dfIE&dMmxsjBvs)D6(M4 z?fn7tC@#Nj;bCsk?YWIZmyRbx8gD9Vx}zQI>eh6JS9eEaef_KZ*RNZ>uFjW?`)VYg z{Hnr5!?$&3yC^y$(nMo@kMQK-MH+{QEF`-S7lobllUwh4eD3m*8^8R;6(XT2-)V)+R`{(`OEU*)F{V)s` zqW=pVUQs~)2^TxxhI}DAKXjq5a%s21MZV32{tqtnN8o!Qf4=A9|7;gKZ@Sp&cJcoi z7dw?M?Y`w=|4kSB#V&RlUF^`j^5Sxq<1T)p*@E()cA?)3JM%FM(|*-L1sOmI0TO*| zv%)JiU6^KPPdsKOtzg13O<^{*?J&cUM5HsCv?7VN9rfMu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swI_oi9 z=+Ni=p<`7_k=H4kVLir|K%Z#rf7-7&qgvnZ7=O)HLik*!+t=bbI_o>vFT`cBLq`*m zpVkJe^!lTDGz%B|&;24A`z{sr#{@l?P%g8ALgiTLss!`#SxEl}`gb1se<1eaK*j$7 DjBZj^ literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..3b96ccfda1f9edf9d5ec4bee99cdfb3ea570463f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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 0000000000000000000000000000000000000000..205a57fcb8ca1385c9874daf0a8257636365a178 GIT binary patch literal 18456 zcmeHP3vgW3c|N# zfA{WXuLqnoozCcJ?)}gIJpcL6es3-#@y=uyofR`w;V8|X4Bt5V6==qAnP*f?L^vWr# zaW|~hjxl7uU0#5ckpl8=!yVdxhE6?-&Aw$ye&5Pscv8pZ4(Swl+AkuL-T|$5K?EcW^ssNbODOn(oB*#)fOUW6Qb|$-e$&{VN)mH8un@sbHPt zlV3HssQK1yY8UlSWe<9-hn=+^{@>_f=Uxx`w><1zdrJrTdmsKXjdd{ zTIo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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 0000000000000000000000000000000000000000..29860143c3a7b1c163038f41044a1b5c5f574ffe GIT binary patch literal 14942 zcmd5@&2Jp%d0#uW>n^Gw2vVen9$wRcyi_Psl3hnyeH1H_mS|BVLsFLIx)p}AL+wPf zGwYdI(lnEzmjJyKNRL4-J@g;wssBRIL;rvrdMJVvMbS$yMK48vzvp@1nRj-VvPi0d z$EL`A=lyuTzusrJ9(?PK@3r23yVd&Zpa1gHmiN5fy4)HDak?0eMvr#iY`ylke|LGm z-7+%ewbmcCTIN^(`+L9m^=p&Amo8mso8`3~bM4x4*WAyB(P5T9H}79)WBl$oPWmQD z`(~79VH6c6Fh!P^QQstS`doe$rOEcqR)9s>-jAcOEEchHcMxF;-o=Gm5vL|7!YJ*> z=`)k%{U|qkN2bKY5f_J9KjO+>QH+NX#)Go?l3R___=~Yj!_3EdT$WLaUAm^XXm-kR zKT1oy*p3e3=n&9dG!L_WydUFFPP)YvkvYo7xk&>YGKli%4i>Dftac54Us@vCOH0+( zcPF;StK~tEKEsB1B~xYXr-YtsFSOr{(=Y)f=2n~)WgZ1XoKO$$&loFajg>j*!YleupOBPJMpSeh0 z&111F?MF#eMh}BDcoyZI6Q>nXkcR^|O;0&-az8FcNpSS_voEycv^13E&isY;G4U~Y zEckIc8kZjdg$?rWwwX(VGS1QrRVxA45ndGb}Yn&EQUKUiJAOVY z$R85RTer;IHc+S73i5*I=W}y><5tKlI|kd5RBVp-JMUBnK$3X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9p>)wrB4}L?yDAcR@=4mwj(F`F_60S#*^^>pr-nUQTJp??)qUbnh3E1? zk7YLo`|qZI<1DJQ<3kalr_oj0M8`40t_7?9ChX523nAck!~RX*j$q~vyMNqNwDcV- z2{0GcnTfd@poE*64lz|YkD~E#awfvJ5ba5*+oNyg7|yclZ^6dwG@97?2`;~7{!xzU zq$K7Y<(%g5gn_K_w5+M?cukWjY_~_$Qx#Qf$tv?I3gzIbCkw8Vb43)U_7!9&r+O&6 z&8?XiTqmcC@Oao}kXNX)ZRoSIBHZ!r2{h5ae+H&#l#S z>Mdb+Z{kvyo3MCtFAi`~-d^wNq8nE~#H#{jaPvu)plKiuC)eH`?;+bnn0hx#vO_5f z?Tqt%d^4lHwY{pie0U8t4BD*i=m>?%>hzr`K>eP#!8^~7Om7cW+y#R{>~)l-{r27M zW%Fnk#JG0d+|7^hWsl_`#hZEus_sp*6(^vCSe2a;7Nfng`iY)=^(q=$4`Q@ap@>qB z*m+)^{xCyJ76NGEe=o|C>>04T7Y*AFA3fI9SFW3f<2W~uvkYHFn6dEVY>>*7fBmap z6aWqZPd^Wm_Hu7s0srs=3vxm()mtdsyNKuSBTkeyZU*=FV-)yT$mOzqZ@p(868WoF z%)KNC5#>*=-;VYZ2m%upSNYlsin85XUseR({{R>0sI26{S7xms3^#?bCD%LtgT3cv^L?MkfuQh-!XZxuPxirP#RX4U#6y)f~=5B748gjvtfD<0M7L* zU<1Bj6F2v=FgP_-F%_A2NVh**+f>$Hyon6n@3Z`pwytCPEy6tW??%*YTtv#^gbg!ubX>0DK=YUdS(ZrR$4`a zfam?9jb2BqZZ}L1UviqJ8Q5Q+QW4W)+Q6 z=n^Xa@n7Elr~i1P)#A_AgV$dFUaS6=pWipfNWS@yg{O||Cy(%}n{2?VpI@O_PfJ}a zF`fG$zr4g^Y5n}{-Nr$uE1gc_Q<|Gcj hNYI(q&(E*5&g)vY9=r+f-D +#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); + } + +} + +